Linux ls Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux ls command to list files and directories, and explore various options to retrieve detailed information about them. The lab covers the basic usage of the ls command, as well as more advanced options to display file permissions, ownership, and other metadata. You will also learn how to navigate directory structures using the ls command. This lab is part of the "Basic File and Directory Operations" skill set, which is essential for Linux system administration and development tasks.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/FileandDirectoryManagementGroup -.-> linux/pwd("`Directory Displaying`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") subgraph Lab Skills linux/cd -.-> lab-422776{{"`Linux ls Command with Practical Examples`"}} linux/pwd -.-> lab-422776{{"`Linux ls Command with Practical Examples`"}} linux/ls -.-> lab-422776{{"`Linux ls Command with Practical Examples`"}} end

Understand the Basic Usage of ls Command

In this step, you will learn the basic usage of the ls command in Linux. The ls command is used to list the contents of a directory. It provides information about files and directories, such as their names, permissions, ownership, and more.

Let's start by running the basic ls command in the ~/project directory:

ls

Example output:

file1.txt  file2.txt  folder1  folder2

The output shows the files and directories present in the current directory.

You can also use the ls command with various options to get more detailed information. For example, the -l option displays the long-format listing, which includes additional details about each file and directory:

ls -l

Example output:

total 8
-rw-r--r-- 1 labex labex 0 Apr 12 12:34 file1.txt
-rw-r--r-- 1 labex labex 0 Apr 12 12:34 file2.txt
drwxr-xr-x 2 labex labex 4096 Apr 12 12:34 folder1
drwxr-xr-x 2 labex labex 4096 Apr 12 12:34 folder2

The long-format listing provides information such as file permissions, owner, group, file size, and modification time.

Another useful option is -a, which displays all files, including hidden files (files starting with a dot):

ls -a

Example output:

.  ..  .hidden_file  file1.txt  file2.txt  folder1  folder2

You can combine multiple options, such as -l and -a, to get both long-format and hidden file listings:

ls -la

Example output:

total 16
drwxr-xr-x 4 labex labex 4096 Apr 12 12:34 .
drwxr-xr-x 4 labex labex 4096 Apr 12 12:34 ..
-rw-r--r-- 1 labex labex    0 Apr 12 12:34 .hidden_file
-rw-r--r-- 1 labex labex    0 Apr 12 12:34 file1.txt
-rw-r--r-- 1 labex labex    0 Apr 12 12:34 file2.txt
drwxr-xr-x 2 labex labex 4096 Apr 12 12:34 folder1
drwxr-xr-x 2 labex labex 4096 Apr 12 12:34 folder2

In the next step, you will explore more advanced ls command options to retrieve detailed file information.

Explore ls Command Options for Detailed File Information

In this step, you will explore more advanced options of the ls command to retrieve detailed information about files and directories.

Let's start by using the -l (long format) option to display additional details about the files and directories:

ls -l

Example output:

total 8
-rw-r--r-- 1 labex labex 0 Apr 12 12:34 file1.txt
-rw-r--r-- 1 labex labex 0 Apr 12 12:34 file2.txt
drwxr-xr-x 2 labex labex 4096 Apr 12 12:34 folder1
drwxr-xr-x 2 labex labex 4096 Apr 12 12:34 folder2

The long-format listing provides the following information for each file and directory:

  • File permissions
  • Number of hard links
  • Owner
  • Group
  • File size
  • Modification time
  • Filename

You can also use the -h (human-readable) option to display file sizes in a more readable format:

ls -lh

Example output:

total 8.0K
-rw-r--r-- 1 labex labex 0 Apr 12 12:34 file1.txt
-rw-r--r-- 1 labex labex 0 Apr 12 12:34 file2.txt
drwxr-xr-x 2 labex labex 4.0K Apr 12 12:34 folder1
drwxr-xr-x 2 labex labex 4.0K Apr 12 12:34 folder2

The file sizes are now displayed in a human-readable format (e.g., 4.0K instead of 4096).

To list files in reverse order, you can use the -r (reverse) option:

ls -lr

Example output:

total 8
drwxr-xr-x 2 labex labex 4096 Apr 12 12:34 folder2
drwxr-xr-x 2 labex labex 4096 Apr 12 12:34 folder1
-rw-r--r-- 1 labex labex 0 Apr 12 12:34 file2.txt
-rw-r--r-- 1 labex labex 0 Apr 12 12:34 file1.txt

The files and directories are now listed in reverse order.

You can also combine multiple options to get the desired output. For example, to list all files (including hidden files) in long format and reverse order:

ls -alr

Example output:

total 16
drwxr-xr-x 4 labex labex 4096 Apr 12 12:34 ..
drwxr-xr-x 4 labex labex 4096 Apr 12 12:34 .
-rw-r--r-- 1 labex labex 0 Apr 12 12:34 .hidden_file
drwxr-xr-x 2 labex labex 4096 Apr 12 12:34 folder2
drwxr-xr-x 2 labex labex 4096 Apr 12 12:34 folder1
-rw-r--r-- 1 labex labex 0 Apr 12 12:34 file2.txt
-rw-r--r-- 1 labex labex 0 Apr 12 12:34 file1.txt

In the next step, you will learn how to utilize the ls command for navigating directory structures.

In this step, you will learn how to use the ls command to navigate through directory structures.

First, let's create a new directory and some files inside it:

mkdir ~/project/new_folder
touch ~/project/new_folder/file3.txt ~/project/new_folder/file4.txt

Now, you can use the ls command to list the contents of the new_folder directory:

ls ~/project/new_folder

Example output:

file3.txt  file4.txt

To list the contents of the current directory and its subdirectories, you can use the -R (recursive) option:

ls -R ~/project

Example output:

~/project:
file1.txt  file2.txt  folder1  folder2  new_folder

~/project/folder1:

~/project/folder2:

~/project/new_folder:
file3.txt  file4.txt

The -R option recursively lists the contents of the current directory and all its subdirectories.

You can also use the ls command to navigate to a specific directory. For example, to list the contents of the new_folder directory:

cd ~/project/new_folder
ls

Example output:

file3.txt  file4.txt

After navigating to the new_folder directory, you can use the basic ls command to list its contents.

To go back to the parent directory, you can use the cd .. command:

cd ..
ls

Example output:

file1.txt  file2.txt  folder1  folder2  new_folder

This way, you can use the ls command to navigate through your directory structure and list the contents of different directories.

In the previous steps, you learned the basic usage of the ls command and explored its various options. In this step, you practiced using the ls command to navigate through directories and list their contents. These skills will help you manage your files and directories more efficiently in a Linux environment.

Summary

In this lab, you learned the basic usage of the ls command in Linux, which is used to list the contents of a directory. You explored various options such as -l to display detailed file information, and -a to show hidden files. You also learned how to combine multiple options to get more comprehensive file listings. The lab then covered more advanced ls command options to retrieve detailed file information, including file permissions, ownership, and timestamps. Finally, you learned how to navigate directory structures using the ls command.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like