How the ls command works on linux

How the ls command works on linux

Written by Johnny on Mar 9th, 2022 Views Report Post

On a linux or unix based system, ls is one of the most useful commands you can know. Fundamentally, it tells you information of files and folders on your system - and is extremely useful when you only have access to a server or computer via a terminal window. Let's look at how it works, and discuss some of the features of ls that you may not know.

The syntax for ls looks like this, where [OPTIONS] are optional settings, and [FILE|FOLDER] is an optional link to the file or folder we want to check. If we only type ls, it will check the current folder.

ls [OPTIONS] [FILE|FOLDER]

How to use the ls command on Linux or Mac

At its most basic, we can simply type ls in our terminal window to get a list of all files and folders in the folder you're currently in. If we type ls and press enter, we will get something like this, a list of all files and folders in that location:

backups  lib    lock  mail   opt    run    tmp
cache    local  log   named  qmail  spool  www

If we want to target a specific file or folder, we can add it to the end of the ls command. For example, the below returns ls on the folder Documents:

ls ~/Documents

If a file or folder doesn't exist, ls will return the following error:

ls: file.extension: No such file or directory 

Getting a List with the ls command on Linux or Mac

The most common use of ls is with the option -l. This provides a full list of all folders and files along with their permissions, owners, users and any system links. For example, running ls -l may return something like this:

drwxr-xr-x  2 root root   4096 Mar  4 06:43 backups
drwxr-xr-x 19 root root   4096 Dec 14 01:02 cache
drwxr-xr-x 59 root root   4096 Jul  9  2021 lib
lrwxrwxrwx  1 root root      9 May  6  2020 lock -> /run/lock
drwxrwxr-x 19 root syslog 4096 Mar  5 00:00 log
lrwxrwxrwx  1 root root      4 May  6  2020 run -> /run
drwxrwxrwt  9 root root   4096 Mar  5 03:25 tmp
drwxr-xr-x  7 root root   4096 Nov 29  2020 www

In order, this list shows us the file permissions, hard links to the file, the owner, the group, the file size, last modified date and time, and the folder name along with any system links after the -> symbol.

Using this command, we can easily get an idea of what a specific file or folder contains. If we want to see the author, too, we can write:

ls -l --author

Getting all Files and Folders Recursively with the ls command

It is also possible to get all files and folders within the folders listed using ls. To do that, we use ls -R. Note: this will often return a large number of files and folders!

If we want to get all files and folders recursively, and in a list format, we can combine options and write ls -lR:

ls -lR

Sorting the ouput of the ls command on Linux or Mac

We can also sort the output. If we wanted to see all of our files and folders, sorted by size for instance, we could use the following command:

ls -l --sort=size

Similarly, we can sort by extension, time or version - just replace size with one to change the sorting type.

Shortcuts for sorting

Shortcuts also exist for sorting:

  • Instead of writing --sort=size, we can write -S
  • Instead of writing --sort=extension, we can write -X
  • Instead of writing --sort=time, we can write -t
  • Instead of writing --sort=version, we can write -v
  • That means ls -l --sort=size can simply be written as ls -lS.

All of these sorting methods sort either alphabetically, or from highest to lowest. To reverse the order, add -r to your options. For example, a list sorted from lowest to highest size looks like this:

ls -lSr

Returning entries that begin with a dot using the ls command

By default, any entries starting with a . are ignored when using ls. If we want to see them, we need to add the -a or --all option:

ls -a

Formatting the output of the ls command on Linux and Mac

As well as -l to show the long list of files and folders, there are other format options available:

  • -x - the default, where files and folders are separated by spaces.
  • -m - comma separated list.
  • -l - long form list of files and folders.
  • -1 - (as in the number 1) a single column of files and folders.
  • -C - files and folders are separated into columns to fit the screen.

Comments (0)