Share this post
FaceBook  Twitter  

1. Introduction

If you ever tried to work with Linux command line, ls command was surely one of the first commands you have executed. In fact, ls command is so frequently used, that its name is often considered as the best choice to name a Trojan Horse.  Even though you use ls command on daily basis, its wast number of options always makes you to reach for ls's manual page. Doing so you learn something new every time you open ls's manual page . This guide will try to do the same. ls command belongs to a group of core utilities on your Linux system. GNU ls was written by Stallman and David MacKenzie based on the original AT&T code written in the 60s.

Let's get started, no previous Linux skills are required. First, we will cover ls's frequently used options and then we will introduce some more advanced features.

2. Frequently used options

  •  -l
    This is very common option of ls command. By default ls displays only name of a file or directory. -l , alias long listing format, will instruct ls command to display more information for any given output.
  • -a, --all
    Display also hidden files. In shell hidden files contain a "." in front of its name. -a option will ensure that these files are not omitted from ls output.
  • -t
    Sort output by modification date listing the oldest modification date as last
  • -r, --reverse
    This options will simply reverse any ls's output.
  • -h, --human-readable
    With combination of -l option this fill print sizes in human readable format (e.g, 3K, 12M or 1G ).

2.1. Long listing format

This is very common and often use ls's option. Not only this option displays additional information for a file or directory, this option is also required as a combination with some other ls options. The first thing we are going to do is to execute ls command without any options and arguments. You cannot go more basic with ls than that:

$ ls
dir1  dir3  dir5       file2.txt  file4.txt
dir2  dir4  file1.txt  file3.txt  file5.txt

All what ls command did was to list all files and directories in our current working directory. Now with use of -l option we are able to see more information:

$ ls -l
total 32
drwxr-xr-x 2 lubos lubos 4096 Jan 14 17:07 dir1
drwxr-xr-x 2 lubos lubos 4096 Jan 14 17:07 dir2
drwxr-xr-x 2 lubos lubos 4096 Jan 14 17:07 dir3
drwxr-xr-x 2 lubos lubos 4096 Jan 14 17:07 dir4
lrwxrwxrwx 1 lubos lubos    4 Jan 14 17:14 dir5 -> dir1
-rw-r--r-- 2 lubos lubos    2 Jan 14 17:15 file1.txt
-rw-r--r-- 1 lubos lubos    2 Jan 14 17:15 file2.txt
-rw-r--r-- 2 lubos lubos    2 Jan 14 17:15 file3.txt
-rw-r--r-- 1 lubos lubos    2 Jan 14 17:15 file4.txt
lrwxrwxrwx 1 lubos lubos    9 Jan 14 17:13 file5.txt -> file2.txt

From the output we can get a following information about file1.txt:

  • permissions -> -rw-r--r--
  • hard link count -> 2
  • owner -> lubos
  • group -> lubos
  • size in bytes -> 2
  • modification date -> Jan 14 17:15
  • name -> file1.txt

Furthermore, additional information can be read from permission column. Note that our file1.txt permission starts with "-" which means that file1.txt is a regular file as oppose to file5.txt which is a symbolic link. Here are additional code meanings:

  • - : Regular file. Can be text file, executable, image and etc.
  • d : Directory.
  • l : Symbolic link. When accessing this file Linux tries to access linked file
  • p : Named Pipe. 
  • s : Socket
  • b : Block Device
  • c : Character device

2.2. Show hidden files

 

In Linux, all hidden files and directories start with "." in their file name. By default ls ignores all entries starting with . thus not showing hidden files or directories. To display all hidden files and directories we can use -a option. For example:

$ ls
$ touch file
$ touch .file
$ ls
file
$ ls -a
.  ..  file  .file

First we have created regular non-hidden file and with second command we created hidden .file. Only ls with -a option will display both files.

2.3. Sort output by modification date

By default, ls command sorts any output by file name in alphabetical order. -t option instructs ls command to display output sorted by modification time. For example:

$ ls -l
total 0
-rw-r--r-- 1 lubos lubos 0 Sep 26  2000 0
-rw-r--r-- 1 lubos lubos 0 Jul  6  1978 A
-rw-r--r-- 1 lubos lubos 0 Jan  1  2011 B
-rw-r--r-- 1 lubos lubos 0 Jan 30  1942 C
$ ls -lt
total 0
-rw-r--r-- 1 lubos lubos 0 Jan  1  2011 B
-rw-r--r-- 1 lubos lubos 0 Sep 26  2000 0
-rw-r--r-- 1 lubos lubos 0 Jul  6  1978 A
-rw-r--r-- 1 lubos lubos 0 Jan 30  1942 C

As you can see ls command with -t option will sort output sorted by date with most recently modified files first.

2.4. Reverse output order

Previously, we have seen how to make ls command display all entries sorted by modification date. With -r option we can reverse this order to display last modified files as last.

$ ls -lt
total 0
-rw-r--r-- 1 lubos lubos 0 Jan  1  2011 B
-rw-r--r-- 1 lubos lubos 0 Sep 26  2000 0
-rw-r--r-- 1 lubos lubos 0 Jul  6  1978 A
-rw-r--r-- 1 lubos lubos 0 Jan 30  1942 C
$ ls -ltr
total 0
-rw-r--r-- 1 lubos lubos 0 Jan 30  1942 C
-rw-r--r-- 1 lubos lubos 0 Jul  6  1978 A
-rw-r--r-- 1 lubos lubos 0 Sep 26  2000 0
-rw-r--r-- 1 lubos lubos 0 Jan  1  2011 B

This time we have listed files with the oldest modification date as first.

2.5. Human readable output

This option is relevant only to a file size and only with a combination of -l option. ls command displays file size in number of bytes by default. To get a more human readable output, -h option can be used, which translates bytes into KB, MB, GB and etc.

$ ls -l
total 1813500
-rw-r--r-- 1 lubos lubos      19666 Feb  1 12:12 file1
-rw-r--r-- 1 lubos lubos  471957504 Feb  1 12:12 file2
-rw-r--r-- 1 lubos lubos 1257832448 Feb  1 12:12 file3
-rw-r--r-- 1 lubos lubos  127205376 Feb  1 12:12 file4
$ ls -lh
total 1.8G
-rw-r--r-- 1 lubos lubos  20K Feb  1 12:12 file1
-rw-r--r-- 1 lubos lubos 451M Feb  1 12:12 file2
-rw-r--r-- 1 lubos lubos 1.2G Feb  1 12:12 file3
-rw-r--r-- 1 lubos lubos 122M Feb  1 12:12 file4

3. Advanced options and features

3.1. Sort by file size

-S options will sort output by file size with smallest file last.

$ ls -S
file3  file2  file4  file1
$ ls -Sl
total 1813500
-rw-r--r-- 1 lubos lubos 1257832448 Feb  1 12:12 file3
-rw-r--r-- 1 lubos lubos  471957504 Feb  1 12:12 file2
-rw-r--r-- 1 lubos lubos  127205376 Feb  1 12:12 file4
-rw-r--r-- 1 lubos lubos      19666 Feb  1 12:12 file1

In this case the -l option is optional. We have used it just to display file size.

3.2. Combining multiple options

There is no harm in combining multiple ls options with a single ls command. In fact you are encouraged to do so. For example, we would like ls command not to display group ( -G ), show long listing format ( -l ) in human readable output ( -h ) and sort by size ( -S ) with smallest file first ( -r ).

$ ls -GlhSr
total 572M
prw-r--r-- 1 lubos    0 Feb  1 12:26 file5
lrwxrwxrwx 1 lubos    4 Feb  1 12:27 file3 -> file
-rw-r--r-- 1 lubos   39 Feb  1 12:24 file
drwxr-xr-x 2 lubos 4.0K Feb  1 12:25 dir1
-rw-r--r-- 1 lubos  20K Feb  1 12:12 file1
-rw-r--r-- 1 lubos 122M Feb  1 12:12 file4
-rw-r--r-- 1 lubos 451M Feb  1 12:12 file2

The above is a equivalent to:

ls -G -l -h -S -r
Can you guess what this ls command does?:
$ ls -l -a -Shr

3.3. Using color terminal output

On some Linux systems ls command automatically prints output in a color to distinguish file type. This is caused by an alias "ls --color=auto". If you do not have this option enabled you can switch it on by:

ls command color output

If you do not like the default color set you can change it by defining LS_COLORS environment variable. This is beyond the scope of this guide but just as an example we can change default directory color to white on blue background and file to red on green background with:

$ export LS_COLORS="di=97;104:fi=31;42"

ls command custom color output

3.4. List Subdirectories Recursively

By default ls command will list only directories and files in your current working directory. which means that if a directories in your current working directory contains other files or other directories they will not be listed. Here is an example:

$ mkdir -p dir1/dir2/dir3
$ touch dir1/dir2/file.txt $ tree 
.
└── dir1
    └── dir2
        ├── dir3
        └── file.txt

3 directories, 1 file
$ ls 
dir1
$ ls -R
.:
dir1

./dir1:
dir2

./dir1/dir2:
dir3  file.txt

./dir1/dir2/dir3:

First we have created directories and file. Default ls command will then show only single directory dir1. With use of -R option ls command outputs all files and directories recursively.

3.5. ls command and wildcards

Wildcards is very broad topic. However, one must understand the basics in order to use ls and other Linux command efficiently. What follows is really just an introduction to wildcards.

3.5.1. Asterisk - *

Asterisk is a most common wildcard. It allows us to list only specific files where part of the filename can be replaces with "*" to mach any characters. For example, we want to list only files with txt extension:

$ ls
file1.txt  file2.txt  file.sh  file.txt  pic1.png  pic.jpg  pic.png
$ ls *.txt
file1.txt  file2.txt  file.txt

Or we wish to list only files which start with "p":

$ ls p*
pic1.png  pic.jpg  pic.png

Or we can list only files with a letter "n"  within its filename:

$ ls *n*
pic1.png  pic.png

3.5.2. Question mark - ?

? will represent any single character. For example:

$ ls
file1.txt  file2.txt  file.sh  file.txt  pic1.png  pic.jpg  pic.png
$ ls file?.txt
file1.txt  file2.txt

Or we can display file which extension consist only of 2 characters:

ls *.??
file.sh

3.5.3. Brackets - [ ]

Brackets are similar to ? as they can represent single character. However, in this case they can also represent one or more characters and we have a choice of which character to include in our wildcard. For example we can display all file names which contain number and start with p:

$ ls
file1.txt  file2.txt  file.sh  file.txt  pic1.png  pic.jpg  pic.png
$ ls f*[0-9]*
file1.txt  file2.txt

or extension ends with h or g:

$ ls *[h,g]
file.sh  pic1.png  pic.jpg  pic.png

3.5.4. Curly Brackets - { }

Curly brackets allow us to specify one or more terms, where term is a single character or wildcard. For example, we can list only files with txt and sh extension. Each term is separate by "," ( comma ):

$ ls *{*.txt,*.sh}
file1.txt  file2.txt  file.sh  file.txt

To combine with [] we can list only files which has extension sh and txt but "."  ( dot )is preceded by any character but number.

$ ls
file1.txt  file2.txt  file.sh  file.txt  pic1.png  pic.jpg  pic.png
$ ls *[a-z]\.{*txt,*sh}
file.sh  file.txt

3.5.5. Exclamation mark - !

Exclamation mark acts as a negator. For example we want to list all files which do NOT end with "g" in their filename:

$ ls
file1.txt  file2.txt  file.sh  file.txt  pic1.png  pic.jpg  pic.png
$ ls *[!g]
file1.txt  file2.txt  file.sh  file.txt

Wildcards are powerful feature of Linux shell. For more information enter:

$ man 7 glob

3.6. List directories entries

This may sound intuitive but for some listing directory other than the current working directory is a mystery. With -d option ls will display directory itself rather than its content:

$ ls -l /var/
total 44
drwxr-xr-x  2 root root  4096 Jan 26 06:25 backups
drwxr-xr-x 16 root root  4096 Jan 12 21:15 cache
drwxr-xr-x 48 root root  4096 Jan 19 06:25 lib
drwxrwsr-x  2 root staff 4096 Oct  3 13:52 local
drwxrwxrwt  3 root root  4096 Feb  1 06:29 lock
drwxr-xr-x 10 root root  4096 Feb  1 06:29 log
$ ls -dl /var/
drwxr-xr-x 13 root root 4096 Jan  3 09:20 /var/

Display permissions of a current working directory:

$ ls -ld 
drwxr-xr-x 2 linuxcareer linuxcareer 4096 Feb  1 14:02 .

Using wildcard display permissions for all directories in /var/log/:

$ ls -ld /var/log/*/
drwxr-xr-x 2 root root 4096 Jan  3 09:23 /var/log/apt/
drwxr-xr-x 2 root root 4096 Jan  3 10:05 /var/log/ConsoleKit/
drwxr-xr-x 2 root root 4096 Jan 26 06:25 /var/log/cups/
drwxr-xr-x 2 root root 4096 Jan  3 09:21 /var/log/fsck/

4. Examples

Learning Linux ls command with examples
Linux command syntaxLinux command description
ls -1
Display output vertically.
ls -ld dir
Display long listing format of directory dir
ls -li file
Print inode number for a file
ls -gG
Display long listing format but do not show owner and group
ls -m
Print comma separated output
ls -p
Display slash after each directory
ls -l | grep ^d
Display only directories
for i in $( ls *.jpg ); do convert -resize 1024x $i re_$i; done
Re-size all images with extension JPG in a current working directory. Prefix new image with re_
ls -alct --full-time
Display all hidden and non-hidden files and directories sorted by creation time with full time exact time
ls -lact --time-style="+%Y"
Display all hidden and non-hidden files and directories sorted by creation time. But display only year for each entry

Partners

Who are we?

LinuxCareer.com is not affiliated with any local or international company, nor is it a recruitment or employment agency. We specialise in Linux based careers and closely related Information Technology fields by providing careers advice and latest employment opportunities.

JOIN LINUXCAREER

You can also get involved in the LinuxCareer project by participating on our FORUM or SUBMITTING A LINUX ARTICLE. We offer a range of privileges to our authors and good company.