12 examples of ls command in Linux

Normal ls command without any switch

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

Long listing using ls -l

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

Information is displayed column wise where –

First column is file/directory permission details

Second column is tree count

Third column is owner of the file/directory

Forth column is group of file/directory

Fifth column is size in blocks

Sixth,seventh column is Date

Eight column has last modification time of file/directory

Last column is file or directory name.

Listing hidden files using ls

Normal ls command wont display hidden files. Hidden files in Linux are files whose names starts with .

These files can be listed using -a switch.

You can see in 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 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 this file sizes will be displayed in human readable format.

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 Linux system. Once can view them using -i switch.

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 switch format of ls command. Switch used are -l (long listing), -r (reverse sort), -t (sort with modification time). Due to reverse sort, latest updated file will be shown at the bottom of output.

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.

Listing directories by appending / to their names

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

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

Listing directories recursively

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

In output you can see –

First part . means current directory and then list of files/directories within it.

Second part says ./directory1 and then list of files/directories within it.

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 top.

Listing only owners of files

Want to list only owners of files? Use -o switch. Group wont be listed in output.

Listing only groups of files

Opposite of above. Group will be listed and users wont be listed for -g switch.