06 January 2012

ls Command in Linux/Unix

Leave a Comment
ls Command in Linux/Unix


                                            ls command list the content of the directory.

Syntax


                         ls  [options]  dir-name


Example
$ ls
abc.lst  def.lst           examples.desktop     jscript           pqr.lst           sandeep.lst  
css       Desktop        html                          Music          Public           shell
css1     Documents   java                           newfile        sandeep        Templates
cut1     Downloads   js                               Pictures       sandeep2.lst  Videos
 xyz.lst
$
* if no directory name is specified then  ls list the content of current directory , it does not shows the hidden files.


ls option


1. -l : This option used for long listing of the directory content.

Syntax


                  ls  -l   dir-name


Example
$ ls -l
total 96
-rw-r--r-- 1 sandeep sandeep 31 2012-01-01 20:10 abc.lst
drwxr-xr-x 2 sandeep sandeep 4096 2011-12-27 13:44 css
drwxr-xr-x 3 sandeep sandeep 4096 2012-01-03 19:50 css1
-rw-r--r-- 1 sandeep sandeep 48 2012-01-08 10:59 cut1
-rw-r--r-- 1 sandeep sandeep 31 2012-01-08 10:53 def.lst
drwxr-xr-x 3 sandeep sandeep 4096 2012-01-07 10:56 Desktop
drwxr-xr-x 4 sandeep sandeep 4096 2011-10-05 18:38 Documents
drwx------ 5 sandeep sandeep 4096 2012-01-07 11:33 Downloads
-rw-r--r-- 1 sandeep sandeep 357 2011-10-02 14:14 examples.desktop
drwxr-xr-x 3 sandeep sandeep 4096 2012-01-01 11:42 html
drwxr-xr-x 2 sandeep sandeep 4096 2011-12-13 20:42 java

* 1st column is permission on the file or directory .
2nd column is number of links for that file.
3rd  and 4th column is owner name and group name to which file or directory belongs.
5th column contain size of the file or directory.
6th and 7th column is last modified date and time.
8th column is file or directory name.
  
               

2. -a : This option used for display all files and directory(hidden also) of the current directory .

Syntax


                  ls  -a   dir-name


Example
$ ls -a
.                        examples.desktop                       .macromedia shell
..                      .fontconfig .mission-control        .sh_history
abc.lst              .gconf                                          .mozilla .ssh
.adobe               .gconfd                                     Music      
.appletviewer    .gegl-0.0                                  .nautilus .swp
 .sudo_as_admin_successful
$

* .(dot) represent current directory and ..(dot dot) represent parent directory.
* All files starting with .(dot) are a hidden files.



3. -d : This option used for display only dir-name if dir-name is a directory , it will not going to show content of directory.

Syntax


                  ls  -d   dir-name


Example
$ ls -d
.
$ ls -d /
/
$ ls -ld /etc
drwxr-xr-x 150 root root 140 2012-01-08 11:42 /etc
$




4. -x : This option used for display multi-columnar output. .

Syntax


                  ls  -x   dir-name


Example
$ ls
abc.lst  def.lst           examples.desktop   jscript         pqr.lst         sandeep.lst   xyz.lst
css       Desktop        html                        Music        Public          shell
css1     Documents   java                         newfile      sandeep        Templates
cut1     Downloads   js                             Pictures     sandeep2.lst  Videos

$ ls -x
abc.lst           css                 css1                        cut1   def.lst      Desktop
Documents   Downloads   examples.desktop    html   java          js
jscript            Music           newfile                   Pictures pqr.lst     Public
sandeep        sandeep2.lst   sandeep.lst             shell     Templates Videos
xyz.lst
$





5. -R : This option used for display Recursive output .

Syntax


                  ls  -R   dir-name


Example

$ ls
abc.lst  def.lst           examples.desktop     jscript           pqr.lst           sandeep.lst
css       Desktop        html                          Music          Public           shell
css1     Documents   java                           newfile        sandeep        Templates
cut1     Downloads   js                               Pictures       sandeep2.lst  Videos
 xyz.lst


$ ls -R css1
css1:
css index.html

css1/css:
index.html
$




6. -F : Mark executable with *  , directories with / and  , symbolic link with @.

Syntax


                  ls  -F   dir-name


Example
$ cd /

$ ls -F
bin/     dev/      initrd.img@     lost+found/

$ cd

$ ls -F
abc.lst       def.lst      examples.desktop     jscript/

$ ls -F shell
abc.lst    customer_report      fact.sh*    shell1.sh*    shell3.sh*


7. -i : Display inode number.

Syntax


                  ls  -i   dir-name


Example
$ls -i
4764 abc.lst   4732 Downloads            4770 newfile     4749 shell
4766 css       4736 examples.desktop   4638 Pictures     3982 Templates

* Every number before file or directory is inode number of that file or directory.

you can combine different options of ls to produce more useful results like

a.   ls  -al 

$ ls -al
total 572
drwxr-xr-x 66 sandeep sandeep 480 2012-01-08 11:29 .
drwxr-xr-x 5 root root 60 2011-12-18 10:46 ..
-rw-r--r-- 1 sandeep sandeep 31 2012-01-01 20:10 abc.lst
drwx------ 3 sandeep sandeep 4096 2011-10-05 18:34 .adobe
-rw-r--r-- 1 sandeep sandeep 140 2011-11-25 21:23 .appletviewer
-rw------- 1 sandeep sandeep 5945 2012-01-08 11:32 .bash_history
-rw-r--r-- 1 sandeep sandeep 220 2011-10-02 14:14 .bash_logout
-rw-r--r-- 1 sandeep sandeep 3115 2011-10-02 14:14 .bashrc
drwxr-xr-x 2 sandeep sandeep 4096 2011-12-31 10:14 .bluefish
drwx------ 21 sandeep sandeep 140 2012-01-08 10:21 .cache


b.   ls  -il


$ ls -li
total 96
4764 -rw-r--r-- 1 sandeep sandeep 31 2012-01-01 20:10 abc.lst
4766 drwxr-xr-x 2 sandeep sandeep 4096 2011-12-27 13:44 css
4747 drwxr-xr-x 3 sandeep sandeep 4096 2012-01-03 19:50 css1
4760 -rw-r--r-- 1 sandeep sandeep 48 2012-01-08 10:59 cut1
4737 -rw-r--r-- 1 sandeep sandeep 31 2012-01-08 10:53 def.lst
180 drwxr-xr-x 3 sandeep sandeep 4096 2012-01-07 10:56 Desktop
4636 drwxr-xr-x 4 sandeep sandeep 4096 2011-10-05 18:38 Documents
4732 drwx------ 5 sandeep sandeep 4096 2012-01-07 11:33 Downloads
4736 -rw-r--r-- 1 sandeep sandeep 357 2011-10-02 14:14 examples.desktop

*first column is inode number.

If You Liked This Post Please Take a Time To Share This Post

You May Also Like...

0 comments:

Post a Comment