How one can Use the ls Command in Linux

Getting detailed information about files in your storage is difficult if you don't know how to use the ls command. Here we are going to discuss everything related to the ls command on Linux, along with some of the different flags used with it.

The ls command on Linux

The ls command lists all files and folders that are in your current working directory. You can also get various information about the files using the same command. Since it is already included in the GNU Core Utilities package, you don't have to install an additional package on your system to use it.

You can chain ls with other bash commands as well. For example pipelines a grep Statement with ls This option allows you to search and filter the directory for specific files.

Using the ls command

The basic syntax of the ls command is:

ls (options) (directory)

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


If you run the above instruction from the root directory of your system, you will see 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

List files in a specific directory

To list files that belong to another folder (not the current working directory), you must pass the directory path along with the command name.

ls (directory)

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

ls / boot

The output will now show the files and folders that exist in the specified directory name.

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

Use of -F The flag with the command adds an / sign at the end of each 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 room Character.

ls / boot / usr

/ boat:
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 other directories and files on your system. It is the top folder in your computer's directory hierarchy. A root directory is commonly referred to as the /. Character.

ls /

It doesn't matter what directory you are in when you enter the command. The above command produces an output listing all subfolders and files in the root directory.

List files in the parent directory

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

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

ls ..

bin etc include lib lib32 lib64 local sbin share src

Adding another .. This takes you to the parent directory of the parent directory. For example, / var / log / old is your current working directory. ls .. lists the folders that are in the /Log Directory during ls ../ .. You will get a list of all files and folders in the / var Directory.

ls ../ ..

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

List files in the home directory

The home directory under Linux is marked with ~ Character. To list the content available in your home directory:

ls ~

List directories only (no files)

If for some reason you only want to list folders that exist in a directory, use the -d Flag with the standard command ls.

ls -d / home

List files with subdirectories

Use of * * Characters with the command ls you get a list of all files and folders in the current working directory as well as the subdirectories.

ls *

List files recursively

Use of -R The flag with the standard command lists all files and folders present in a directory down to the last level.

ls -R

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

List files by their size

Use the key to get the names of all files along with their size -s Flag with the command.

ls -s / yay-git

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

Related: Moving Files on Linux Using the Mv Command

Linux mv command

List files with detailed information

The -l Use flag to get a list of the contents of a Linux directory with a detailed description of each entry. The following information is included in the output:

  1. File and folder permissions

  2. Number of links

  3. Content owner

  4. Group owner

  5. Content size

  6. Filenames

  7. Date and time of the last change

ls -l

total of 2944
drwxr-xr-x 3 sharmadeepesh sharmadeepesh 4096 February 8 13:53 pkg
drwxr-xr-x 4 sharmadeepesh sharmadeepesh 4096 February 8th 13:52 src
drwxr-xr-x 7 sharmadeepesh sharmadeepesh 4096 February 8th 13:54 yay
-rw-r – r– 1 sharmadeepesh sharmadeepesh 2998674 February 8 13:53 yay-git-10.1.2.r0.g7d849a8-2-x86_64.pkg.tar.zst

The first column is reserved for file and folder permissions. The first character indicates the file type and the next nine characters indicate the file's permissions.

The different types of files you will come across frequently:

  1. Normal files (-)

  2. Block special files (b)

  3. Character special files (c)

  4. Directory (d)

  5. Symbolic connection (l)

  6. Network file (s)

  7. FIFO (p)

  8. Socket (s)

In relation to file permissions, the following characters are used in the output.

  1. Readable (r)

  2. Writable (w)

  3. Executable file (x)

Let's take drw-r – r– as an an example. The first character indicates that the entry is a directory. The following two characters indicate that the current user has read and write permissions. The remaining characters contain information about file permissions for other users.

List files of readable size

The -s The command gives you a numeric value that is assigned to each entry. And how obvious, you wouldn't know what the meaning of this value is. So use the option to list files and their sizes in a readable manner -lh Flag along with the command.

ls -lh

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

The output uses sizes for bytes (B), megabytes (MB), gigabytes (GB), and terabytes (TB).

List hidden files

The standard ls The command does not include any hidden files in the output. To list the content set as hidden by the user, pass the -on Flag with the command ls.

ls -a

Piping ls with grep command

The grep command is used to match patterns that follow a particular regular expression. You can chain this command with ls to look for files that are present on your system. In your root directory, enter:

ls | grep l

This will list all files and folders that begin with the l character. You can also use grep to filter your files by their extensions.

Sort files by time and date

Use the option to list all files and sort them by time and date they were created / modified -t Flag together with ls.

ls -t

Sort files by size

The -S With flag you can sort the files and folders according to their file size.

ls -S

By default, the files are 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

Use of > Characters, you can send the output of the ls command to any file.

ls> ls-output.txt

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

View the contents of a directory with the ls command

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

The number one tip for getting used to Linux is to memorize a few basic commands. This will surely help you become much more efficient and faster while using your system.

most used linux terminal cmds

The Linux Commands Reference Guide

With this simple cheat sheet, you can be up and running with the Linux command line terminal in no time.

About the author

Deepesh Sharma
(12 articles published)

Deepesh is a tech blogger and has been writing informative content for over 3 years. He is currently completing his Bachelor in Computer Applications at a prestigious university in India. In his spare time he enjoys writing, listening to music and playing the guitar.

From Deepesh Sharma

Subscribe to our newsletter

Subscribe to our newsletter for tech tips, reviews, free e-books, and exclusive deals!

One more step …!

Please confirm your email address in the email we just sent you.

. "border =" 0

Leave a Reply

Your email address will not be published. Required fields are marked *