8 Powerful ‘ls’ Tricks Every Linux User Should Know
This guide walks through eight advanced usages of the Linux ls command—including recursive listings, pattern filtering, directory‑only views, time‑based sorting, size‑based sorting, counting files and directories, and generating absolute paths—complete with command examples and option explanations.
The ls command is a staple for Linux users, but beyond the basic ls -l there are many powerful options that can streamline file management tasks.
Usage 1: List detailed information recursively
Command: ls -lR /home/alvin/test_dir/ The -l flag produces a long listing, while -R makes the command recurse into all sub‑directories, showing every file and folder under the specified path.
Usage 2: List files that start with a specific prefix
Command: ls -l atb* This lists detailed information for all files whose names begin with atb in the current directory.
Usage 3: Show only sub‑directories
Method 1 : ls -F /home/alvin/test_dir | grep /$ Method 2 : ls -p /home/alvin/test_dir | grep /$ Method 3 : ls -l /home/alvin/test_dir | grep "^d" Method 4 : ls -d */ The -F and -p options append a character to each name to indicate its type (e.g., / for directories). The regular expression / at the end of the line ( /\$) filters for those entries. -d treats directories as plain files, preventing their contents from being listed.
Usage 4: List files by modification time (oldest last)
Command:
ls -ltr -tsorts by modification time (newest first) and -r reverses the order, so the oldest entries appear at the bottom.
Usage 5: Sort files by size
Command:
ls -lhS -Ssorts by size (largest first). Adding -h makes sizes human‑readable (e.g., K, M). Use -r together with -S to list smallest files first.
Usage 6: Count files and directories
Count files : ls -l | grep "^-" | wc -l The pattern ^- matches regular files; the pipeline counts them.
Count directories : ls -l | grep "^d" | wc -l The pattern ^d matches directory entries; the pipeline counts them.
Usage 7: List absolute paths of all files (non‑recursive)
Command: ls | sed "s:^:`pwd`/:" The sed expression prefixes each filename with the current working directory ( `pwd`), producing full paths.
Usage 8: List absolute paths of all files including hidden ones, without recursion
Command:
find $PWD -maxdepth 1 | xargs ls -ld find $PWD -maxdepth 1lists every entry in the current directory (including hidden files). Passing the result to ls -ld via xargs prints a detailed listing with absolute paths.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
