How to List Files and Directories in Ubuntu with the ls Command

Listing Files and Directories in Ubuntu: Ubuntu is a strong and flexible system, and the ls command is one of the most important tools you can use. Whether you’re new to Ubuntu or just need a reminder, knowing how to list files and directories with this command will help you work faster and get around your system more easily. In this guide, I’ll show you how to use the ls command, from the basics to some useful tips.

What is the ls Command?

The ls command stands for “list.” It’s one of the most basic commands in Ubuntu (and Linux in general) that lets you list files and directories in a directory. When you open your terminal and type ls, Ubuntu will show you the files and folders in the current directory you’re in. Sounds simple, right? Well, there’s more to it than meets the eye. You can customize ls in many ways to show you detailed information about your files, sort them, and much more.

Why Should You Use the ls Command?

The ls command is like the backbone of navigating files in Ubuntu. While graphical user interfaces (GUIs) make it easy to see what files you have, the command line offers speed, flexibility, and power. Especially if you’re managing remote servers or working without a GUI, the ls command becomes a daily essential.

How to Use the ls Command: Basic Syntax

The basic syntax of the ls command is super easy:

ls [options] [file_or_directory]
  • Options: These are optional flags you can add to customize what information ls shows.
  • file_or_directory: This is the file or directory you want to list. If you don’t specify one, it will default to the current directory.

Example:

If you’re in the /home/username directory and simply type:

ls

It will list all files and directories within that location.

Common Variations of the ls Command

Let’s break down some common variations of the ls command to give you a clearer picture of its flexibility. We’ll start simple and gradually move on to more advanced options.

1. List Files with ls

By default, when you type ls, you’ll get a list of files and directories in the current folder.

Example:

ls

Output:

Documents  Downloads  Pictures

This command will show the folder names: Documents, Downloads, and Pictures.

2. List Files in a Specific Directory

You can also specify a directory if you want to list the contents of a different folder.

Example:

ls /etc

Output:

apache2  network  passwd

In this case, ls /etc lists all the files and directories inside the /etc directory.

3. Show Hidden Files with ls -a

Files that start with a dot (.) are hidden by default. Use the -a option to display them.

Example:

ls -a

Output:

.  ..  .bashrc  .profile  Documents  Downloads

Here, you’ll notice .bashrc and .profile, which are normally hidden.

4. Detailed View with ls -l

To get more information about each file, such as file permissions, ownership, and modification dates, you can use the -l option.

Example:

ls -l

Output:

-rw-r--r-- 1 username username  200 Sep 25 14:55 file.txt
drwxr-xr-x 2 username username  4096 Sep 24 10:15 Documents

In this detailed view:

  • The first column shows file permissions (rw-r--r--).
  • The second column tells you how many links are pointing to the file.
  • The third and fourth columns show the user and group owners.
  • The fifth column shows the file size.
  • The sixth and seventh columns display the last modification date and time.
  • Finally, the file or directory name appears.

5. Sort Files by Date with ls -lt

If you want to list files by their last modification date, the -lt option comes in handy.

Example:

ls -lt

Output:

-rw-r--r-- 1 username username  200 Sep 25 14:55 file.txt
drwxr-xr-x 2 username username  4096 Sep 24 10:15 Documents

6. Display File Sizes in Human-Readable Format with ls -lh

The -h option makes file sizes easier to read by showing them in kilobytes (K), megabytes (M), etc.

Example:

ls -lh

Output:

-rw-r--r-- 1 username username 200K Sep 25 14:55 file.txt
drwxr-xr-x 2 username username 4.0K Sep 24 10:15 Documents

7. List Files Recursively with ls -R

If you want to see files in the current directory and all subdirectories, use the -R option.

Example:

ls -R

Output:

./:
Documents  Downloads

./Documents:
file1.txt  file2.txt

Understanding File Permissions with ls -l

File permissions in Ubuntu define who can read, write, or execute a file. Let’s break down the file permission part of the ls -l output. For example:

-rw-r--r-- 1 username username 200 Sep 25 14:55 file.txt
  • The first character (-) shows the file type. If it’s a d, it means it’s a directory.
  • The next three characters (rw-) show the owner’s permissions: read (r), write (w), and execute (x).
  • The second set (r--) shows the group’s permissions.
  • The third set (r--) shows the permissions for everyone else (others).

Changing File Permissions

To change permissions, you can use the chmod command. But that’s a topic for another day!

Table: Common ls Options at a Glance

OptionDescription
-aShows all files, including hidden ones
-lDisplays detailed information about files
-hMakes file sizes human-readable
-RLists files recursively through directories
-tSorts files by modification time

Real-Life Example: Using ls in a Backup Script

Let me share a practical tip from my personal experience. When I’m managing backups, I often need to check which files have changed. Instead of manually sifting through folders, I use:

ls -lt /backup_directory

This shows me the latest files at the top, so I know which ones were most recently modified. It’s a real time-saver!

FAQs

Q1: Can I use ls to list files sorted by size?

Yes, use ls -lS to sort files by size.

Q2: Why can’t I see some files with ls?

If the files are hidden, try ls -a to include them.

Q3: How do I list files in another directory without switching to it?

Just type ls /path/to/directory and it will list the contents of that directory.

Conclusion

The ls command is one of the simplest yet most powerful tools you can use in Ubuntu. Whether you’re just getting started or looking to refine your command-line skills, learning how to list files and directories effectively is a great foundation. You can combine different options to customize the output to your needs and speed up your workflow.

So, the next time you’re navigating through your system or managing files on a remote server, remember these handy ls commands. Go ahead, fire up your terminal, and put your new knowledge to the test!

Deepak Maurya

About the Author: Deepak Maurya

I hold a Computer Science degree, which built my skills in programming and system administration. Over time, my passion for Linux grew, not just for its tech power but because of the supportive community around it. Writing about Linux, especially Ubuntu, lets me share what I’ve learned. I understand how challenging it can be to start with something as flexible as Linux, so my aim is to simplify complex topics for everyone, no matter their skill level.

Full Bio »

Leave a Comment