1

How would I rename a directory from within that directory?

For example:

~/tmp $ rn tmp2
~/tmp2 $ 

I can do that with: cd .. && mv tmp tmp2 && cd tmp2, but not sure how to make it into one command.

  • out of curiousity: why does it have to be one command? – Bananguin Jul 18 '13 at 10:38
  • For example, if I'm in a git rep, and want to change it's name, I can: 1. cd'ing out of it, change the name, cd into it. 2. use a one liner. It's out of sheer laziness. – mirandalol Jul 18 '13 at 11:03
  • cd ..; mv foo bar; cd bar is a one liner. but i'll buy into the sheer laziness :-) – Bananguin Jul 18 '13 at 13:10
1

Adding this to your .bashrc.zshrc file would allow you to replicate the behaviour I described:

#in .{bash,zsh}rc file
rn () {
  local D="${PWD##*/}"
  cd .. && mv -- "$D" "$1" && cd -- "$1"
}

#in terminal
~/tmp $ rn tmp2
~/tmp2 $ 

[1] ${PWD##*/} taken from this SO Question

0
$mv /tmp /tmp2 

but to move in tmp2 , you'll have to do

$cd ..

$cd tmp2
0

mv ../dir ../dir2

but then you might get errors like directory not found or ls not working f you don't reenter the directory with the new name.

in "one command":

mv ../dir ../dir2 && cd ../dir2
  • I want to stay in the directory, that is be moved into the new directory. – mirandalol Jul 18 '13 at 9:53
  • you can still work, but pwd command will not show the correct path, for that you need to reenter the directory. – BitsOfNix Jul 18 '13 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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