Wednesday, January 17, 2018

‘cd’ Command in Linux

  • In Linux ‘cd‘ (Change Directory) command is one of the most important and most widely used command for newbies as well as system administrators. For admins on a headless server, ‘cd‘ is the only way to navigate to a directory to check log, execute a program/application/script and for every other task. For newbie it is among those initial commands they make their hands dirty with.
  • Thus, keeping in mind, we here bringing you basic commands of ‘cd‘ using tricks and shortcuts to reduce your efforts on the terminal and save time by using these known tricks.

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

manish@localhost:~$ cd /usr/local
manish@localhost:/usr/local$

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

manish@localhost:/usr/local$ cd /usr/local/lib
manish@localhost:/usr/local/lib$

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

manish@localhost:/usr/local$ cd lib 
manish@localhost:/usr/local/lib$ 

4. (a) Move one directory back from where you are now.

manish@localhost:/usr/local/lib$ cd - 
/usr/local 
manish@localhost:/usr/local$ 

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

manish@localhost:/usr/local/lib$ cd .. 
manish@localhost:/usr/local$ 

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

manish@localhost:/usr/local$ cd -- 
/home/manish

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

manish@localhost:/usr/local$ cd ../ ../ 
manish@localhost:/usr$

7. Move to users home directory from anywhere.

manish@localhost:/usr/local$ cd ~ 
manish@localhost:~$ 
or
manish@localhost:/usr/local$ cd 
manish@localhost:~$ 

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

manish@localhost:~/Downloads$ cd . 
manish@localhost:~/Downloads$ 
or
manish@localhost:~/Downloads$ cd ./ 
manish@localhost:~/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.

manish@localhost:/usr/local/lib/python3.4/dist-packages$ cd ../../../../../home/manish/Desktop/ 
manish@localhost:~/Desktop$ 

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

manish@localhost:/var/www$ cd /v<TAB>/w<TAB>/h<TAB>
manish@localhost:/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.

manish@localhost:~$ cd /etc/v* 
manish@localhost:/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 ‘manish‘ (not sure if it is manish )home directory, without using TAB.

manish@localhost:/etc$ cd /home/manish? 
manish@localhost:~$ 

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.

manish@localhost:~$ pushd /var/www/html 
/var/www/html ~ 
manish@localhost:/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.

manish@localhost:/var/www/html$ popd 
~ 
manish@localhost:~$ 

14. Change to a directory containing white spaces.

manish@localhost:~$ cd test\ rip/ 
manish@localhost:~/test rip$ 
or
manish@localhost:~$ cd 'test rip' 
manish@localhost:~/test rip$ 
or 
manish@localhost:~$ cd "test rip"/ 
manish@localhost:~/test rip$ 

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


manish@localhost:/usr$ cd ~/Downloads && ls
.
service_locator_in.xls 
sources.list 
teamviewer_linux_x64.deb 
tor-browser-linux64-3.6.3_en-US.tar.xz 
.
...

No comments:

Post a Comment

Configure NFS Client

This example is based on the environment below. +----------------------+           |           +----------------------+ | [...