DEV Community

AJ
AJ

Posted on

Top 17 ls command options

We are going to learn about Linux command called ls. It is regular usage command in any distro of Linux to list out the directories and files in the current directory.

  • To list out the directories and files in the working directory simply use the command
ls
Enter fullscreen mode Exit fullscreen mode

1) Classification of Files

ls -F
Enter fullscreen mode Exit fullscreen mode
  • Here we have to note the indicator patterns at the end of the directories and files to determine the type.
    • Regular file: No indicator
    • Directory: /
    • Executable file: *
    • Symbolic link: @
    • Unix domain socket: =
    • Named pipe (FIFO): |
    • Socket: =

2) Long Listing of Files

ls -l
Enter fullscreen mode Exit fullscreen mode
  • Displays detailed information about each file, including permissions, number of links, owner, group, size, and time of last modification.
  • It's a daily driver command, alias could be
alias ll="ls -l"
Enter fullscreen mode Exit fullscreen mode

3) List the files with human readable

ls -lh
Enter fullscreen mode Exit fullscreen mode
  • When used with -l, this option shows file sizes in human-readable format (e.g., KB, MB) instead of bytes, mostly for the files present in the current directory.

4) List all Files

ls -a
Enter fullscreen mode Exit fullscreen mode
  • List all the files and directories including hidden ones those starting with .

5) Recursive

ls -R
Enter fullscreen mode Exit fullscreen mode
  • Lists all files in the current directory and all sub-directories recursively.

6) Sort by time

ls -lt
Enter fullscreen mode Exit fullscreen mode
  • Sorts the files by the time of last modification, with the most recently modified files appearing first.

7) Sort by Size

ls -lS
Enter fullscreen mode Exit fullscreen mode
  • Sorts files by size, with the largest files appearing first.

8) One file per line

ls -1
Enter fullscreen mode Exit fullscreen mode
  • Lists one file per line. Mostly useful when there are many files in the current Directory.

9) Directories only

ls -d */
Enter fullscreen mode Exit fullscreen mode
  • Lists directories themselves, not their contents.

10) Inode Number

ls -li
Enter fullscreen mode Exit fullscreen mode
  • Displays the inode number of each file, which is a unique identifier for the file on the filesystem.

11) Sort by Extension

ls -lX
Enter fullscreen mode Exit fullscreen mode
  • Sort the files or directories alphabetically.

12) Natural Sort by order

ls -lv
Enter fullscreen mode Exit fullscreen mode
  • Sorts files by natural version number order, which is useful when filenames contain numbers (e.g., file1, file2, file10).

13) Group the Directories

ls --group-directories-first
Enter fullscreen mode Exit fullscreen mode
  • Lists directories first, then files, rather than mixing them together.

14) Color the output

ls --color=auto
Enter fullscreen mode Exit fullscreen mode
  • Forces the output to be colorized or not (never, always, or auto).

15) Time style on long listing

ls -l --time-style=long-iso
Enter fullscreen mode Exit fullscreen mode
  • Customizes the format of the time stamps. Common styles include full-iso, long-iso, and iso.

16) Access Time Sorting

ls -lu
Enter fullscreen mode Exit fullscreen mode
  • Sorts files by their last access time instead of modification time.

17) Ignore pattern

ls --ignore-pattern=*.tmp
Enter fullscreen mode Exit fullscreen mode
  • Excludes files matching the specified pattern (e.g., *.tmp) from the output, very useful if there is lot of files with same extension

Mastering the ls command enhances your efficiency by customizing directory listings. I hope you found this content helpful and learned something new.

Top comments (0)