14 Useful 'ls' Command Examples in Linux

Channel: Linux
Abstract: boot/ dev/ home/ lib64/ lost+found/ mnt/ proc/ sbin/ srv/ tmp/ var/bin boot cgroup dev etc home lib lib64 lost+found media mnt opt proc root sbin seli

Linux ls (list) command displays a list of files and directories. As per my knowledge, ls command in linux, is the most frequently used command. It supports several options, some of them are listed in the below table along with a short description of each. In this post we will discuss 14 different examples of ls command which can be useful for beginners.

Syntax of ls command :

# ls [options] [file | directory]

Note : For more detailed options of ls command, please refer ls man page.

Example 1) List the files & directories in current working directory (ls)

Run ls command from command working directory to list or view files and directory, example is shown

[[email protected] /]# ls
bin boot cgroup dev etc home lib lib64 lost+found media mnt opt proc root sbin selinux srv sys tmp usr var
[[email protected] /]#
Example 2) List files and directory in long listing format (ls -l)

To display the long listing of files and directories of your current working directory, run ‘ls -l’ command

[[email protected] /]# ls -l
total 94
dr-xr-xr-x.  2 root root  4096 Nov 23 03:13 bin
dr-xr-xr-x.  5 root root  1024 Nov 23 05:29 boot
drwxr-xr-x  10 root root  4096 Nov 23 03:42 cgroup
drwxr-xr-x  18 root root  3680 Dec 12 21:32 dev
drwxr-xr-x. 64 root root  4096 Dec 12 21:32 etc
drwxr-xr-x   8 root root  4096 Jul 19 02:43 home
dr-xr-xr-x.  8 root root  4096 May  3  2014 lib
dr-xr-xr-x.  8 root root 12288 Nov 23 03:13 lib64
-rw-r--r--   1 root root    72 Dec 12 21:40 linux-stuff
drwx------.  2 root root 16384 May  3  2014 lost+found
drwxr-xr-x.  2 root root  4096 Sep 23  2011 media
drwxr-xr-x.  3 root root  4096 Jun 22 01:14 mnt
..................................................

If you have noticed the above output carefully, ‘ls -l’ also shows file’s and directory’s permissions, modification time and size too.

Example 3) List files and directories of a specific directory

To list the files and directories of specific directory then specify the directory path followed by ls -l command

[[email protected] /]# ls -l /tmp/
total 8
-rw-r--r-- 1 root root 0 Aug 3 14:31 bad-blocks.txt
drwx------. 2 root root 4096 May 3 2014 gpg-auCCFn
-rw-------. 1 root root 0 May 3 2014 yum.log
-rw------- 1 root root 1428 Nov 23 03:58 yum_save_tx-2014-11-23-03-58BHO8Jg.yumtx

To list only directory directory permissions, use ‘-ld‘ option. Example is shown below

[[email protected] /]# ls -ld /tmp/
drwxrwxrwt. 4 root root 4096 Dec 12 21:34 /tmp/
Example 4) List all files in the current working directory with their file types (ls -F)

To list file types with ls command then use ‘-F’ options,

[[email protected] /]# ls -F
bin/ cgroup/ etc/ lib/ linux-stuff media/ opt/ root/ selinux/ sys/ usr/
boot/ dev/ home/ lib64/ lost+found/ mnt/ proc/ sbin/ srv/ tmp/ var/
[[email protected] /]#

When we use ‘-F‘ option with ls command , then in the output ‘/’ will appended at every directory and for normal files nothing is appended.

Example 5) List files and directories sorted by date & time (ls -lt)

If you wish to list files and directories sorted by date and time then use ‘-t’ option in ls command,  example is show below

[[email protected] /]# ls -lt
total 94
-rw-r--r--   1 root root    72 Dec 12 21:40 linux-stuff
dr-xr-xr-x  13 root root     0 Dec 12 21:36 sys
drwxrwxrwt.  4 root root  4096 Dec 12 21:34 tmp
drwxr-xr-x  18 root root  3680 Dec 12 21:32 dev
drwxr-xr-x. 64 root root  4096 Dec 12 21:32 etc
dr-xr-xr-x  95 root root     0 Dec 12 21:31 proc
dr-xr-xr-x.  5 root root  1024 Nov 23 05:29 boot
drwxr-xr-x  10 root root  4096 Nov 23 03:42 cgroup
dr-xr-xr-x.  2 root root  4096 Nov 23 03:13 sbin
dr-xr-xr-x.  8 root root 12288 Nov 23 03:13 lib64
dr-xr-xr-x.  2 root root  4096 Nov 23 03:13 bin
dr-xr-x---.  5 root root  4096 Nov 23 02:53 root
drwxr-xr-x. 18 root root  4096 Aug 24 05:14 var
drwxr-xr-x   8 root root  4096 Jul 19 02:43 home
............................
Example 6) List all files of current directory with their sizes in human readable format (ls -lh)

To list file’s size in human readable format (2K, 34M or 5G) using ls command then use ‘-h’ option , example is shown

[[email protected] /]# ls -lh
total 94K
dr-xr-xr-x. 2 root root 4.0K Nov 23 03:13 bin
dr-xr-xr-x. 5 root root 1.0K Nov 23 05:29 boot
drwxr-xr-x 10 root root 4.0K Nov 23 03:42 cgroup
drwxr-xr-x 18 root root 3.6K Dec 12 21:32 dev
drwxr-xr-x. 64 root root 4.0K Dec 12 21:32 etc
drwxr-xr-x 8 root root 4.0K Jul 19 02:43 home
dr-xr-xr-x. 8 root root 4.0K May 3 2014 lib
dr-xr-xr-x. 8 root root 12K Nov 23 03:13 lib64
-rw-r--r-- 1 root root 72 Dec 12 21:40 linux-stuff
drwx------. 2 root root 16K May 3 2014 lost+found
drwxr-xr-x. 2 root root 4.0K Sep 23 2011 media
drwxr-xr-x. 3 root root 4.0K Jun 22 01:14 mnt
drwxr-xr-x. 2 root root 4.0K Jun 22 01:25 opt
......................................
Example 7) List all files including the hidden files in the current directory (ls -la)

‘-a’ option is used in ls command to list the hidden files. To list hidden files in long listing format use ‘ls -la’ command

[[email protected] /]# ls -la
total 102
dr-xr-xr-x. 23 root root 4096 Dec 12 21:49 .
dr-xr-xr-x. 23 root root 4096 Dec 12 21:49 ..
-rw-r--r-- 1 root root 0 Dec 12 21:32 .autofsck
-rw-r--r-- 1 root root 0 Jul 19 02:17 .autorelabel
dr-xr-xr-x. 2 root root 4096 Nov 23 03:13 bin
dr-xr-xr-x. 5 root root 1024 Nov 23 05:29 boot
drwxr-xr-x 10 root root 4096 Nov 23 03:42 cgroup
drwxr-xr-x 18 root root 3680 Dec 12 21:32 dev
drwxr-xr-x. 64 root root 4096 Dec 12 21:32 etc
drwxr-xr-x 8 root root 4096 Jul 19 02:43 home
dr-xr-xr-x. 8 root root 4096 May 3 2014 lib
dr-xr-xr-x. 8 root root 12288 Nov 23 03:13 lib64
-rw-r--r-- 1 root root 0 Dec 12 21:48 .linux-rocks
-rw-r--r-- 1 root root 72 Dec 12 21:40 linux-stuff
-rw-r--r-- 1 root root 0 Dec 12 21:49 .linux-tips
drwx------. 2 root root 16384 May 3 2014 lost+found
drwxr-xr-x. 2 root root 4096 Sep 23 2011 media
..................................................................
Example 8) To list files and directories recursively (ls -R)

Let’s assume we wish to list the files and directories of /etc directory recursively then use ‘-R’ option in ls command. example is shown below

[[email protected] /]# ls -R /etc/
/etc/:
adjtime
aliases
aliases.db
alternatives
anacrontab
audisp
audit
bash_completion.d
bashrc
blkid
centos-release
..........................
Example 9) List the files sorted by their size (ls -lhs)

Use ‘-hs’ option in ls command to list file sorted by size (human readable size like K, M & G), example is shown below:

[[email protected] /]# ls -lhS
total 13M
-rw-r--r-- 1 root root 12M Dec 12 22:05 linux-stuff
drwx------. 2 root root 16K May 3 2014 lost+found
dr-xr-xr-x. 8 root root 12K Nov 23 03:13 lib64
dr-xr-xr-x. 2 root root 4.0K Nov 23 03:13 bin
drwxr-xr-x 10 root root 4.0K Nov 23 03:42 cgroup
drwxr-xr-x. 64 root root 4.0K Dec 12 21:32 etc
drwxr-xr-x 8 root root 4.0K Jul 19 02:43 home
dr-xr-xr-x. 8 root root 4.0K May 3 2014 lib
drwxr-xr-x. 2 root root 4.0K Sep 23 2011 media
drwxr-xr-x. 3 root root 4.0K Jun 22 01:14 mnt
..............................................

Above command will display those files and directory first which are bigger in size.

Example 10) List inode numbers of files & directories (ls -li)

To list inode numbers of file and directory using ls command then use ‘-i’ option,

[[email protected] /]# ls -li 
total 12378
 786436 dr-xr-xr-x. 2 root root 4096 Nov 23 03:13 bin
 2 dr-xr-xr-x. 5 root root 1024 Nov 23 05:29 boot
 655361 drwxr-xr-x 10 root root 4096 Nov 23 03:42 cgroup
 3 drwxr-xr-x 18 root root 3680 Dec 12 21:32 dev
 393217 drwxr-xr-x. 64 root root 4096 Dec 12 21:32 etc
 2 drwxr-xr-x 8 root root 4096 Jul 19 02:43 home
 917506 dr-xr-xr-x. 8 root root 4096 May 3 2014 lib
 262146 dr-xr-xr-x. 8 root root 12288 Nov 23 03:13 lib64
 2668 -rw-r--r-- 1 root root 12582912 Dec 12 22:05 linux-stuff
...................................................................
Example 11) Display the current version of ls command  (ls –version)
[[email protected] /]# ls --version 
ls (GNU coreutils) 8.4
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Richard M. Stallman and David MacKenzie.
Example 12) List uid & gid of file and directory (ls -n)

To list file’s and directory’s UID and GID with ls command, use ‘-n’ option, example is shown below

[[email protected] /]# ls -n 
total 14476
-rw-rw-r-- 1 1000 1000 193 Aug 16 16:10 1
-rw-rw-r-- 1 1000 1000 262 Nov 28 12:35 Anjali and Aakash help Yohan-B3MzR5cTpd2mLBSZfkPKppip8vCTKUdd.mp4.part
-rw-rw-r-- 1 1000 1000 1849048 Jun 7 2014 BINGO from Super Simple Songs-9mmF8zOlh_g.mp4.part
drwxr-xr-x 18 1000 1000 4096 Dec 13 08:59 Desktop
drwxr-xr-x 2 1000 1000 4096 Sep 27 13:09 Documents
drwxr-xr-x 2 1000 1000 4096 Dec 2 10:10 Downloads
drwx------ 3 1000 1000 4096 Nov 30 15:36 Dropbox
-rw-rw-r-- 1 1000 1000 94296 Nov 20 02:00 dropbox_2.10.0_amd64.deb
-rw-r--r-- 1 1000 1000 8980 Apr 20 2014 examples.desktop
-rw-rw-r-- 1 1000 1000 203 Oct 26 16:02 fs.sh
drwxrwxr-x 2 1000 1000 4096 Sep 1 14:31 Google Drive
................................................................
Example 13)  Default Aliases of ls command

Type the alias command on the terminal to display the default aliases set for ls command.

[[email protected] /]# alias 
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
Example 14) Enable timestamps in ls command output (–time-style)

Use ‘–time-style’ option in ls command to enable timestamps in output, example is shown below:

[[email protected] /]# ls -l --time-style="+%Y-%m-%d $newline%m-%d %H:%M"
total 12378
dr-xr-xr-x. 2 root root 4096 2014-11-23 11-23 03:13 bin
dr-xr-xr-x. 5 root root 1024 2014-11-23 11-23 05:29 boot
drwxr-xr-x 10 root root 4096 2014-11-23 11-23 03:42 cgroup
drwxr-xr-x 18 root root 3680 2014-12-12 12-12 21:32 dev
drwxr-xr-x. 64 root root 4096 2014-12-12 12-12 21:32 etc
drwxr-xr-x 8 root root 4096 2014-07-19 07-19 02:43 home
dr-xr-xr-x. 8 root root 4096 2014-05-03 05-03 05:24 lib
dr-xr-xr-x. 8 root root 12288 2014-11-23 11-23 03:13 lib64
-rw-r--r-- 1 root root 12582912 2014-12-12 12-12 22:05 linux-stuff
drwx------. 2 root root 16384 2014-05-03 05-03 05:19 lost+found
drwxr-xr-x. 2 root root 4096 2011-09-23 09-23 07:50 media
......................................................................

Also Read14 Grep Command Examples in Linux

Also Read : Linux Zip and Unzip Command with Examples

Ref From: linuxtechi

Related articles