How to list contents of a directory ?

To list the contents of a directory in linux use the ls command. ls command when used with out any argument will display the contents of the current directory.


usage:
$ ls
Downloads                                   httpd-devel-2.2.3-53.el5_7.3.i386.rpm   README.txt
httpd-2.2.3-53.el5_7.3.i386.rpm  httpd-manual-2.2.3-53.el5_7.3.i386.rpm  RHEL6-Package-en-US.pdf


To see the contents of  /opt directory from the current directory with ls command

usage:
$ ls /opt

Adobe         google    LifeStudioBackup  nikto-2.1.4   PostgreSQL   teamviewer9    whatweb-0.4.7
clamav-0.97.3  firefox        kompozer  nessus            PostgresPlus  teamviewer8  testdisk-6.13

ls alone won't display files starting with '.' (dot). They are hidden from the display. To display all the contents including the ones starting with the '.' run ls command with -a


usage:
$ ls -a 

 .                    .bash_profile   httpd-2.2.3-53.el5_7.3.i386.rpm              .mozilla          
 ..                   .bashrc           httpd-devel-2.2.3-53.el5_7.3.i386.rpm     README.txt
 .bash_history  .config            httpd-manual-2.2.3-53.el5_7.3.i386.rpm   RHEL6-Package-en-US.pdf
.bash_logout    Downloads      .lesshst                                                     .xauthR4fnPz  

To view long list use ls with option -l

usage: $ ls -l
$ ls -l

total 3220 
drwxrwxr-x  2 calypso calypso    4096 Feb  4 13:22 Downloads
-rw-r--r--  1 calypso calypso 1288684 Feb  4 13:24 httpd-2.2.3-53.el5_7.3.i386.rpm
-rw-r--r--  1 calypso calypso  154457 Feb  4 13:24 httpd-devel-2.2.3-53.el5_7.3.i386.rpm
-rw-r--r--  1 calypso calypso  834919 Feb  4 13:24 httpd-manual-2.2.3-53.el5_7.3.i386.rpm
-rw-r--r--. 1 calypso calypso    4244 Sep 26  2011 README.txt
-rw-------  1 calypso calypso 1001207 Jan  7 12:27 RHEL6-Package-en-US.pdf
ls -l
As you can see in the above example ls -l will display fields, which represents the type of file and its mode bits, number of hard links, owner name, group name, size, time stamp and the file name, in that order. The fifth field represents the file size in bytes. For a more human readable format you can add the option h along with ls -l 

usage:
$ ls -lh

total 3.2 M
drwxrwxr-x  2 calypso calypso  4.0K Feb  4 13:22 Downloads
-rw-r--r--  1 calypso calypso  1.3M Feb  4 13:24 httpd-2.2.3-53.el5_7.3.i386.rpm

-rw-r--r--  1 calypso calypso  151K Feb  4 13:24 httpd-devel-2.2.3-53.el5_7.3.i386.rpm
-rw-r--r--  1 calypso calypso  816K Feb  4 13:24 httpd-manual-2.2.3-53.el5_7.3.i386.rpm
-rw-r--r--. 1 calypso calypso  4.2K Sep 26  2011 README.txt
-rw-------  1 calypso calypso  978K Jan  7 12:27 RHEL6-Package-en-US.pdf

 By default ls will display the file names sorted by alphabetic order (a to z). To view the files in reverse order you can use the -r option. To sort the files by time stamp or modification time, use with option -t (newest first). And to sort by file size (largest first) use ls with option -S

For example if you want to view the contents of a directory in long list  format sorted by modification time with newest being the first use the command as ls -lt

usage:
$ ls -lt

total 3220
-rw-r--r--  1 calypso calypso  834919 Feb  4 13:24 httpd-manual-2.2.3-53.el5_7.3.i386.rpm
-rw-r--r--  1 calypso calypso  154457 Feb  4 13:24 httpd-devel-2.2.3-53.el5_7.3.i386.rpm
-rw-r--r--  1 calypso calypso 1288684 Feb  4 13:24 httpd-2.2.3-53.el5_7.3.i386.rpm
drwxrwxr-x  2 calypso calypso    4096 Feb  4 13:22 Downloads
-rw-------  1 calypso calypso 1001207 Jan  7 12:27 RHEL6-Package-en-US.pdf
-rw-r--r--. 1 calypso calypso    4244 Sep 26  2011 README.txt

To view the the above result  in the reverse order (oldest first) run the command as


usage:
$ ls -ltr

total 3220
-rw-r--r--. 1 calypso calypso    4244 Sep 26  2011 README.txt
-rw-------  1 calypso calypso 1001207 Jan  7 12:27 RHEL6-Package-en-US.pdf
drwxrwxr-x  2 calypso calypso    4096 Feb  4 13:22 Downloads
-rw-r--r--  1 calypso calypso 1288684 Feb  4 13:24 httpd-2.2.3-53.el5_7.3.i386.rpm
-rw-r--r--  1 calypso calypso  154457 Feb  4 13:24 httpd-devel-2.2.3-53.el5_7.3.i386.rpm
-rw-r--r--  1 calypso calypso  834919 Feb  4 13:24 httpd-manual-2.2.3-53.el5_7.3.i386.rpm

To view the contents of a directory in human readable, long list  format sorted by size (largest first), run ls command with the following options


$ ls -lSh

total 3.2M
-rw-r--r--  1 calypso calypso 1.3M Feb  4 13:24 httpd-2.2.3-53.el5_7.3.i386.rpm
-rw-------  1 calypso calypso 978K Jan  7 12:27 RHEL6-Package-en-US.pdf
-rw-r--r--  1 calypso calypso 816K Feb  4 13:24 httpd-manual-2.2.3-53.el5_7.3.i386.rpm
-rw-r--r--  1 calypso calypso 151K Feb  4 13:24 httpd-devel-2.2.3-53.el5_7.3.i386.rpm
-rw-r--r--. 1 calypso calypso 4.2K Sep 26  2011 README.txt
drwxrwxr-x  2 calypso calypso 4.0K Feb  4 13:22 Downloads

For more details on ls command see the man page

Comments

Popular posts from this blog

Understanding awk command with examples

Understanding sed command with example -Part 1

How to install a Software in Linux