Linux : File List Command All Options And Examples

April 22, 2013 | By
| Reply More

Anybody having even a little introduction about Linux must have heard about the ls command. One of the most commonly used commands on Linux. If you are a regular Linux user, there must hardly be a day when you haven't used ls command. Very simple yet powerful command used for listing files and directories. This article is about the common options of ls command which will be very handy at times.

ls command usage

At its raw form, you can simply issue ls command without any option or argument. This will list files and directories in the current directory.

$ ls
abi-3.5.0-17-generic memtest86+.bin
config-3.5.0-17-generic memtest86+_multiboot.bin
grub System.map-3.5.0-17-generic
initrd.img-3.5.0-17-generic vmlinuz-3.5.0-17-generic

You can list any file/directory by providing it as argument to the ls command.

$ ls /
bin etc initrd.img.old media proc sbin sys var
boot home lib mnt root selinux tmp vmlinuz
dev initrd.img lost+found opt run srv usr

Options of ls command

• -a (--all) or -A

The -a (--all) or -A option will list all files including the ‘.’ files (hidden). The -A option does not show '.' and '..' files.

# ls -a
. .gconf nohup.out
.. .gconfd odesk
all_redhat_description.txt .gnome .redhat
all_redhat_error.txt .gnome2 remi-release-5.rpm
anaconda-ks.cfg .gnome2_private scsrun.log
.bash_history .gstreamer-0.10 .serverauth.2631
.bash_logout .gtkrc-1.2-gnome2 .serverauth.2734
<--output truncated-->

# ls -A
all_redhat_description.txt .gconfd nohup.out
all_redhat_error.txt .gnome odesk
anaconda-ks.cfg .gnome2 .redhat
.bash_history .gnome2_private remi-release-5.rpm
.bash_logout .gstreamer-0.10 scsrun.log
.bash_profile .gtkrc-1.2-gnome2 .serverauth.2631
.bashrc .ICEauthority .serverauth.2734
<--output truncated-->

• -l

Display output in long listing format, i.e. it not only prints the name of the file, but also some attributes such as owner, group owner, link count, permissions etc.

# ls -l /boot/
total 9637
-rw-r--r-- 1 root root 69815 Dec 20 2010 config-2.6.18-238.el5
drwxr-xr-x 2 root root 1024 Jul 3 2012 grub
-rw------- 1 root root 3375123 Jul 3 2012 initrd-2.6.18-238.el5.img
-rw------- 1 root root 3375075 Aug 3 2012 initrd-latest.img
drwx------ 2 root root 12288 Jul 3 2012 lost+found
-rw-r--r-- 1 root root 112421 Dec 20 2010 symvers-2.6.18-238.el5.gz
-rw-r--r-- 1 root root 979708 Dec 20 2010 System.map-2.6.18-238.el5
-rw-r--r-- 1 root root 1888084 Dec 20 2010 vmlinuz-2.6.18-238.el5

The first field shows the type of file ('-' for simple file, 'd' for directory etc.)
Next follows the file permissions (rw-r--r-- in the first entry).
Following column shows the link count, i.e. number of hard links to the file.
Then owner and group owner follow respectively.
Next field shows the size of the file.
File size follows time stamp for the file further followed by the file name.

• -h

The file size shown in the above output can be displayed in human readable form, i.e. in KB/MB/GB etc with -h option

# ls -lh /boot/
total 9.5M
-rw-r--r-- 1 root root 69K Dec 20 2010 config-2.6.18-238.el5
drwxr-xr-x 2 root root 1.0K Jul 3 2012 grub
-rw------- 1 root root 3.3M Jul 3 2012 initrd-2.6.18-238.el5.img
-rw------- 1 root root 3.3M Aug 3 2012 initrd-latest.img
drwx------ 2 root root 12K Jul 3 2012 lost+found
-rw-r--r-- 1 root root 110K Dec 20 2010 symvers-2.6.18-238.el5.gz
-rw-r--r-- 1 root root 957K Dec 20 2010 System.map-2.6.18-238.el5
-rw-r--r-- 1 root root 1.9M Dec 20 2010 vmlinuz-2.6.18-238.el5

• -d (useful with -l)

If you want to print the listing of a directory rather than files in it, use -d.
For example, if you want to list the properties of /etc directory, the command "ls -l /etc" will print the long listing of all the files and directories IN /etc directory not the /etc directory itself. In this case, -ld option is used.

# ls -l /etc
total 3484
-rw-r--r-- 1 root root 15288 Jan 15 2008 a2ps.cfg
-rw-r--r-- 1 root root 2562 Jan 15 2008 a2ps-site.cfg
drwxr-xr-x 4 root root 4096 Jul 3 2012 acpi
-rw-r--r-- 1 root root 45 Feb 18 13:52 adjtime
drwxr-xr-x 4 root root 4096 Jul 3 2012 alchemist
-rw-r--r-- 1 root root 1512 Apr 25 2005 aliases
<--output truncated-->

# ls -ld /etc
drwxr-xr-x 100 root root 12288 Apr 15 10:39 /etc

• -t

This sorts files by modification time (newest file first)

# ls -lt /etc/
total 3484
-rw-r--r-- 1 root root 280568 Apr 15 10:39 prelink.cache
-rw-r----- 1 root smmsp 12288 Apr 15 09:33 aliases.db
-rw-r--r-- 1 root root 135 Apr 15 09:33 printcap
-rw-r--r-- 1 root root 435 Apr 15 09:32 reader.conf
drwxr-xr-x 2 root root 4096 Apr 15 09:32 blkid
-rw-r--r-- 1 root root 292 Apr 15 09:32 mtab
-rw-r--r-- 1 root root 45 Feb 18 13:52 adjtime
<--output truncated-->

• -r

Reverse sorts the files (useful with -l and -lt)

# ls -ltr /etc/
total 3484
-rw-r--r-- 1 root root 0 Jan 13 2000 motd
-rw-r--r-- 1 root root 347 Jan 13 2000 hosts.deny
-rw-r--r-- 1 root root 161 Jan 13 2000 hosts.allow
-rw-r--r-- 1 root root 17 Jul 23 2000 host.conf
-rw-r--r-- 1 root root 1615 Aug 30 2001 rpc
-rw------- 1 root root 122 Feb 17 2003 securetty
-rw-r--r-- 1 root root 1696 Sep 23 2004 nsswitch.conf
-rw-r--r-- 1 root root 758 Sep 23 2004 inputrc
<--output truncated-->

• -c

Show ctime with -l option. The time stamp shown here is ctime, which by default is mtime. With -lt, it sorts with ctime.

# ls -ltc /etc/
total 3484
-rw-r----- 1 root smmsp 12288 Apr 15 11:51 aliases.db
-rw-r--r-- 1 root root 135 Apr 15 11:51 printcap
drwxr-xr-x 2 root root 4096 Apr 15 11:51 blkid
-rw-r--r-- 1 root root 435 Apr 15 11:51 reader.conf
-rw-r--r-- 1 root root 292 Apr 15 11:51 mtab
-rw-r--r-- 1 root root 280568 Apr 15 10:39 prelink.cache
-rw-r--r-- 1 root root 45 Feb 18 13:52 adjtime
<--output truncated-->

• -u

This can be compared t -c option. But the timestamp used in this case is access time.

# ls -ltu /etc/ | head
total 3484
-rw-r--r-- 1 root root 719 Apr 15 13:28 group
-rw-r--r-- 1 root root 37144 Apr 15 13:28 ld.so.cache
-rw-r--r-- 1 root root 1696 Apr 15 13:28 nsswitch.conf
-rw-r--r-- 1 root root 1772 Apr 15 13:28 passwd
drwx------ 2 root root 4096 Apr 15 13:28 cron.d
-rw-r--r-- 1 root root 265 Apr 15 13:27 localtime
lrwxrwxrwx 1 root root 22 Apr 15 13:17 grub.conf -> ../boot/grub/grub.conf
lrwxrwxrwx 1 root root 11 Apr 15 13:17 init.d -> rc.d/init.d
lrwxrwxrwx 1 root root 7 Apr 15 13:17 rc -> rc.d/rc

• -S

Sorts by file type.

# ls -lS
total 368
-rw-r--r-- 1 root root 214112 Feb 15 11:11 all_redhat_description.txt
-rw-r--r-- 1 root root 30152 Jul 3 2012 install.log
-rw-r--r-- 1 root root 12232 Jul 25 2012 epel-release-5-4.noarch.rpm
-rw-r--r-- 1 root root 4974 Jul 25 2012 remi-release-5.rpm
-rw------- 1 root root 4934 Dec 1 22:10 mbox
drwxr-xr-x 2 root root 4096 Jul 3 2012 Desktop
drwx------ 2 root root 4096 Dec 2 00:16 Mail
drwxr-xr-x 3 root root 4096 Dec 6 23:28 odesk
-rw-r--r-- 1 root root 3662 Jul 3 2012 install.log.syslog
-rw------- 1 root root 1293 Jul 3 2012 anaconda-ks.cfg
-rw-r--r-- 1 root root 195 Jul 3 2012 scsrun.log
-rw-r--r-- 1 root root 90 Feb 15 11:10 all_redhat_error.txt
-rw-r--r-- 1 root root 54 Oct 14 2012 file_for_grep
-rw------- 1 root root 11 Oct 15 22:06 nohup.out

• -i

This is very important option. It lists the inode number of the file.

# ls -i /etc/
1212974 a2ps.cfg 1212603 maven
1212973 a2ps-site.cfg 1212569 mgetty+sendfax
1213652 acpi 1212468 mime.types
1213224 adjtime 1212933 minicom.users
1213183 alchemist 1212833 mke2fs.conf
1212434 aliases 1213965 modprobe.conf
1213530 aliases.db 1212426 modprobe.conf~
<--output truncated-->

# ls -li /etc/
total 3484
1212974 -rw-r--r-- 1 root root 15288 Jan 15 2008 a2ps.cfg
1212973 -rw-r--r-- 1 root root 2562 Jan 15 2008 a2ps-site.cfg
1213652 drwxr-xr-x 4 root root 4096 Jul 3 2012 acpi
1213224 -rw-r--r-- 1 root root 45 Feb 18 13:52 adjtime
1213183 drwxr-xr-x 4 root root 4096 Jul 3 2012 alchemist
1212434 -rw-r--r-- 1 root root 1512 Apr 25 2005 aliases
1213530 -rw-r----- 1 root smmsp 12288 Apr 15 11:51 aliases.db
1212721 drwxr-xr-x 4 root root 4096 Jul 3 2012 alsa
1212480 drwxr-xr-x 2 root root 4096 Nov 8 11:02 alternatives
<--output truncated-->

• -n

This is like -l option, but instead of showing owner and group owner, it shows the UID and GID.

# ls -n /etc/ | head
total 3484
-rw-r--r-- 1 0 0 15288 Jan 15 2008 a2ps.cfg
-rw-r--r-- 1 0 0 2562 Jan 15 2008 a2ps-site.cfg
drwxr-xr-x 4 0 0 4096 Jul 3 2012 acpi
-rw-r--r-- 1 0 0 45 Feb 18 13:52 adjtime
drwxr-xr-x 4 0 0 4096 Jul 3 2012 alchemist
-rw-r--r-- 1 0 0 1512 Apr 25 2005 aliases
-rw-r----- 1 0 51 12288 Apr 15 11:51 aliases.db
drwxr-xr-x 4 0 0 4096 Jul 3 2012 alsa
drwxr-xr-x 2 0 0 4096 Nov 8 11:02 alternatives

• -L or --dereference

When showing information about the symbolic links (e.g. in -l option), show information about the file itself and not about the link. In the following example, the file /sbin/telinit links to the init file in the same directory. With -lL, it shows information about the init file.

# ls -l /sbin/telinit
lrwxrwxrwx 1 root root 4 Jul 3 2012 /sbin/telinit -> init

# ls -lL /sbin/telinit
-rwxr-xr-x 1 root root 38652 Sep 18 2008 /sbin/telinit

# ls -l /sbin/init
-rwxr-xr-x 1 root root 38652 Sep 18 2008 /sbin/init

• -m

Separate the files with commas.

# ls -m
all_redhat_description.txt, all_redhat_error.txt, anaconda-ks.cfg, Desktop,
epel-release-5-4.noarch.rpm, file_for_grep, install.log, install.log.syslog,
Mail, mbox, nohup.out, odesk, remi-release-5.rpm, scsrun.log

• -B

Ignore backups. This option does not list files ending with ~. Backup files are created along with some file having the same name as the file itself followed by '~'. This option omits such entries. In the following example, demo.java~, server.java~ etc are such files although original files are missing (deleted or moved).

$ ls
demo.java~ mac.txt raghu.html
Document1.txt~ Message_Evaluation_Project seamonkey
g2ipmsg-0.9.6 Nida_pic_sign2.jpg seamonkey-2.15.2.tar.bz2
g2ipmsg-0.9.6.tar.gz Nida_pic_sign.jpg server.java~
gaurav.html odesk TestPrime.java~
java Prime.java~ unetbootin-linux-568
kompozer Python vboxshare

$ ls -B
g2ipmsg-0.9.6 Message_Evaluation_Project seamonkey
g2ipmsg-0.9.6.tar.gz Nida_pic_sign2.jpg seamonkey-2.15.2.tar.bz2
gaurav.html Nida_pic_sign.jpg unetbootin-linux-568
java odesk vboxshare
kompozer Python
mac.txt raghu.html

• -F

This option classifies different types of files. A forward slash symbol follows the directories, @ implies symbolic links etc.

# ls -F /dev/
adsp loop3 ram11 shm/ tty24 tty48 usbdev1.1_ep00
audio loop4 ram12 snapshot tty25 tty49 usbdev1.1_ep81
autofs loop5 ram13 snd/ tty26 tty5 usbdev2.1_ep00
bus/ loop6 ram14 sr0 tty27 tty50 usbdev2.1_ep81
cdrom@ loop7 ram15 stderr@ tty28 tty51 vcs
cdrom-hdc@ MAKEDEV@ ram2 stdin@ tty29 tty52 vcs1
<--output truncated-->

• -p

Appends forward slash to the directories.

# ls -p
all_redhat_description.txt anaconda-ks.cfg epel-release-5-4.noarch.rpm install.log Mail/ nohup.out remi-release-5.rpm
all_redhat_error.txt Desktop/ file_for_grep install.log.syslog mbox odesk/ scsrun.log

• --full-time

This option outputs long format listing, like -l option, but the time is shown in full format.

# ls --full-time
total 368
-rw-r--r-- 1 root root 214112 2013-02-15 11:11:19.000000000 +0530 all_redhat_description.txt
-rw-r--r-- 1 root root 90 2013-02-15 11:10:19.000000000 +0530 all_redhat_error.txt
-rw------- 1 root root 1293 2012-07-03 03:56:37.000000000 +0530 anaconda-ks.cfg
drwxr-xr-x 2 root root 4096 2012-07-03 13:17:39.000000000 +0530 Desktop
-rw-r--r-- 1 root root 12232 2012-07-25 10:16:59.000000000 +0530 epel-release-5-4.noarch.rpm
-rw-r--r-- 1 root root 54 2012-10-14 13:36:50.000000000 +0530 file_for_grep
-rw-r--r-- 1 root root 30152 2012-07-03 03:56:31.000000000 +0530 install.log
-rw-r--r-- 1 root root 3662 2012-07-03 03:55:58.000000000 +0530 install.log.syslog
drwx------ 2 root root 4096 2012-12-02 00:16:34.000000000 +0530 Mail
-rw------- 1 root root 4934 2012-12-01 22:10:04.000000000 +0530 mbox
-rw------- 1 root root 11 2012-10-15 22:06:30.000000000 +0530 nohup.out
drwxr-xr-x 3 root root 4096 2012-12-06 23:28:11.000000000 +0530 odesk
-rw-r--r-- 1 root root 4974 2012-07-25 10:08:52.000000000 +0530 remi-release-5.rpm
-rw-r--r-- 1 root root 195 2012-07-03 04:00:51.000000000 +0530 scsrun.log

• -g, -G and -o

-g is like -l, i.e. outputs in long format, but does not print the owner. -G does not output group owner of the file but works with -l option. -o works like -Gl.

# ls -g
total 368
-rw-r--r-- 1 root 214112 Feb 15 11:11 all_redhat_description.txt
-rw-r--r-- 1 root 90 Feb 15 11:10 all_redhat_error.txt
-rw------- 1 root 1293 Jul 3 2012 anaconda-ks.cfg
drwxr-xr-x 2 root 4096 Jul 3 2012 Desktop
-rw-r--r-- 1 root 12232 Jul 25 2012 epel-release-5-4.noarch.rpm
-rw-r--r-- 1 root 54 Oct 14 2012 file_for_grep
-rw-r--r-- 1 root 30152 Jul 3 2012 install.log
-rw-r--r-- 1 root 3662 Jul 3 2012 install.log.syslog
drwx------ 2 root 4096 Dec 2 00:16 Mail
-rw------- 1 root 4934 Dec 1 22:10 mbox
-rw------- 1 root 11 Oct 15 22:06 nohup.out
drwxr-xr-x 3 root 4096 Dec 6 23:28 odesk
-rw-r--r-- 1 root 4974 Jul 25 2012 remi-release-5.rpm
-rw-r--r-- 1 root 195 Jul 3 2012 scsrun.log

# ls -lG
total 368
-rw-r--r-- 1 root 214112 Feb 15 11:11 all_redhat_description.txt
-rw-r--r-- 1 root 90 Feb 15 11:10 all_redhat_error.txt
-rw------- 1 root 1293 Jul 3 2012 anaconda-ks.cfg
drwxr-xr-x 2 root 4096 Jul 3 2012 Desktop
-rw-r--r-- 1 root 12232 Jul 25 2012 epel-release-5-4.noarch.rpm
-rw-r--r-- 1 root 54 Oct 14 2012 file_for_grep
-rw-r--r-- 1 root 30152 Jul 3 2012 install.log
-rw-r--r-- 1 root 3662 Jul 3 2012 install.log.syslog
drwx------ 2 root 4096 Dec 2 00:16 Mail
-rw------- 1 root 4934 Dec 1 22:10 mbox
-rw------- 1 root 11 Oct 15 22:06 nohup.out
drwxr-xr-x 3 root 4096 Dec 6 23:28 odesk
-rw-r--r-- 1 root 4974 Jul 25 2012 remi-release-5.rpm
-rw-r--r-- 1 root 195 Jul 3 2012 scsrun.log

# ls -o
total 368
-rw-r--r-- 1 root 214112 Feb 15 11:11 all_redhat_description.txt
-rw-r--r-- 1 root 90 Feb 15 11:10 all_redhat_error.txt
-rw------- 1 root 1293 Jul 3 2012 anaconda-ks.cfg
drwxr-xr-x 2 root 4096 Jul 3 2012 Desktop
-rw-r--r-- 1 root 12232 Jul 25 2012 epel-release-5-4.noarch.rpm
-rw-r--r-- 1 root 54 Oct 14 2012 file_for_grep
-rw-r--r-- 1 root 30152 Jul 3 2012 install.log
-rw-r--r-- 1 root 3662 Jul 3 2012 install.log.syslog
drwx------ 2 root 4096 Dec 2 00:16 Mail
-rw------- 1 root 4934 Dec 1 22:10 mbox
-rw------- 1 root 11 Oct 15 22:06 nohup.out
drwxr-xr-x 3 root 4096 Dec 6 23:28 odesk
-rw-r--r-- 1 root 4974 Jul 25 2012 remi-release-5.rpm
-rw-r--r-- 1 root 195 Jul 3 2012 scsrun.log

• -Z

Show SELinux context in long listing format

# ls -Z /etc/ | head
-rw-r--r-- root root system_u:object_r:etc_t a2ps.cfg
-rw-r--r-- root root system_u:object_r:etc_t a2ps-site.cfg
drwxr-xr-x root root system_u:object_r:etc_t acpi
-rw-r--r-- root root system_u:object_r:adjtime_t adjtime
drwxr-xr-x root root system_u:object_r:etc_t alchemist
-rw-r--r-- root root system_u:object_r:etc_aliases_t aliases
-rw-r----- root smmsp system_u:object_r:etc_aliases_t aliases.db
drwxr-xr-x root root system_u:object_r:etc_t alsa
drwxr-xr-x root root system_u:object_r:etc_t alternatives
-rw-r--r-- root root system_u:object_r:etc_t anacrontab

• -1

Shows one file per line.

# ls -1 /etc/ | head
a2ps.cfg
a2ps-site.cfg
acpi
adjtime
alchemist
aliases
aliases.db
alsa
alternatives
anacrontab

• --help

Show help for the command.

# ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort.

Mandatory arguments to long options are mandatory for short options too.
-a, --all do not ignore entries starting with .
-A, --almost-all do not list implied . and ..
--author with -l, print the author of each file
-b, --escape print octal escapes for nongraphic characters
--block-size=SIZE use SIZE-byte blocks
-B, --ignore-backups do not list implied entries ending with ~
<--output truncated-->

• --version

Print the version information of the command.

# ls --version
ls (GNU coreutils) 5.97
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software. You may redistribute copies of it under the terms of
the GNU General Public License .
There is NO WARRANTY, to the extent permitted by law.
Written by Richard Stallman and David MacKenzie.

More Linux Commands

Filed Under : HOWTOS

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.