Linux - Working with directories
Introduction
In this session, we have covered an overview of the most common commands to work with directories : pwd, cd, ls, mkdir, rmdir. These commands are available on any Linux (or Unix) system. We have also discussed the absolute and relative paths and path completion in the bash shell.
pwd
Displays the full path-name for the current directory. Open a command line interface (like gnome-terminal, konsole, xterm, or a tty) and type pwd.
datasoft@datasoft-linux:~$ pwd
/home/datasoft
cd
You can change your current directory (working directory) with the cd command.
datasoft@datasoft-linux:~$ cd /etc
datasoft@datasoft-linux:/etc$ pwd
/etc
datasoft@datasoft-linux:/etc$ cd /bin
datasoft@datasoft-linux:/bin$ pwd
/bin
datasoft@datasoft-linux:/bin$ cd /home/datasoft/
datasoft@datasoft-linux:~$ pwd
/home/datasoft
cd ~<
Typing cd without a target directory will return you to your home directory. Typing cd ~ has the same effect.
datasoft@datasoft-linux:~$ cd /etc
datasoft@datasoft-linux:/etc$ pwd
/etc
datasoft@datasoft-linux:/etc$ cd
datasoft@datasoft-linux:~$ pwd
/home/datasoft
datasoft@datasoft-linux:~$ cd ~
datasoft@datasoft-linux:~$ pwd
/home/datasoft
To go to the parent directory (moves you up one directory), type cd ..
datasoft@datasoft-linux:~$ pwd
/home/datasoft
datasoft@datasoft-linux:~$ cd ..
datasoft@datasoft-linux:/home$ pwd
/home
To stay in the current directory, type cd . ;-) We will see the useful use of the . character representing the current directory later.
cd -
Another useful shortcut with cd is to just type cd - to go to the previous directory.
datasoft@datasoft-linux:~$ pwd
/home/datasoft
datasoft@datasoft-linux:~$ cd /etc
datasoft@datasoft-linux:/etc$ pwd
/etc
datasoft@datasoft-linux:/etc$ cd -
/home/datasoft
datasoft@datasoft-linux:~$ cd -
/etc
Absolute paths
Absolute paths begin at the top / (referred to as root) and then look down for the requested directory. The tree command below is used as an example for the purpose of explaining cd.
Suppose you are currently in the user directory and you want to switch to the user1 directory, you need to move up in the directory tree.
If you type :
While you are in the user directory, you will get an error message "No such file or directory". This is because there is no user1 directory below user directory.
To go to user1 directory, you would type :
This is an absolute path. It tells linux to start at the top (here it is a home directory) and look down until it finds the user1 directory.
Therefore absolute paths will take you TO any directory, FROM any directory.
Relative paths
Any path that does not begin with a / or a ~ is a relative path. You can use an absolute path anywhere, a relative path can be used as well. Unlike an absolute path where your current location doesn't matter, it does matter where your current directory is, when using a relative path. Two special symbols "." (dot) and ".." (dot dot) are used to represent relative positions in the file system tree. The tree command below is used as an example for the purpose of explaining relative path.
Let's change the working directory to /home/user
Let's say that we want to change the working directory to /home/usre1 (see the above tree structure). We can do that two different ways. Either with an absolute pathname :
Or, with a relative pathname :
Therefore we get an identical result from two different methods.
Here is another example :
We can change the working directory from /home to /home/user1 in two different ways. Either using an absolute pathname:
Or, with a relative pathname:
It is suggested that you can omit the "./". See the following command which will give you the identical result.
Be aware of where you are
Always you should know which working directory you are in before you state the relative path to the directory or file you want to get to. You do not have to worry about your position in the file system, though, when you state the absolute path to another directory or file. If you are not sure, type pwd (print the name of the current directory).
path completion
The tab key can help you in typing a path without errors. Typing cd /et followed by the tab key will expand the command line to cd /etc/. When typing cd /Et followed by the tab key, nothing will happen because you typed the wrong path (upper case E).
You will need fewer key strokes when using the tab key, and you will be sure your typed path is correct!
ls
ls command is used to list names of files and sub-directories of a directory.
datasoft@datasoft-linux:~$ ls
abc.txt Documents examples.desktop Pictures Templates Videos
Desktop Downloads Music Public typescript
datasoft@datasoft-linux:~$
ls -a
Lists all the files in the directory, including the hidden files (.filename).
datasoft@datasoft-linux:~$ ls
abc.txt Documents examples.desktop Pictures Templates Videos
Desktop Downloads Music Public typescript
datasoft@datasoft-linux:~$ ls -a
. .cache Downloads Pictures Videos
.. .compiz examples.desktop .pki .Xauthority
abc.txt .config .gconf .profile .xsession-errors
.bash_history Desktop .ICEauthority Public .xsession-errors.old
.bash_logout .dmrc .local Templates
.bashrc Documents Music typescript
datasoft@datasoft-linux:~$
The .. and . at the top of your list refer to the parent directory and the current directory, respectively.
Each directory file has an entry for itself (the dot file (.)) and for its parent directory (the dot-dot file (.. ). The -a option can be combined with -l option.
ls -l
Lists details about contents, including permissions (modes), owner, group, size, creation date, whether the file is a link to somewhere else on the system and where its link points.
datasoft@datasoft-linux:~$ ls -l
total 48
-rw-rw-r-- 1 datasoft datasoft 729 Jul 28 15:34 abc.txt
drwxr-xr-x 2 datasoft datasoft 4096 Jun 20 12:41 Desktop
drwxr-xr-x 2 datasoft datasoft 4096 Jun 20 12:41 Documents
drwxr-xr-x 2 datasoft datasoft 4096 Jun 20 12:41 Downloads
-rw-r--r-- 1 datasoft datasoft 8980 Jun 20 11:42 examples.desktop
drwxr-xr-x 2 datasoft datasoft 4096 Jun 20 12:41 Music
drwxr-xr-x 2 datasoft datasoft 4096 Jun 20 12:41 Pictures
drwxr-xr-x 2 datasoft datasoft 4096 Jun 20 12:41 Public
drwxr-xr-x 2 datasoft datasoft 4096 Jun 20 12:41 Templates
-rw-rw-r-- 1 datasoft datasoft 0 Jul 28 16:21 typescript
drwxr-xr-x 2 datasoft datasoft 4096 Jun 20 12:41 Videos
datasoft@datasoft-linux:~$
The output shown in above file displays the :
- Column-1 : File type - First character.
- Column-2 : No of links
- FAP : Column-1, 2nd-10th characters
- Column-3 : File Owner (user name)
- Column-4 : Group Owner (group name)
- Column-5 : File size (in bytes)
- Column-6,7 & 8 : Last modification of the file.
- Column-9 : File Owner (user name)
ls -lh
Another frequently used ls option is -h. It shows the numbers (file sizes) in a more human readable format. Also shown below is some variation in the way you can give the options to ls. We will explain the details of the output later in this book.
datasoft@datasoft-linux:~$ ls -l -h
total 52K
-rw-rw-r-- 1 datasoft datasoft 729 Jul 28 15:34 abc.txt
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Desktop
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Documents
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Downloads
-rw-r--r-- 1 datasoft datasoft 8.8K Jun 20 11:42 examples.desktop
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Music
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Pictures
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Public
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Templates
-rw-rw-r-- 1 datasoft datasoft 4.0K Jul 28 16:46 typescript
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Videos
datasoft@datasoft-linux:~$ ls -lh
total 52K
-rw-rw-r-- 1 datasoft datasoft 729 Jul 28 15:34 abc.txt
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Desktop
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Documents
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Downloads
-rw-r--r-- 1 datasoft datasoft 8.8K Jun 20 11:42 examples.desktop
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Music
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Pictures
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Public
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Templates
-rw-rw-r-- 1 datasoft datasoft 4.0K Jul 28 16:46 typescript
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Videos
datasoft@datasoft-linux:~$ ls -hl
total 52K
-rw-rw-r-- 1 datasoft datasoft 729 Jul 28 15:34 abc.txt
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Desktop
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Documents
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Downloads
-rw-r--r-- 1 datasoft datasoft 8.8K Jun 20 11:42 examples.desktop
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Music
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Pictures
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Public
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Templates
-rw-rw-r-- 1 datasoft datasoft 4.0K Jul 28 16:46 typescript
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Videos
datasoft@datasoft-linux:~$ ls -h -l
total 52K
-rw-rw-r-- 1 datasoft datasoft 729 Jul 28 15:34 abc.txt
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Desktop
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Documents
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Downloads
-rw-r--r-- 1 datasoft datasoft 8.8K Jun 20 11:42 examples.desktop
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Music
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Pictures
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Public
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Templates
-rw-rw-r-- 1 datasoft datasoft 4.0K Jul 28 16:46 typescript
drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Videos
datasoft@datasoft-linux:~$
mkdir
mkdir command is used to create a new dir. You have to give at least one parameter to mkdir, the name of the new directory to be created. Think before you type a leading / .
datasoft@datasoft-linux:~/MyDir$ mkdir test
datasoft@datasoft-linux:~/MyDir$ cd test
datasoft@datasoft-linux:~/MyDir/test$ ls -al
total 8
drwxrwxr-x 2 datasoft datasoft 4096 Jul 28 16:51 .
drwxrwxr-x 5 datasoft datasoft 4096 Jul 28 16:51 ..
datasoft@datasoft-linux:~/MyDir/test$ mkdir staff
datasoft@datasoft-linux:~/MyDir/test$ mkdir casualstaff
datasoft@datasoft-linux:~/MyDir/test$ ls -l
total 8
drwxrwxr-x 2 datasoft datasoft 4096 Jul 28 16:53 casualstaff
drwxrwxr-x 2 datasoft datasoft 4096 Jul 28 16:52 staff
datasoft@datasoft-linux:~/MyDir/test$
mkdir -p
When given the option -p, then mkdir will create parent directories as needed.
datasoft@datasoft-linux:~$ mkdir -p test3/Mytest/sample
datasoft@datasoft-linux:~$ ls test3
Mytest
datasoft@datasoft-linux:~$ ls test3/Mytest
sample
datasoft@datasoft-linux:~$ ls test3/Mytest/sample
datasoft@datasoft-linux:~$
rmdir
rmdir is used to remove an empty directory.
datasoft@datasoft-linux:~$ rmdir test3/Mytest/sample
datasoft@datasoft-linux:~$ ls
abc.txt Documents examples.desktop MyDir Public test3 Videos
Desktop Downloads Music Pictures Templates typescript
datasoft@datasoft-linux:~$ cd ..
datasoft@datasoft-linux:~$ rmdir test3/Mytest/sample
datasoft@datasoft-linux:~$
rmdir -p
And similar to the mkdir -p option, you can also use rmdir to recursively remove directories.
datasoft@datasoft-linux:~$ mkdir -p dir/subdir/subdir2
datasoft@datasoft-linux:~$ rmdir -p dir/subdir/subdir2
datasoft@datasoft-linux:~$
Exercise and Solution:
1. Display your current directory.
pwd
2. Change to the /etc directory.
Code:
cd /etc
3. Now change to your home directory using only three key presses.
Code:
cd (and the enter key)
4. Change to the /boot/grub directory using only eleven key presses.
Code:
cd /boot/grub (use the tab key)
5. Go to the parent directory of the current directory.
Code:
cd .. (with space between cd and ..)
6. Go to the root directory.
Code:
cd /
7. List the contents of the root directory.
Code:
ls
8. List a long listing of the root directory.
Code:
ls -l/
9. Stay where you are, and list the contents of /etc.
Code:
ls /etc
10. Stay where you are, and list the contents of /bin and /sbin.
Code:
ls /bin /sbin
11. Stay where you are, and list the contents of ~.
Code:
ls ~
12. List all the files (including hidden files) in your home directory.
Code:
ls -al ~
13. List the files in /boot in a human readable format.
Code:
ls -lh /boot
14. Create a directory testdir in your home directory.
Code:
mkdir ~/testdir
15. Change to the /etc directory, stay here and create a directory newdir in your home directory.
Code:
cd /etc ; mkdir ~/newdir
16. Create in one command the directories ~/dir1/dir2/dir3 (dir3 is a subdirectory from dir2,
and dir2 is a subdirectory from dir1 ).
Code:
mkdir -p ~/dir1/dir2/dir3
17. Remove the directory testdir.
Code:
rmdir testdir
18. If time permits (or if you are waiting for other students to finish this practice), use and understand pushd and popd. Use the man page of bash to find information about these commands.
Code:
man bash
Previous:
Linux - Control-operators
Next:
Linux - Working with files
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/linux-system-administration/working-with-directories.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics