DZone
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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • How to Move System Databases to Different Locations in SQL Server on Linux
  • Loop Device in Linux
  • Iptables Basic Commands for Novice
  • How to Install VPN on Linux?

Trending

  • SaaS in an Enterprise - An Implementation Roadmap
  • Creating a Web Project: Caching for Performance Optimization
  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 2
  • Implementing API Design First in .NET for Efficient Development, Testing, and CI/CD
  1. DZone
  2. Software Design and Architecture
  3. Performance
  4. 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.

By 
Johnny Simpson user avatar
Johnny Simpson
DZone Core CORE ·
Mar. 22, 22 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
2.8K 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.

Related

  • How to Move System Databases to Different Locations in SQL Server on Linux
  • Loop Device in Linux
  • Iptables Basic Commands for Novice
  • How to Install VPN on Linux?

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!