20 Basic ‘ls’ Command Examples in Linux

Here are 20 essential examples of the ls command in Linux, ranging from basic to advanced usage.

Alby Andersen

1. List Files in Current Directory

ls

Lists all visible files and directories in the current folder.


2. Detailed List (Long Format)

ls -l

Shows permissions, owner, size, and modification time in columns.


3. Show Hidden Files

ls -a

Includes hidden files (those starting with ., like .bashrc).


4. Combine -l and -a

ls -la

Detailed list including hidden files.


5. Human-Readable File Sizes

ls -lh

Displays sizes in KB, MB, or GB (e.g., 4.0K instead of 4096).


6. Sort by Modification Time

ls -lt

Newest files appear first.


7. Reverse Sort Order

ls -lr

Reverse the default sorting (e.g., alphabetical Z → A).


8. List Files in a Specific Directory

ls /var/log

List contents of /var/log instead of the current directory.


9. Recursive Listing

ls -R

Lists all files and subdirectories recursively.


10. Filter by File Extension

ls *.txt

Lists only files ending with .txt.


11. Display Inode Numbers

ls -i

Shows the inode number (unique identifier) for each file.


12. Append File Type Indicators

ls -F

Adds symbols like / for directories and * for executables.


13. Sort by File Size

ls -lS

Largest files appear first.


14. Show Directory Itself (Not Contents)

ls -d */

Lists only directories in the current location.


15. Comma-Separated Output

ls -m

Displays files as a comma-separated list: file1, file2, dir1.


16. Show Numeric UID/GID

ls -n

Displays user/group IDs instead of names (useful for scripting).


17. One Entry Per Line

ls -1

Forces single-column output (helpful for parsing).


18. Colorized Output

ls --color=auto

Highlights directories, executables, and symlinks in color.


19. Sort by Access Time

ls -lu

Sorts by last access time instead of modification time.


20. Get Help

ls --help

Displays all available ls options and flags.


Pro Tip: Combine Flags

ls -lath
  • -l: Long format
  • -a: Show hidden files
  • -t: Sort by time
  • -h: Human-readable sizes

This shows all files in a detailed, time-sorted list with readable sizes.


Master these to navigate and manage files efficiently in Linux! 🐧

Share This Article