20 Basic Linux 'ls' Command with Examples for Beginners (2023)

Listing files is one of the most widely undertaken tasks by ordinary Linux users and system administrators. In Linux, the ls command, short for “list” is used to list or display the contents of a directory.

This could either be your current directory or any other directory on the system. The command displays both files and subdirectories and in most cases distinguishes between different file types using color codes.

Without any command-line options, the ls command will simply list all the directory contents. However, it provides an array of handy command-line options to manipulate the output and display the desired output.

You might also like:

  • 5 Useful Tools to Remember Linux Commands Forever
  • How to List All Files Ordered by Size in Linux
  • How to Find and List Modified Files in Linux
  • How to Sort Files in Linux by Date and Time

In this article, we’ll be discussing the basics of ls command examples with all the available various command options that it provides in Linux.

Table of Contents

ls Command Options in Linux

The ls command takes the following syntax:

(Video) 1. Linux Tutorials: Linux ls command with examples

$ ls [ options ] /path/to/directory

The options section represents the command-line arguments that can be passed to manipulate the output of the command.

In this tutorial, we will cover the following ls command arguments.

OptionsDescription
ls -mLists directory contents separated by a comma.
ls -QDisplays directory contents enclosed by quotation marks.
ls -lDisplays files in a long-list format.
ls -lhDisplay file size in a human-readable format.
ls -gOmits group ownership column.
ls -FAdds a forward slash to directories.
ls -iDisplay inode number of files and directories.
ls -aDisplay all files including hidden files.
ls *.Filters files according to the file extension.
ls -laDisplays all files and directories in long list format.
ls -RDisplay files and directories recursively.
ls -rSort Files in reverse.
ls -XSort files alphabetically by file extension.
ls -tlDisplay files according to file creation date and time.
ls -nList UIDs and GIDs.

1. List Files and Directories in Linux

Running ls command without passing any command-line options or arguments, the ls command simply lists the directory contents in alphabetical order. Here we won’t be able to view details like file types, size, modified date and time, permission and links, etc.

$ ls
20 Basic Linux 'ls' Command with Examples for Beginners (1)

2. Long Listing of Files in Linux

The -l command option lets you print out detailed information about the directory contents in a columnar format that includes size, modified date and time, file or directory name and owner of the file, and its permission.

$ ls -l
20 Basic Linux 'ls' Command with Examples for Beginners (2)

Starting from the far left, we have:

  • 1st column – File/directory permissions.
  • 2nd column – Number of links.
  • 3rd column – Name of the owner.
  • 4th column – Name of the group that the file belongs to.
  • 5th column – File size in bytes.
  • 6th column to 8th column – Last modification date.
  • 9th column – File / Directory name.

3. List Hidden Files and Directories

Hidden files are special files that store user settings and configuration files, which are used by running programs and services for reading and storing information.

For example. the .bashrc file is a script that contains user settings and configurations of the currently logged-in user, which include command aliases, shell history, the coloring of the terminal font, etc.

The .bash_logout file is executed when you log out of your bash sessions. It’s mainly used for cleanup purposes i.e. carrying out any operations that need to be performed once you exit the bash shell.

To list hidden files, pass the -a option as shown, which displays both hidden files and directories.

(Video) Linux Tutorials | The ls command | GeeksforGeeks

$ ls -a
20 Basic Linux 'ls' Command with Examples for Beginners (3)

4. List All Files in Linux

As you have noticed the -a option not only lists hidden files but all the files and directories. For better viewing, you can use the -la option

$ ls -la
20 Basic Linux 'ls' Command with Examples for Beginners (4)

5. Display File Size in a Human-readable Format

To present the output in a better format, add the -h flag to print the file size in a human-readable format. From the output, the file size is displayed in Kilobytes, Megabytes, and Gigabytes. By all means, this looks more presentable.

$ ls -lh
20 Basic Linux 'ls' Command with Examples for Beginners (5)

6. Distinguish Directories and Files in Linux

When running the ls command, it’s not always easy to make a clear distinction between files and directories. The -F option adds a forward slash (/) to directories, making it easier for them to stand out from the rest of the files.

$ ls -F
20 Basic Linux 'ls' Command with Examples for Beginners (6)

7. Sorting Files in Reverse Order

By default, the ls command sorts files and directories alphabetically (From A – Z). You can opt to sort the directory contents in reverse order using the -r option.

$ ls -lr 
20 Basic Linux 'ls' Command with Examples for Beginners (7)

In addition, you can sort the file extensions alphabetically using the -X flag.

$ ls -X

8. List Files Recursively in Linux

The -R flag lists files recursively. First, the command lists all the files and directories in your current directory, then proceeds to display files contained in individual directories and subdirectories.

$ ls -R

In the following example, the files in individual directories have been listed as well.

20 Basic Linux 'ls' Command with Examples for Beginners (8)

9. Sort Files By Modification Time in Linux

The ls -ltr command shows the files in the long listing format in reverse sorted by modification time, which means it will display detailed information about each file or directory in reverse order based on their last modified date/time stamp.

$ ls -ltr
20 Basic Linux 'ls' Command with Examples for Beginners (9)

10. Sort Files By Newest to Oldest in Linux

You can sort files by time and date using the -t option, which sorts the files in order starting from the newest to the oldest.

(Video) 20 basic linux commands for everyone (2021) | Hands-on demo on google cloud (gcp) linux vm

$ ls -tl
20 Basic Linux 'ls' Command with Examples for Beginners (10)

11. Sort Files by File Size in Linux

With a combination of -lS option, it will displays file size in descending order (biggest to smallest in size).

$ ls -lS
20 Basic Linux 'ls' Command with Examples for Beginners (11)

12. List File Inode Number in Linux

You can display the files and directories’ inode numbers using the -i option as shown.

$ ls -i
20 Basic Linux 'ls' Command with Examples for Beginners (12)

13. List Files and Directories Separated by Commas

The -m flag lists the directory contents one after the other separated by a comma.

$ ls -m

With the -Q flag, all the directory contents are enclosed by double quotation marks as shown.

$ ls -Q
20 Basic Linux 'ls' Command with Examples for Beginners (13)

14. Omit Group Ownership in a Long-List Format

When used with the -l command option, the ls command prints both user and group ownership of the file. You can opt to omit the group column by passing the -g option.

$ ls -g
20 Basic Linux 'ls' Command with Examples for Beginners (14)

15. List Specific File Types or Extensions

To list specific file types or extensions, use the wildcard notation (*) followed by the file extension.

For example, to display all files with a .jpg extension, run the command:

$ ls *.jpg

Similarly, to list all PDF files, run the command:

$ ls *.pdf
20 Basic Linux 'ls' Command with Examples for Beginners (15)

16. List the UID and GID of Files

To display the UID and GID of files and directories, use the -n option as shown.

(Video) The 50 Most Popular Linux & Terminal Commands - Full Course for Beginners

$ ls -n
20 Basic Linux 'ls' Command with Examples for Beginners (16)

17. Check ls Command Version

If you are a little curious and want to check the version of the ls command, you can do so as follows:

$ ls --version

From the output, you can see that we are running ls version 9.1.

20 Basic Linux 'ls' Command with Examples for Beginners (17)

18. Show ls Command Help Page

The ls program provides a wealth of command-line options. What we have covered are just some of the commonly used ones. For a comprehensive list of all the command options, run the following command:

$ ls --help
20 Basic Linux 'ls' Command with Examples for Beginners (18)

Optionally, you can visit the man pages by running:

$ man ls
20 Basic Linux 'ls' Command with Examples for Beginners (19)

19. List Directory Information in Linux

With the ls -l command list files under directory /tmp. Wherein with -ld options, it will display information of the /tmp directory.

$ ls -l /tmp$ ls -ld /tmp/
20 Basic Linux 'ls' Command with Examples for Beginners (20)

20. Create ls Command Aliase

We have made an alias for the ls command when we execute the ls command it will take the -l option by default and display a long listing as mentioned earlier.

$ alias ls="ls -l"

To view a number of aliases available in your system, use the below alias command and the same can be unalias as shown below example.

$ alias

To remove an alias previously defined, just use the unalias command.

$ unalias ls
20 Basic Linux 'ls' Command with Examples for Beginners (21)

[ You might also like: How to Create and Use Alias Command in Linux ]

(Video) 30 BASIC COMMANDS IN LINUX / UNIX || LINUX COMMANDS || UNIX COMMANDS || OPEN SOURCE

In this guide, we have demonstrated how to use the ls command to view the contents of a folder or directory. In addition, we went a step further and explored command options that can be used with the ls command in Linux.

In our next article, we’ll cover more advanced ls commands with their examples. Also, I suggest you go through some interview questions on the ls command, and also if we’ve missed anything in the list, please update us via the comment section.

FAQs

How many ls commands are there in Linux? ›

Linux ls command options
ls optionDescription
ls -rIt is used to print the list in reverse order.
ls -RIt will display the content of the sub-directories also.
ls -lXIt will group the files with same extensions together in the list.
ls -ltIt will sort the list by displaying recently modified filed at top.
14 more rows

What is the ls a command in Linux? ›

ls -a will list all files including hidden files (files with names beginning with a dot). ls -F gives a full listing, indicating what type files are by putting a slash after directories and a star after executable files (programs you can run).

How do I list the first 10 files in Linux? ›

Find the Largest Top 10 Files and Directories On a Linux
  1. du command : It estimates file space usage.
  2. sort command : Sort lines of text files or given input data.
  3. head command : Output the first part of files i.e. to display first 10 largest file.
  4. find command : It Searches file on Linux machine.
Jan 20, 2020

How do you find and list files in Linux? ›

To search for files in a directory, the user invoking the find command needs to have read permissions on that directory. The option -L (options) tells the find command to follow symbolic links. The /var/www (path…) specifies the directory that will be searched.

How can I learn Linux by myself? ›

How to Learn Linux: Understanding the Basics
  1. Find solid resources and learning materials.
  2. Learn and practice the basics.
  3. Spend time exploring the OS.
  4. Start a project and put your knowledge into practice.
  5. Keep practicing to refine your skills and learn more.
Aug 1, 2022

Can Linux be self taught? ›

You can learn the basic Linux commands in a few days but it will likely take a few weeks to become more familiar with these commands. More advanced commands may take years for full mastery. The Linux command line is known for the degree of customization it supports.

How can I practice Linux? ›

13 exercises to boost your Linux skills
  1. Choose Linux. A home lab can serve many purposes, and you need some idea of what you want to use your lab to achieve. ...
  2. Install at least three distributions. ...
  3. Adjust user permissions. ...
  4. Use text editors. ...
  5. Manage users and groups. ...
  6. Manage files. ...
  7. Manage software. ...
  8. Boot to the CLI and GUI.
Nov 29, 2021

How to list ls in Linux? ›

-
  1. To list all files in the current directory, type the following: ls -a This lists all files, including. dot (.) ...
  2. To display detailed information, type the following: ls -l chap1 .profile. ...
  3. To display detailed information about a directory, type the following: ls -d -l .

What is the difference between ls and ls Linux? ›

ls is standing for listing directories and files under a directory. In your situation, ls (without a directory argument) is going to list directories and files under the current directory(pwd). The other command, ls / is going to list files and directories under the root directory which is / .

What is the syntax of ls? ›

What is use for LS command? ›

The ls command is used to list files or directories in Linux and other Unix-based operating systems. Just like you navigate in your File explorer or Finder with a GUI, the ls command allows you to list all files or directories in the current directory by default, and further interact with them via the command line.

How to delete a file in Linux? ›

Deleting a single file

You can quickly and easily delete a single file with the command “rm” followed by the file name. With the command “rm” followed by a file name, you can easily delete single files in Linux.

How create a file in Linux? ›

Using the touch command

The touch command is the most commonly used command for creating a new file in Linux. To create a new file in the current directory, you need to run the touch command followed by the name of the file.

Why is ls used? ›

The ls command is one of the most commonly used commands in daily Linux/UNIX operations. The command is used in listing contents inside a directory and is one of the few commands beginners learn from the onset.

What is the difference between ls and ls L? ›

Long Listing Format. The default output of the ls command shows only the names of the files and directories, which is not very informative. The -l ( lowercase L) option tells ls to print files in a long listing format.

What is the difference between ls and ls command in Linux? ›

2 Answers. ls is standing for listing directories and files under a directory. In your situation, ls (without a directory argument) is going to list directories and files under the current directory(pwd). The other command, ls / is going to list files and directories under the root directory which is / .

How do I check permissions on a file in Linux? ›

The ls command along with its -l (for long listing) option will show you metadata about your Linux files, including the permissions set on the file.

Videos

1. Introduction to Linux and Basic Linux Commands for Beginners
(sakitech)
2. 60 Linux Commands you NEED to know (in 10 minutes)
(NetworkChuck)
3. Linux ls command summary with examples
(FactorPad)
4. 20 basic Linux commands for beginners
(PythonPrince)
5. Linux Command Line Basics Tutorials - Ls Command in Linux
(ProgrammingKnowledge)
6. Learn 20 Basic but Important Commands Of Linux Terminal in just 20 minutes
(Ocean of Code)
Top Articles
Latest Posts
Article information

Author: Velia Krajcik

Last Updated: 23/09/2023

Views: 5253

Rating: 4.3 / 5 (54 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Velia Krajcik

Birthday: 1996-07-27

Address: 520 Balistreri Mount, South Armand, OR 60528

Phone: +466880739437

Job: Future Retail Associate

Hobby: Polo, Scouting, Worldbuilding, Cosplaying, Photography, Rowing, Nordic skating

Introduction: My name is Velia Krajcik, I am a handsome, clean, lucky, gleaming, magnificent, proud, glorious person who loves writing and wants to share my knowledge and understanding with you.