Monday, 27 January 2014

One of the most used commands by Linux users and the one that a Linux beginner must learn is- 'ls' Command. It is usually used to view the contents of current directory. So, when you run this command, the files and sub-directories included under the current directory will be listed before you. Very useful command and everyone who uses Linux should know this command.

This tutorial will guide you to know more about 'ls' command with it's basic usages. Here we start !

1. ls Command - Basic Usage

The most basic use of any command is when it is used without any option or parameter. So, when ls command is executed with no option or parameter, then it will enlist files and sub-directories of current directory.

Syntax:

ls

Output:

2. A Long List

ls command when used with option -l (L in small caps), it will display the contents of the current directory in a long list format.

6th column will display the recent time and date at which the file was modified,

and the last and 7th column is the actual file/directory name.

3. Displaying Size in Human Readable Format

Sometimes it is very confusing to read the size of the files in terms of bytes, especially when the file size is very large. So, instead of printing the file size in terms of bytes, we can display it in human readable form, like MB, GB, TB and so on. This can be achieved when ls command is executed with the option -h as follows:

Syntax:

ls -lh

Output:

4. Sort According to File Size

You can sort the ls command output such that largest file (according to size) will be displayed first and smallest one at the last and this can be done with the use of option -S.

Syntax:

ls -lhS

Output:

5. Determine the Block Size

If you wish to display the size of all the files in a specific human readable format, you can do it by using following syntax:

Syntax:

ls -l --block-size=[SIZE]

The [SIZE] in above syntax can be replaced by following letters in order to scale the size as per your requirement:

K = Kilobyte

M = Megabyte

G = Gigabyte

T = Terabyte

P = Petabyte

E = Exabyte

Z = Zettabyte

Y = Yottabyte

Output:

In the above example, I have scaled the file size to KB and to do so, I have replaced the [SIZE] thing in the syntax with letter 'K'.

6. List the Hidden Files too

In Linux, the files can be hidden by starting a file name with a "." (dot) symbol. You can list these hidden files by the use of option -a with ls command.

Syntax:

ls -a

Output:

7. Show Me Only Sub-Directories

If you wish to exclude all the files and display only sub-directories, then you can use option -d as follows:

Syntax:

ls -d */

Output:

8. Don't Display Owner and Group Info

You can exclude the columns providing Owner and Group information by using options -g and -G respectively.

Syntax:

ls -g

Output:

ls -g : Owner Info Excluded

ls -lG

ls -G : Group Info Excluded

9. Display User ID and Group ID

When ls command is run with option -n, it will print UID and GID of the owner and group instead of their names.

Syntax:

ls -n

Output:

10. Color Changes

This will simply colorize and de-colorize the list when the [VALUE] in the syntax given below is replaced by proper values. You can de-colorize it by replacing [VALUE] by never and colorize it by replacing it with auto.

Thanks for the helpful blog! Lots of LS command examples you have shared here. I would like to include few more examples like : show the size of file ($ ls -lh), print entries without owner information ($ ls -g), there are lot more examples, check 20 'LS' Command in Linux here.