Here I am going to show you some frequently used commands on a Unix / Linux running computer system.
In this post we will see how to do a directory listing of files and directories.
Command: ls [Options] [FileOrDirectoryName]
Usage:
$ whatis ls
ls (1) - list directory contents
Examples:
$ ls
bin bin.tar.bzip2 co contacts contacts-new hello hi Mail man mbox mysql-init test
$
Only directory and file names are listed. No extra or detailed information is displayed along with them.
We can also pass a file or directory name along with ls
$ ls binIn the above example we have passed the directory name "bin" as the argument to the command ls so that its contents are displayed.
case child.sh contacts hello parent.sh readLine.sh variable.sh
case.man compVar.sh dumpHTML.sh manifest readFile.sh scriptPid.sh
$
We can also pass a file name. If it exists then the file name itself is echoed otherwise we get an error:
$ ls contactsFor a detailed information we can use "-l" option:
contacts
$ ls salary
ls: salary: No such file or directory
$
$ ls -l
total 48
-rwxr-xr-x 1 root root 243 Aug 26 19:15 case
-rw-r--r-- 1 root root 289 Aug 26 19:51 case.man
-rwxr-xr-x 1 root root 26 Aug 30 16:28 child.sh
-rw-r--r-- 1 root root 402 Aug 31 16:20 compVar.sh
-rw-r--r-- 1 root root 162 Aug 30 18:58 contacts
-rwxr-xr-x 1 root root 376 Sep 2 20:59 dumpHTML.sh
-rw-r--r-- 1 root root 0 Sep 5 18:59 hello
-rwxr-xr-x 1 root root 156 Aug 27 14:22 manifest
$
To include hidden files (file and directory names that begin with a dot ".") we can pass "-a" option:
$ ls -a
. .bashrc contacts .gstreamer-0.8 Mail mysql-init .Xauthority
.. bin contacts-new .gtkrc man .ssh
.bash_history bin.tar.bzip2 .fonts.cache-1 hello mbox test
.bash_profile co .gnome hi .mysql_history .viminfo
$
Sorting the directory list displaying largest to smallest files and directories:
$ ls -lSSorting the directory list displaying newest to oldest files and directories:
total 64
-rw-r--r-- 1 root root 10240 Oct 6 21:50 hello
-rw-r--r-- 1 root root 10240 Oct 6 21:54 hi
drwxr-xr-x 2 root root 4096 Sep 5 18:59 bin
drwx------ 2 root root 4096 Sep 26 15:34 Mail
drwxr-xr-x 3 root root 4096 Aug 26 20:59 man
drwxr-xr-x 3 root root 4096 Sep 17 14:22 test
-rw-r--r-- 1 root root 1739 Oct 6 22:12 bin.tar.bzip2
-rw------- 1 root root 784 Sep 26 15:34 mbox
-rw-rw-rw- 1 root root 218 Oct 10 16:13 contacts
-rw-r--r-- 1 root root 210 Oct 10 16:04 contacts-new
-rw-r--r-- 1 root root 207 Oct 6 22:02 co
-rw-r--r-- 1 root root 85 Sep 21 18:53 mysql-init
$
$ ls -ltTo reverse the sorting, i.e. to sort in descending order, we can use "-r" option. We can use any combination of options. For example to display all the files and directories, including the hidden ones, from oldest to newest we can issue the following command:
total 64
-rw-rw-rw- 1 root root 218 Oct 10 16:13 contacts
-rw-r--r-- 1 root root 210 Oct 10 16:04 contacts-new
-rw-r--r-- 1 root root 1739 Oct 6 22:12 bin.tar.bzip2
-rw-r--r-- 1 root root 207 Oct 6 22:02 co
-rw-r--r-- 1 root root 10240 Oct 6 21:54 hi
-rw-r--r-- 1 root root 10240 Oct 6 21:50 hello
-rw------- 1 root root 784 Sep 26 15:34 mbox
drwx------ 2 root root 4096 Sep 26 15:34 Mail
-rw-r--r-- 1 root root 85 Sep 21 18:53 mysql-init
drwxr-xr-x 3 root root 4096 Sep 17 14:22 test
drwxr-xr-x 2 root root 4096 Sep 5 18:59 bin
drwxr-xr-x 3 root root 4096 Aug 26 20:59 man
$ ls -alrtBy default only directory names are displayed, i.e. their contents are not displayed. To display sub-directories and their contents recursively we can use "-R" option as shown below:
total 132
drwx------ 2 root root 4096 Jul 27 2004 .gnome
drwxr-xr-x 2 root root 4096 Jul 27 2004 .gstreamer-0.8
-rw-r--r-- 1 root root 59 Sep 16 2004 .fonts.cache-1
-rw-r--r-- 1 root root 120 Oct 25 2004 .gtkrc
drwx------ 2 root root 4096 Mar 29 2005 .ssh
-rw------- 1 root root 259 Mar 29 2005 .Xauthority
-rw-r--r-- 1 root root 207 Aug 26 20:58 .bash_profile
drwxr-xr-x 3 root root 4096 Aug 26 20:59 man
drwxr-xr-x 2 root root 4096 Sep 5 18:59 bin
drwxr-xr-x 3 root root 4096 Sep 17 14:22 test
-rw-r--r-- 1 root root 85 Sep 21 18:53 mysql-init
drwx------ 2 root root 4096 Sep 26 15:34 Mail
-rw------- 1 root root 784 Sep 26 15:34 mbox
-rw-r--r-- 1 root root 10240 Oct 6 21:50 hello
-rw-r--r-- 1 root root 10240 Oct 6 21:54 hi
-rw-r--r-- 1 root root 207 Oct 6 22:02 co
-rw-r--r-- 1 root root 1739 Oct 6 22:12 bin.tar.bzip2
-rwxr-xr-x 1 root root 50 Oct 8 15:44 .bashrc
-rw-r--r-- 1 root root 210 Oct 10 16:04 contacts-new
-rw-rw-rw- 1 root root 218 Oct 10 16:13 contacts
-rw------- 1 root root 6215 Oct 10 16:13 .viminfo
drwxr-xr-x 19 root root 4096 Oct 19 19:13 ..
-rw------- 1 root root 3107 Oct 19 19:55 .mysql_history
-rw------- 1 root root 11437 Oct 19 19:55 .bash_history
drwxr-xr-x 2 root root 4096 Oct 19 20:29 .hidden
drwxr-xr-x 10 root root 4096 Oct 19 20:29 .
$ ls -RThere are many other options available with the command "ls". We can check out the manual page by issuing the following command:
.:
bin bin.tar.bzip2 co contacts contacts-new hello hi Mail man mbox mysql-init test
./bin:
case child.sh contacts hello parent.sh readLine.sh variable.sh
case.man compVar.sh dumpHTML.sh manifest readFile.sh scriptPid.sh
./Mail:
./man:
case.l.gz cat1
./man/cat1:
./test:
tube
./test/tube:
go goback year
$
$ man ls
No comments:
Post a Comment