Tag Archives: ls command cookbook

12 examples of ls command in Linux for daily use

Beginners guide to ls command. Learn ls command with 12 examples that can be used in your daily Linux tasks. 

Learn ‘ls’ command

The very first command anyone types, when they logged into the terminal, is either ls or hostname! Yes, ls is the first command all beginners, newbies learn and use when they introduced to Linux world. It is one of the most used rather smashed commands in terminal 🙂

ls stands for list. This command helps in listing files and directories in Linux or Unix. There are many switches available to use with use to fit your need. We will walk you through 15 different examples of ls commands which can be useful for you in your daily routine.

Normal ls command without any switch

Without any switch ls command shows all files and directories names in a single line separated by space.

root@kerneltalks # ls
directory1  directory2  testfile1  testfile2

Long listing using ls -l

For more detailed information, use long listing. That is using -l switch with ls command.

root@kerneltalks # ls -l
total 16
drwxr-xr-x 2 root admin 4096 Sep 14 18:07 directory1
drwxr-xr-x 2 root admin 4096 Sep 14 18:07 directory2
-rw-r--r-- 1 root admin    8 Sep 14 18:08 testfile1
-rw-r--r-- 1 root admin   51 Sep 14 18:08 testfile2

Information is displayed column wise where –

  • The first column is file/directory permission details
  • The second column is tree count
  • The third column is the owner of the file/directory
  • A fourth column is a group of file/directory
  • The fifth column is the size in blocks
  • Sixth, the seventh column is Date
  • Eight columns have the last modification time of file/directory
  • The last column is file or directory name.

Listing hidden files using ls

Normal ls command won’t display hidden files. Hidden files in Linux are files whose names start with .

These files can be listed using -a switch.

root@kerneltalks # ls -al
total 32
drwxr-xr-x   4 root admin  4096 Sep 14 18:08 .
drwxrwxrwt. 11 root root    12288 Sep 14 18:07 ..
drwxr-xr-x   2 root admin  4096 Sep 14 18:07 directory1
drwxr-xr-x   2 root admin  4096 Sep 14 18:07 directory2
-rw-r--r--   1 root admin    15 Sep 14 18:08 .account_detail
-rw-r--r--   1 root admin     8 Sep 14 18:08 testfile1
-rw-r--r--   1 root admin    51 Sep 14 18:08 testfile2

You can see in the above output, hidden file  .account_detail (name starts with .) is listed.

Listing human readable file sizes

In long listing we have seen that file size is displayed in block size. This is not a user-friendly format since you have to convert blocks to conventional byte size. Easy human-readable format like KB, Mb, GB is available with switch -h. Using these file sizes will be displayed in a human-readable format.

root@kerneltalks # ls -hl
total 16K
drwxr-xr-x 2 root admin 4.0K Sep 14 18:07 directory1
drwxr-xr-x 2 root admin 4.0K Sep 14 18:07 directory2
-rw-r--r-- 1 root admin    8 Sep 14 18:08 testfile1
-rw-r--r-- 1 root admin   51 Sep 14 18:08 testfile2

Here size is displayed as 4K for directories i.e. 4 Kilobyte.

Listing inode numbers of files

Inodes are the numbers assigned to each file/directory in the Linux system. Once can view them using -i switch.

root@kerneltalks # ls -i
18 directory1  30 directory2  32 testfile1  43 testfile2

Numbers 18, 30, 32, and 43 are respective inodes of those files and directories on right.

Sorting files with time of last modify time

This is one of the most widely used formats of ls command. Switch used are -l (long listing), -r (reverse sort), -t (the sort with modification time). Due to the reverse sort, the latest updated file will be shown at the bottom of the output.

root@ kerneltalks # ls -lrt
total 16
drwxr-xr-x 2 root admin 4096 Sep 14 18:07 directory1
drwxr-xr-x 2 root admin 4096 Sep 14 18:07 directory2
-rw-r--r-- 1 root admin    8 Sep 14 18:08 testfile1
-rw-r--r-- 1 root admin   51 Sep 14 18:08 testfile2

Listing file owners with their IDs

Normal long listing shows owner and group as their names. To list owner and group as UID and GID you can use -n switch.

root@kerneltalks # ls -n
total 16
drwxr-xr-x 2 0 512 4096 Sep 14 18:07 directory1
drwxr-xr-x 2 0 512 4096 Sep 14 18:07 directory2
-rw-r--r-- 1 0 512    8 Sep 14 18:08 testfile1
-rw-r--r-- 1 0 512   51 Sep 14 18:08 testfile2

Listing directories by appending / to their names

ls command without argument lists all files and directory names. But without long listing (in which directories have their permission string starts with d) you won’t be able to identify directories. So here is a tip. Use -p switch. It will append / to all directory names and you will identify them easily.

root@kerneltalks # ls -p
directory1/  directory2/  testfile1  testfile2

You can see both directories has / appended to their names.

Listing directories recursively

The long listing or normal ls command shows you only directories residing in the current directory (from where you are running the command). To view files inside those directories you need to run ls command recursively i.e using -R switch.

root@kerneltalks # ls -R
.:
directory1  directory2  testfile1  testfile2

./directory1:
file1  file2

./directory2:
file3  file4

In output you can see –

  • First part . means current directory and then a list of files/directories within it.
  • Second part says ./directory1 and then a list of files/directories within it.
  • The third part lists files/directories within ./directory2.
  • So it listed all the content of both directories which resides on our present working directory.

Sorting files by file size

Sorting list with their size. Use -S switch. It will sort in descending order i.e. high size files being at the top.

root@kerneltalks # ls -lS
total 16
drwxr-xr-x 2 root admin 4096 Sep 14 18:16 directory1
drwxr-xr-x 2 root admin 4096 Sep 14 18:16 directory2
-rw-r--r-- 1 root admin   51 Sep 14 18:08 testfile2
-rw-r--r-- 1 root admin    8 Sep 14 18:08 testfile1

Listing only owners of files

Want to list only owners of files? Use -o switch. Group won’t be listed in the output.

root@kerneltalks # ls -o
total 16
drwxr-xr-x 2 root 4096 Sep 14 18:16 directory1
drwxr-xr-x 2 root 4096 Sep 14 18:16 directory2
-rw-r--r-- 1 root    8 Sep 14 18:08 testfile1
-rw-r--r-- 1 root   51 Sep 14 18:08 testfile2

Listing only groups of files

Opposite of the above. Group will be listed and users won’t be listed for -g switch.

root@kerneltalks # ls -g
total 16
drwxr-xr-x 2 admin 4096 Sep 14 18:16 directory1
drwxr-xr-x 2 admin 4096 Sep 14 18:16 directory2
-rw-r--r-- 1 admin    8 Sep 14 18:08 testfile1
-rw-r--r-- 1 admin   51 Sep 14 18:08 testfile2

This is it! 12 different examples of ls command which can be helpful to you in your daily Linux learning. Do subscribe to our blog to get the latest post notifications about Linux howto guides. Let us know if you want to cover some beginner’s topics in the comments section below.