Tutorial

ls command in Linux/UNIX

Published on August 4, 2022
author

Pankaj

ls command in Linux/UNIX

The ls command is one of the most commonly used commands in daily Linux/UNIX operations. The command is used in listing contents inside a directory and is one of the few commands beginners learn from the onset. In this guide, we will discuss Common ls commands in Linux and other parameters as well that may be used alongside the command.

Listing Files with ls command without any arguments

The ls command without any options lists files and directories in a plain format without displaying much information like file types, permissions, modified date and time to mention just but a few. Syntax

$ ls

ls command in Linux/UNIX

Listing files in reverse order

To list files in reverse order, append the -r flag as shown Syntax

$ ls -r

common ls commands in linux As you can see above, the order of the listing has changed from the last to the first in comparison to the previous image.

Listing file and directory permissions with -l option

using the -l flag, you can list the permissions of the files and directories as well as other attributes such as folder names, file and directory sizes, and modified date and time. Syntax

$ ls -l

ls  commands in Linux

Viewing files in a human-readable format

As you may have noticed, the file and folder sizes displayed are not easy to decipher and make sense of at first glance. To easily identify the file sizes as kilobytes (kB), Megabytes (MB) or Gigabytes (GB), append the -lh flag as shown Syntax

$ ls -lh

common ls commands in Linux

Viewing Hidden files

You can view hidden files by appending the -a flag. Hidden files are usually system files that begin with a full stop or a period. Syntax

$ ls -a

Listing files recursively

To display the directory tree of files and folders use the ls -R command as shown Syntax

$ ls -R

common ls commands in Linux

Listing files and directories with the ‘/’ character at the end

If you wish to go ahead and further distinguish files from folders, use the -F flag such that folder will appear with a forward slash character ‘/’ at the end. Syntax

$ ls -F

common ls commands in Linux

Displaying the inode number of files and directories

To display the inode number of files and directories, append the -i flag at the end of the ls command as shown Syntax

$ ls -i

common ls commands in Linux

Displaying the UID & GID of files and directories

If you want to display the UID as well as the GId of files and directories, append the -n parameter as shown Syntax

$ ls -n

common ls commands in Linux

Defining ls command in aliases

Aliases are customized or modified commands in Linux shell which are used in the place of the original commands. We can create an alias for the ls command this way Syntax

$ alias="ls -l"

What this does is that it tells the system to execute the ls -l command instead of the ls command. Be sure to observe that the output you get when running the ls command thereafter, will be as though you run the ls -l command. To remove the added alias, run

unalias ls

colorizing ls command output

To add some flair to the output display based on the types of files, you may want to colorize your output to easily distinguish files, folders and other attributes such as file and directory permissions. To achieve this run Syntax

ls --color

Displaying the ls command version

If you are a bit curious as to what version of ls you are running, execute the command below

# ls --v
ls (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Richard M. Stallman and David MacKenzie.
#

You can also execute the command ls --version to print the ls command version.

Displaying the ls command help page

To view more options and what you can do with ls simply run]

ls --help

common ls commands in Linux

Accessing ls man pages

Alternatively, you can view the manpages to find out more about its usage by running

man ls

common ls commands in Linux That’s all we had for you today. We hope at this point, you will be more comfortable using the ls command in your day to day operations. Feel free to weigh in your feedback. Thanks!

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
May 10, 2021

what’s output of this command(ls -aF)?

- Mamata

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    November 26, 2019

    First of all, Thanks a lot for the article!! Secondly, the image in ‘Displaying the ls command version’ is incorrect. Please correct it.

    - Sharko

      Try DigitalOcean for free

      Click below to sign up and get $200 of credit to try our products over 60 days!

      Sign up

      Join the Tech Talk
      Success! Thank you! Please check your email for further details.

      Please complete your information!

      Become a contributor for community

      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

      DigitalOcean Documentation

      Full documentation for every DigitalOcean product.

      Resources for startups and SMBs

      The Wave has everything you need to know about building a business, from raising funding to marketing your product.

      Get our newsletter

      Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

      New accounts only. By submitting your email you agree to our Privacy Policy

      The developer cloud

      Scale up as you grow — whether you're running one virtual machine or ten thousand.

      Get started for free

      Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

      *This promotional offer applies to new accounts only.