How to list files and folders in Linux ?

In Linux, the ability to list files and folders is a basic yet essential skill for file system navigation and management. Unlike Windows, which uses the dir command in its command-line interface, Linux utilizes a different set of commands in its terminal.

These commands are integral for effective file management and system organization. Automation and scripting make these tasks efficient, especially in directories with a large number of files.

Linux was invented by Linus Torvald, he also created a foundation.

1. Basic file listing using ls command

The ls command is fundamental in Linux for listing contents of directories. By default, it displays all files and folders in the current directory. Here are some details it can show:

  • File and directory names.
  • File types: indicated by symbols like - for files and d for directories.
  • File permissions and the number of links.
  • Owner and group of the files.
  • File size.
  • Last modification date and time.

This bash command below will also show the total number of files and folders, similar to the dir command in Windows.


2. Advanced options for listing files in Linux

The ls command offers various advanced options for a more tailored output:

  • -a: Include hidden files (those starting with .) in the listing.
  • -l: Use a long listing format to view detailed file information.
  • -R: Recursively list subdirectories.
  • -h: Show file sizes in a human-readable format (e.g., KB, MB).
  • –sort=size: Sort files by size.
  • *.ext: List files with a specific extension (e.g., ls *.txt).

You can combine these options for more specific needs, such as ls -lR to list all files and directories recursively with detailed information.

3. Examples of scripts using ls for file listing

3.1 Display files with specific details

This example will list files in a detailed format, showing permissions, number of links, owner, size, and modification date:

ls -l

3.2 Recursively list files in Linux subdirectories

This script will list all files and directories, including those in subdirectories:

ls -R

3.3 List files with a specific extension

To display files with a specific extension (e.g., .txt), use:

ls *.txt

3.4 Combine ls options for detailed output

To list .txt files with detailed information, including those in subdirectories:

ls -lR *.txt

Listing files and folders in Linux is a necessary skill

From a personal point of view, we often get used to list files using GUI, especially with Windows. But when you start using Linux, you realize how quick and efficient it is to list files using scripts.

The ls command in Linux is a versatile tool for managing and listing files and folders. It allows for example to perform listings, including attributes like file permissions, owner, size, and modification date.

The command is also flexible, especially when combined with various options. It makes it an invaluable tool for Linux users. Whether for automating tasks or viewing of directory contents, the ls bash command is a precious command in the Linux toolkit.

To explore further, consider learning about file operations like copying files using commands like cp and moving files with mv in Linux.

How to copy files and folders in Linux ?

Leave a Comment

Your email address will not be published. Required fields are marked *