Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Is there any way of executing a mv command without typing the full path in the second argument?


mv my/file/that/has/a/really/long/path/foo.bar some_magic_operator_that_means_the_same_directory/foo.baz

share|improve this question
Any reason you can't just cd to the directory and mv from there? –  zpletan Mar 27 '12 at 18:35
I frequently find myself executing these kinds of commands across multiple directories in my day to day work. I often need to execute several commands from a given parent directory. –  Finbarr Mar 27 '12 at 19:10
Somehow I found that using !! (double exclamation signs) in a terminal uses to run the last command executed. You can also add parameters/text to the last command but never tried to change/remove text from the last command. Try it and if you succeed I will drop this like an answer. Good luck! –  Geppettvs D'Constanzo Mar 27 '12 at 20:05

4 Answers 4

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.

share|improve this answer
That's glorious. I didn't know you could do that. –  Finbarr Mar 27 '12 at 20:08

You might try:

pushd .
cd /really/long/directory/name/
mv whatever.1 whatever.2
share|improve this answer

With a variable

  1. Save the directory in a variable : `DIR=./really/long/path/
  2. use move : mv "$DIR"foo.bar "$DIR"foo.bz

In one line : DIR=./really/long/path/; mv "$DIR"foo.bar "$DIR"foo.bz

Changing directory

  1. You could also cd to the directory you want to work in : cd ./really/long/path
  2. then change the file name : mv foo.bar foo.bz

In one line : cd ./really/long/path && mv foo.bar foo.bz

share|improve this answer
that second one looks familiar ;-) –  zpletan Mar 27 '12 at 18:44
Best use && 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 can you alias command to short the directory name:

alias src='/directory/name/of/source'
alias dst='/directory/name/of/destination'
mv src dst
share|improve this answer
-1 alias test='/var/log' ; cd test gives -bash: cd: test: No such file or directory. –  zpletan Mar 27 '12 at 18:50

protected by Community May 26 '14 at 11:52

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.