ls - Unix, Linux Command

ls - list directory contents.
ls [OPTION]... [FILE]... 

ls [OPTION]... [FILE]... 
ls List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
Mandatory arguments to long options are mandatory for short options too.
Example-1:
To list all files of current directory:
$ ls
output:
# lsbin   dev  home        lib    lost+found  mnt  proc  run   srv  tmp  varboot  etc  initrd.img  lib64  media       opt  root  sbin  sys  usr  vmlinuz
Example-2:
output:
# ls -1binbootdevetchomeinitrd.imgliblib64lost+foundmediamntoptprocrootrunsbinsrvsystmpusrvarvmlinuz
Example-3:
To display all information about files/directories:
$ ls -l
output:
# ls -ltotal 81drwxr-xr-x   2 root root  4096 Dec 28 05:25 bindrwxr-xr-x   4 root root  1024 Dec 28 05:27 bootdrwxr-xr-x  17 root root  4320 Jan  7 17:57 devdrwxr-xr-x  97 root root  4096 Jan  7 18:18 etcdrwxr-xr-x   4 root root  4096 Jan  6 12:43 homelrwxrwxrwx   1 root root    33 Dec 28 05:08 initrd.img -> boot/initrd.img-3.19.0-25-genericdrwxr-xr-x  23 root root  4096 Dec 28 05:25 libdrwxr-xr-x   2 root root  4096 Dec 28 05:07 lib64drwx------   2 root root 16384 Dec 28 05:07 lost+founddrwxr-xr-x   3 root root  4096 Dec 28 05:07 mediadrwxr-xr-x   2 root root  4096 Apr 11  2014 mntdrwxr-xr-x   2 root root  4096 Aug  5  2015 optdr-xr-xr-x 167 root root     0 Jan  7 17:57 procdrwx------   2 root root  4096 Jan  7 18:12 rootdrwxr-xr-x  19 root root   680 Jan  7 20:41 rundrwxr-xr-x   2 root root 12288 Dec 28 05:28 sbindrwxr-xr-x   2 root root  4096 Aug  5  2015 srvdr-xr-xr-x  13 root root     0 Jan  7 17:57 sysdrwxrwxrwt   2 root root  4096 Jan  7 20:17 tmpdrwxr-xr-x  10 root root  4096 Dec 28 05:07 usrdrwxr-xr-x  12 root root  4096 Dec 28 05:25 varlrwxrwxrwx   1 root root    30 Dec 28 05:08 vmlinuz -> boot/vmlinuz-3.19.0-25-generic
Example-4:
output:
# ls -ld /etcdrwxr-xr-x 97 root root 4096 Jan  7 18:18 /etc
Example-5:
To order files based on last modified time:
$ ls -lt
output: 
# ls -lttotal 81dr-xr-xr-x  13 root root     0 Jan  7 20:44 sysdrwxr-xr-x  19 root root   680 Jan  7 20:41 rundrwxrwxrwt   2 root root  4096 Jan  7 20:17 tmpdrwxr-xr-x  97 root root  4096 Jan  7 18:18 etcdrwx------   2 root root  4096 Jan  7 18:12 rootdrwxr-xr-x  17 root root  4320 Jan  7 17:57 devdr-xr-xr-x 166 root root     0 Jan  7 17:57 procdrwxr-xr-x   4 root root  4096 Jan  6 12:43 homedrwxr-xr-x   2 root root 12288 Dec 28 05:28 sbindrwxr-xr-x   4 root root  1024 Dec 28 05:27 bootdrwxr-xr-x  12 root root  4096 Dec 28 05:25 vardrwxr-xr-x   2 root root  4096 Dec 28 05:25 bindrwxr-xr-x  23 root root  4096 Dec 28 05:25 liblrwxrwxrwx   1 root root    30 Dec 28 05:08 vmlinuz -> boot/vmlinuz-3.19.0-25-genericlrwxrwxrwx   1 root root    33 Dec 28 05:08 initrd.img -> boot/initrd.img-3.19.0-25-genericdrwxr-xr-x   3 root root  4096 Dec 28 05:07 mediadrwxr-xr-x  10 root root  4096 Dec 28 05:07 usrdrwxr-xr-x   2 root root  4096 Dec 28 05:07 lib64drwx------   2 root root 16384 Dec 28 05:07 lost+founddrwxr-xr-x   2 root root  4096 Aug  5  2015 optdrwxr-xr-x   2 root root  4096 Aug  5  2015 srvdrwxr-xr-x   2 root root  4096 Apr 11  2014 mnt
Example-6:
To order files based on last modified time(In Reverse Order):
$ ls -ltr
output:
# ls -ltrtotal 81drwxr-xr-x   2 root root  4096 Apr 11  2014 mntdrwxr-xr-x   2 root root  4096 Aug  5  2015 srvdrwxr-xr-x   2 root root  4096 Aug  5  2015 optdrwx------   2 root root 16384 Dec 28 05:07 lost+founddrwxr-xr-x   2 root root  4096 Dec 28 05:07 lib64drwxr-xr-x  10 root root  4096 Dec 28 05:07 usrdrwxr-xr-x   3 root root  4096 Dec 28 05:07 medialrwxrwxrwx   1 root root    33 Dec 28 05:08 initrd.img -> boot/initrd.img-3.19.0-25-genericlrwxrwxrwx   1 root root    30 Dec 28 05:08 vmlinuz -> boot/vmlinuz-3.19.0-25-genericdrwxr-xr-x  23 root root  4096 Dec 28 05:25 libdrwxr-xr-x   2 root root  4096 Dec 28 05:25 bindrwxr-xr-x  12 root root  4096 Dec 28 05:25 vardrwxr-xr-x   4 root root  1024 Dec 28 05:27 bootdrwxr-xr-x   2 root root 12288 Dec 28 05:28 sbindrwxr-xr-x   4 root root  4096 Jan  6 12:43 homedr-xr-xr-x 166 root root     0 Jan  7 17:57 procdrwxr-xr-x  17 root root  4320 Jan  7 17:57 devdrwx------   2 root root  4096 Jan  7 18:12 rootdrwxr-xr-x  97 root root  4096 Jan  7 18:18 etcdrwxrwxrwt   2 root root  4096 Jan  7 20:17 tmpdrwxr-xr-x  19 root root   680 Jan  7 20:41 rundr-xr-xr-x  13 root root     0 Jan  7 20:44 sys
Example-7:
To display hidden files:
$ ls -a
It will show all the files including the ‘.’ (current directory) and ‘..’
(parent directory). To show the hidden files, but not the ‘.’ (current directory) and ‘..’ (parent directory), use option -A.
$ ls -A
output:
# ls -a.  ..  .bash_history  .bash_logout  .bashrc  .cache  .profile  test1.sh  test.sh  .viminfo
# ls -A.bash_history  .bash_logout  .bashrc  .cache  .profile  test1.sh  test.sh  .viminfo
Example-8:
To display files recursively:
$ ls -R /etc/network
output:
# ls -R /etc/network//etc/network/:if-down.d  if-post-down.d  if-pre-up.d  if-up.d  interfaces  interfaces.d  run/etc/network/if-down.d:resolvconf  upstart  wpasupplicant/etc/network/if-post-down.d:bridge  wireless-tools  wpasupplicant/etc/network/if-pre-up.d:bridge  ethtool  wireless-tools  wpasupplicant/etc/network/if-up.d:000resolvconf  ethtool  ntpdate  openssh-server  upstart  wpasupplicant/etc/network/interfaces.d:
Example-9:
To display file inode number:
$ ls -i /etc/wgetrc
output:
# ls -i /etc/wgetrc526576 /etc/wgetrc
Example-10:
output:
$ ls -n .bash_logout-rw-r--r-- 1 1000 1000 220 Dec 28 05:28 .bash_logout
Example-11:
Visual Classification of Files With Special Characters:
$ ls -F
Instead of doing the ‘ls -l’ and then the checking for the first character to 
determine the type of file. You can use -F which classifies the file with different 
special character for different kind of files.

output:
# ls -F /bin/  boot/  dev/  etc/  home/  initrd.img@  lib/  lib64/  lost+found/  media/  mnt/  opt/  proc/  root/  run/  sbin/  srv/  sys/  tmp/  usr/  var/  vmlinuz@

    129 Lectures 
     23 hours 

 Eduonix Learning Solutions

    5 Lectures 
     4.5 hours 

 Frahaan Hussain

    35 Lectures 
     2 hours 

 Pradeep D

    41 Lectures 
     2.5 hours 

 Musab Zayadneh

    46 Lectures 
     4 hours 

 GUHARAJANM

    6 Lectures 
     4 hours 

 Uplatz
 Print
 Add Notes
 Bookmark this page