Skip to main content
X

Explore your training options in 10 minutes

Linux ls Command: A Guide

James Gallagher - January 04, 2021


One of the most fundamental commands you need to know when using the Linux command line is the ls command. This command allows you to see what files and directories exist within the file system. It also provides additional information about a file or folder, such as its access and ownership permissions.

In this guide, we’re going to discuss how to use the Linux ls command. We’ll also provide a few examples of the command in action so you can start putting it to use.

How to Use the ls Command

The ls command allows you to list all the files and folders in a given directory.

Get offers and scholarships from top coding schools illustration

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses










By continuing you agree to our Terms of Service and Privacy Policy , and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

The most basic usage of the ls command is to use it without any options. When you run ls alone, it will display a list of all the files and folders in your current working directory. Here’s an example of the ls command in action:

ls

Here’s the output of this command:

README.md      lib         package-lock.json     pages        styles.css
components     node_modules     package.json     public       yarn.lock

As you can see, the ls command has returned a list of all the files in the folder currently being viewed. Also note that ls orders the files in a folder alphabetically. Capital letters are given priority over lowercase letters, which is why “README.md” appears at the start of our list of tiles.

You can also use the command to list the files in a particular directory. Suppose you want to know what files exist in the “/var” directory on your computer. You could do so using this command:

ls /var

You can only use the ls command if you have permission to read a file and its contents. Otherwise, an error will be returned.

List Hidden Files

If you have some experience working with Linux, you’ll know there are a lot of hidden files on your computer. These are files that begin with a period and do not appear when you try to list the files and folders in a directory.

To view these files, you need to specify the -a flag with the ls command, like so:

ls -a 

This command returns:

..
.bash_history
.devaccounts
Desktop

This is only a sample of the files returned by this command. As you can see, our current working folder contains files like “.bash_history” and “.devaccounts” which would otherwise have been hidden if we had not specified the -a flag.

Show Detailed File Information

The ls command, by default, shows file names. But what if you want to learn more about those files? What if you want to know the owners of that file, or when it was last modified? That’s where the -l flag comes in.

The -l flag instructs Linux to print out a list of files with detailed descriptions.

Venus, a software engineer at Rockbot

"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"

Venus, Software Engineer at Rockbot

Suppose we want more information about the file “/etc/passwd”. We can get it using this command:

ls -l /etc/passwd

Our command returns:

-rw-r--r--  1 root  wheel  6946 Feb 29 06:10 /etc/passwd

As you can see, there’s a lot more information now displayed for us to review. The -l flag shows us the following pieces of information, in order:

  • The file type
  • File permissions
  • Number of hard links to the file
  • The owner of the file
  • The file group
  • The size of the file
  • When the file was last modified
  • The name of the file and its location

This tells us that the /etc/passwd file is owned by the user “root”, and that the file was last modified on February 29 at 6:10am.

List Files Recursively

By default, the ls command only lists the files and folders in a particular directory. However, you can also use the command to generate a recursive list of the contents of all subdirectories in a folder. You can do so using the -R flag, like so:

ls -R

This command returns:

app.rb        	config.rb        	README.md   	lib

./lib:
create_schema.rb

In the output of this command, we can see not only a list of the files in our current folder, but also a recursive list of the contents of all subdirectories. In this case, the “lib” directory contains the file “create_schema.rb”, which is displayed in our output alongside all the files in our current directory.

Sort Files by File Size

The -lS flag allows you to sort the files and folders returned by the ls command by size. The files will be ordered in descending order of their size.

Suppose we want to generate a list of all the files in the “~/ folder” in order of their size. We could do so using this command:

ls -lS

Our command returns:

drwx------@ 98 James  staff  3136 May 16 17:37 Library
drwxrwxr-x  18 James  staff   576 Jun  9 10:34 Projects
drwx------@ 14 James  staff   448 Apr 10 12:48 Dropbox
…

The output of this command has been edited for brevity. As you can see, our files and folders are ordered in descending order of their size. “Library” is the biggest folder whose size is 3136, so it appears at the top of our list.

Conclusion

The Linux ls command allows you to view a list of the files and folders in a given directory. You can also use this command to display details of a file, such as the owner of the file and the permissions assigned to the file.

If you want to learn more about the ls command, run the command man ls in your terminal. This will display the Linux manual description for the command.

About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.

What's Next?

James Gallagher

About the author: James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.

Skip to main content