12 ms·
Something 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"
by kelnos 7y ago
Something 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 7y agoMinor nitpick for the second case: mv foo-bar{,-baz}.txt
- kelnos 7y agoGood catch; updated
- rambojazz 7y 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 7y 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 7y ago[deleted]
- kelnos 7y 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 7y 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 7y 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 7y 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 7y 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 7y agoWow, I had not realized that one could compute Cartesian products using a shell.
- foobarian 7y 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 7y agoLooks like it also works with single letters. $echo {A..D} A B C D
- smcameron 7y 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 7y agowow, genius! (the second doesn't work on older versions of bash, like macos)
- perlwle 7y 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 7y agoIt's also inside Perl as bsd_glob(). use File::Glob ':bsd_glob';
- 0xEFF 7y 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 7y agoThe { .. } stuff expands regardless of whether it matches files. Unlike *, ? [a-z] ...
- sigwinch28 7y agoThis is a technique I use often: mv file.name{,.bak} cp process.log{,.old} etc.
- IngoBlechschmid 7y agoAn alternative is: mv file.name !#:1.bak cp process.log !#:1.old !#:1 expands to the word with index 1.
- kesor 7y agoAnd it supports regex replace as well $ echo foobar !#:1:s/foo/bar echo foobar barbar foobar barbar
- garryq 7y agoslightly shorter !#$ and i always use ctrl-alt-e to expand and check even if i dont want to edit
- Anthony-G 7y 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 7y 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 7y agoI didn't know {} could nest!
- outworlder 7y agoyou can also create a bunch of directories that way. Or anything really. mkdir /var/{log,spool,asdf,bar}
- fizixer 7y 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 7y agoor with caf for any of them.
- matheusmoreira 7y agoThis is such an awesome bash feature. Makes it so easy to generate variations of names. What is it called?
- haecceity 7y 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 7y agoThis feature is brace expansion.
- LeifCarrotson 7y 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 7y agoProtip: for difficult to Google syntax, add "" and `site:stackoverflow.com` to your search.
- RMPR 7y agoOR site:superuser.com
- 7y ago
- naikrovek 7y 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 7y 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 7y agoUsing echo is the wrong way. What you do is ‘CTRL-x *’. That will expand any glob right there on the command line.
- kazinator 7y agoBrace expansion isn't a glob, unfortunately. Did you try it?
- TwoBit 7y agoDo people ever type all this obscura in any reality? Seems a bit ridiculous to me.
- deleted 7y ago[deleted]
- kstrauser 7y agoI do that all the time. It's not particularly obscure: your shell was designed to help you do stuff like this.
- taejavu 7y agoAlmost every day. This syntax is no more obscure than a link in markdown, for example.
- markstos 7y 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 7y agoSeems like some deliberate practice in the shell might be a productive use of your time.
- RMPR 7y 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 7y 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 7y agoTIL about brace expansion, thanks! (also "namsral" in the GitHub gist comment section beat you to the punch)
- exadeci 7y agoMETA: https://news.ycombinator.com/item?id=22854475 https://news.ycombinator.com/item?id=22854475
- jcelerier 7y agoI much prefer rename -n 's/baz/quux/' foo-bar-*
- xyproto 7y 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 7y 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 7y 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.