DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > How the LS Command Works on Linux

How the LS Command Works on Linux

The ls command is one of the most important commands you can know. Let's learn about it.

Johnny Simpson user avatar by
Johnny Simpson
CORE ·
Mar. 22, 22 · Web Dev Zone · Tutorial
Like (3)
Save
Tweet
2.34K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Shell
 
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:

Plain Text
 
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:

Shell
 
ls ~/Documents

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

Plain Text
 
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:

Plain Text
 
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:

Shell
 
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:

Shell
 
ls -lR

Sorting the Output 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:

Shell
 
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:

Shell
 
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:

Shell
 
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.
Command (computing) Linux (operating system)

Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Top 7 Features in Jakarta EE 10 Release
  • Migrating Legacy Applications and Services to Low Code
  • When Writing Code Isn't Enough: Citizen Development and the Developer Experience
  • 12 Modern CSS Techniques For Older CSS Problems

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo