![]() |
| Linux: Working with Directories - Module2 |
Module1 Module2 Practice
4) ls
You can list the contents of a directory with ls.
dhiru@dhiru:~$ ls
wintr.txt hot.txt services sumr.txt
4.1) ls -a
It is used to show all files means including the hidden files.
When a file name on a Linux file system starts with a dot, it is considered
a hidden file and it doesn't show up in regular file listings.
dhiru@dhiru:~$ ls
. Desktop .mozilla Pictures
.. .dmrc .cache .pki
all Documents .mysql_history .profile
.bash_history Downloads .nbi Public
.bash_logout .gconf
4.2) ls -l
Many times you will be using options with ls to display the contents of the directory in
different formats or to display different parts of the directory.
Typing ls -l (that is a letter L, not the number 1) gives you a long listing.
dhiru@dhiru:~$ ls -l
total 2304
-rw-r--r-- 1 dhiru dhiru 24 Jun 25 13:57 all
drwxr-xr-x 8 dhiru dhiru 4096 Jul 1 13:37 Desktop
drwxr-xr-x 2 dhiru dhiru 4096 Jul 10 2016 Documents
drwxr-xr-x 9 dhiru dhiru 12288 Jun 29 15:14 Downloads
drwxr-xr-x 2 dhiru dhiru 4096 Oct 22 2016 Pictures
drwxr-xr-x 2 dhiru dhiru 4096 Jul 10 2016 Public
-rw-r--r-- 1 dhiru dhiru 198 Jun 27 12:12 tailing.txt
drwxr-xr-x 2 dhiru dhiru 4096 Jul 10 2016 Templates
4.3) 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.
Note:- We use the letter L as an option in example, not the number 1.
dhiru@dhiru:~$ ls -lh
total 2.3M
-rw-r--r-- 1 dhiru dhiru 24 Jun 25 13:57 all
drwxr-xr-x 8 dhiru dhiru 4.0K Jul 1 13:37 Desktop
drwxr-xr-x 2 dhiru dhiru 4.0K Jul 10 2016 Documents
drwxr-xr-x 9 dhiru dhiru 12K Jun 29 15:14 Downloads
drwxr-xr-x 2 dhiru dhiru 4.0K Oct 22 2016 Pictures
drwxr-xr-x 2 dhiru dhiru 4.0K Jul 10 2016 Public
-rw-r--r-- 1 dhiru dhiru 198 Jun 27 12:12 tailing.txt
drwxr-xr-x 2 dhiru dhiru 4.0K Jul 10 2016 Templates
drwxr-xr-x 2 dhiru dhiru 4.0K Jul 10 2016 Videos
Note:- You can use also ls -hl, ls -l -h, ls -h -l .
5) mkdir
You can create your own directories with mkdir.Type mkdir followed by the name of the new directory to be created. Think before you type a leading / .
dhiru@dhiru:~$ mkdir pri
dhiru@dhiru:~$ cd pri
dhiru@dhiru:~/pri$ ls -al
total 8
drwxr-xr-x 2 dhiru dhiru 4096 Sep 17 00:07 .
drwxr-xr-x 48 dhiru dhiru 4096 Sep 17 00:07 ..
dhiru@dhiru:~/pri$ mkdir pri1
dhiru@dhiru:~/pri$ mkdir pri2
dhiru@dhiru:~/pri$ ls -l
total 8
drwxr-xr-x 2 dhiru dhiru 4096 Sep 17 00:08 pri2
drwxr-xr-x 2 dhiru dhiru 4096 Sep 17 00:08 pri1
dhiru@dhiru:~/pri$
5.1) mkdir -p
This command is used to create sub-directories within directories
For example:
dhiru@dhiru:~$ mkdir dhir/dhir1/dhir2
mkdir: cannot create directory ‘dhir/dhir1/dhir2’: No such file or directory
In the above example we can't create directory.
When given the option -p, then mkdir will create parent directories as needed.
dhiru@dhiru:~$ mkdir -p dhir/dhir1/dhir2
dhiru@dhiru:~$ cd dhir
dhiru@dhiru:~/dhir$ ls -l
total 4
drwxr-xr-x 3 dhiru dhiru 4096 Sep 17 00:11 dhir1
dhiru@dhiru:~/dhir$ cd dhir1
dhiru@dhiru:~/dhir/dhir1$ ls -l
total 4
drwxr-xr-x 2 dhiru dhiru 4096 Sep 17 00:11 dhir2
dhiru@dhiru:~/dhir/dhir1 cd dhir2/
dhiru@dhiru:~/dhir/dhir1/dhir2$ pwd
/home/dhiru/dhir/dhir1/dhir2
6) rmdir
When a directory is empty, you can use rmdir to remove the directory.
dhiru@dhiru:~/pri$ ls -l
total 8
drwxr-xr-x 2 dhiru dhiru 4096 Sep 17 00:08 pri2
drwxr-xr-x 2 dhiru dhiru 4096 Sep 17 00:08 pri1
dhiru@dhiru:~/pri$ rmdir pri2
dhiru@dhiru:~/pri$ cd ..
dhiru@dhiru:~$ rmdir pri
rmdir: failed to remove ‘pri’: Directory not empty
dhiru@dhiru:~$ rmdir pri/pri1
dhiru@dhiru:~$ rmdir pri
dhiru@dhiru:~$
6.1) rmdir -p
Similar to the mkdir -p option, you can also use rmdir followed by -p to recursively remove directories.
dhiru@dhiru:~$ mkdir -p test/test1
dhiru@dhiru:~$ rmdir -p test/test1
dhiru@dhiru:~$
Module1 Module2 Practice

Comments
Post a Comment