Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

At the command line, lets say my pwd is something like:


And there is a file in some deep directory that I want to rename. Normally I would do this:

# mv /var/some/deep/folder/structure/fileA /var/some/deep/folder/structure/fileB

What question is, are there any command line tricks to rename a file/dir without having to type out the entire directory structure again? Something like:

# mv /var/some/deep/folder/structure/fileA fileB

The problem with that command is that it moves fileA to your pwd. Obviously I want to keep it in the same location and simply rename it. So as I said, are there any tricks to achieve this without having to type out the entire directory structure again? It's simply a question out of curiosity and laziness.

share|improve this question
up vote 10 down vote accepted

Depends on your shell. In zsh you can:

mv /var/some/deep/folder/structure/{fileA,fileB}

If you're using bash, consider migrating to zsh - it's a pretty straightforward process and I'm sure you'll love zsh. :)


It seems to work in bash, see Lunar_Lamp's comment.

share|improve this answer
I use this quite often in Bash, but I forgot to suggest that in my answer! – Lunar_Lamp Jan 24 '12 at 9:29
Wow, it works in bash too, I didn't knew it :) – Georgi Hristozov Jan 24 '12 at 9:31
Perfect, thanks! – Jake Wilson Jan 24 '12 at 17:44

There are at least three tricks to eliminate the repetitive typing.

… and they all work in the Bourne-Again shell, the Korn shell, and the Z shell.

  • Use shell variables:
    mv "${dir}"/fileA mv "${dir}"/fileB
  • Use brace expansion:
    mv /var/some/deep/folder/structure/{fileA,fileB}
  • Use a subshell:
    (cd /var/some/deep/folder/structure/ && mv fileA fileB)

Other typing-reduction tricks include more shell-specific tricks such as functions and aliases, and shell-neutral tricks such as using your terminal emulator's copy and paste facilities. ☺

share|improve this answer

While there may be more efficient shortcuts, these would be the two methods I'd use frequently:

mv "$aa"fileA "$aa"fileB


cd /var/some/deep/folder/structure
mv fileA fileB
cd -

Now, obviously they're not the efficient solutions that you're after, but they can both (in my opinion) be quicker than typing out the full path each time.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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