ls Command in Linux

superior_hosting_service

ls Command in Linux With Example

ls-command-in-linux.jpg

ls is one of the basic commands that any Linux user should know.

The ls command lists files and directories within the file system, and shows detailed information about them. It is a part of the GNU core utilities package which is installed on all Linux distributions.

This article will show you how to use the ls command through practical examples and detailed explanations of the most common ls options.

How to Use the ls Command

The syntax for the ls command is as follows:

$ ls [OPTIONS] [FILES]

When used with no options and arguments, ls displays a list of the names of all files in the current working directory :

$ ls

The files are listed in alphabetical order in as many columns as can fit across your terminal:

OUTPUT
cache  db  empty  games  lib  local  lock  log  mail  opt  run  spool  tmp

To list files in a specific directory, pass the directory path as an argument to the ls command. For example, to list the contents of the /etc directory, you would type:

$ ls /etc

You can also pass multiple directories and files separated by space:

$ ls /etc /var /etc/passwd

If the user you are logged in with doesn’t have read permissions to the directory, you will get a message saying that ls can’t open the directory:

$ ls /root
OUTPUT
ls: cannot open directory '/root': Permission denied

The ls command has a number of options. In the sections below, we will explore the most commonly used options.

Long Listing Format

The default output of the ls command shows only the names of the files and directories, which is not very informative.

The -l ( lowercase L) option tells ls to print files in a long listing format.

  • The file type.
  • The file permissions.
  • Number of hard links to the file.
  • File owner.
  • File group.
  • File size.
  • Date and Time.
  • File name.

Here is an example:

$ ls -l /etc/hosts
OUTPUT
-rw-r--r-- 1 root root 337 Oct  4 11:31 /etc/hosts

Let’s explain the most important columns of the output.

The first character shows the file type. In this example, the first character is -, which indicates a regular file. Values for other file types are as follows:

  • - – Regular file.
  • b – Block special file.
  • c – Character special file.
  • d – Directory.
  • l – Symbolic link.
  • n – Network file.
  • p – FIFO.
  • s – Socket.

The next nine characters are showing the file permissions. The first three characters are for the user, the next three are for the group, and the last three are for others. You can change the file permissions with the chmod command. The permission character can take the following value:

  • r – Permission to read the file.
  • w – Permission to write to the file.
  • x – Permission to execute the file.
  • s – setgid bit.
  • t – sticky bit.

In our example, rw-r--r-- means that the user can read and write the file, and the group and others can only read the file. The number 1 after the permission characters is the number of hard links to this file.

The next two fields root root are showing the file owner and the group, followed by the size of the file (337), shown in bytes. Use the -h option if you want to print sizes in a human-readable format. You can change the file owner using the chown command.

Oct 4 11:31 is the last file modification date and time.

The last column is the name of the file.

Show Hidden Files

By default, the ls command will not show hidden files. In Linux, a hidden file is any file that begins with a dot (.).

To display all files including the hidden files use the -a option:

$ ls -la ~/
OUTPUT
drwxr-x--- 10 reconshell  reconshell  4096 Feb 12 16:28 .
drwxr-xr-x 18 reconshell  reconshell  4096 Dec 26 09:21 ..
-rw-------  1 reconshell  reconshell  1630 Nov 18  2017 .bash_history
drwxr-xr-x  2 reconshell  reconshell  4096 Jul 20  2018  bin
drwxr-xr-x  2 reconshell  reconshell  4096 Jul 20  2018  Desktop
drwxr-xr-x  4 reconshell  reconshell  4096 Dec 12  2017 .npm
drwx------  2 reconshell  reconshell  4096 Mar  4  2018 .ssh

Sorting the Output

As we already mentioned, by default, the ls command is listing the files in alphabetical order.

The --sort option allows you to sort the output by extension, size, time and version:

  • --sort=extension (or -X ) – sort alphabetically by extension.
  • --sort=size (or -S) – sort by file size.
  • --sort=time ( or -t) – sort by modification time.
  • --sort=version (or -v) – Natural sort of version numbers.

If you want to get the results in the reverse sort order, use the -r option.

For example, to sort the files in the /var directory by modification time in the reverse sort order you would use:

$ ls -ltr /var

It’s worth mentioning that the ls command does not show the total space occupied by the directory contents. To get the size of a directory , use the du command.

List Subdirectories Recursively

The -R option tells the ls command to display the contents of the subdirectories recursively:

$ ls -R

Conclusion

The ls command lists information about files and directories. For more information about ls visit the GNU Coreutils page or type man ls in your terminal.

If you have any questions or feedback, feel free to leave a comment.