• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

Ls Command in Linux (List Files and Directories)

Written by Admin, Updated On June 20, 2020
terminal
Ls Command in Linux (List Files and Directories)

The ls is a Linux shell command, used to list information about files and directories within the file system. By default ls utility is installed on all Linux distributions. This tutorial explains how to use the ls command with examples.

How to Use the ls Command#

Following is the basic syntax for the ls command:

ls [OPTIONS] [FILES]

If no options or arguments used with ls command, it will show a list of the names of all files in the current working directory:

ls

By default, the files will be listed in alphabetical order.

If you want to list the content of a specific directory, pass the path to the directory as an argument with ls command. For example, to list the content of the /etc directory you would type type:

ls /etc

You also can pass the multiple directories path in a single command separated by space:

ls /etc /var /etc/passwd

Make sure the user in which you are logged in should have read permission otherwise it will show the error message, ls can’t open the directory.

ls /root
ls: cannot open directory '/root': Permission denied

Long Listing Format#

By default the ls command shows only the names of the files and directories. You can use -l option with ls to show files in long listing format. Following details will be displayed when long listing format used.

  • The file type
  • The file permissions
  • Number of hard links to the file
  • File owner
  • Group of File
  • File size
  • Date and Time
  • File name

For example, to view details of /etc/hosts:

ls -l /etc/hosts
-rw-r--r-- 1 root root 597 Sep 14 2019 /etc/hosts

In above output, the first character shows the file type. In our example, the first character is - which indicates a regular file. Values for other file types are as follows:

  • - – Regular file
  • b – Block special file
  • c – Character special file
  • d – Directory
  • l – Symbolic link
  • n – Network file
  • p – FIFO
  • s – Socket

After that, nine characters are displaying the permission of the file. Out of nine, first three characters are for user, next three for the group and last three for others. As per your requirement you can change the file permissions using chmod command. Below characters are used for permissions:

  • r – File read permission
  • w – Permission to write to the file
  • x – Permission to execute the file
  • s – setgid bit
  • t – sticky bit

In our last example, rw-r--r-- means that the user can read and write the file, and the group and others can only read the file. The number 1 after the permission characters shows the number of hard links to this file.

The next two fields root root are showing the file owner and the group, followed by the size of the file (597 bytes). To show the size of file in human readable format use -h option. You can change the file owner using the chown command.

After that Sep 14 2019 is the last file modification date and time. In last column shows name of file.

Show Hidden Files#

By default, the ls command will not show hidden files. In Linux, a hidden file is any file that begins with a dot (.).

Use -a option to display all files including hidden files.

ls -la ~/
drwxr-xr-x 8 tecnstuff tecnstuff  4096 Jun  2 02:14  .
drwxr-xr-x 3 tecnstuff tecnstuff  4096 Sep 14  2019  ..
-rw------- 1 tecnstuff tecnstuff 10740 Jun 19 11:45  .bash_history
-rw-r--r-- 1 tecnstuff tecnstuff   220 Sep 14  2019  .bash_logout
-rw-r--r-- 1 tecnstuff tecnstuff  3771 Sep 14  2019  .bashrc
drwx------ 3 tecnstuff tecnstuff  4096 Sep 18  2019  .cache
drwxrwxr-x 6 tecnstuff tecnstuff  4096 Sep 18  2019  Desktop

Sorting the Output#

As we seen before, by default ls command shows output in alphabetical order. The --sort option allows you to sort the output by extension, size, time and version.

  • --sort=extension (or -X ) – sort alphabetically by extension.
  • --sort=size (or -S) – sort by file size.
  • --sort=time ( or -t) – sort by modification time.
  • --sort=version (or -v) – Natural sort of version numbers.

To sort result in reverse order use the -r option.

For example, to sort files of /var directory by modification time in reverse sort order you would use:

ls -ltr /var

List Subdirectories Recursively#

The -R option tells the ls command to display the contents of the subdirectories recursively:

ls -R

Conclusion#

The ls command lists information about files and directories.

To know more about ls command visit the GNU Coreutils page or type man ls in your terminal.

If you have any questions or feedback, feel free to leave a comment.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Check if a File or Directory Exists in Bash
Next Article   Type Command in Linux with Examples

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install SSH Keys on Ubuntu 22.04
    How to Set up SSH Keys on Ubuntu 22.04 January 7, 2023
  • How to Install Mongodb on Debian 11
    How to Install MongoDB on Debian 11 Linux January 11, 2023
  • How to Install Puppet Agent on Ubuntu 22.04
    How to Install Puppet Agent on Ubuntu 22.04 January 22, 2023
  • How to Install Jenkins on Debian 11
    How to Install Jenkins on Debian 11 January 5, 2023
  • How to Change-Hostname Ubuntu 22.04
    How to Change Hostname on Ubuntu 22.04 January 19, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook