How to use the Linux ls command

How to use the Linux ls command

The ls command lists files and directories in Linux and Unix-like operating systems. It is an essential tool for navigating and managing files which lets you view, sort, and organize file information in your current working directory or other specified locations.

This command-line utility works well for beginners by providing simple outputs with just a few keystrokes. Meanwhile, advanced users benefit from its ability to deliver detailed insights by appending additional options.

In this article, we’ll explore the ls command’s syntax, common options, and practical examples. By the end of this guide, you’ll be able to use this command to simplify file management and navigate your Linux system efficiently.

Syntax of the ls command

The ls command’s basic syntax is:

ls [options] [file/directory]
  • [options] – modifies how the command behaves or formats the output. For example, -l displays a detailed list, while -a includes hidden files.
  • [file/directory] – specifies the path of a file or directory to list its contents. For instance, ls /home/user/Documents displays the contents of the Documents folder.

By default, running ls without arguments or options lists the files and directories in the currently active location in a compact format.

Commonly used ls command options

Like other Linux commands such as cp or mv, you can combine ls with various options to modify its behavior or format the output. Below are the most commonly used options:

OptionDescription
-lDisplays a detailed list of file attributes, including permissions, size, and modification time.
-aIncludes hidden files, which start with a dot (.).
-hFormats file sizes in a human-readable format, such as KB or MB.
-RLists the contents of directories recursively, including subdirectories.
-tSorts files by modification time, showing the most recently modified first.
-SSorts files by size, from largest to smallest.
-rReverses the sorting order of any other option.
-FAppends symbols to file names to indicate file types, such as / for directories or @ for symbolic links.
–colorAdds color to distinguish file types and permissions; it’s enabled by default on many systems.
-dLists only directory names without showing their contents.
-iDisplays each file’s inode number.

You can also combine options to produce more detailed or customized outputs. For example:

  • ls -lah – lists all files, including hidden ones, in a detailed view with human-readable sizes.
  • ls -lR – displays a detailed view of files and directories, including all subdirectories.
  • ls -ltSr – sorts files by size, showing the smallest files first, and displays them in a detailed view.

Examples of using the ls command

Before running ls commands, open a terminal window on your Linux machine. For local computers, use the built-in terminal software.

If you’re on a virtual private server (VPS), access the server remotely via SSH. Open your terminal and log in to the server with the following command, replacing your_vps_ip with your actual credentials:

ssh root@your_vps_ip

Hostinger VPS customers can find their SSH credentials in hPanel by navigating to VPS → Manage → Overview → SSH access.

Hostinger users can also use our Browser terminal feature to run commands directly from a browser tab, eliminating the need for a separate terminal application. Hit the corresponding button in the top-right corner of your VPS dashboard, and you’re good to go.

Listing files in the current directory

Start by running the ls command without any options. This basic usage is perfect for directory navigation when you only need to see the visible contents of the current folder:

ls

The output displays the names of files and directories in alphabetical order, excluding additional details like file size or modification time:

Listing files in a specific directory

Besides listing files in the current directory, the ls command lets you specify a location to view its contents. This eliminates the need to navigate to the folder first, which helps manage complex directory structures.

You can use an absolute path, which starts from the root (/) directory, like the following example:

ls /home/user/Documents

When executed, the command lists the contents of the Documents folder in /home/user, regardless of your current working directory.

Alternatively, a relative path specifies the location relative to your current directory. For instance:

ls ./Projects

This shows the contents of the Projects folder located in your current working directory.

Pro tip

Single dot (.) refers to the current directory. For example, ls ./ explicitly lists the current directory’s contents. Meanwhile, double dots (..) refer to the parent directory (one level up). For instance, ls ../ shows the contents of the parent folder.

Using ls for a detailed view of file attributes

The ls command provides more than just file names; you can use it to view detailed information about files and directories by adding the -l option:

ls -l

The long format includes the following file attributes:

  • File permissions – indicates “read,” “write,” and “execute” permissions for the file owner or group, shown in a format like -rw-r–r–.
  • Number of links – shows the number of hard links to the file or directory.
  • Owner – displays the username of the file’s owner.
  • Group – displays the group to which the file belongs.
  • File size – shows the file size in bytes.
  • Modification date – indicates the last time the file was modified.
  • File name – lists the name of the file or directory.

Here’s what the command might display:

This output shows that example.txt is owned by the root user, belongs to the developers group, is 2635 bytes in size, and was last modified on January 15 at 00:46.

Listing hidden files

To display hidden files in Linux, run the ls -a command. This reveals all files and directories, including hidden ones. It’s convenient when working with configuration files or troubleshooting hidden system files.

ls -a

When executed, the command lists all files and directories, including special entries like . (current directory) and .. (parent directory).

If you want to exclude these special entries while still viewing other hidden files, append the -A option instead:

ls -A

This makes the output cleaner, as shown below:

Sorting files by size or time

Sorting files helps you quickly identify the largest or most recently modified files. Use ls with the -S option to sort files by size, displaying the largest files first:

ls -S

Meanwhile, the -t option sorts files by modification time, showing the most recently modified files first:

ls -t

Pro tip

To display additional details like file permissions and size while sorting, combine these options with -l. For example, use ls -lS to sort by size or ls -lt to sort by modification time.

Combining options

The ls command becomes even more powerful when you combine multiple options. This approach saves time and provides more detailed or focused results. Here are some examples:

  • ls -lAh – lists all files, including hidden ones, in a long format with file sizes displayed in KB, MB, or GB:
  • ls -ltSr – displays a detailed view of files sorted by size (smallest to largest) and then by modification time:
  • ls -Rla – shows all files, including hidden ones, recursively through subdirectories, displaying the folder hierarchy in a detailed format:

Using ls in scripts for automation

Combining the ls command with shell scripting techniques lets you automate repetitive tasks like batch file processing, system monitoring, or generating reports.

Here’s a simple bash script to find and list files larger than 1 MB in a directory, sorted by size:

#!/bin/bash

# Directory to scan

directory="/path/to/directory"

# List files larger than 1 MB in long format, sorted by size

echo "Files larger than 1 MB in $directory:"

ls -lS "$directory" | awk '$5 > 1048576 {print $0}'
  • awk ‘$5 > 1048576 {print $0}’ – filters files larger than 1 MB (1 MB = 1048576 bytes) and prints their details.
  • directory=”/path/to/directory” – Specifies the directory to scan; in this example, it’s/path/to/directory.

Another example generates a text file report of all hidden files in a directory:

#!/bin/bash

# Directory to scan

directory="/path/to/directory"

# Output file

output_file="hidden_files_report.txt"

# Generate a report of hidden files

echo "Generating hidden files report for $directory..."

ls -la "$directory" | grep "^\." > "$output_file"

echo "Report saved to $output_file."
  • grep “^\.” – filters entries that start with a dot (.), indicating hidden files.
  • output_file= “hidden_files_report.txt” – saves the output to hidden_files_report.txt.

Suggested reading

Learn simple bash script examples to get started with automating file management tasks.

The ls command can also help distinguish symbolic links and various file types. Use the -F option to append symbols to file names, indicating their type:

  • / – directory.
  • @ – symbolic link.
  • * – executable file.
  • = – socket.
  • | – FIFO (named pipe).
ls -F

The output will look like this:

Sorting files by extension or name

Sorting files by extension or name is a practical way to organize and locate specific file types within a directory.

Use the –sort=extension option to group files by their extensions. This is handy for directories containing a mix of file formats, such as TXT or JPG. For example:

ls --sort=extension

By default, ls sorts files alphabetically by their names. To reverse the sorting, add the -r option:

ls -r

You can also combine –sort=extension with -r to list files by extension in reverse order:

ls --sort=extension -r

Using ls with wildcards

The ls command supports wildcards, so you can list files and directories that match specific naming patterns or file types. This helps you quickly locate the files you need in a directory.

Here are some common wildcards and their examples:

  • Asterisk (*) – matches zero or more characters in a file name.
ls *.txt
  • Question mark (?) – matches exactly one character in a file name.
ls file?.jpg
  • Square brackets ([ ]) – match any single character inside the brackets. You can also specify a range of characters.
ls file[1-3].txt

ls file[a-c].txt

You can combine wildcards for more complex patterns. For instance, to list all image files in a directory:

ls *.jpg *.png

Or, to list files with two-character names followed by any extension:

ls ??.*

Displaying inode numbers

Every file and directory in Linux has a unique identifier called an inode number, which stores metadata such as the file’s size, permissions, owner, and location on disk.

To view inode numbers alongside file names, execute the ls command with the -i option:

ls -i

You should see something like this:

Here, 12884901889 and 12884901890 are the inode numbers for file1.txt and file2.txt, respectively.

Conclusion

The ls command is an essential tool for showing files and folders in Linux. In this article, we’ve explored its syntax, commonly used options, and illustrative examples, such as sorting files, displaying hidden files, and identifying symbolic links or inode numbers.

You can also experiment with multiple options to create customized outputs, combine ls with wildcards to filter specific file types or use bash scripts to automate tasks.

By mastering these techniques, you can unlock the ls command’s full potential to handle file management tasks in your Linux system. If you have any questions about the ls command, feel free to ask in the comment section below!

ls command FAQ

What does the ls command do in Linux?

The ls command lists files and directories in the current working directory or a specified location. By adding options, it can also sort or filter contents and display detailed file attributes, such as permissions, size, and modification dates.

How do I list hidden files with ls?

To list hidden files in Linux, use the ls -a command. It displays all files, including hidden ones or those starting with a dot (.). For a cleaner view, that excludes . (current directory) and .. (parent directory) entries, use the -A option instead.

Can I list files in reverse order using ls?

Yes, you can list files in reverse order with the -r option. Combine it with other options, like -t to reverse time-based sorting or –sort=extension to reverse extension-based sorting, for more tailored results.

Author
The author

Ariffud Muhammad

Ariffud is a Technical Content Writer with an educational background in Informatics. He has extensive expertise in Linux and VPS, authoring over 200 articles on server management and web development. Follow him on LinkedIn.