Tuesday, October 15, 2024

15 Practical Examples of ‘cd’ Command in Linux

 

Tutorial Details
  1. Command Name : cd
  2. Stands for : Change Directory
  3. Availability : All Linux Distribution
  4. Execute On : Command Line
  5. Permission : Access own directory or otherwise assigned.
  6. Level : Basic/Beginners

1. Change from current directory to /usr/local.

avi@tect:~$ cd /usr/local

avi@tect:/usr/local$ 

2. Change from current directory to /usr/local/lib using absolute path.

avi@tect:/usr/local$ cd /usr/local/lib 

avi@tect:/usr/local/lib$ 

3. Change from current working directory to /usr/local/lib using relative path.

avi@tect:/usr/local$ cd lib 

avi@tect:/usr/local/lib$ 

4. (a) Switch back to previous directory where you working earlier.

avi@tect:/usr/local/lib$ cd - 

/usr/local 
avi@tecmint:/usr/local$ 

4. (b) Change Current directory to parent directory.

avi@tect:/usr/local/lib$ cd .. 

avi@tect:/usr/local$ 

5. Show last working directory from where we moved (use ‘–‘ switch) as shown.

avi@tect:/usr/local$ cd -- 

/home/avi 

6. Move two directory up from where you are now.

avi@tect:/usr/local$ cd ../ ../ 

avi@tect:/usr$

7. Move to users home directory from anywhere.

avi@tect:/usr/local$ cd ~ 

avi@tect:~$ 

or

avi@tect:/usr/local$ cd 

avi@tecmint:~$ 

8. Change working directory to current working directory (seems no use of in General).

avi@tect:~/Downloads$ cd . 
avi@tect:~/Downloads$ 

or

avi@tect:~/Downloads$ cd ./ 
avi@tect:~/Downloads$ 

9. Your present working Directory is “/usr/local/lib/python3.4/dist-packages/ ”, change it to “/home/avi/Desktop/ ”, in one line command, by moving up in the directory till ‘/’ then using absolute path.

avi@tect:/usr/local/lib/python3.4/dist-packages$ cd ../../../../../home/avi/Desktop/ 

avi@tect:~/Desktop$ 

10. Change from current working directory to /var/www/html without typing in full using TAB.

avi@tect:/var/www$ cd /v<TAB>/w<TAB>/h<TAB>

avi@tect:/var/www/html$ 

11. Navigate from your current working directory to /etc/v__ _, Oops! You forgot the name of directory and not supposed to use TAB.

avi@tect:~$ cd /etc/v* 

avi@tect:/etc/vbox$ 

Note: This will move to ‘vbox‘ only if there is only one directory starting with ‘v‘. If more than one directory starting with ‘v‘ exist, and no more criteria is provided in command line, it will move to the first directory starting with ‘v‘, alphabetically as their presence in standard dictionary.

12. You need to navigate to user ‘av‘ (not sure if it is avi or avt) home directory, without using TAB.

avi@tect:/etc$ cd /home/av? 

avi@tect:~$ 

13. What are pushd and popd in Linux?

Pushd and popd are Linux commands in bash and certain other shell which saves current working directory location to memory and bring to the directory from memory as current working directory, respectively as well as changes directory.

avi@tect:~$ pushd /var/www/html 

/var/www/html ~ 
avi@tect:/var/www/html$ 

The above command saves the current location to memory and changes to the requested directory. As soon as popd is fired, it fetch the saved directory location from memory and makes it current working directory.

avi@tect:/var/www/html$ popd 
~ 
avi@tect:~$ 

14. Change to a directory containing white spaces.

avi@tect:~$ cd test\ tecmint/ 

avi@tect:~/test tecmint$ 

or

avi@tect:~$ cd 'test tecmint' 
avi@tect:~/test tecmint$ 

or 

avi@tect:~$ cd "test tecmint"/ 
avi@tect:~/test tecmint$ 

15. Change from current working directory to Downloads and list all its settings in one go.

avi@tect:/usr$ cd ~/Downloads && ls

…
.
service_locator_in.xls 
sources.list 
teamviewer_linux_x64.deb 
tor-browser-linux64-3.6.3_en-US.tar.xz 
.
...

This is our attempt, to make you aware of Linux Workings and executions in least possible words and with as much as user friendliness as it used to be before.

That’s all for now. I’ll be here again with another interesting topic soon. Till then stay tuned and connected to Tecmint. Don’t forget to provide us with your valuable feedback in the comments below.

No comments:

Post a Comment

HTTP Appache Server LAB 7

 Apache HTTP Server (httpd) Configuration,