Beginners Guide for ls Command in Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

The ls command is used to list all files and directories in the specified directory. Apart from that, it will also show you the properties of the files and directories, like their permissions, ownership, size, and modification date.

Tutorial Details

Descriptionls
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitesls, grep
Internet RequiredNo

Syntax of the ls Command

The ls command takes two arguments as input, and both are optional.

$ ls [OPTION] [LOCATION]

Listing All Files in the Present Directory

The following command, without specifying any flag or path, will list the files and directories in the current directory in a single row.

$ ls

Output:

Listing the content of the directory

Listing All Files in the Home Directory

If your PWD is not your home directory, you can use the following command to list all files and directories in your home directory.

$ ls ~

Output:

Listing the content of the home directory

Listing All Files in the Parent Directory

The following command will list the parent directory files.

$ ls ../

Output:

Listing the content of the parent directory

Listing All Files with Trailing Slashes Added to Directory Names

The “-F” flag will add the trailing “/” slashes at the end of the directory name that make them easy to spot.

$ ls -F

Output:

Listing the content of a directory with trailing slashes

Listing One File Per Line with their Properties

The “-l” flag is used to list one file per line, including their properties like permissions, ownership, size, and modification date.

$ ls -l

Output:

Listing the content of directory in newline

Listing the Hidden Files

The following command will list the hidden files in your current working directory.

$ ls -a

Output:

Listing all the files, including those hidden in the directory

Listing the Hidden Files without the Current and Parent Directories

The following command will list all the hidden files and directories except “.” (current directory) and “..” (parent directory).

$ ls -A

Output:

Removing the current and parent listings while listing the content of the directory

Listing Files Recursively

The following command will recursively list all the files and directories within the current directory tree.

$ ls -R

Output:

Recursively listing the content of the directory

Listing All Files with their Properties

The following command will display a long format list for all files, including their permissions, ownership, size, and modification date.

$ ls -la

Output:

Listing the all the files including hidden in newline with their property

Listing All Files with Size Displayed Using Human Readable Units

The following command will display all the files in a long format list with human-readable units (KiB, MiB, GiB).

$ ls -lh

Output:

Listing the file size in human-readable format

To view the hidden files as well, add the “-a” flag to your present command, like this: “ls -lha“.

Listing All Files Sorted By Size (Descending)

The following command will list all the files in your current working directory based on their size, from descending to ascending.

$ ls -lS

Output:

Sorting all files based on their size (descending order)

You can see above that “file.txt” with 0 bytes ended up at the end.

Listing All Files Sorted By Modification Date (Oldest First)

The following command will list all the files in your current working directory based on their modification dates, from oldest to newest.

$ ls -ltr

Output:

Sorting the files based on their modification dates (descending)

Only List Files

The following command will ignore all the directories by listing only the files in your current working directory using the grep command.

$ ls -p | grep -v /

Output:

Only list the files

Only List Directories

The following command will only list the directories in your current working directory.

$ ls -d */

Output:

Only the directories are listed.

Only List Hidden Files and Directories

The following command will only list the hidden files and directories in your current working directory using the grep command.

$ ls -a | grep "^\."

Output:

Only list the hidden files and directories

Alternatively, you can also use the “ls -ld .*” command to list them with their properties.

$ ls -ld .*

Output:

List the hidden files and directories with their properties

Listing All Files by Inode Number

The following command will list the inode number for all files and directories in your current working directory that can be later used for internal maintenance.

$ ls -i

Output:

Listing all files and directories inode number

Groups the Files with Command Extensions

The following command will group files with the same extension together in the list.

$ ls -lX

Output:

Grouping the files based on their types

That was the end; I tried to include all the necessary examples in one article so you wouldn’t have to worry.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.