Ugacomp

How to list files and directories in Linux using the terminal

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

The Linux command line provides a powerful interface for interacting with your system, and one of the fundamental tasks is listing files and directories. In this article, we’ll explore various commands and options to efficiently display the contents of your file system.

Using the ls Command

The most basic command for listing files and directories is ls. Open a terminal and type:

ls

This simple command will display the files and directories in the current working directory. To include detailed information, such as permissions, owner, size, and modification date, use the -l option:

ls -l

List Hidden Files

By default, ls doesn’t show hidden files (those starting with a dot). To include them in the listing, use the -a option:

ls -a

Display Size in Human-Readable Format

To make file sizes more human-readable, use the -h option:

ls -lh

This will show file sizes in kilobytes (KB), megabytes (MB), or gigabytes (GB), depending on the actual size.

Sort by Modification Time

To sort files and directories by modification time, use the -t option:

ls -lt

This command displays the latest modifications at the top.

Reverse Order Sorting

To reverse the order of the listing, combining the -r option with others:

ls -lr

This is useful when you want to see the oldest files or directories first.

List Files Recursively

If you want to list files and directories recursively, including those in subdirectories, use the -R option:

ls -R

Display File Types

To display file types along with file names, use the -F option:

ls -F

This adds symbols like / for directories and * for executables.

In addition to basic listing commands, Linux command line enthusiasts often leverage wildcards to perform more advanced file and directory listings. Wildcards are symbols that represent unknown or multiple characters, allowing for more dynamic and flexible commands.

List Files with a Specific Extension

To list files with a particular extension, use the * wildcard. For example, to list all text files in the current directory:

ls *.txt

List Files Matching a Pattern

You can use wildcards to match specific patterns in file names. For instance, to list files starting with “file” and ending with any characters:

ls file*

List Directories Only

To exclusively list directories, use the -d option along with a wildcard:

ls -d */

This command appends a slash to directory names, making them easily distinguishable.

Combine Multiple Listing Options

You can combine multiple listing options to tailor the output according to your requirements. For example, to list files in the current directory, including hidden files and displaying detailed information:

ls -la

Filter Files by Size

To filter files based on size, use the -size option. The following command lists files larger than 1MB in the current directory:

find . -type f -size +1M

List Files Modified in the Last N Days

To list files modified in the last N days, use the -mtime option. For instance, to list files modified in the last 7 days:

find . -type f -mtime -7

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.