If present directory is DIR/ and new name project1/ is passed through argument, the present directory name should be replaced by project1.

  • 1
    Have you try to use mv command? – Romeo Ninov Jul 21 '17 at 10:17
  • 1
    Read the manpage for mv: man mv. Note that you first should leave the directory to rename it. – Philippos Jul 21 '17 at 10:18

If you rename the current working directory, most shells will fail to notice the change (in their $PWD variable or their prompt for instance), so it would be a better idea to use a function instead of a script so you can notify the shell of the change after you've done it, like:

rename_cwd() {
  cd . || return
  mv -- "$PWD" "$new_dir" &&
    cd -- "$new_dir"

That assumes the current directory is not / and that the target directory doesn't already exist (see the -T option with GNU mv to improve matters in that case).

Note that under some circumstances, $PWD may be out of sync with the reality so the code above may end up changing the current working directory. The cd . is there to reduce the likelihood of it in some shells.


I got it by below code

mv -T "$PWD" "../$1"

Argument passed as

./rename_pwd.sh dir1


cd ..


It shows

  • Good point. Note however that if $PWD is a symlink, for instance, /a/b/c symlink to /1/2/3 (where /1/2/3 is not a symlink itself), then rename_pwd.sh d will do the equivalent of mv /a/b/c /1/2/d. Doing a mv /a/b/c /a/b/d (like in my answer) would probably be closer to what one would generally want. – Stéphane Chazelas Jul 21 '17 at 12:01

I wrote this a long time ago. It's a crappy little script but, it will rename stuff for you.

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.