Linux ls Command Explained

By Asahluma Tyika

When working with Linux, you will often need to see what files and folders are inside a directory. Instead of opening a file manager, you can use the terminal to inspect your files quickly.

One of the most useful commands for this is ls.

The ls command lists files and directories in your current location. With a few additional options, you can also view hidden files, check file permissions, display file sizes in a readable format, and sort files by modification time.

In this guide, we will explore the most useful ls options and learn how to combine them.

What Is the ls Command?

The ls command is short for “list.” It is used to display the contents of a directory.

For example:

ls

A possible result might look like this:

Documents  Downloads  Music  Pictures  Projects

The output shows the files and directories inside your current working directory.

You can check your current location using:

pwd

For example:

/home/user

Running ls in this directory displays the contents of /home/user.

1. Display Detailed Information with -l

By default, ls displays names only. If you want more information about each file, use the -l option.

ls -l

Example output:

drwxr-xr-x 2 user user 4096 Sep 10 10:21 Documents
-rw-r--r-- 1 user user  220 Sep  9 16:03 notes.txt
-rwxr-xr-x 1 user user 1024 Sep  8 12:40 script.sh

The long format displays information such as:

  • File type: Whether the item is a file or directory.
  • Permissions: Who can read, write, or execute the file.
  • Number of links: The number of hard links associated with the file.
  • Owner: The user who owns the file.
  • Group: The group associated with the file.
  • Size: The file size in bytes.
  • Modification time: When the file was last modified.
  • Name: The name of the file or directory.

For example, the first character in this entry is d:

drwxr-xr-x

This indicates that the item is a directory.

A regular file usually begins with -:

-rw-r--r--

The -l option is useful when you need more than just the names of files.

2. Show Hidden Files with -a

Linux allows files and directories to be hidden by beginning their names with a dot (.).

Examples include:

.bashrc
.config
.gitignore

These files are not displayed by a normal ls command.

To show hidden files, use:

ls -a

Example output:

.  ..  .bashrc  .config  Documents  Downloads

The special entries have specific meanings:

  • . refers to the current directory.
  • .. refers to the parent directory.

The -a option displays almost everything, including these special directory entries.

What About ls -A?

There is another option:

ls -A

This displays hidden files but excludes the special . and .. entries.

This can make the output easier to read when you only want to see actual files and directories.

3. Display Human-Readable File Sizes with -h

When using ls -l, file sizes are normally displayed in bytes.

For example:

-rw-r--r-- 1 user user 1048576 Sep 10 10:21 video.mp4

A size of 1048576 bytes is not immediately easy to read.

The -h option displays file sizes in a more understandable format, such as KB, MB, or GB.

ls -lh

Example output:

-rw-r--r-- 1 user user 1.0M Sep 10 10:21 video.mp4
-rw-r--r-- 1 user user 2.5K Sep  9 16:03 notes.txt

The -h option is most useful when combined with -l.

Important: The -h option is intended to make sizes easier to read. It does not display additional file information by itself.

4. Sort Files by Modification Time with -t

Sometimes you want to find the files that were modified most recently.

The -t option sorts directory entries by modification time, with the newest entries appearing first.

ls -lt

Example:

-rw-r--r-- 1 user user 1200 Sep 14 18:20 latest.txt
-rw-r--r-- 1 user user  800 Sep 13 12:10 notes.txt
-rw-r--r-- 1 user user  500 Sep 11 09:30 old.txt

This is useful when working on a project and trying to locate recently changed files.

Display the Newest Files in a Readable Format

You can combine -t with -l and -h:

ls -lht

This provides detailed information, human-readable sizes, and sorting by modification time.

5. List Files Recursively with -R

Normally, ls displays the contents of one directory.

The -R option allows it to display the contents of subdirectories as well.

ls -R

Imagine you have this directory structure:

project/
├── index.html
├── css/
│   └── style.css
└── js/
    └── app.js

Running:

ls -R project

Could produce output similar to:

project:
css  index.html  js

project/css:
style.css

project/js:
app.js

This is useful when you want to inspect the structure of a project without opening every folder individually.

However, recursive listings can become very long when a directory contains many files.

6. Combine Multiple Options

One of the most useful features of Linux commands is that you can combine options.

For example:

ls -lah

This combines three options:

Option Meaning
-l Display detailed information
-a Include hidden files
-h Display readable file sizes

A possible result:

total 28K
drwxr-xr-x 4 user user 4.0K Sep 14 18:20 .
drwxr-xr-x 8 user user 4.0K Sep 14 18:00 ..
-rw-r--r-- 1 user user  220 Sep 14 18:01 .bashrc
-rw-r--r-- 1 user user 1.2K Sep 14 18:20 notes.txt
drwxr-xr-x 2 user user 4.0K Sep 13 11:30 Projects

This is a useful command when you want a complete overview of a directory.

Combine Options with Sorting

You can also combine options to sort files by modification time:

ls -laht

This command:

  • Shows detailed information.
  • Includes hidden files.
  • Displays readable file sizes.
  • Lists recently modified items first.

7. List the Contents of Another Directory

You do not need to change directories before listing their contents.

For example:

ls Documents

This displays the contents of the Documents directory.

You can also use an absolute path:

ls /home/user/Documents

Or a relative path:

ls ../

The ../ notation refers to the parent directory.

You can also inspect a specific file or directory using:

ls -l notes.txt

If the file exists, Linux displays its information.

8. Find the Largest Files in a Directory

The ls command can help you inspect file sizes.

For example:

ls -lhS

The -S option sorts files by size, with the largest files appearing first.

To include hidden files as well:

ls -lahS

This can be useful when investigating which files are taking up space in a directory.

Keep in mind that this lists entries in the directory; it does not calculate the total disk space used by every nested directory.

9. Useful ls Command Combinations

Here are some practical combinations to remember:

List files normally

ls

Show detailed information

ls -l

Include hidden files

ls -a

Show readable file sizes

ls -lh

Show hidden files with details

ls -la

Show the newest files first

ls -lt

Show the largest files first

ls -lS

Display the directory contents recursively

ls -R

Show almost everything with readable sizes

ls -lah

10. Practice Using ls

The best way to learn Linux commands is to try them yourself.

Open a Linux terminal and run:

mkdir ls-practice
cd ls-practice

Create a few files:

touch notes.txt
touch project.txt
touch .hidden-file

Create a directory:

mkdir projects

Now run:

ls

You should see:

notes.txt  project.txt  projects

The hidden file will not appear in the normal listing.

Try:

ls -a

You should now see .hidden-file as well.

Next, run:

ls -lah

This displays detailed information, including hidden files and readable sizes.

Finally, try:

ls -lR

This explores the directory and its subdirectories recursively.

Common Mistakes to Avoid

Forgetting the Difference Between -a and -A

The -a option includes . and .., while -A hides those two special entries.

Assuming ls -h Shows Everything

The -h option is mainly useful with long-format output:

ls -lh

Using -R in Very Large Directories

Recursive listings can produce a lot of output. If a directory contains thousands of files, the terminal may become difficult to read.

Confusing File Size with Directory Size

When using ls -l, the size shown for a directory is not the total size of everything inside it.

To inspect directory disk usage, you can use:

du -sh directory_name

Conclusion

The ls command is one of the first Linux commands worth learning because it helps you understand and navigate your file system.

Although ls looks simple, its options make it useful for everyday tasks such as:

  • Checking file permissions.
  • Finding hidden configuration files.
  • Reading file sizes.
  • Locating recently modified files.
  • Exploring project directories.
  • Investigating large files.

Start with ls, ls -l, and ls -a. Once you are comfortable with those, try combining options such as:

ls -lah

A few simple options can make working in the Linux terminal much easier.

Share: X (Twitter) Facebook LinkedIn