[email protected]Office hours: Mon – Fri 9:00AM – 5:00PMLinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedJune 1, 2023

How to use ls command.

How To Use Ls Command.

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

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.

 

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:

 file              liviu.texts         sor : 0
anaconda-ks.cfg                                         file1             liviu.texts.sh      substraction.sh
archive.tar.gz                                          file2             ll.txt              test.py
binar.sh                                                file3             music.gz            test.txt
calculator.sh                                           file.out          my                  test.y
database_name-20230503.sql                              file.text         MY_ARR              top.clasic.music.sh
database_name.sql.gz                                    file.txt          myfavoritemusic.gz  top.music.sh
desktop                                                 fillename  

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

When the long listing format is used, you can see the following file information:

  • 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
-rw-r--r--. 1 root root 100 Jan 25 20:40 /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.

  • ssetgid bit.

  • tsticky 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 ~/
dr-xr-x---. 10 root root   4096 May 31 16:04 .
dr-xr-xr-x. 18 root root   4096 Feb 15 18:17 ..
-rwxr-xr-x.  1 root root    144 Jan 28 12:23 Addition.sh
-rw-------.  1 root root   1303 Sep 25  2021 anaconda-ks.cfg
-rw-r--r--.  1 root root     45 Feb  3 20:23 archive.tar.gz
-rw-------.  1 root root  20276 May 31 22:30 .bash_history
-rw-r--r--.  1 root root     18 Dec 29  2013 .bash_logout
-rw-r--r--.  1 root root    176 Dec 29  2013 .bash_profile
-rw-r-----.  1 root root    251 Apr 12 20:19 .bashrc
-rwxr-xr-x.  1 root root    560 Jan 28 12:28 binar.sh
-rwxrwxrwx.  1 root root    606 Jan 28 12:20 calculator.sh
-rw-r--r--.  1 root root    100 Dec 29  2013 .cshrc

 

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.

Keep reading
How to Implement Network Segmentation and Resource Isolation in Linux.
February 6, 2024

How to Implement Network Segmentation and Resource Isolation in Linux.

How to Implement Network Segmentation and Resource Isolation in Linux. Securing and managing networks in a Linux environment involves advanced practices like network segmentation and resource isolation. This guide explores these concepts, providing detailed instructions and examples for…

Read article
How to open or close ports in Linux.
February 6, 2024

How to open or close ports in Linux.

How to open or close ports in Linux. Effectively managing ports on your Linux system is crucial for both security and service accessibility. This comprehensive guide will provide a detailed walkthrough on opening and closing ports using the robust tools iptables and ufw. Understanding these…

Read article
How to use ypdomainname command.
January 23, 2024

How to use ypdomainname command.

How to use ypdomainname command. In the realm of Linux networking, the ypdomainname command plays a crucial role in the context of the Network Information Service (NIS). NIS, formerly known as Yellow Pages, is a distributed database service that facilitates the sharing of network configuration and…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, colocation, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Office hours: Mon – Fri 9:00AM – 5:00PM