1

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

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

3 Answers 3

4

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
  new_dir=${PWD%/*}/$1
  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.

0
2

I got it by below code

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

Argument passed as

./rename_pwd.sh dir1

Then

cd ..

ls

It shows

dir1
1
  • 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. Commented Jul 21, 2017 at 12:01
-2

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .