What is the ls command in Linux?

The ls command in Linux is used to list the files of a given directory.

The ls command comes as part of the GNU Core Utilities package, available on all Unix-like operating systems.

Arguments

The ls command has many functionalities. It accepts different flags or arguments in accordance with the required functionality.

This shot will go through the most frequently used flags of the ls command.

ls

The ls command is used without any arguments to list all the files and folders in the terminal’s current directory.

ls path

To list all the files and folders in a particular directory, the path of this directory is input as the first argument to the ls command.

ls /

The / argument is used to list all files and folders in the root directory.

ls . .

The .. argument is used to list down the files and folders in the parent directory of the current directory.

ls ~

The ~ argument is used to list all files and folders in the home directory.

ls -R

The -R argument is used to recursively list down all the folders and files in all the directories and sub-directories of the system.

ls -lS

The -lS flag lists down folders and files in sorted order (according to size).

ls -a

The -a argument is used to list down any hidden folders or files.

Example

In the example below, we first use the ls command without any argument to list all the files and folders in the current directory.

Subsequently, we input the path of the bin folder and list all the folders and files in it.

Lastly, we use the -R argument of the ls command to list down all the files and folders of the system. The contents of all the sub-directories and directories are fetched recursively.

Terminal 1
Terminal
Loading...
Copyright ©2024 Educative, Inc. All rights reserved