28 January 2021
4 mins read

Linux LS Command – List Files in a Directory + Options + Output

Linux is a free, open-source Unix-like operating system based on the main component Linux kernel. Linux command line gives a fast and efficient user experience.

The ls command is a basic command used in Linux operating system. It allows users to list files and directories in the filesystem. The ls tool is packaged under the GNU core utilities (coreutils package).

In this tutorial, we learn about ls command in Linux and some of its important options with practical examples

What is ls in Linux

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. Commonly used to list directory contents.

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 file by date and time, list files in reverse, and list files UID and GID.

By default ls command list files in alphabetical order.

How to use ls command

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

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

Syntax:

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

ls command options

The main ls command options are:

OptionsDescription
ls -aDisplay all files including hidden files
ls -lList long format mostly used to display file permissions
ls -lhList with columns in human-readable file format
ls -lsList columns with readable file size
ls -rSort contents in the reverse order
ls -tSort by time, newest at top
ls -RList Subdirectories Recursively
ls -FAppend indicator – Adding “/” at the end of each directory
ls -mAfter each directory and file, a comma is added
ls -QAll directories and files list with quotation marks
ls -iList directories and files with inode number
ls -XDisplay alphabetically by file extension

Let’s check some of the basic ls commands usage with examples.

List Files in a Directory

The ls command is most frequently used to list files in a directory. This could be current, specific, root, parent directory, or home directory

List files and directories in the current working directory

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

List files and directories from a specific directory

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

List content in the parent directory

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

You may also ls ../.. to list files two levels above.

List files in the user’s home directory

List files in long format

The ls -l command list the content of the directory in table format with 9 columns.

ls -l /path/to/directoryList in long format the directory contents
ls -l /path/to/fileList in long format the file information

ls command columns names:

1. File type

2. Content permissions

3. Number of hard links to the content

4. Owner of the file or directory

5. Group owner of the file or directory

6. Content size in bytes

7. Last modified date and time of the content

8. The actual file or directory name

ls command doesn’t display the column name headers, but you can use modern replacement for ls – exa.

Let’s describe the output of ls -l command.

First column: Contains 10 characters. The first character denotes the file type and the remaining 9 character represents the file permissions.

File type character would be any of the following:

  • - – Regular file
  • b – Block special file
  • c – Character special file
  • d – Directory
  • l – Symbolic link
  • n – Network file
  • p – FIFO
  • s – Socket

After the file type, the content permission is represented in 9 characters. The first 3 character shows the read(r)/write(w)/execute(x) permission of the owner, the next 3 characters for the group, and the last 3 characters for others. There are two flags that represent setgid/setguid and sticky bit.

Second column: This shows the number of hard links the content file or directory has.

Third column: The owner of the file or directory.

Fourth column: Group owner of file or directory.

Fifth column: Content size, by default in bytes

Sixth column: The last modification date and time of the content

Last column: The name of the file or directory.

To modify file permissions use chmod command and to change ownership of files and directories use chown command.

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:

You can use du command to estimate the disk usage of files and directories in the Linux file system.

List just directories

To list only directories using ls command, type ls -d */ command.

List only files

To list files only using ls command, type ls -l | egrep -v '^d' command

List files with their sizes

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

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.

For example ls -a or ls -al command:

Ls command Sort

The output of ls command can be sorted using the –sort option.

Type ls -X or --sort=extension to sort alphabetically by extension:

Type ls -S or --sort=size to sort by file size:

Type ls -t or --sort=time to sort by last modification time (top will have recently modified):

Type ls -ltr means to list files and directories with the last modification time in the reverse order sorted.

List files and output to text file

Type ls > results.txt to redirect the output of ls command to a text file.

You can use cat results.txt command to display the contents of the file.

List UID and GID of Files

To list files and directories along with their UID and GID, type ls -n command.

List files in all directories and subdirectories

Type ls - R command to list files in all directories and subdirectories recursively:

The list may be large so you can use ls -lR | more to display one screen at a time and allows to scroll up and down through the page.

Conclusion

In this tutorial, we learned about ls command and its use in Linux. In case you forget any command you can type ls –help or man ls to display ls command manual page.

Did you find this article helpful?

We are glad you liked the article. Share with your friends.

Sorry about that.

How can we improve it?

Bobbin Zachariah

Bobbin Zachariah

Bobbin Zachariah is the editor-in-chief of Linoxide and has an experienced team of Linux enthusiastic authors who makes this blog awesome. Linoxide is one of the top 20 Linux Blog by whizlabs.

Leave a Reply

Your email address will not be published.

Previous Story

How to Upgrade a Single Package in Ubuntu

Next Story

Linspeed – Gui Tool to Test Internet Speed on Linux Desktop

Latest from Blog

Top 8 Reasons to Use Garuda Linux

Have you been going back and forth between multiple Linux flavors in search of an exciting experience? Or perhaps you are coming from a Windows or MAC environment and want to try

How to Rename Multiple Files in Linux

In a Linux system, you can easily rename a file using mv command. But, if you have multiple files which you want to rename, in this situation you need some extra tools

How to Install TensorFlow on Ubuntu 20.04

Tensorflow is an open-source platform for machine learning and artificial intelligence. It is developed by the Google Brain team. It contains tools, libraries, and community resources for developers to build ML powered
Go toTop