Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Ls Command in Linux (List Files and Directories) - DesignLinux

Jun 20 2020
designlinux 0 Comments
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.

Related

Tags: terminal

How to Change Hostname on Ubuntu 20.04

Prev Post

How to Install NetBeans IDE 12 in Debian, Ubuntu and Linux Mint

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 1
  • 700
  • 606,450

DesignLinux.com © All rights reserved

Go to mobile version