What Does cd do on Linux


cd stands for "change directory" and is used to navigate the file system on a Linux computer. When used with a specific directory path as an argument, cd will change the current working directory to that location. For example, the command cd /home/user/documents will change the current working directory to the "documents" folder located within the "user" folder in the root directory. If you use cd command without any argument it will take you to your home directory.

The Meaning of – With cd

The "-" (dash) symbol is a shortcut that can be used with the cd command to quickly switch between the current directory and the previous directory. For example, if you are currently in the directory /home/user/documents and you run the command cd /home/user/pictures, you can then use the command cd - to quickly switch back to the /home/user/documents directory.

Additionally, it can be used to change the directory to the last directory you were working on. If you are in a directory and you use the command cd - it will change the directory to the last directory you were in.

Some examples of using the cd command in Linux are −

  • To change to your home directory: cd ~

  • To change to the parent directory of the current directory: cd ..

  • To change to a specific directory: cd /path/to/directory

  • To change to a directory named "documents" within the current directory: cd documents

  • To change to the last directory you were working on: cd -

  • To change to the previous directory: cd "$OLDPWD"

It's important to note that when using cd command, it's case sensitive so you should type the directory name correctly. Also, you should have the appropriate permissions to access the directory you're trying to change to.

Equivalent Command to cd –

The equivalent command to cd - is cd "$OLDPWD". The $OLDPWD environment variable is automatically set by the shell to the previous working directory, so using this command will have the same effect as using cd -, which is to change the current working directory to the previous directory.

In other words, you can use cd "$OLDPWD" instead of cd - to change the current working directory to the previous directory, and both will have the same effect.

As I mentioned before, the equivalent command to cd - is cd "$OLDPWD". This command uses the $OLDPWD environment variable, which is automatically set by the shell to the previous working directory.

Here are some examples of using the cd "$OLDPWD" command in Linux −

  • If you are currently in the directory /home/user/documents and you run the command cd /home/user/pictures, you can then use the command cd "$OLDPWD" to quickly switch back to the /home/user/documents directory.

  • If you are in a directory /home/user/downloads and you use the command cd /home/user/docs, you can then use the command cd "$OLDPWD" to change the directory back to /home/user/downloads

  • If you are in a directory /home/user/ and you use the command cd "$OLDPWD" it will change the directory to the last directory you were in.

It's important to note that $OLDPWD is a environment variable that stores the previous working directory, so using cd "$OLDPWD" command ensures that you can move back to previous directory even if you don't remember the path of it.

Conclusion

In conclusion, cd is a command in Linux used to navigate the file system by changing the current working directory. The "-" symbol is a shortcut that can be used with the cd command to quickly switch between the current directory and the previous directory. An equivalent command to cd - is cd "$OLDPWD", which changes the current working directory to the previous directory using the $OLDPWD environment variable.

Updated on: 25-Jan-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements