Getting detailed information related to files on your storage is tricky if you do not know how to use the ls command. Here we'll discuss everything associated with the ls command on Linux, along with some various flags used with it.

The ls Command on Linux

The ls command is used to list down all the files and folders present in your current working directory. You can also get a variety of information about the files using the same command. Since it is already included in the GNU core utilities package, you don't need to install any additional package on your system to use it.

You can chain ls with other bash commands as well. For example, piping a grep statement with ls will allow you to search and filter the directory for specific files.

How to Use the ls Command

The basic syntax of the ls command is:

        ls [options] [directory]
    

One of the most simple use of the command is to list all the files and folders in your current working directory.

        ls
    

If you execute the aforementioned statement in your system's root directory, you will see an output that looks something like this.

        bin    dev   home   lib64        mnt   proc   run    srv   tmp   var
boot etc lib lost+found opt root sbin sys usr

Listing Files in a Specific Directory

For listing files that belong to a different folder (not the current working directory), you will have to pass the directory path along with the command name.

        ls [directory]
    

To get the list of all the files present in the /boot directory:

        ls /boot
    

The output will now show the files and folders present in the directory name provided.

        EFI   grub   initramfs-linux-fallback.img   initramfs-linux.img   vmlinuz-linux
    

Using the -F flag with the command will add a / character at the end of every directory.

        EFI/   grub/   initramfs-linux-fallback.img   initramfs-linux.img   vmlinuz-linux
    

You can also pass multiple directories by separating the path names with a Space character.

        ls /boot /usr
    
        Output

/boot:
EFI grub initramfs-linux-fallback.img initramfs-linux.img vmlinuz-linux

/usr:
bin etc include lib lib32 lib64 local sbin share src

List Files in the Root Directory

The root directory contains all the other directories and files on your system. It is the top-most folder in your computer's directory-hierarchy. A root directory is generally denoted by the / character.

        ls /
    

It doesn't matter which directory you're in at the time of entering the command, the above-mentioned command will produce an output that lists all the sub-folders and files present inside the root directory.

List Files in the Parent Directory

A parent directory in Linux is a directory above the current directory. Let's take /usr/bin as an example. Here, /bin is your current working directory, and /usr is the parent directory.

To get a list of all the files in a parent directory:

        ls ..
    
        bin   etc   include   lib   lib32   lib64   local   sbin   share   src
    

Adding another .. will take you to the parent directory of the parent directory. For example, /var/log/old is your current working directory. ls .. will list the folders present in the /log directory whereas ls ../.. will provide you with a list of all the files and folders contained in the /var directory.

        ls ../..
    
        cache   db   empty   games   lib   local   lock   log   mail   opt   run   spool   tmp
    

List Files in the Home Directory

The home directory in Linux is denoted by the ~ character. Therefore, to list the content available in your home directory:

        ls ~
    

List Only Directories (No Files)

If for any reason you only want to list folders present in a directory, use the -d flag with the default ls command.

        ls -d /home
    

List Files With Sub-Directories

Using the * character with the ls command will provide you with a list of all the files and folders in the current working directory, along with the sub-directories as well.

        ls *
    

List Files Recursively

Using the -R flag with the default command will list down all the files and folders present inside a directory down to the last level.

        ls -R
    

Note that you can also pass the directory path along with the recursive flag. This means that ls /usr/home -R is a valid command.

List Files With Their Size

To get the names of all the files along with their size, use the -s flag with the command.

        ls -s /yay-git
    
        total 2944
4 pkg 4 src 4 yay 2932 yay-git-10.1.2.r0.g7d849a8-2-x86_64.pkg.tar.zst

Related: Move Files in Linux With the Mv Command

List Files With Detailed Information

The -l flag allows you to get a list of a Linux directory's content with a detailed description of each entry. Following information are included in the output:

  1. File and folder permissions
  2. Number of links
  3. Content owner
  4. Group owner
  5. Content size
  6. File name
  7. Last-modified date and time
        ls -l
    
        total 2944
drwxr-xr-x 3 sharmadeepesh sharmadeepesh 4096 Feb 8 13:53 pkg
drwxr-xr-x 4 sharmadeepesh sharmadeepesh 4096 Feb 8 13:52 src
drwxr-xr-x 7 sharmadeepesh sharmadeepesh 4096 Feb 8 13:54 yay
-rw-r--r-- 1 sharmadeepesh sharmadeepesh 2998674 Feb 8 13:53 yay-git-10.1.2.r0.g7d849a8-2-x86_64.pkg.tar.zst

The first column is reserved for the file and folder permissions. The first character denotes the type of file and the next nine characters denote the permissions of the file.

The various types of files that you'll often come across:

  1. Regular files (-)
  2. Block special files (b)
  3. Character special files (c)
  4. Directory (d)
  5. Symbolic link (l)
  6. Network file (n)
  7. FIFO (p)
  8. Socket (s)

Talking about file permissions, the following characters are used in the output.

  1. Readable (r)
  2. Writable (w)
  3. Executable (x)

Let's take drw-r--r-- as an example. The first character tells that the entry is a directory. The following two characters denote that the current user has read and write permissions. The rest of the characters provide information on the file permissions for other users.

List Files With Readable Size

The -s command provides you with a numeric value associated with each entry. And as obvious, you wouldn't know what is the meaning of this value. Therefore, to list down files and their sizes in a readable manner, use the -lh flag along with the command.

        ls -lh
    
        total 2.9M
drwxr-xr-x 3 sharmadeepesh sharmadeepesh 4.0K Feb 8 13:53 pkg
drwxr-xr-x 4 sharmadeepesh sharmadeepesh 4.0K Feb 8 13:52 src
drwxr-xr-x 7 sharmadeepesh sharmadeepesh 4.0K Feb 8 13:54 yay
-rw-r--r-- 1 sharmadeepesh sharmadeepesh 2.9M Feb 8 13:53 yay-git-10.1.2.r0.g7d849a8-2-x86_64.pkg.tar.zst

Size specifiers for bytes(B), megabytes(MB), gigabytes(GB), and terabytes(TB) are used in the output.

List Hidden Files

The default ls command doesn't include hidden files in the output. To list the content which is set as hidden by the user, pass the -a flag with the ls command.

        ls -a
    

Piping ls With Grep Command

The grep command is used to match patterns that follow a specific regular expression. You can chain this command with ls in order to search for files present in your system. In your root directory, type:

        ls | grep l
    

This will list down all the files and folders that start with l character. You can also filter your files according to their extensions using grep.

Sort Files by Time and Date

To list all the files and sort them according to the time and date of creation/modification, use the -t flag along with ls.

        ls -t
    

Sort Files by Size

The -S flag will allow you to sort the files and folders in accordance with their file size.

        ls -S
    

By default, the files will be sorted in descending order (largest file first). However, you can easily reverse this behavior by adding r with the -S flag.

        ls -Sr
    

List Files and Send Output to a File

Using the > character, you can send the output of the ls command to any file.

        ls > ls-output.txt
    

Later, you can read the content of the newly created file by typing cat ls-output.txt in your terminal.

Displaying Contents of a Directory With ls Command

The ls command is one of the most powerful commands provided to Linux users. To get the most out of your commands in terminal, you can try learning chaining commands together. You can even pipe the mv command for moving files with ls.

The number one tip to get comfortable with Linux is to memorize some basic commands. This will surely help you in becoming much efficient and quick while using your system.