Knowledgebase

Know About the ls Command in Linux With Examples

Ever wondered how to quickly check what files and folders are inside a directory in Linux?

That’s where the ls command comes in!

It’s one of the most fundamental and frequently used commands in Linux, making file management easy. Whether you are a beginner or an experienced user, knowing how to use ls efficiently can save you time and effort.

This guide will break down the ls command with simple examples and detailed explanations to help you master it.

What is the ls Command?

The ls command stands for ‘list’ and is used to display the contents of a directory. By default, running ls without options will list the files and directories in the current working directory.

Syntax:

ls [options] [directory]
  • options modify the behaviour of the command.
  • directory specifies the folder whose contents you want to list. If omitted, ls lists the contents of the current directory.


Also Read: Create Zip File With Command in Linux With Examples


Top Basic ls Command Examples

Example 1: Listing Files and Directories

ls

➔ This command lists all files and directories in the current directory.

Example 2: Listing Files in a Specific Directory

ls /home/user/Documents

➔ This command lists all files in the Documents directory inside the user folder.

Example 3: Listing Files with Detailed Information (-l)

ls -l

➔ The -l option displays detailed information, including:

  • File Permissions: Indicates read (r), write (w), and execute (x) permissions for the owner, group, and others.
  • Number of Links: Shows the number of hard links.
  • Owner and Group: Displays the user and group associated with the file.
  • File Size: Shows the file size in bytes.
  • Modification Date: Displays the last modification time.
  • Filename: The name of the file or directory.

Sample Output:

drwxr-xr-x  2 user user 4096 Sep 10 10:30 Documents
-rw-r–r–  1 user user 1024 Sep 10 10:00 file.txt

Explanation:

  • d at the beginning indicates a directory.
  • rwxr-xr-x represents permissions.
  • user user shows the owner and group.
  • 4096 is the file size (in bytes).
  • Sep 10 10:30 is the last modification date.
  • Documents is the file/directory name.

Example 4: Listing Hidden Files (-a)

ls -a

➔ This command lists all files, including hidden files (files that start with a dot .).

Sample Output:

.  ..  .bashrc  .profile  Documents  file.txt
  • . represents the current directory.
  • .. represents the parent directory.
  • .bashrc, .profile are hidden files.

Example 5: Listing Files in Human-Readable Format (-lh)

ls -lh

➔ The -h option makes file sizes more readable by displaying them in KB, MB, or GB instead of bytes.

Sample Output:

drwxr-xr-x  2 user user 4.0K Sep 10 10:30 Documents
-rw-r–r–  1 user user 1.0K Sep 10 10:00 file.txt


Also Read: A Guide to Echo Command in Linux With Examples


Example 6: Sorting Files by Modification Time (-lt)

ls -lt

➔ It will list files sorted by modification time, with the most recently modified files appearing first.

Example 7: Listing Files in Reverse Order (-r)

ls -lr

➔ This will list files in reverse order.

Example 8: Displaying File Type Indicators (-F)

ls -F

Adds symbols to indicate file types:

  • / for directories
  • * for executable files
  • @ for symbolic links

Example 9: Listing Only Directories (-d */)

ls -d */

➔ This command lists only directories.

Example 10: Listing Files Recursively (-R)

ls -R

➔ It Displays files and directories recursively, including subdirectories.


Also Read: How To Remove Directory in Linux With Command?


Example 11: Combining Multiple ls Options

➔ You can combine multiple options in a single command for more control.

ls -lah

This command:

  • -l shows detailed information.
  • -a includes hidden files.
  • -h makes file sizes human-readable.

Example 12: Using ls with grep to Filter Results

ls -l | grep “txt”

➔ It Lists only files containing “txt” in their name.

Example 13: Counting the Number of Files

ls -1 | wc -l

➔ This command counts the number of files in the directory.

Example 14: Sorting Files by Size (-lS)

ls -lS

➔ It Lists files sorted by size, the largest first.


Also Read: How to Install RPM files on Different Linux Distributions?


Conclusion

Mastering the ls command is a great first step in confidently navigating the Linux command line. Whether simply listing files, filtering results, or checking file details, ls provides powerful ways to manage your directories efficiently.

The more you experiment with different options, the more comfortable you’ll become with Linux file management.

Start practising today, and soon, using ls will become second nature!

Hidden-Costs-and-Fees
Buy Linux VPS That Don’t Affect Your Pocket

Affordable pricing with top-notch features for business