how to change the working directory in linux from command line

When you are working from the command line in Linux, you will often need to change the current working directory. Although almost all functions can be performed without changing the working directory, it is often clearer and convenient when you can perform the operation from within the intended directory.

One thing to remember is that there is always the concept of a current working directory when working from within the command prompt. Each running shell or command window has its own current working directory and it is not shared between the shells or windows. You can always find the current directory from within the shell by using the pwd command

bash$ pwd

Most Linux commands accept an absolute file path as an argument, which means that you can actually execute the commands without changing the working directory. But also remember that most commands will assume the current working directory as the default if one is not specified.

This means that executing these commands will be easier by changing to the desired directory. That way you do not have to use the folder path as argument each and every time. Also, most times you will be working exclusively from a single directory which also makes it much easier to just change the working directory.

You can change the current working directory from the command line using the cd command. cd stands for change directory and is probably one of the easiest of all Linux commands as it comes with not many arguments and options.

The generic format of the cd command is as below:

bash$ cd <path to the target folder>

This will change the current working directory to the new folder specified as the argument of the command. The path can either be an absolute or a relative path. The relative path is, of course relative to the working directory at the time of executing the command.

You can execute the command without any argument as well. When you execute the command without specifying the target folder, the default is the home directory of the user executing the command (ie. ~).

So, for example if a user named tom whose home directory is set to /home/tom/ executes the cd command without any command line arguments, then the working directory will be changed to /home/tom/ regardless of where it is executed from. The example is illustrated below…

bash$ pwd
/home/tom/music/artists/u2/
bash$ cd
bash$ pwd
/home/tom

As mentioned above, in order to change to the desired directory you execute the command with the folder path as argument, either an absolute path or a relative path.

bash$ cd /home/tom/music

That is the absolute path, which often starts with a / (forward slash) to denote the top root folder of the system.

bash$ cd artists/LadyGaga/
bash$ pwd
/home/tom/music/artists/LadyGaga/

This is the relative path, without the starting / (forward slash) and is relative to the current folder, which happens to be /home/tom/music in this example.

Tips

The top most folder of the system is denoted by /. This means that all absolute paths start from this folder and is thus with a forward slash.

The ~ is used to denote the home directory of the user. You can use this as a shortcut when specifying paths. For example, ~ denotes the path /home/tom/ for user tom and thus can be used as below:

bash$ cd ~/music/artists/