Now that we know how to move around the filesystem, how do we figure out what is available to us? The ls command lists files and directories so you can inspect your current location or another path.
Command Line · Lesson 4
ls (List Directories)
Learn how to use ls options to inspect files, hidden entries, details, sizes, and sort order.
Basic Usage of the ls Command
By default, the ls command will list the directories and files in your current directory. However, you can also specify a path to list the contents of a different directory.
$ ls
$ ls /home/pete
You can list a specific file too:
$ ls /etc/hosts
/etc/hosts
Which command lists the contents of /home/pete without changing into it?
Viewing Hidden Files
Not all files in a directory are visible by default. In Linux, filenames that start with a dot (.) are hidden. You can view them with the -a option, which stands for all.
$ ls -a
. .. .bashrc Documents Pictures
Dotfiles are hidden by default and often store configuration, such as .bashrc.
Which command includes hidden files in the listing?
Getting Detailed Information
Another essential ls option is -l for long format. It shows file permissions, number of links, owner, group, size, modification time, and name.
$ ls -l
Here is an example of the output:
pete@icebox:~$ ls -l
total 80
drwxr-x--- 7 pete penguingroup 4096 Nov 20 16:37 Desktop
drwxr-x--- 2 pete penguingroup 4096 Oct 19 10:46 Documents
drwxr-x--- 4 pete penguingroup 4096 Nov 20 09:30 Downloads
drwxr-x--- 2 pete penguingroup 4096 Oct 7 13:13 Music
drwxr-x--- 2 pete penguingroup 4096 Sep 21 14:02 Pictures
drwxr-x--- 2 pete penguingroup 4096 Jul 27 12:41 Public
drwxr-x--- 2 pete penguingroup 4096 Jul 27 12:41 Templates
drwxr-x--- 2 pete penguingroup 4096 Jul 27 12:41 Videos
For easier file sizes, add -h for human-readable output:
$ ls -lh
Which command shows long-format details with human-readable sizes?
Sorting in Reverse Order
Sometimes you may want to change the sort order. The -r option lists files and directories in reverse order.
$ ls -r
You can sort by modification time with -t, then reverse it with -r:
$ ls -lt
$ ls -ltr
Which command sorts by modification time and then places the newest entries last?
Combining Command Flags
Commands have flags, also called options, to add more functionality. As we saw with -a and -l, you can combine them into a single command like ls -la. The order of the flags often does not matter, so ls -al works the same way.
$ ls -la
Useful combinations include:
$ ls -lh
$ ls -la
$ ls -ltr
Common ls Options
-a: Show all files, including hidden files.-l: Use long format.-h: Show human-readable sizes with-l.-r: Reverse the sort order.-t: Sort by modification time.-S: Sort by file size.-d: List the directory itself instead of its contents.
Which command lists the projects/ directory entry instead of its contents?
Some systems display ls output in different colors for different file types. This behavior commonly comes from an alias or environment setting, so colors may vary between systems.
To reinforce your understanding of the ls command, try this hands-on lab:
- Linux ls Command: Content Listing - Practice using the
lscommand to efficiently list and analyze file and directory contents. You'll learn various options for detailed listings, hidden file display, human-readable sizes, and sorting techniques to enhance your command-line skills.
This lab will help you apply the concepts in a real scenario and build confidence with directory listing in Linux.
Lesson complete
You finished ls (List Directories)
You can now use ls to inspect directory contents and control how entries are displayed.
List the current directory or another path.
Include hidden files in a listing.
Show detailed information with readable sizes.
Sort entries by modification time in reverse order.
List a directory entry without listing its contents.
Keep your learning progress
Create a free account to save this lesson and continue learning on any device.
Create a free account