ls Command in Linux to List File & Directory

Linux Topic

January 28, 2023 by Ashley

ls Command in Linux to List File & Directory

ls Command is a simple command used to display a list of files and sub-directories in Linux. Whether you are a Linux system administrator or a regular user, the ls command is one of the first ones you will use to see the content of a directory and the files in it.

In this article, you will learn how to use ls Command in Linux to List File & Directory. Also, all other required parameters and important flags will be explained since they are used alongside the ls command. Using practical examples, you will learn the most common ls options till the end of this guide.

Prerequisites to Use ls Command in Linux to List File & Directory

To let this tutorial work correctly, consider the below requirements and move on.

  • A system running Linux with your preferred distro installed.
  • Basic familiarity with navigating around the CLI.
  • A system with directories and files.
  • Listening to the Rigoletto Opera while OperaVPS is teaching you.

Tutorial ls Command in Linux with Examples

As a Linux/Unix administrator or user, you will use the ls command in daily operations. Even it is one of the commands that beginners would learn. One of the many Linux commands is the ls command which helps users to list files or directories from the Command Line Interface (CLI) and view detailed information.

Certainly, as a Linux user, you have learned all comprehensive Linux Commands. Join us with this article to get more familiar with the ls command and its important role in listing contents inside a directory in Linux.

As we mentioned, you will review all the options that the ls command uses to list files and directories. But, firstly, let’s see what you will see by running the ls command without any options or arguments. The following command will list files in your current working directory.

$ ls

Using the above command displays lists and file directories. But it does not show file type, modified data, permissions, and time to mention.

26 Important Commands to List Files in a Directory

Previously, you learned how to create directory in Linux using mkdir command. It is time to learn how to list files in a directory using ls command and its options.

Generally, the ls command has the following syntax:

ls [OPTIONS] [FILES]

Stay with us to review all vital Linux ls command examples.

1. Display comprehensive file information

When you run the following command, the current directory’s contents are shown one per line in a long listing format. The file or directory permission, owner and group names, file size, created/modified date and time, and file/folder name are some of the attributes that are listed at the beginning of the line.

ls -l

2. List the files of another directory

You can use the ls command as shown below to list the contents of another directory:

ls [directory path here]

3. List files in reverse order

Adding the -r flag will list files in reverse order, as demonstrated.

$ ls -r

4. View hidden files and directories

To view hidden files and directories in the current directory, use the ls command’s -a option.

ls -a

In this way, the files that start with the (.) will be hidden. Also, you can use the following commands to list files or directories in a table format with extra information including hidden files or directories:

ls -al
ls -la
ls -a -l
ls -l -a

In this way, you can view the list of the files in the long format including hidden files.

5. List files in the root directory

Run the command below when you need to list the content of the root directory:

ls /

6. Display files in a readable form

You may have observed that the file and folder sizes displayed are not immediately clear and understandable. So, you can add the -lh parameter as indicated to quickly identify the file sizes as kilobytes (KB), megabytes (MB), or (GB).

$ ls -lh

In this way, you can use this command to display the file sizes in human-readable format rather than bytes and also the whole details of the file or directory.

7. List files in a particular directory

To list files in a specific directory, for example, /etc directory, run:

ls /etc

Also, you can type other directory names continuously after /etcto view multiple directories and files.

8. Classify the files with specific characters

Using the -F argument, the ls command groups the files into categories. It denotes directories with slash (/) endings. Symbolic links with a trailing at-the-rate sign (@), FIFOs with a trailing vertical bar (|), and regular files with no content are all examples of files that can be run.

9. List files in the parent directory

Use the following command to list the contents of the parent directory one level above.

ls ..

Also, to list the contents of the parent directory two levels above, type:

ls ../..

10. List files recursively using ls command

Run the command below to view the directory tree of files and folders.

ls -R

Using the above command helps you to list all files and directories with their corresponding subdirectories down to the last file.

11. Display file Index number

You might need to know a file’s index number for internal use. Using the index number, you can delete files whose names contain special characters. To do this, type:

ls -i

12. List files in the user’s home directory

To list the contents in the user’s home directory, (/home/user), run:

ls ~

13. List files and directories to distinguish files from folders

Use the command below to let that folder appear with a forward slash character ‘/’ at the end.

$ ls -F

14. Display the last edited file

As the file is sorted by modification time, the most recently updated file is shown first. To access the most recently modified file in the current directory, use the ls and head commands.

ls -t

ls -t | head -1

Move on to explore more ls Command List File and Directory in Linux.

15. How to list only directories

To list only directories, type:

ls -d */

16. View the inode number of files and directories

Run the following command to display the inode number of files and directories:

$ ls -i

17. View Reverse Output Order by Date

The t parameter sorts all the files and directories according to the modification time and lists the more recent ones first in the command above, while the r option reverses the sorting order.

ls -ltr

In order to list all the directories and file names, the above command reverse-sorts the modified date.

18. How to list files with subdirectories

Use the following command to list the contents of the directory with its subdirectories:

ls *

19. Display the UID and GID of files and directories

To display the UID (User identifier) and the GID (Group identifier) of files and directories, use the command below:

$ ls -n

The root UID and GID have zero while a typical user and group (UID and GID) have 1000.

20. Display files and directories separated by a comma

Run the following command to list the files and directories separated by a comma:

ls -m

21. How to list files with their actual sizes

To list files or directories with their sizes, type:

ls -s

Keep in mind that the s must be typed in lowercase

22. List all the files and directories without the owner’s information

Similar to the “ls -l” option, the “ls -g” option omits the file and directory owner detail.

ls -g

23. Create an alias for the ls command

Define ls command in aliases, type something like:

$ alias="ls -l"

This instructs the computer to run the ls -l command rather than the ls command. Keep in mind that using the ls command again will result in the same output as executing the ls -l command.

24. View ls command version

To check the version of the ls command, run:

ls --version

25. ls command output coloring

You may wish to colorize your output to make it easier to identify between files, directories, and other attributes like file and directory permissions in order to add some flair to the output display dependent on the sorts of files.

ls --color

26. Display ls command Help page

To explore other options and what the ls command may accomplish, run:

ls --help

When the user does not have read permission to the directory you will face this message. Take care of it.

Conclusion

In this article, you learned How to ls Command in Linux to List File & Directory. Several of the ls command parameters are listed above with examples. One of the easiest commands for Linux is the ls command. There are some other commands to list out files and directories. However, you can combine multiple commands together at once to meet your needs. I guess you will feel more at ease utilizing the ls command in daily operations.

If you encounter any problems, please do not hesitate to contact us. Our technical support team will try their best to solve your problems.

Helpful

​ ​ ​ ​

About the Author Ashley

Leave a Reply

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