Become a Pro with the ls Command in Linux: Tips and Tricks :B5:

Khushal MalikKhushal Malik
5 min read

The ls command in Linux is a powerful tool used to list files and directories. It comes with a plethora of options that allow you to customize and refine the output to suit your needs. In this blog, we'll explore the various options available with the ls command and provide practical examples to help you become proficient in using this essential command.

The Basics of ls

At its core, the ls command is straightforward:

ls

This command lists the files and directories in the current directory. However, to unlock its full potential, you need to understand the various options available.

ls Options Explained

Here is a description of each ls option in one line:

  1. -a: Includes all files, including hidden files (those starting with a dot .).

  2. -A: Includes all files except . (current directory) and .. (parent directory).

  3. -c: Sorts by and shows the change time (time of last modification of file status information).

  4. -d: Lists directories themselves, not their contents.

  5. -h: With -l, prints sizes in human-readable format (e.g., KB, MB).

  6. -H: Follows symbolic links listed on the command line.

  7. -l: Uses a long listing format to display detailed information about files and directories.

  8. -o: Similar to -l but omits the group information.

  9. -r: Reverses the order of the sort.

  10. -s: Prints the allocated size of each file, in blocks.

  11. -S: Sorts files by size, largest first.

  12. --sort=WORD: Sorts files by the specified criteria (e.g., name, size, time, version).

  13. -t: Sorts files by modification time, with the newest files first.

  14. -u: Sorts by and shows access time.

  15. -v: Sorts files by version name (natural sort of numbers within text).

  16. -1: Lists one file per line.

Listing Files in a Long Listing Format

Using the ls -l command provides detailed information about each file and directory. Here's an example and a breakdown of the output:

Command:

ls -l

Example Output:

total 28
drwxr-xr-x  2 user user 4096 Jun 24 10:00 directory1
drwxr-xr-x  3 user user 4096 Jun 23 09:30 directory2
-rw-r--r--  1 user user 1024 Jun 22 08:15 file1.txt
-rw-r--r--  1 user user 2048 Jun 21 07:00 file2.txt
-rwxr-xr-x  1 user user 3072 Jun 20 06:45 script.sh

Detailed Breakdown of the Example Output:

  • drwxr-xr-x 2 user user 4096 Jun 24 10:00 directory1:

    • File Type: This is a directory (d at the beginning).

    • Permissions: It has read, write, and execute permissions for the owner (rwx), and read and execute permissions for the group and others (r-x).

    • Links: It has 2 hard links.

    • Owner: Owned by user.

    • Group: Group user.

    • Size: Size is 4096 bytes (typical for directories).

    • Modification Date: Last modified on June 24 at 10:00.

    • Name: Named directory1.

  • -rw-r--r-- 1 user user 1024 Jun 22 08:15 file1.txt:

    • File Type: This is a regular file (- at the beginning).

    • Permissions: It has read and write permissions for the owner (rw-), and read permissions for the group and others (r--).

    • Links: It has 1 hard link.

    • Owner: Owned by user.

    • Group: Group user.

    • Size: Size is 1024 bytes.

    • Modification Date: Last modified on June 22 at 08:15.

    • Name: Named file1.txt.

Using Human-Readable Format:

To make the file sizes more readable, you can use the -h option:

ls -lh

Example Output with -lh:

total 28K
drwxr-xr-x  2 user user 4.0K Jun 24 10:00 directory1
drwxr-xr-x  3 user user 4.0K Jun 23 09:30 directory2
-rw-r--r--  1 user user 1.0K Jun 22 08:15 file1.txt
-rw-r--r--  1 user user 2.0K Jun 21 07:00 file2.txt
-rwxr-xr-x  1 user user 3.0K Jun 20 06:45 script.sh

In this output, file sizes are displayed in a human-readable format (e.g., 4.0K, 1.0K).

Listing the Ten Most Recently Modified Files

To list the ten most recently modified files, use:

ls -lt | head

Listing All Files Including Dotfiles

To list all files including hidden ones (dotfiles), you can use:

ls -a

or

ls -A

Listing Files in a Tree-Like Format

For a tree-like format, you can use the tree command. If you don't have tree installed, you can install it using your package manager (sudo apt install tree for Debian-based systems). To list files in a tree-like format:

tree -L 1 -d /tmp

Example Output:

/tmp
  ├── directory1
  └── directory2

Listing Files Sorted by Size

To list files sorted by size:

ls -l -S

For reversed order:

ls -l -S -r

Listing Files Without Using ls

If you need to list files without using the ls command, you can use:

printf "%s\n" *

To list only directories:

printf "%s\n" */

To list files with specific extensions:

printf "%s\n" *.{gif,jpg,png}

Conclusion

The ls command is a versatile and essential tool for navigating and managing files and directories in Linux. By mastering the various options and combinations, you can customise your file listings to meet your specific needs. Whether you're sorting by size, date, or simply listing hidden files, the ls command has you covered. Happy Reading!

Note:-B5 is blog post 5 of this series

B1 link :- khushalmalik.hashnode.dev/the-rise-of-linux

Make sure you subscribe to Newsletter and Do follow !!!!!!!

'ls' is simple and frequently used; customize it with the options above.

#Khushal_Malik #Devops #Cloud #lsCommand #LinuxCommands #FileListing #DirectoryListing #LinuxTerminal #CommandLine #LinuxTips #LinuxTricks #FileManagement #LinuxBasics #HowToUseLsCommandInLinux #LsCommandOptionsExplained #ListingFilesInLinuxTerminal #LinuxLsCommandExamples #UnderstandingLsCommandOutput #CustomizingLsCommandOutput #ListingHiddenFilesInLinux #SortingFilesWithLsCommand #LsCommandForBeginners #AdvancedLsCommandUsageInLinux

0
Subscribe to my newsletter

Read articles from Khushal Malik directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Khushal Malik
Khushal Malik

I am an Devops Cloud Eng. with 2 years of experience