Linux CD Command with 10 Examples

cd command linux

Cd command is used to change the current working directory of a user in Linux and other Unix-like operating systems. If you are on the terminal, the current working directory is a folder or directory in which you are working. You can use pwd command to list the directory name you are working on now.

Cd command is one of the most basic and common commands used among Linux users, but this is not limited only to Linux systems and this is also available in operating systems such as Unix, DOS, OS/2, AmigaOS, Windows, ReactOS.

In this tutorial, I will show you how to use cd command and navigate to the directory structure.

1) Using absolute path

Here we are changing the current directory using an absolute path ( here in example '/etc/cron.d' is the path).

fluser@fluser-virtual-machine:~$ cd /etc/cron.d
fluser@fluser-virtual-machine:/etc/cron.d$ pwd
/etc/cron.d

2) Using relative path

Here we are changing the current directory using a relative path (relative to where you are located now).

fluser@fluser-virtual-machine:/etc/cron.d$ pwd
/etc/cron.d
fluser@fluser-virtual-machine:/etc/cron.d$ cd ../network/
fluser@fluser-virtual-machine:/etc/network$ pwd
/etc/network

3) Switch back to previous directory

This can be used to go back to your previous working directory.

fluser@fluser-virtual-machine:/etc/network$ cd -
/etc/cron.d

4) Change current directory to the parent directory

This can be used to go back or up by one directory along the directory tree.

fluser@fluser-virtual-machine:/etc/cron.d$ cd ..
fluser@fluser-virtual-machine:/etc$ pwd
/etc

5) Go back two directories

This command takes you back two directories of your directory tree.

fluser@fluser-virtual-machine:~/Downloads$ pwd
/home/fluser/Downloads
fluser@fluser-virtual-machine:~/Downloads$ cd ../../
fluser@fluser-virtual-machine:/home$ pwd
/home

6) Go to current user's home directory

This is usually the /home/{user} directory and the current working directory changes to that location.

fluser@fluser-virtual-machine:/etc$ cd ~
fluser@fluser-virtual-machine:~$ pwd
/home/fluser

7) Go to the home directory of another user

This can be only if you are root or if you have root privileges. Similar to above command, this command takes you to another user's home directory.

root@fluser-virtual-machine:/etc$ cd ~testuser
root@fluser-virtual-machine:~$ pwd
/home/testuser

8) Change working directory to current working directory

This command takes you to the current working directory. This seems no use of in general as no change will be done to your current working directory.

fluser@fluser-virtual-machine:~$ pwd
/home/fluser
fluser@fluser-virtual-machine:~$ cd .
fluser@fluser-virtual-machine:~$ pwd
/home/fluser

9) Go to root directory

Root directory if Unix system is / and this command takes you to this location.

fluser@fluser-virtual-machine:/home$ cd /
fluser@fluser-virtual-machine:/$ pwd
/

10) Create a directory and move into that directory

You can perform 2 actions from a single command as below.This commands creates the directory 'test' and changes the current working directory to 'test'.

fluser@fluser-virtual-machine:~/Documents$ pwd
/home/fluser/Documents
fluser@fluser-virtual-machine:~/Documents$ mkdir test && cd $_
fluser@fluser-virtual-machine:~/Documents/test$ pwd
/home/fluser/Documents/test

Conclusion

Let me conclude this tutorial with few commands to verify that cd is a internal tool ((shell-built in commands and they are loaded into the system at the time of booting and are not in need of separate processes to run these commands).

1) Check and verify your current shell

Shell is the command interpreter of the Unix operating system and it acts as a program that executes other programs.

fluser@fluser-virtual-machine:~$ echo $SHELL
/bin/bash

As can be seen, the current shell is the bash shell of this user.

2) Check the path of cd command binary(if any)

For this purpose, 'which' commands can be used, which is a Unix command used to identify the location of executables, and in this case the location of cd command.

fluser@fluser-virtual-machine:~$ which cd
fluser@fluser-virtual-machine:~$

It results in no output. Its because no binary is present on the system for cd command. Still, you are able to execute the command. This is because cd is a BASH internal command. Internal commands are built into the shell.

3) Verify

Internal command 'type' gives you the information that cd is internal command as below.

fluser@fluser-virtual-machine:~$ type cd
cd is a shell builtin
Raghu 11:09 am

Comments

Your email address will not be published. Required fields are marked *