Linux ls Command – List Files and Directories

Written by: Linuxopsys   |   Last updated: June 11, 2023

The ls command is one of the most frequently used Linux commands. Just like File explorer for GUI, ls command is a CLI version of it.

ls command is generally used to list files and directories in the file system, other useful features are to list hidden files, list file size, list files by date and time, list files in reverse, and list files UID and GID.

By default ls command list files in alphabetical order.


Simply type ls command on your terminal to display the contents of the current working directory.

ls output

Now almost all Linux terminals display files and directories in colors to differentiate each other. If you wish you can change the color of ls.


ls [options] [file-name/directory-name]


The main ls command options are:

-aDisplay all files including hidden files.
-lList long format
-hList in human-readable file format.
-sList size of files and directories
-rList in reverse order.
-tSort by time, showing the most recently modified files first.
-RList subdirectories recursively.
-FList files and directories name appended with their types using special characters. For example '/' will be appended to directory name to indicate that it is directory.
-mDisplay files and directories as comma separated list.
-QList files and directories with quotation marks.
-iList directories and files with its inode number.
-XDisplay alphabetically by file extension.


Let's look into some basic usage of ls command.

Listing files and directories

The default behavior of ls command is to display files and directories under the current directory

$ ls
ls output current directory

You may use pwd command to verify the absolute path of the current working directory.

Listing specific directory

Type ls <path/to/directory> to list files and directories in that directory:

$ ls Pictures
ls output specific directory

Here ls list all the names of files and directories within the directory named Pictures. However, if you include a trailing slash ("/") after the directory name, it will still work correctly.

You can recursively list all files and directory inside a specific directory using * (wildcard):

$ ls sample *
output of ls sample * command

List content in the parent directory

Type ls .. command to list files and directories in the parent directory:

$ ls ..
ls output of parent directory

The ".." represents the parent directory itself. This way you can easily interact with files and directories one level above your current working directory. You may use ls ../.. to list files two levels above.

Common Options with Examples

Let's look into some of the common options of ls command with its examples.

List files in the user’s home directory

You can combine ~ with ls to quickly view files and directories of the current user's home directory.

$ ls ~

If your home directory is /home/user, ls ~ allows to view contents of it without having to specify the full path.

List files in long format (ls -l)

The ls -l command list files and directories in along format - that includes additional information about each file and directory.


ls -l
ls -l /path/to/directory
ls -l /path/to/filename
ls -l output

ls -l output format:

-rw-r--r-- 1 user group   size   date   time   filename
drwxr-xr-x 2 user group   size   date   time   directory

Each column in the output:

1File PermissionsContains 10 characters. The first character denotes the file type and the remaining 9 character represents the file permissions.
2Number of linksshows the number of hard links the content file or directory has
3OwnerThe owner of the file or directory
4GroupGroup owner of file or directory
5SizeContent size, by default in bytes
6DateThe last modification date of the file or directory.
7TimeThe modification time of the file or directory.
8Filename/Directory name The actual name of the file or directory.

By default ls -l shows all symbolic links and its linked file or directory name. This is indicated by link-filename -> target-file-or-directory-name. You can use ls -L to dereference the symbolic link (not show the linked targets).

You can add --full-time option if you want ls -l to include the seconds and the time zone information.

If you are interested in a more user-friendly and feature-rich listing of files and directories - you can refer to exa.

List file size in human readable format

Type ls -lh to print out a long listing format of files and directories with size in a human readable format. The h option adjusts the file sizes to a more readable format, such as using kilobytes (KB), megabytes (MB), or gigabytes (GB) instead of raw byte counts.

$ ls -lh
ls output in human readable format

List just directories

You can use -d option with ls to list only directories. For example to list only directories in the current directory , type:

$ ls -d */
list only directories

Where the */ pattern matches any directory name because * is a wildcard that represents any characters, and / indicates that it should match directories.

List only files

To list only files - there is no straight forward commad using ls. We can combine with egrep to exclude line start with 'd'.


$ ls -l | egrep -v '^d'
list only files using ls command

List files with their sizes

Use -s option with ls to list files and directories displaying its size in blocks

To list files and directories with their sizes, type ls -s command:

$ ls -s

list files with size

Remember the sizes displayed are the allocation sizes in blocks, not the actual sizes of the files or directories in terms of the number of bytes they occupy. A block is equals to a KiloByte ie 4 indiciates 4 x 64KB.

Display hidden files

In Linux, a hidden file is any file that starts with a dot. By default, ls command won't show hidden files. To view hidden files run ls command with -a the option. This will list all files including hidden files.


$ ls -a 
$ ls -al 
list all files including hidden files

Hidden files or directories in Linux are generally used for configuration file ( eg .bashrc), user specific settings (eg .bash_history) or version control (eg .git directory) requirements.

Ls command - Sort

Use ls with -X or --sort=extension to list file and directories, sort alphabetically by extension.


$ ls -X
$ ls --sort=extension
ls output sort alphabetically by extension

Use -S option with ls to sort by size. Example

$ ls -lS
$ ls --sort=size -l
ls sort files and directories by size

Use ls with -t or --sort=time option to list file and directories by modification time. By default most recently modified files appear first.

$ ls -lt
$ ls --sort=time -l

On that note to sort by modifcation time in ascending order - add -r option to reverse the sorting order. Example:

$ ls -ltr
ls -ltr output

Redirect ls output to a File

To redirect output to a file we can use output redirection operator '>". For example to redirect the output of ls -l to a file named results.txt, type:

$ ls -l results.txt
ls output redirect

You can open the "results.txt" file to view the directory listing using a text editor or display the contents using a command like cat results.txt.

List UID and GID of Files

Use ls with -n option to list and directories with numeric user (UID) and group IDs (GID) . By default ls shows user name group names.


$ ls -n
ls output uid and gid

List files in all directories and subdirectories

To recursively list all files and directories in tree formart use -R with ls command. This would be in a hierarchical format.


$ ls -R
ls -R recursive directory listing - hierarchical format

The subdirectories will be indicated by colon (:) at the end of their names. And its contents will be shown just below it.

ls ** is an alternative to ls - R. But shows list files and directories recursively. The pattern ** is known as "globstar" or recursive globbing. This feature depends on shell and its versions.

For more detailed information and additional options, you can refer to man ls or type the same from the terminal.



Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment