How to List Files and Folders in Linux

Another most commonly-used commands in Linux. Use this command to list information of files and directories such as the file permissions, owners, and timestamp to name a few. By default, it will list files current directory when it’s used without passing any arguments.

General syntax for ls command:

$ ls [OPTION...] [FILE...]
Display hidden files
ls -a

This option will display the entire list of directories, including hidden files. Hidden files start with a dot (.)

Example: Let’s display all hidden files of the ExampleDir directory.

list hidden files
Sorting by file size
ls -S

Use the ls -S command to sort files and directories by size in descending order, and then print them to the terminal.

Example: Sorting all the files and directories of the NewDir directory.

sort by filesize
Display files in long format
ls -l

The -l option displays the contents of the directory in a more detailed way. Displaying the file’s owner and group, last modified time, and more.

Example: Displaying the detailed information about the files in the NewDir directory.

list files in long format
Sort by date and time
ls -t

This command sorts files by last modified time. The most recently edited files will appear at the top of the output, making them easy to find.

Example: Let’s display and sort by time the files from the ExampleDir directory.

list by date and time
Display only directories
ls -d */

Use this command to list the sub-directories excluding all other files.

Example: Listing the directories of the NewFolder directory.

list only dir
List files and save results to a file
ls > [filename]

The ls > [filename] command allows you to save the output of the preceding command to a file.

Example: Saving the output of the ls -d command to the file result.txt.

list and save results to file
List file owners with their ID
ls -n

This option displays the owner and group as UID and GID.

Example: Let’s display the UID and GID of the files in the NewDir directory.

list file owners and ID