How to List Files and Folders in Linux

Understand the power of the 'ls' command for efficient file management.

The ls Linux command is used to list files and directories in the current directory. When you type ls and press Enter in the terminal, it will display all the files and folders in the directory you are currently in. It’s a fundamental command that helps users navigate and manage their file system by providing an overview of the contents.

You can also use various options with the ls command to view additional details, such as file permissions, ownership, file size, and modification dates. In this post, we explore all possible uses of the ls command.

General syntax for ls command:

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

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

Example:

.  ..  .config  .bashrc  file1.txt  folder1  .hiddenfolder  picture.jpg

In this example, the single dot (.) represents the current directory, and the double dot (..) represents the parent directory. The .config and .bashrc files, along with the .hiddenfolder, are hidden files and directories that would not be shown with the plain ls command but are displayed here because of the -a option. The other items are regular, non-hidden files and directories.

2. 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:

file_large.mp4
image1.jpg
document.pdf
textfile.txt
folder1/
folder2/

In this example, file_large.mp4 is the largest file, and textfile.txt is the smallest file. The directories folder1/ and folder2/ are also listed, but the -S option doesn’t consider their sizes in the sorting. If you want to see the sizes along with the files, you can combine the -S option with the -l option, like ls -lS.

3. 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:

total 48
drwxr-xr-x 5 user user 4096 Aug  6 10:30 Documents
-rw-r--r-- 1 user user  123 Aug  6 10:20 file.txt
drwxr-xr-x 2 user user 4096 Aug  6 10:15 Music
-rwxr-xr-x 1 user user 2048 Aug  6 10:10 script.sh
drwxr-xr-x 3 user user 4096 Aug  6 10:05 Pictures

Here’s a breakdown of what each part means:

  • drwxr-xr-x: The file permissions (e.g., d indicates a directory, rwx means read, write, and execute permissions for the owner).
  • 5: The number of hard links to the file or directory.
  • user: The owner of the file or directory (listed twice, once for the owner and once for the group).
  • 4096: The file size in bytes.
  • Aug 6 10:30: The date and time the file or directory was last modified.
  • Documents: The name of the file or directory.
4. 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:

report.txt  image.png  project/  notes.docx  old_data.csv

In this example, report.txt is the most recently modified file, while old_data.csv is the oldest. If you were to run the command in a different directory or at a different time, the output would vary based on the files and their modification times in that specific directory.

5. Display only directories
ls -d */

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

Example:

Here’s a sample output for the command, assuming you have three directories named Documents, Pictures, and Music in the current directory:

Documents/  Pictures/  Music/

Each directory name is followed by a slash (/), indicating that it is a directory.

6. 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:

The command ls > filename.txt doesn’t display any output in the terminal. Instead, it redirects the output of the ls command to a file named filename.txt.

If you were to run the ls command in a directory containing files and folders, the names of those files and folders would be written to filename.txt. You wouldn’t see anything in the terminal itself.

Here’s an example of what might be inside filename.txt if the directory contained three files and one folder:

file1.txt
file2.jpg
folder1
file3.pdf

The exact contents would depend on the files and directories present in the current directory where the command was run.

7. List file owners with their ID
ls -n

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

Example:

drwxr-xr-x 2 1001 1001 4096 Apr  1 12:34 directory1
-rw-r--r-- 1 1001 1001    0 Apr  1 12:34 file1.txt
-rwxr-xr-x 1 1002 1002  123 Apr  1 12:34 script.sh

In this example, the first column shows the file permissions, the second column shows the number of hard links, the third and fourth columns show the user and group IDs, the fifth column shows the file size in bytes, and the sixth and seventh columns show the date and time of the last modification. The last column shows the name of the file or directory.

More Linux commands:
Directory Operations rmdir · cd · pwd · exa · ls
File Operations cat · cp · dd · less · touch · ln · rename · more · head
File System Operations chown · mkfs · locate
Networking ping · curl · wget · iptables · mtr
Search and Text Processing find · grep · sed · whatis · ripgrep · fd · tldr
System Information and Management env · history · top · who · htop · glances · lsof
User and Session Management screen · su · sudo · open
WebsiteFacebookTwitterInstagramPinterestLinkedInGoogle+YoutubeRedditDribbbleBehanceGithubCodePenWhatsappEmail