Is there any way of executing a mv command without typing the full path in the second argument?
Example:
mv my/file/that/has/a/really/long/path/foo.bar some_magic_operator_that_means_the_same_directory/foo.baz
Is there any way of executing a mv command without typing the full path in the second argument?
Example:
mv my/file/that/has/a/really/long/path/foo.bar some_magic_operator_that_means_the_same_directory/foo.baz
You can use bash's brace expansion. This:
mv my/file/that/has/a/really/long/path/foo.{bar,baz}
will expand into:
mv my/file/that/has/a/really/long/path/foo.bar my/file/that/has/a/really/long/path/foo.baz
and then mv is run with those two arguments. See http://wiki.bash-hackers.org/syntax/expansion/brace for more on brace expansion.
As an alternative to brace expansion, you could use the bash shell's history expansion features:
! introduces a history expansion# event designator denoting the current command$ word designator referring to the last argument You can perform substitutions on the expansion using a sed-like s/pattern/replacement syntax e.g.
somecommand path/to/foo.bar !#$:s/.bar/.baz
Ex.
$ mv -v somepath/Original/Dir3/SubDir1/foo.bar !#$:s/.bar/.baz
mv -v somepath/Original/Dir3/SubDir1/foo.bar somepath/Original/Dir3/SubDir1/foo.baz
'somepath/Original/Dir3/SubDir1/foo.bar' -> 'somepath/Original/Dir3/SubDir1/foo.baz'
If you want the path specifically (for example, to mv a file to a completely different name that is not easily obtained by substitution), you can use the h modifier:
$ mv -v somepath/Original/Dir3/SubDir1/foo.baz !#$:h/baz.foo
mv -v somepath/Original/Dir3/SubDir1/foo.baz somepath/Original/Dir3/SubDir1/baz.foo
'somepath/Original/Dir3/SubDir1/foo.baz' -> 'somepath/Original/Dir3/SubDir1/baz.foo'
For other options, including readline key combinations to yank the last argument, see How to repeat currently typed in parameter on bash console?
`DIR=./really/long/path/mv "$DIR"foo.bar "$DIR"foo.bzIn one line : DIR=./really/long/path/; mv "$DIR"foo.bar "$DIR"foo.bz
cd to the directory you want to work in : cd ./really/long/pathmv foo.bar foo.bzIn one line : cd ./really/long/path && mv foo.bar foo.bz
&& in that last one-liner. Otherwise, if the cd fails, the mv will be run in the wrong directory, potentially overwriting a file. With cd ./really/long/path && mv foo.bar foo.baz, mv will only be run if cd succeeds in changing directory.
– geirha
Mar 27 '12 at 19:02
You might try:
pushd .
cd /really/long/directory/name/
mv whatever.1 whatever.2
popd
You can you alias command to short the directory name:
alias src='/directory/name/of/source'
alias dst='/directory/name/of/destination'
mv src dst
alias test='/var/log' ; cd test gives -bash: cd: test: No such file or directory.
– zpletan
Mar 27 '12 at 18:50
cdto the directory andmvfrom there? – zpletan Mar 27 '12 at 18:35cdto whatever path you can thencd -to go back instantly. – bloody Apr 10 '20 at 20:47