What Happens If You Rename The Current Working Directory While You Are in It on Linux Mint?

Share

If you rename the current working directory while you're inside of it, the shell prompt won't change but programs like ls will still work. (below, $ represents the shell prompt).

/foo$ ls
foo-file

/foo$ readlink --canonicalize foo-file
/foo/foo-file

/foo$ mv /foo /bar

/foo$ ls
foo-file

/foo$ readlink --canonicalize foo-file
/bar/foo-file

The reason this happens even though it makes no sense at all is that the shell keeps track of the current directory's inode, which it can use to always get its actual filepath even after it has been renamed, but the default shell prompt and pwd just prints the $PWD environment variable, which isn't automatically updated when you do this.

To print the actual current working directory in this case, you need to use pwd -P. It's worth noting that pwd is a shell built-in that uses the $PWD variable, but /bin/pwd is a program that prints the actual current working directory.

/foo$ pwd -P
/bar

/foo$ /bin/pwd
/bar
Written by Noel Santos.

About the Author

I'm a self-taught Brazilian programmer graduated in IT from a FATEC. In a world of increasingly complex and essential computers, I decided to use my technical expertise in hardware, desktop applications, and web technologies to create an informative resource to make PC's easier to understand.

View Comments