![]() |
| Linux: Working with directories - Module1 |
Module1 Module2 Practice
This module is a brief overview of the most common commands to work with directories:
pwd, cd, ls, mkdir and rmdir. These commands are available on any Linux (or Unix)
system.
This module also discusses absolute and relative paths and path completion in the bash
shell.
1) pwd
You are here signing can be displayed with the pwd command (Print Working Directory).
Go ahead, try it: Open a command line interface (also called a terminal, console or xterm)
and type pwd. The tool displays your current directory.
dhiru@dhiru:~$ pwd
/home/dhiru
2) cd
You can change your current directory with the cd command (Change Directory).
dhiru@dhiru$ cd /etc
dhiru@dhiru$ pwd
/etc
dhiru@dhiru$ cd /bin
dhiru@dhiru$ pwd
/bin
dhiru@dhiru$ cd /home/dhiru/
dhiru@dhiru$ pwd
/home/dhiru
2.1) cd ~
The cd and cd ~ both have the same effect. Both are used to get back into your home directory.
dhiru@dhiru$ cd /etc
dhiru@dhiru$ pwd
/etc
dhiru@dhiru$ cd
dhiru@dhiru$ pwd
/home/dhiru
dhiru@dhiru$ cd /bin
dhiru@dhiru$ pwd
/bin
dhiru@dhiru$ cd ~
dhiru@dhiru$ pwd
/home/dhiru
2.2) cd ..
To go to the parent directory (the one just above your current directory in the directory tree).
(Note:- A space between cd and .. when ykou type cd .. command.)
dhiru@dhiru$ pwd
/usr/share/games
dhiru@dhiru$ cd ..
dhiru@dhiru$ pwd
/usr/share
To stay in the current directory, type cd . ;-) We will see useful use of the . character representing the current directory later.
8.2.3. cd -
To go to the previous directory use cd -
dhiru@dhiru$ pwd
/home/dhiru
dhiru@dhiru$ cd /etc
dhiru@dhiru$ pwd
/etc
dhiru@dhiru$ cd -
/home/dhiru
dhiru@dhiru$ cd -
/etc
3) Absolute and Relative paths
When you type a path starting with a slash (/), then the root of the file tree is assumed. If you don't start your path
with a slash, then the current directory is the assumed starting point.
Below the first example shows the current directory /home/dhiru.
you have to type cd /home instead of cd home to go to the /home directory.
dhiru@dhiru$ pwd
/home/dhiru
dhiru@dhiru$ cd home
bash: cd: home: No such file or directory
dhiru@dhiru$ cd /home
dhiru@dhiru$ pwd
/home
When inside /home, you have to type cd dhiru instead of cd /dhiru to enter the subdirectory dhiru of the current directory /home.
dhiru@dhiru$ pwd
/home
dhiru@dhiru$ cd /dhiru
bash: cd: /dhiru: No such file or directory
dhiru@dhiru$ cd dhiru
dhiru@dhiru$ pwd
/home/dhiru
In case your current directory is the root directory /, then you can use both cd /home and cd home for the /home directory.
dhiru@dhiru$ pwd
/
dhiru@dhiru$ cd home
dhiru@dhiru$ pwd
/home
dhiru@dhiru$ cd /
dhiru@dhiru$ pwd
/
dhiru@dhiru$ cd /home
dhiru@dhiru$ pwd
/home
Note:- The path completion
The tab key can help you in typing a path without errors. Typing cd /et and press the tab key will expand the command line to cd /etc/. (It's case sensitive)
Module1 Module2 Practice

Comments
Post a Comment