21 ms·
Renaming files with mv without typing the name two times
- kelnos 6y agoSomething like this isn't really necessary. I do something like this (bash) all the time when I want to change a part of a filename (renames "foo-bar-baz.txt" to "foo-bar-quux.txt"): mv foo-bar-{baz,quux}.txt You can have an 'empty' bit to add or remove something from the name (renames "foo-bar.txt" to "foo-bar-baz.txt"): mv foo-bar{,-baz}.txt That will work with pathname parts as well (as in the linked demo video) if you include them in the command. I guess the linked script is useful if you need to do some complex edits to the filename, since you can't usefully have more than one curly-brace-group for this use case. But in that case honestly I'm fine just double-clicking the first argument to select, and then middle-clicking to paste, and then using the arrow keys to edit.
- phoe-krk 6y agoMinor nitpick for the second case: mv foo-bar{,-baz}.txt
- kelnos 6y agoGood catch; updated
- rambojazz 6y agoWhy does this work? I always thought brackets were used to select a set of files. For example foo-bar-{baz,quux}.txt = foo-bar-baz.txt foo-bar-quux.txt
- iokanuon 6y agoThat's exactly why that works, the shell expands it, so mv foo-bar-{baz,quux}.txt becomes mv foo-bar-baz.txt foo-bar-quux.txt
- deleted 6y ago[deleted]
- kelnos 6y agoDon't think of it as selecting files. It's just simple string substitution/expansion. "foo-bar-{baz,quux}.txt" just expands to "foo-bar-baz.txt" and "foo-bar-quux.txt", which are then passed to "mv" as separate arguments. The corresponding files need not exist, as this is a separate bit of functionality than file globbing.
- wahern 6y agoPathname expansion (i.e. globbing) can't even be used this way as POSIX requires matched names to be lexicographically sorted. That's the first thing that came to mind, before I remembered that brace expansion is a widely-supported extension not defined by POSIX.
- zenexer 6y agoBrace expansion doesn't fall under pathname expansion. There is no expectation that the resulting expansion will correspond to files. From `man bash`: > Expansion is performed on the command line after it has been split into words. There are seven kinds of expansion performed: brace expansion, tilde expansion, parameter and variable expansion, command substitution, arithmetic expansion, word splitting, and pathname expansion. That's not to say it's POSIX-compliant--I have no idea whether it is. But it definitely isn't grouped in with pathname expansion. bash does have an option to disable brace expansion, but it's not toggled by `--posix`, which leads me to believe it might be POSIX-compliant.
- wahern 6y ago> Brace expansion doesn't fall under pathname expansion. I thought that was implied in my post, but perhaps I should have made it explicit. > That's not to say it's POSIX-compliant--I have no idea whether it is I didn't know one way or another, but since you brought it up, it looks like it probably is not compliant. See, e.g., this argument that `echo {1,2}` must print '{1,2}' because POSIX doesn't require '{1,2}' to be quoted: https://www.austingroupbugs.net/view.php?id=1193 https://www.austingroupbugs.net/view.php?id=1193 Brace expansion is a nearly universally supported extension, though, so I doubt it's a real problem. And the above link proposes fixing the standard to make the extension compliant.
- carb 6y agoBraces just evaluate and expand. $ echo {A,B} A B $ echo {A,B}{C,D} AC AD BC BD $ echo {A,B}{C,D}{E,F} ACE ACF ADE ADF BCE BCF BDE BDF
- jraph 6y agoWow, I had not realized that one could compute Cartesian products using a shell.
- foobarian 6y agoOn the topic of things you didn't know the shell did: I found out about sequences recently and love them: $ echo {0..19} 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 $ echo {00..19} 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19
- pkaye 6y agoLooks like it also works with single letters. $echo {A..D} A B C D
- smcameron 6y agoHmm, I'd always used the "seq" command for that... $ echo `seq 0 19` 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
- m463 6y agowow, genius! (the second doesn't work on older versions of bash, like macos)
- perlwle 6y agoPerl is very much like this too. $ perl -le 'print join(" ", "00".."20")' 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20
- tyingq 6y agoIt's also inside Perl as bsd_glob(). use File::Glob ':bsd_glob';
- 0xEFF 6y agoShell expansion into the two strings and the order you mentioned. These two strings are passed along the argument vector to the mv command.
- emmelaich 6y agoThe { .. } stuff expands regardless of whether it matches files. Unlike *, ? [a-z] ...
- sigwinch28 6y agoThis is a technique I use often: mv file.name{,.bak} cp process.log{,.old} etc.
- IngoBlechschmid 6y agoAn alternative is: mv file.name !#:1.bak cp process.log !#:1.old !#:1 expands to the word with index 1.
- kesor 6y agoAnd it supports regex replace as well $ echo foobar !#:1:s/foo/bar echo foobar barbar foobar barbar
- garryq 6y agoslightly shorter !#$ and i always use ctrl-alt-e to expand and check even if i dont want to edit
- Anthony-G 6y agoI didn't know about the Ctrl-Alt-E so that's useful to know when using a new computer or a shared user account. I have `shopt -s histverify` set in my own bashrc. It's a useful sanity check when using history expansion. After typing the command, press Enter to see what it expands to, verify that it's what I intend and hit Enter a second time to confirm the comand.
- tzs 6y agoAnother common rename is the three way: mv file file.bak; mv file.new file Nested {} can handle that one, cutting four 'file's down to one: eval mv file{,{.bak\;mv\ ,.new},} Anyone have a way to hack that to cut the two 'mv's down to one?
- JadeNB 6y agoI didn't know {} could nest!
- outworlder 6y agoyou can also create a bunch of directories that way. Or anything really. mkdir /var/{log,spool,asdf,bar}
- fizixer 6y agoExactly. Speaking of which, how do you make a tarball of a directory? tar czf verylongdirectoryname{.tar.gz,} Feel feel to replace czf with cjf for bz2, or cJf for xz.
- eMSF 6y agoor with caf for any of them.
- matheusmoreira 6y agoThis is such an awesome bash feature. Makes it so easy to generate variations of names. What is it called?
- haecceity 6y agoBrace expansion https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html https://www.gnu.org/software/bash/manual/html_node/Brace-Exp...
- KrisJordan 6y agoThis feature is brace expansion.
- LeifCarrotson 6y agoOne of my pet peeves about expressive, terse syntaxes like this is that the features are not Google-able. Entering {} or !#:1 or !? in the search bar is more like swearing than like learning. This one happens to be called "Brace Expansion". https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html https://www.gnu.org/software/bash/manual/html_node/Brace-Exp... Importantly, realize it's a feature of Bash that deals with strings, not a feature of mv that deals with filenames. It just replaces a word containing a brace expression with a set of words that contain each pattern in the list. In general, though, I wish that terminals and text editors that do nice things like syntax highlighting and autocomplete supported help text better. Perhaps selecting some text and hovering with your mouse or hitting F1 could bring up a minimal syntax example of the feature you're looking at...
- Godel_unicode 6y agoProtip: for difficult to Google syntax, add "" and `site:stackoverflow.com` to your search.
- RMPR 6y agoOR site:superuser.com
- 6y ago
- naikrovek 6y agoI love/hate comments like yours. You have technical knowledge applicable to the problem, and you share it. I love that. You miss an important feature of the solution shown in (I presume) your rush to demonstrate your bash knowledge: the ability to edit a filename shown, in place. Brace expansion doesn't do that, doesn't show you the new filename before you commit the name change and isn't even close to interactive. This part is less great. > Something like this isn't really necessary. It may very well be necessary for someone else.
- kazinator 6y ago> doesn't show you the new filename $ echo mv foo{x,y}bar > the ability to edit a filename shown, in place Easily done right in the command line. First, type this: $ mv oldname _ The _ denotes the cursor. Now erase the last word using Ctrl-W: $ mv _ Now pastte it twie with Ctrl-Y Ctrl-Y: $ mv oldname oldname _ Edit in place, submit.
- matt-attack 6y agoUsing echo is the wrong way. What you do is ‘CTRL-x *’. That will expand any glob right there on the command line.
- kazinator 6y agoBrace expansion isn't a glob, unfortunately. Did you try it?
- TwoBit 6y agoDo people ever type all this obscura in any reality? Seems a bit ridiculous to me.
- deleted 6y ago[deleted]
- kstrauser 6y agoI do that all the time. It's not particularly obscure: your shell was designed to help you do stuff like this.
- taejavu 6y agoAlmost every day. This syntax is no more obscure than a link in markdown, for example.
- markstos 6y agoBenefits are derived from optimizing common tasks. If you commonly use use the shell, it's worth learning how to use it efficiently. If you don't use it frequently, your are likely to forget it and it's not worth optimizing.
- Godel_unicode 6y agoSeems like some deliberate practice in the shell might be a productive use of your time.
- RMPR 6y agoFortunately for OP, the HN crowd already discussed exactly this. https://news.ycombinator.com/item?id=19988548 https://news.ycombinator.com/item?id=19988548
- husam212 6y agoThis doesn't work to change a file name to something completely different, while you can do that using the posted gist, so there's a necessity for it to not re-type the path to the file twice.
- xyst 6y agoTIL about brace expansion, thanks! (also "namsral" in the GitHub gist comment section beat you to the punch)
- exadeci 6y agoMETA: https://news.ycombinator.com/item?id=22854475 https://news.ycombinator.com/item?id=22854475
- jcelerier 6y agoI much prefer rename -n 's/baz/quux/' foo-bar-*
- xyproto 6y agomv is scary, though. It's one of the commands that can easily lead to data loss if you have a misplaced space or asteriks.
- SEJeff 6y agoI came to this thread hoping someone wrote exactly what you did, otherwise I was going to do it myself. Thanks For others not aware of this functionality, it is a well documented and old bash feature: https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.ht... If you want to go deeper into the weeds, the parameter substitution bits are also very useful: https://www.tldp.org/LDP/abs/html/parameter-substitution.html https://www.tldp.org/LDP/abs/html/parameter-substitution.htm...
- citiguy 6y agoI tend to use tmux for this. Once you are proficient it is much easier to cut/paste without taking your hands from the keyboard. Admittedly the learning curve is way steeper than some bash expansions and/or a simple rewrite function.
- reanimus 6y agoThis is a nice alternative to using curly braces, especially when the changes aren't simple substitutions :D
- Twisell 6y agoMost people are bragging about braces while OP commented about that. It's especially useful for multiple alterations.
- JoshTriplett 6y agoYou can also hit Ctrl-x Ctrl-e to edit the command line in your preferred text editor, and run it when saved and closed in the editor. So you can type the mv, tab-complete the current filename, and use the editor for the new filename.
- stilley2 6y agoThis is my new favorite bash trick. Thanks!
- JadeNB 6y ago> You can also hit Ctrl-x Ctrl-e to edit the command line in your preferred text editor, and run it when saved and closed in the editor. So you can type the mv, tab-complete the current filename, and use the editor for the new filename. This doesn't seem to work in readline's vi mode. Do you know the equivalent there? EDIT: Ah, kesor points out elsewhere (https://news.ycombinator.com/item?id=22861894 https://news.ycombinator.com/item?id=22861894) that it's just 'v' in normal mode.
- JoshTriplett 6y agoYou can also bind "edit-and-execute-command" to another key in your ~/.inputrc .
- Hamuko 6y agoAlt+E in fish shell in case someone's running it in place of bash.
- chronolitus 6y agoThis might be the compromise which unites all camps: - it only uses standard tools and keymaps, which pleases the bash greybeards. - it uses your favorite-editor expertise instead of relying on situation-specific new/rare commands (Ctrl-W Ctrl-something what was it again?), which pleases the limited-braincache crowd. - like the OP solution, it is interactive. you start typing mv something/yourfile, then realize things are going to get complicated: no need to backtrack, look up the manual for brace expansion, etc, just Ctrl-x Ctrl-e, do your thing. this pleases the intuitive UX crowd.
- Symbiote 6y agoZsh users can instead add this to their .zshrc: autoload copy-earlier-word zle -N copy-earlier-word bindkey '^[,' copy-earlier-word Then it's the default Alt-dot to copy the final argument of the previous command, and Alt-comma to copy the final argument of the current command. The move command is then "mv filename <Alt-Comma>". Also, given this: echo 1 2 3 echo 4 5 6 echo 7 8 9 Then on the next command, Alt-dot will copy/replace 9→6→3. Pressing Alt-comma after Alt-dot will replace that with 8→7→echo.
- nerdponx 6y agoAlternatively you can use ZMV http://zsh.sourceforge.net/Doc/Release/User-Contributions.html#index-zmv http://zsh.sourceforge.net/Doc/Release/User-Contributions.ht... https://unix.stackexchange.com/a/19291/73256 https://unix.stackexchange.com/a/19291/73256
- lkuty 6y agoThat's why I love HN comments. Always learning a lot of stuff and regularly the comments are more interesting than the original post itself. This is the case for this one I think. Or at least a nice complement.
- barrkel 6y agoAny time renaming gets more complicated than (a) bash curly braces or (b) rename command (Perl script that applies sed expression to each file name), I break out Emacs wdired. Then you can use all the Emacs tools to perform a mass edit across multiple file names, and get them right before committing to the rename. Multiple cursors makes a great addition, as does iedit-mode.
- ashton314 6y agoThis. When I figured out you could edit the directory listing like a file and then have Emacs apply those edits I felt like I had acquired superpowers. Actually I generally feel like I have superpowers when I use Emacs. The feeling was stronger than usual that day.
- sdegutis 6y agoMultiple cursors plus C-x C-q on a dired buffer is amazingly powerful: you literally just make changes to the lines in the buffer and run C-c C-c to commit them, or C-c C-k to cancel. I've used this for very powerful mass-renames before. (I imagine it also works for deleting files by removing that line but never needed to try it before.)
- ashton314 6y agoI use Emacs inside iTerm2. (i.e. `emacs -nw`) Do you know if the multiple cursors package (which I just found today thanks to your comment) works with that? Or is it only in the native windowed version?
- kemitchell 6y ago`qmv`, `imv`, `qcp`, `icp`, and friends from the `renameutils` package come in handy. I use `qmv -f do ...` with `EDITOR=vim` quite a bit.
- roryokane 6y agoLink: https://www.nongnu.org/renameutils/ https://www.nongnu.org/renameutils/ On macOS with Homebrew, install with `brew install renameutils`.
- bmn__ 6y ago`prename` is a thing; much more ergonomic than the solution in the article: $ prename '$_=lc; s/jpeg/jpg/' IMG0001.jpeg ### IMG0001.jpeg → img0001.jpg This is probably the most reimplemented program I use, seven times last time I counted.
- shawnz 6y agoThis doesn't seem more ergonomic to me, and also it is not the same solution (OP also inserted a dash in the filename)
- lopsidedBrain 6y agoMy trick has always been to rely on tab auto-completion. mv foo-<tab> mv foo-bar-baz foo-<tab> mv foo-bar-baz foo-bar-baz Now I can edit the second part pretty quickly. Downside: you have to at least type `foo-` twice. Upside: command line history still has the full command.
- premek 6y agoI do this too, but sometimes its just too many tabs
- figomore 6y agoIn ZSH I do: mv foo-bar <alt>-m It’ll repeat foo-bar. It’s very useful. I miss it when I’m using Fish or Bash.
- grifball 6y agoIn bash, you can do ctrl-w ctrl-y ctrl-y to cut paste paste as long as it doesnt have spaces
- loeg 6y agoTab-completion is somewhat obnoxious in directories with lots of files sharing a common prefix, for example. Also, if you accidentally tab-complete the same file for output and input, depending on how clever the program is, you may end up deleting the source file.
- em-bee 6y agoi always run mv -i to make sure i don't accidentally overwrite an existing file. i don't alias mv to mv -i either, to avoid getting used to the idea that just mv is going to ask me, because i might work on a server where the alias is not set.
- loeg 6y agoI double check the filenames instead, because many of the tools I work with don't have -i flags.
- qsdf38100 6y agoNice, feels like Windows File Explorer ;D
- anthk 6y agoI just use vidir(1) an ed(1), I'm lazy :).
- lucb1e 6y agoFor those like me who don't know it, in Debian vidir is in the "moreutils" package. As the name hints, its purpose is to "edit a directory in your text editor".
- reality101 6y agotype file then ctrl-a mv then ctrl-k ctrl-y space ctrl-y change file name enter.
- lottin 6y agoYou can accomplish the same with basic editing commands, e.g. mv <filename> C-M-b C-k C-y C-y It may seem overly complicated but notice that you don't have to release the control key, so it's actually very few key strokes.
- outworlder 6y agoYou can also do control+x control+e to send the command line to your editor(whatever $EDITOR points to).
- RMPR 6y agoOn short commands, it tends to be slow in comparison of using GNU Readline directly
- deleted 6y ago[deleted]
- sashavingardt2 6y agoWhy is this at the top of HN?
- wondringaloud 6y agoI've been asking myself that question more and more. It seems HN has been slowly becoming a Reddit-lite. There are significantly more low-quality "thanks! this is great!" comments and "witty" one-liners everywhere. Not to mention posts like these.
- toomuchtodo 6y agoEveryone is a beginner at some point. Community and content shape the culture.
- dang 6y agoPeople have been saying this since before Startup News was renamed to Hacker News in 2007: https://news.ycombinator.com/item?id=13852 https://news.ycombinator.com/item?id=13852 https://hn.algolia.com/?dateRange=all&page=0&prefix=true&query=turning%20%22into%20reddit%22&sort=byDate&type=comment https://hn.algolia.com/?dateRange=all&page=0&prefix=true&que... More here: https://news.ycombinator.com/item?id=21755721 https://news.ycombinator.com/item?id=21755721
- dsr_ 6y agoThis is a FAQ now, I think. The answer is a combination of: * somebody submitted it * people upvoted it * the algorithm weights new more than old * lots of people have things to say about it, including recommendations for other tools, neat hacks, and observations on general utility
- dang 6y agoIt's common for quirky, easy-to-understand technical posts, for example that cover something familiar from an unfamiliar angle, to do well on HN.
- m463 6y agoI'm reminded of this story: https://stallman.org/articles/on-hacking.html https://stallman.org/articles/on-hacking.html
- gitgud 6y agoIf you rename the 'mv' command, won't this break a lot of things relying on it?
- premek 6y agoLets see, I hope not :) I changed it only for when you call it with just 1 argument, which is not valid normally. Otherwise it calls the original mv.
- downerending 6y agoSeveral distros unfortunately include 'mv' and 'cp' aliases by default. To get predictable behavior on the command line, always use '\mv' and '\cp'. (for bash)
- JadeNB 6y agoI didn't know that! Why does it work?
- downerending 6y agoThe backslash basically means "ignore any aliases or functions by the same name, and run the first matching command in my $PATH instead". One reason for providing this is so that you can have an alias like alias mv '\mv -=myfavoritearg ' but it's also good for ensuring that you didn't pick up some distro nonsense.
- JadeNB 6y agoThanks! So '\' is special cased here—it's not some specific manifestation of a more general phenomenon of specialised escape-handling in the shell?
- downerending 6y agoIf I understand correctly, yes, that's how I'd describe it.
- l8again 6y agoThis is one of those times when I just love coming to hacker news. Seemingly trivial quality of life improvement makes it to the top and the comments section is lit up with several other cool alternatives.
- xixixao 6y agoThis post and the comments here show what a poor UI the command line is (by default).
- downerending 6y agoCompared to what, though?
- dylan604 6y agoExactly what I was thinking. As if Windows Explorer and Finder are so perfect themselves.
- trhaynes 6y agoIn this case Finder wins pretty easily IMHO. "Enter" to rename, edit in place, "Enter" to commit ("Esc" to cancel).
- downerending 6y agoThere's a whole lot of ??? in that edit in place. For me, it usually goes like: <click> okay, check that I'm really editing. Crap, I'm not, I started a drag. <click> Okay, got lucky and it looks like I'm really in edit mode. Type a character. Oh crap, some of it I wanted to keep was selected, and now it's gone. Hit escape. Try again. <click> Crap, dragged it again. <click> Okay, editing. Carefully click again to clear the default selection. Carefully click again to put the cursor where I want to edit. Carefully type in the new name. Hit enter. Whew. What could be easier?
- em-bee 6y agoi agree with your rant. that is what makes guis inconvenient for that sort of task, but it's missplaced as the 'enter' should already put you into edit mode, so no messing with mouse clicks.
- 6y ago
- idoubtit 6y agoI use zsh's function copy-prev-shell-word. You can bind it to some key, e.g. alt-m with `bindkey "^[m" copy-prev-shell-word`. Use ctrl-v shortcut for another key. For instance, adding a suffix to a file name: mv myfile alt-m.suffix
- mkl 6y agomv myfile{,.suffix} seems easier and more general, and doesn't need zsh.
- calvinmorrison 6y agoShout out to visit(1) for lots of renames
- dnr 6y agoHere's my contribution to this useful class of scripts: http://dnr.im/tech/articles/mvdir/ http://dnr.im/tech/articles/mvdir/ https://bitbucket.org/davidn/mvdir/src/default/mvdir https://bitbucket.org/davidn/mvdir/src/default/mvdir Similar to vidir, but predates it by a few years. I still use it regularly!
- inopinatus 6y agoFor an interactive edit I just use <ESC>-. to repeat the last parameter: ls foo.txt mv <ESC>-. <ESC>-. # and edit
- yread 6y agoIs that the same thing as ALT-. ?
- mrb 6y agoYes. It's also the same as Alt-_
- mrb 6y agoYup, readline's yank-last-arg command Alt-. or Alt-_ (see https://www.gnu.org/software/bash/manual/html_node/Commands-For-History.html#Commands-For-History https://www.gnu.org/software/bash/manual/html_node/Commands-...) is the best off-the-shelf solution that still gives the ability to interactively edit the filename. To minimize the total number of key strokes I would suggest: mv foo.txt ↑ Alt-. That is: intentionally run mv with only one argument (the command fails but is recorded in the history), Up arrow to recall the last command, and Alt-dot (pastes the last argument). This is literally replacing an entire shell script with two key strokes (Up, Alt-.)
- sigjuice 6y agoWhat version of bash do you use? I can't seem to get this to work with mine. For me, Alt-. gets the last argument of the penultimate command. $ echo $BASH_VERSION 5.0.11(1)-release $ mv foo.txt mv: missing destination file operand after 'foo.txt' Try 'mv --help' for more information. $ mv foo.txt $BASH_VERSION # Alt-. pasted $BASH_VERSION instead of foo.txt
- kazinator 6y agoTo make two copies of the last argument, so you can edit one of them in place, in Bash (or anything that uses GNU Readlne), simply do this: $ mv oldname _ # _ is your cursor Now type Ctrl-W to erase the last word: $ mv _ Then type Ctrl-Y twice to yank it: $ mv oldname oldname _ Now edit in place as needed. No utility needed, and just a regular mv command is issued whose inputs are completely recorded in the history. Now,let's automate that. Add this line to ~/.inputrc: # ~/.inputrc entry \C-T: " \C-W\C-Y\C-Y\b" Reload with $ bind -f ~/.inputrc Now Ctrl-T does it: $ mv oldname_ Hit Ctrl-T $ mv oldname oldname_ How about having Ctrl-T end up on the first character of the name? # ~/.inputrc entry \C-T: " \C-W\C-Y\C-Y\b\eb"
- xyst 6y agoI would prefer to use brace expansion after learning about it today, over this.
- FreeFull 6y agoBrace expansion won't work if you need to make edits in multiple places.
- kazinator 6y agoWell, it could work if we have a function to pick off the first and last combination from the Cartesian product, haha. We define: # print first and last argument fl() { local a=("$@"); printf "%s %s\n" "${a[0]}" "${a[${#a[@]} - 1]}" } Now: $ fl {a,b}--{c,d}--{foo,bar}--{x,y}--{m,n} a--c--foo--x--m b--d--bar--y--n We can use this using command substitution, or the old backticks: $ mv $(fl path/{from,to}/subdir/{foo,bar}.png) There we go; edits in multiple places.
- CGamesPlay 6y agoNote: ^T is also a very useful command bound by default to "swap the character under the cursor and the previous one". That is, `sl` followed by left arrow, ^T, produces "ls".
- 6y ago
- Proven 6y agoPress F2 and rename
- asamarin 6y agoThe latest addition to my renaming-toolbelt has been `perl-rename'. This thing is just wonderful: I can unleash all the power of perl-based regexes to rename things in bulk. Case in point: I had a directory containing thousands of .jpg images imported from a foreign filesystem, and all of those files had tildes in them, something like: $ ls -1 EL+�CTRICO_0001.jpg EL+�CTRICO_0002.jpg EL+�CTRICO_0003.jpg ... You get the idea; note those ugly unrepresentable characters over there. On the original filesystem they read as "ELÉCTRICO", but that tilde was saved using who knows what encoding, and I simply wanted to get rid of them and have nice ascii "ELECTRICO_xxxx.jpg" files. After finding out that the strange unrepresentable character was the byte 0xEB (so, in order to form an "É" you needed those two characters together: a literal '+' and 0xEB), I could do the bulk renaming with just: $ perl-rename 's/\+\xeb/E/' *.jpg Felt so good!
- baby 6y agoFor things like this, I don’t understand why we don’t have more interactive CLIs, instead of CLIs that expect one line of a bunch of arguments.
- fl0wenol 6y agoBecause it's the shell itself that gets the focus for the interactivity. You use generic suggestions (as listed elsewhere in this thread) to do things like tab-completion, substituting in previously used arguments, copying the last thing you typed, etc. but they work across commands.
- deleted 6y ago[deleted]
- jacobsenscott 6y agoAll the "something like this isn't really necessary, just memorize all the esoteric bash globbing rules" posts are so far off, given the solutions in all those posts aren't really necessary either. It is nice not to need to "pre-think" how you are going to type your `mv` command. You just start typing it, and when you realize it will be a bit complex you just hit enter and you have an editor.
- RMPR 6y agoThe GNU readline solution is neat though.
- bspammer 6y agoA good compromise to me seems to be using `imv` from the renameutils package which is pretty much exactly the same as the OP. That way you don't get confused by different `mv` behaviour on different systems. Hitting `<Ctrl-a>i` seems to me a small price to pay :)
- BiteCode_dev 6y agoI think it's very nice, and should be the default behavior. Ergonomics of GNU tooling is lacking to say the least. That's why we love fdfind and ripgrep and love find/grep way less. I would use it if it were the default behavior, but the problem is already solved by the "moreutils" package, which I install on all my machines. This lets you do: vidir filename or vidir directory # default to . And it will open your $EDITOR with one file name per line. You edit it in the comfort of your favorite editor, and it batch renames for you, or rename the single file for the first case. Note that if you use vscode, $EDITOR should be "code -w", not just "code".
- yakshaving_jgt 6y agoRenaming a single file is not very interesting. Renaming a whole bunch of files is much more interesting, and when I need to do that, I use vimv[0]. [0]: https://github.com/thameera/vimv https://github.com/thameera/vimv
- premek 6y agoI rename a single file much more often
- alexellisuk 6y agoFor faas-cli-darwin -> faas-cli, I just do `mv faas-cli{-darwin,}` or `faas-cli.tgz` -> `faas-cli.tar.gz` -> `mv faas-cli faas-cli.{tgz,tar.gz}` This rarely saves me any time, but it's a nice hack to know about.
- bb88 6y agoGraybeard here (never thought I'd say that). 25 years of unix/linux in production systems. Changing the default behavior of a posix command is a footgun. If I wanted to get help from mv: $ mv --help Usage: file [OPTION...] [FILE...] Determine type of FILEs. I get the help for the file command.
- premek 6y agoheh :) youre right
- pwagland 6y agoAnd this is _exactly_ why, although this is cool, it would have been cooler had it been renamed to, for example, `imv` (aka the name from the rename tools project ;-) )
- michaelcampbell 6y agoThis is also why I cringe when people suggest to alias `rm` to `rm -i`. That will bite you, believe me.
- test1235 6y agoI can't remember where it was I read (saw this years ago), but the way to actually get help online isn't to ask a question 'cos nobody responds ... you have to propose an incorrect solution where suddenly everyone jumps up to correct you with the answer you're looking for.
- class4behavior 6y agoCunningham's Law
- bobowzki 6y ago"without typing the name two times" Is that pun intentional? It's hilarious.
- matthewhartmans 6y agoI think this is really neat! Well done OP! :)
- zwischenzug 6y agoI posted about some 'God-like' bash shortcuts here: https://zwischenzugs.com/2019/08/25/seven-god-like-bash-history-shortcuts-you-will-actually-use/ https://zwischenzugs.com/2019/08/25/seven-god-like-bash-hist... Number 6 was a 'refer to current line' one. There are so many ways to do these things that it's hard to get them all under your fingers though. Most of the time I tab my way through the 'problem' anyway.
- ara24 6y agoAs has already been said, bash brace expansion is quite powerful. It has many usecases, such as, diff file{.original,} mkdir -p path/{a,b,c}/folder for i in image{001..060}; do echo $i; done Parameter Expansion is also equally useful, like changing a few characters in long names, mv ${i}.png ${i/imaeg/image}.png
- dejj 6y agoLinks for mass moving mentioned in the page: - 2018: https://github.com/lgommans/vinamer https://github.com/lgommans/vinamer - 2017: https://github.com/thameera/vimv https://github.com/thameera/vimv - 2017: https://github.com/abaldwin88/roamer https://github.com/abaldwin88/roamer - 2006: https://joeyh.name/code/moreutils/ https://joeyh.name/code/moreutils/ , `vidir`
- pvaldes 6y agoor... you can do this mmv '<asterisk>foo<asterisk>' '#1bar#2' where <asterisk> is the common symbol (Will not appear in HN)
- globular-toast 6y agoI discovered the `rename` command surprisingly late in my career. You could write something like this: `rename .htm .html index.htm`. The nice thing is it works for any number of files at once (you could put a glob on the right, for example). Oddly I just checked my Ubuntu machine and it had the man page for rename but not the command. After being prompted to install it it installed a completely different perl command and upon removing that the original manpage was gone. Very strange.
- Sierra-Sam 6y agoHere is my take on part of the problem. https://github.com/Guy-Shaw/libmmv https://github.com/Guy-Shaw/pmmv The original perl-rename has the expressive power, but not the safety features. mmv has been around since 1990 and, in my opinion, has always been under-appreciated, but it could use some modernization.
- rurban 6y agoAlways surprising to hear of people not knowing the basic readline key assignments. Ctrl-w Ctrl-y Ctrl-y is still easiest for single files, rename for multiple.