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.
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.
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.
The output contains Linux file permissions, link count, owner and group information, file size, last modification time, etc.
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
4. Classify Directory Contents
-F option of ls allows you to classify directory contents based on their type. It appends one of the characters from the set
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
--size option. Note that the size information is displayed in blocks, the same as Linux du commands.
6. Display Human Readable Filesizes
ls and the
ls -s command prints the filesize in blocks. Add the
-h option to display this information in a human-readable format.
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.
8. Sort Output by Size
-S option of ls for sorting the output by their respective sizes.
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.
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.
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
12. Display Subdirectory Contents
ls doesn’t display contents that are stored inside subdirectories. However, you can use the
--recursive option to override this. The below example showcases this.
13. Display Directories Only
You can use the
-d option followed by a bash pattern for viewing all sub-directories inside a folder.
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.
15. Display Manual Page
You can consult the man page to find out detailed instructions on all ls options and how to use them.
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