Monday, 27 January 2014

Basic Usage of 'ls' Command in Linux With Examples

    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.

Syntax:

ls -l
Output:


In the output:
  • 1st column will give detailed information regarding file permission
  • 2nd column will tell you about the number of links to the file, 
  • 3rd and 4th columns are associated with owner and group of the file, 
  • 5th column will be displaying the size of the file in bytes, 
  • 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.

Syntax:

ls -color=[VALUE]
Output:


11. Give Me INODE Numbers

When the ls command is complemented by option -i, it will print the Inode (Index) Numbers of the files in the first column. (Read more about INODES in our article - Understanding INODES in Linux/Unix Filesystems)

Syntax:

ls -li
Output:


12. Marking the Directories with a Slash (/) Sign

This is to make identifications of the directories easier.

Syntax:

ls -p
Output:


13. Sort in Reverse Order

This is for sorting the results in reverse order.

Syntax:

ls -r
Output:


14. Show the Contents of Sub-Directories

ls command when used with option -R, it will display the contents of the sub-directories too.

Syntax:

ls -R
Output:


15. Sort by Extensions

With the option -X, you can group the files with same extensions together.

Syntax:

ls -lX
Output:


16. Sort by Modification Time

You can sort the output according to the time of modification. Recently modified file will be displayed first.

Syntax:

ls -lt

17. Listing the Contents of Home Directory

Syntax:

ls ~

18. Listing the Contents of Parent Directory

Syntax:

ls ../

19. Check out the Version Details

Syntax:

ls --version
That's all about ls command !!

3 comments:

  1. Good explanation

    ReplyDelete
  2. Thanks! this helped me a lot !!!

    ReplyDelete
  3. Finally an explanation everybody (included me) understand

    ReplyDelete

    Total Pageviews