The ls Command in Linux

Learn how to use the ls command on Linux or Unix based operating systems.

ls command in Linux

The ls command is crucial for navigating the Linux filesystem. You will quickly find that you will be making use of this command often.

Alongside cd, it is one of the very first commands that you should learn when dealing with Linux.

The ls command is used to list the files within a directory. When used without any options, this will list the files of the current working directory.

Within this guide, we will show you some of the various ways that you can put the ls command to use.

Throughout most of our Linux and Raspberry Pi projects, you will see that we often use ls.

The ls Command Syntax

The ls command uses a simple syntax that is very straightforward to remember.

All you need to do is enter ls, followed optionally by your options or the path to the directory, or file you want to list.

ls [OPTIONS] [FILE|DIRECTORY]

Both arguments are entirely optional, and you can specify one without having to specify the other.

Using ls With No Options

The easiest way to make use of the ls command, is to use it without specifying any additional options.

You will get a list of all the files in the current working directory when you don’t specify any options.

$ ls
bin  boot  dev  etc  home  lib  lost+found  media  mnt  opt  
proc  root  run  sbin  srv  sys  tmp  usr  var

You will only be able to view the name of the files and not any information about them.

Listing Files Within a Specified Directory

While by default, the ls command only lists the files of the current working directory. It is also possible to specify the directory.

Below we are going to give you two examples of how you can specify a directory.

The first way is to specify the directory after the command.

ls /home/pi

However, if you are using an option such as -l you will need to specify the directory after that.

 ls -l /home/pi

More Information From ls Using -l

To get more information from the ls command we can make use of the -l (Make sure you use a lowercase l) option.

This option tells the command that it should list out the files using the long listing format.

ls -l

Out of all of the options, this is the one that you will likely end up using the most as it provides you details about each file.

The long listing format will give you seven different pieces of information about the file.

These include the file permissions, owner, group, file size in bytes, modification date, and filename.

$ ls -l

total 1732
drwxr-xr-x  2 pi pi    4096 May 27 08:18 Bookshelf
-rwxr-xr-x  1 pi pi 1880001 Jun 19 06:04 hello-world
-rw-r--r--  1 pi pi      76 Jun 19 05:58 hello-world.go
drwxr-xr-x 11 pi pi    4096 Jun 19 05:54 mpv-build

Alongside these bits of information, the ls command also shows us the total number of file system blocks that are being used by the files in this directory.

If you would like to learn about permissions, be sure to check out our Linux permission guide.

Using the ls Command to View Hidden Files

One thing you may have noticed is that hidden files are not listed by default. A file is considered hidden in Linux whenever the file name starts with a dot (.).

To combat this behavior, the ls command has the -a option that will tell the tool to list all files.

ls -a

Below you can see how you can use the show hidden files option (-a) alongside the long list format (-l) option.

$ ls -la

total 1772
drwxr-xr-x  7 pi   pi      4096 Jun 22 11:25 .
-rw-r--r--  1 pi   pi      3595 Jun 19 05:56 .bashrc
drwxr-xr-x  2 pi   pi      4096 May 27 08:18 Bookshelf
drwxr-xr-x  3 pi   pi      4096 Jun 19 05:58 .cache
-rwxr-xr-x  1 pi   pi   1880001 Jun 19 06:04 hello-world
-rw-r--r--  1 pi   pi        76 Jun 19 05:58 hello-world.go
drwxr-xr-x 11 pi   pi      4096 Jun 19 05:54 mpv-build
-rw-r--r--  1 pi   pi       807 May 27 08:10 .profile

Showing Human Readable Numbers

One thing that can become tricky to deal with is that the ls command only reports file sizes in bytes.

However, you can specify the -h option to convert the numbers into the human-readable format. This means the numbers will be converted to shorthand to make them easier to read. For example, 4096 bytes would be displayed as 1K.

For this option to be useful you will need to use it alongside the long list format option (-l)

$ ls -lh

total 1.7M
drwxr-xr-x  2 pi pi 4.0K May 27 08:18 Bookshelf
-rwxr-xr-x  1 pi pi 1.8M Jun 19 06:04 hello-world
-rw-r--r--  1 pi pi   76 Jun 19 05:58 hello-world.go
drwxr-xr-x 11 pi pi 4.0K Jun 19 05:54 mpv-build

Listing Files and Directories with More Information

You can use the -F or --classify option alongside the ls command to add additional identifiers to your listed files and directories.

Using this option, you will be able to quickly identify items listed by the ls command, such as directories and executables.

$ ls -F
Bookshelf/  hello-world*  hello-world.go  mpv-build/

Below is a list of the symbols that will be added by utilizing this option. Reference back to this if you ever

  • @ – The at symbol defines that this is a symbolic link or has extended attributes.
  • * – When the asterisk symbol is shown at the end of the file, it is telling you that it is executable.
  • = – The equals sign defines this as a UNIX socket. These are used for local communication.
  • | – The pipe character tells us that this file is a named pipe. Named pipes are persistent pipes that last longer than the process execution.
  • > – If you see the greater than symbol, then this file is a door. In Unix systems, these are used as a way of inter-process communication.
  • / – The most prominent symbol, the forward slash, defines that this item is a directory.

Recursively List Sub-Directories

If we utilize the -R option, we can tell the ls command to list files within any sub-directories.

This option is useful if you need to check several directories for their files.

$ ls -R
.:
Bookshelf  hello-world  hello-world.go  mpv-build

./Bookshelf:
000_RPi_BeginnersGuide_DIGITAL.pdf

./mpv-build:
build       ffmpeg        mpv          uninstall           use-libass-custom
build_libs  ffmpeg_build  mpv_options  update              use-libass-master
clean       go.tar.gz     README.rst   use-ffmpeg-custom   use-mpv-custom
config      install       rebuild      use-ffmpeg-master   use-mpv-master
debian      libass        scripts      use-ffmpeg-release  use-mpv-release

./mpv-build/build_libs:
bin  include  lib  share

./mpv-build/build_libs/bin:
ffmpeg  ffplay  ffprobe

./mpv-build/build_libs/include:
ass         libavdevice  libavformat  libpostproc    libswscale
libavcodec  libavfilter  libavutil    libswresample

Sorting Files/Directories by File Size

If you wanted to order the files so that they are listed in order of their file size, you need to use the -S option.

This option is best used alongside the show file size (-s) option or the long list format (-l) option.

$ls -lS

total 1732
-rwxr-xr-x  1 pi pi 1880001 Jun 19 06:04 hello-world
drwxr-xr-x  2 pi pi    4096 May 27 08:18 Bookshelf
drwxr-xr-x 11 pi pi    4096 Jun 19 05:54 mpv-build
-rw-r--r--  1 pi pi      76 Jun 19 05:58 hello-world.go

Sorting Files/Directories by Time and Date

It is also possible to sort files by their last modified time and date. To do this, you will need to specify the -t option.

Like the sort by file size option, you will need to be using the long list format (-l) option for these results to be obvious.

$ls -lS

total 1732
drwxr-xr-x  2 pi pi    4096 May 27 08:18 Bookshelf
-rwxr-xr-x  1 pi pi 1880001 Jun 19 06:04 hello-world
drwxr-xr-x 11 pi pi    4096 Jun 19 05:54 mpv-build
-rw-r--r--  1 pi pi      76 Jun 19 05:58 hello-world.go

Hopefully, at this stage, you will now understand how to make use of the ls command and a variety of its options.

If you are ever interested in what other arguments can be used, you can make use of the man command followed by ls.

man ls

You can find out more about other commands by browsing our Linux commands category.

If you feel like there is something we missed or got incorrect feel free to leave a comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *