Re:Renaming files with mv without typing the name two times
by sigwinch28 on Monday April 13, 2020 @06:54PM
(#22861057)
This is a technique I use often:
etc.
[ Parent
| on HN ]
Re:Renaming files with mv without typing the name two times
by IngoBlechschmid on Monday April 13, 2020 @08:12PM
(#22861576)
An alternative is:
mv file.name !#:1.bak
cp process.log !#:1.old
!#:1 expands to the word with index 1.
Re:Renaming files with mv without typing the name two times
by kesor on Monday April 13, 2020 @09:21PM
(#22861908)
And it supports regex replace as well
$ echo foobar !#:1:s/foo/bar
echo foobar barbar
foobar barbar
Re:Renaming files with mv without typing the name two times
by garryq on Tuesday April 14, 2020 @04:31AM
(#22863784)
slightly shorter !#$
and i always use ctrl-alt-e to expand and check even if i dont want to edit
Re:Renaming files with mv without typing the name two times
by Anthony-G on Tuesday April 14, 2020 @07:42AM
(#22864754)
I 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.
Re:Renaming files with mv without typing the name two times
by tzs on Monday April 13, 2020 @08:50PM
(#22861766)
Another 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?
Re:Renaming files with mv without typing the name two times
by TheDong on Monday April 13, 2020 @10:42PM
(#22862263)
> Anyone have a way to hack that to cut the two 'mv's down to one?
Yup:
mv -bS .bak file{.new,}
Should give the same result if you're using gnu coreutils 'mv', though it's obviously not as generally useful as nested braces.
Re:Renaming files with mv without typing the name two times
by JadeNB on Monday April 13, 2020 @09:51PM
(#22862045)
I didn't know {} could nest!
Re:Renaming files with mv without typing the name two times
by CraneWorm on Tuesday April 14, 2020 @07:01AM
(#22864571)
It sorta can't... note the use of eval.
Re:Renaming files with mv without typing the name two times
by tzs on Tuesday April 14, 2020 @11:10AM
(#22866922)
The eval isn't to help with the nesting. The {} stuff nests just fine, as we can see by changing the "eval mv" to printf:
$ printf '[%s]' file{,{.bak\;mv,.new},}
[file][file.bak;mv][file.new][file]
(Note: I took out the '\ ' from the original because I realized it is not necessary).The eval is there because in bash text expansion occurs after ';' is interpreted, and we need that to be the other way around. Hence, the escaping of ';' in the input to preserve it for text expansion, and then the eval afterward to get ';' interpreted.
Re:Renaming files with mv without typing the name two times
by CraneWorm on Tuesday April 14, 2020 @05:11PM
(#22871503)
Sorry, I should have checked.
I jumped at it because there are other things one might like to achieve with brace expansion but turn out to be impossible because it is evaluated first - you can't use them with variables.