Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

/home/whatever

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

3 Answers 3

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. :)

Edit:

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

share|improve this answer
1  
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! –  Jakobud 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:
    dir=/var/some/deep/folder/structure
    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:

aa=/var/some/deep/folder/structure/
mv "$aa"fileA "$aa"fileB

Or

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

 
discard

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.