Ugacomp

How to list files and directories using the ls command in Linux

Where necessary, you may need to have access to a VPS server so you can follow how to implement the steps in this article.  You can get a cheaper VPS Server from Contabo with 4vCPU cores, 8GM RAM, and 32TB Bandwidth for less than $5.50 per month. Get this deal here now

Table of Contents

Cloud VPS S

$5.50 Monthly
  • 4 vCPU Cores | 8GB RAM

CLOUD VPS M

$15.50 Monthly
  • 6 vCPU Cores | 16GB RAM

CLOUD VPS L

$17.50 Monthly
  • 8 vCPU Cores | 24GB RAM

If you want to identify the content of a particular directory in Linux, you can use the ls command. This command is essential for navigating and managing your files in Linux, and here is the step-by-step guide on how to use it:

The ls command basic syntax

By default, the ls command lists the contents of the current directory. Just type the following on your Linux terminal:

ls

This command provides a simple list of the files and directories in the current directory

Desktop  Documents  Downloads  Music  Pictures  Videos

Listing Files with Details

Using the ls command with the -l option , it allows you to list files and directories with detailed information like permissions, ownership, size, and modification date:

ls -l

Running the above command will give you an output with the following listing format:

drwxr-xr-x  2 user group 4096 Aug 31 10:20 Desktop
-rw-r--r--  1 user group  543 Aug 30 08:15 file.txt

We can interpret the output as follows:

  • The first column contains file permissions: drwxr-xr-x and -rw-r–r–
  • The second column contains the number of links (hard links).
  • The third column containers the owners of the files called user in this case
  • The fourth column contains the group to which the owner of the file belongs to.
  • The fifth column contains files sizes in bytes.
  • The sixth column contains the date and time of last modifications to the files.
  • The seventh column contains the filenames and directories associated to the previous columns.

RECOMMENDED READING: How to use the cd command to navigate Linux directories

Including Hidden Files

Files that begin with a dot (.) are hidden by default. To list all files, including hidden ones, use the -a option.

ls -a

When you use ls with the -a option, it will list all files in the current directory including the hidden files that start with dots in their names.

Combining Options for Detailed Hidden Files

To make a detail list of files including hidden files, you can use ls with the -la options combination:

ls -la

Sorting the List

The ls command also allows you to sort the output based on various criteria. For example, to sort files by modification time, you can use the -t option:

ls -lt

The -lt options combination will produce a list of files and directories starting with the latest modification date and time:

-rw-r--r--  1 user group  543 Aug 31 10:20 latest-file.txt
-rw-r--r--  1 user group  321 Aug 30 08:15 older-file.txt

For reverse order, add the -r option:

ls -ltr

Listing Files in Human-Readable Format

By default, the ls command produces files sizes in bytes. But you can use the -h option along with -l to display sizes in KB, MB, etc.

ls -lh

The -lh options combination will display file sizes in kilobytes:

drwxr-xr-x  2 user group 4.0K Aug 31 10:20 Desktop
-rw-r--r--  1 user group 543K Aug 30 08:15 file.txt

Recursive Listing

To list all files and directories, including the contents of all subdirectories, use the -R option.

ls -R

This command will produce the following sample output:

.:
Desktop  Documents  Downloads

./Desktop:

./Documents:
file1.txt  file2.txt

Listing Directory Information

If you want to see only the directories, not files, you can use the following combination:

ls -d */

Listing Files by Size

You can list files by sorting them from the largest files to the smallest size using the -S option:

ls -lS

Here is the sample output of using the -lS options combination:

-rw-r--r--  1 user group 1.2M Aug 31 10:20 largefile.txt
-rw-r--r--  1 user group  543 Aug 30 08:15 smallfile.txt

Hire us to handle what you want

Hire us through our Fiverr Profile and leave all the complicated & technical stuff to us. Here are some of the things we can do for you:

  • Website migration, troubleshooting, and maintenance.
  • Server & application deployment, scaling, troubleshooting, and maintenance
  • Deployment of Kubernetes, Docker, Cloudron, Ant Media, Apache, Nginx,  OpenVPN, cPanel, WHMCS, WordPress, and more
  • Everything you need on AWS, IBM Cloud, GCP, Azure, Oracle Cloud, Alibaba Cloud, Linode, Contabo, DigitalOcean, Ionos, Vultr, GoDaddy, HostGator, Namecheap, DreamHost, and more.
 

We will design, configure, deploy, or troubleshoot anything you want. Starting from $10, we will get your job done in the shortest time possible. Your payment is safe with Fiverr as we will only be paid once your project is completed.