Javatpoint Logo
Javatpoint Logo

Linux ls command

The ls is the list command in Linux. It will show the full list or content of your directory. Just type ls and press the enter key. The whole content will be shown.

ls is a command used to list computer directories and files in Unix-like and Unix operating systems. It is developed by the Single Unix Specification and POSIX.
It's available inside the EFI shell, as a component of the UnxUtils group of native Win32 ports of basic GNU Unix-like utilities as an isolated package for Microsoft Windows, or as a component of the MSX-DOS2 Tools of ASCII for MSX-DOS version 2.

The numerical computing environments GNU Octave and MATLAB contain an ls function with the same functionality. In several other environments like Microsoft Windows, OS2, and DOS, the same functionality is given by the dir command. The different implementations include different options, as with almost every utility. We can check the documentation given by the command for suitable options and usage.

Brief History of ls

An ls utility occurred in the AT&T UNIX first version, the title acquired from the same command in Multics also titled 'ls', an acronym for the "list" term. ls is a component of the X/Open Portability Guide from issue 2 of 1987. It was acquired into the POSIX.1 first version and the Single Unix Specification.

Behavior

Unix-like and Unix operating systems manage the thought of a working directory. ls lists several files inside the working directory when used without arguments. In that directory, the files are listed if a directory is mentioned. The arguments might include directories and files.

Names beginning with the "." symbol are hidden. The "." directory is known as the working directory, and the ".." symbol is known as its parent directory. By default, they are shown. All files are displayed using -a. File names explicitly specified are listed always.

ls only displays names without options. Multiple options might be combined. The basic options are:

  • -h: It shows result sizes in a human readable format (for example, 2G 234M, 1K, etc.). This option is not a component of the POSIX standard. However, implemented in many systems, e.g., Solaris 9 in 2002, FreeBSD 4.5 in 2002, and GNU coreutils in 1997.
  • -l: It stands for long format. It shows Unix file types, number of hard links, permissions, group, owner, last modified name and date-time, and size. If the changed date is older than six months, the time is substituted with the year. A few implementations add extra flags to permissions.

It is possible to display different items with different types of colors. It is an area in which implementations differ:

  • FreeBSD ls applies the -G option. It inspects only the Unix file permission and type and utilizes the termcap database.
  • GNU ls applies the --color option. It inspects the Unix file extension, permission, and type and utilizes its database to manage colors controlled using dircolors.

Example:

Below, you can see, after entering ls command, we got the whole content list of /home/sssit directory.

Linux ls command

Linux ls command options

ls optionDescription
ls -aIn Linux, hidden files start with . (dot) symbol and they are not visible in the regular directory. The (ls -a) command will enlist the whole list of the current directory including the hidden files.
ls -lIt will show the list in a long list format.
ls -lhThis command will show you the file sizes in human readable format. Size of the file is very difficult to read when displayed in terms of byte. The (ls -lh)command will give you the data in terms of Mb, Gb, Tb, etc.
ls -lhSIf you want to display your files in descending order (highest at the top) according to their size, then you can use (ls -lhS) command.
ls -l - -block-size=[SIZE]It is used to display the files in a specific size format. Here, in [SIZE] you can assign size according to your requirement.
ls -d */It is used to display only subdirectories.
ls -g or ls -lGWith this you can exclude column of group information and owner.
ls -nIt is used to print group ID and owner ID instead of their names.
ls --color=[VALUE]This command is used to print list as colored or discolored.
ls -liThis command prints the index number if file is in the first column.
ls -pIt is used to identify the directory easily by marking the directories with a slash (/) line sign.
ls -rIt is used to print the list in reverse order.
ls -RIt will display the content of the sub-directories also.
ls -lXIt will group the files with same extensions together in the list.
ls -ltIt will sort the list by displaying recently modified filed at top.
ls ~It gives the contents of home directory.
ls ../It give the contents of parent directory.
ls --versionIt checks the version of ls command.

Linux ls -a command

It will give you the whole list of a directory including the hidden files also. In Linux, hidden files start with a dot (.) and can't be seen in the regular directory.

Example:


linux-directories-ls-a-command

In the above example, you can see the whole list of files, including the hidden files.

Linux ls -l command

The ls command will only display the files. But if you want your files to be displayed in a long list format, then you can use ls -l command.

Example:


Linux List l

Here, as you can see the list in long list format.

Columns above indicate specific things:

  • Column 1 indicates information regarding file permission.
  • Column 2 indicates the number of links to the file.
  • Column 3 & 4 indicates the owner and group information.
  • Column 5 indicayes size of the file in bytes.
  • Column 6 shows th date and time on which the file was recently modified.
  • Column 7 shows the file or directory name.

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

If you want to display the file size of your list in a particular format or size, then you can use this command. Just put the size in place of [SIZE] as per your requirement.

Syntax:

Example:

Let's see the output below.

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

Here, all file size has listed in Megabyte.

You can replace [SIZE] with the following measures:

  • K = Kilobyte
  • M = Megabyte
  • G = Gigabyte
  • T = Terabyte
  • P = Petabyte
  • E = Exabyte
  • Z = Zettabyte
  • Y = Yottabyte

Linux ls -d */

If you only want to display the sub-directories excluding all other files, you can use this command.

Example:


Linux ls -d */

The above result only shows sub-directories excluding all the other files.

Linux ls -g

If you don't want to display the owner information in your list, then you can exclude this column with the help of this command.

Example:


Linux ls -g

Here owner column is excluded.

Linux ls -lG

If you don't want to display the group information in your list then you can exclude this column with the help of this command.


Linux ls -IG

Here group column is excluded.

Linux ls --color=[VALUE]

This command is used to colorize and decolorize the list. If you replace the [VALUE] by 'auto', it will display the colored list. But, if you will replace the [VALUE] by 'never', it will decolorize the list.

Syntax:

Example:



Linux ls -- color

You can easily notice the difference between auto and never command in the above image.

Linux ls ~

Linux ls ~ command shows the contents of the home directory. Let us see the example of ls ~ command.

Example:


Linux ls ~

Linux ls ../

This command contains the list of the parent directory.

In the given example, our current directory is Downloads, and by using ls ../ command, we have listed out the content of its parent directory "home directory".

Example:


Linux ls ../

Open the last modified file with ls -t

It can sort the file by edited time, displaying the last modified file first. To open the last modified file in the current working directory, we can use the ls command and head commands:

Show file size in HRF (human readable format)

To show file size in a human readable format, i.e., GB, G, KB, MB, etc., we can use the below command:

Where h is short for human readable format.

Show directory information

When we use "ls -l", we will get the information of the directories content. But if we wish to see the information of the directory, we can use the -d option.
For instance, if we use the ls -l /etc command, it will show every file in the etc directory. But, if we wish to show the details of the /etc directory, we can use the -ld option. The command is as follows:

Older files related to the last edited time

We can use the ls -lt command for sorting the file names shown in the sequence of the last edited time. We will find it handy to utilize it with the -l option. The command is mentioned below:

Older files related to the last edited time (reverse order)

We can use the ls -ltr command for sorting the file names shown in the sequence of the last edited time in the reverse sequence. It will show the last modified file in the end line. It becomes more accessible when the listing process goes lengthy. The command is mentioned below:







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA