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.

link|improve this question

74% accept rate
feedback

3 Answers

up vote 7 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.

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

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. ☺

link|improve this answer
feedback

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.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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