15 Linux LS Commands You Need to Know

Linux Ls Command Featured

You can use Linux ls commands to print out directory contents. It’s one of the most basic terminal commands in Linux. Thus, a thorough understanding of it is essential for navigating your way around the terminal. Listed below are some useful examples of using the ls utility. Bookmark this as a reference point for the future.

Also read: 17 Fun Linux Commands to Run in the Terminal

1. Display Directory Contents

By default, the ls command displays a list of files and directories present in the current directory. You can also specify the directories using their relative or absolute path.

ls
ls Documents
ls ../

2. Display Additional Information

You can display any additional information about a folder’s contents using the long listing format. Simply add the -l option to your standard ls to enable long format output.

ls -l ~/Documents

The output contains Linux file permissions, link count, owner and group information, file size, last modification time, etc.

Ls Command Long Output

3. Display Hidden Files

Hidden files in Linux start with a dot “.” symbol. The default command does not display these hidden files. However, you can easily view them by using the -a or --all option.

ls -a
ls --all

4. Classify Directory Contents

The -F option of ls allows you to classify directory contents based on their type. It appends one of the characters from the set */=>@|.

ls -F

Directories are represented by /, executables by *, symbolic links by @, and so on.

5. Display Filesizes

If you want to view the filesizes only, use the -s or --size option. Note that the size information is displayed in blocks, the same as Linux du commands.

ls -s
Ls Command Displaying Size

6. Display Human Readable Filesizes

Both standard ls and the ls -s command prints the filesize in blocks. Add the -h option to display this information in a human-readable format.

ls -sh
ls -lh

7. Sort Output by Modification Time

You can use the -t option of ls to sort directory contents based on the latest modification time. Add the -l flag to retrieve more information.

ls -t
ls -lt

8. Sort Output by Size

Use the -S option of ls for sorting the output by their respective sizes.

ls -S

9. Display Files Using Patterns

You can use bash wildcards with ls commands for displaying files based on a pattern. For example, the following command displays only mp3 files.

ls *.mp3
Ls Command Using Patterns

10. Hide Files Using Patterns

We can also hide files or directories based on a predefined pattern. The following examples display all files except for mp3 tracks.

ls -I .mp3 Music/ 
ls --hide=.mp3 Music/
ls --ignore=*.mp3 Music/

11. Display UID and GID

Linux systems use UID (User Identifier) and GID (Group Identifier) for identifying users and groups. You can display this information for all your files using the options -n or --numeric-uid-gid.

ls -n
ls --numeric-uid-gid

12. Display Subdirectory Contents

By default, ls doesn’t display contents that are stored inside subdirectories. However, you can use the -R or --recursive option to override this. The below example showcases this.

ls -R
ls --recursive

13. Display Directories Only

You can use the -d option followed by a bash pattern for viewing all sub-directories inside a folder.

ls -d */
ls --directory */
Ls Command Displaying Directories

14. Display Help Page

The help page of ls contains summarized information on all available options. Use this whenever you need to find out a specific option.

ls --help

15. Display Manual Page

You can consult the man page to find out detailed instructions on all ls options and how to use them.

man ls

Wrapping Up

The ls utility is one of the most simple yet versatile navigation tools in Linux. You can’t really master the Linux command line unless you’re comfortable with ls commands. Luckily, it’s easy to pick up this command once you understand some basic examples like the ones shown in this guide. Check out other ways to list the content of a directory in a Linux terminal.

Image credit: Investigate and analyze by DepositPhotos

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Rubaiat Hossain

Rubaiat is a CS graduate who possesses hands-on experience with Unix Administration, Web Programming, DevOps, and Virtualization. He has a strong passion for enlightening people in open-source technologies.