dir command in Linux with Examples

FREE Online Courses: Elevate Skills, Zero Cost. Enroll Now!

In this article, we will learn everything about the dir command in Linux-based operating systems. We will look at what the dir command is, why it is used, how to install it, the syntax of the dir command, and the options used along with it. In the end, we will also look at some fantastic practical examples of the dir command by pairing it with various options to understand its working. So pay attention, and read till the end for the best benefits.

What is linux dir command?

Dir is a command line-based utility in Linux-based operating systems that lists the contents of a directory. Sounds familiar, doesn’t it? Yup, the dir command does precisely what the ls command does.

Even though the ls command is the most loved of the two, the dir command is no less when it comes to features. Despite their similarities, they also have their differences.

The dir command differs from the ls command in the format of listing contents. Unlike the ls command, the dir command lists the files and folders in columns, sorted vertically. In addition, special characters are represented by backslash escape sequences.

What is the syntax of the dir command in linux?

The syntax of the dir command is exactly the same as the syntax of the ls command (obviously excluding the command itself). The syntax of the dir command is shown below.

dir <options> <directory>

Let us now look at the fields present in the syntax of the fir command.

<options>

This field takes in a range of options that specify how the dir command functions, formats, and prints the output. You can also specify multiple options in this field.

<directory>

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

It takes the name of the directory whose contents you want to display. For example, suppose the directory whose contents you want to list is not in your current working directory. In that case, you can specify the directory’s path instead of going there and specifying its name.

Options used with linux dir command

Just like the ls command, the dir command also has a truckload of options used along with it. Let us take a brief look at each one of them before we look at how to use them in the subsequent section.

1. a

This option does not ignore the entries that start with a period (.). This option can also be written as “–all.”

2. –author

It prints the author of each file when used with the option “-l.”

3. -b

This option prints octal escapes for non-graphic characters. This option can also be written as “–escape.”

4. -B

This option does not list the entries that end with a “~”. You can also write this option as “–ignore-backups.”

5. -C

By default, the dir command does not show coloured output. However, you can turn it on by using the option “-C” followed by when you want to use it. In addition, you can either specify “never”, “always”, or “auto”.

6. -d

This option lists directory entries instead of their contents. It also does not dereference symbolic links.

7. -D

This option generates an output that is designed for Emacs’ directory mode.

8. -H

This option follows symbolic links listed on the terminal. You can also write this option as “–dereferenceg-command-line.”

9. –dereference-command-line-symlink-to-dir

This extremely long option follows each command line symbolic link that points to a directory.

10. –hide

This option does not list the entries that match the pattern you specified. You can override this option by using the option “-a.”

11. -l

This option prints the output in a long format.

12. -L

This option shows information for the file the link references rather than for the link itself when showing file information for a symbolic link. You can also write this option as “–dereference.”

13. -m

This option fills the width with a comma-separated list of entries.

14. -q

This option prints a question mark (?) instead of non-graphic characters. You can also write this option as “–hide-control-chars.”

15. –show-control-chars

This option shows non-graphic characters as” is”.

16. -Q

This option encloses the name of entries in double quotes. You can also write this option as “–quote-name.”

17. -r

This option reverses the order of the output while sorting. This option can also be written as “–reverse.”

18. -R

This option lists the continents of the subdirectories of the directory you are listing the contents. This option can also be written as “–recursive.”

20. -x

This option lists the entries in the form of lines instead of columns.

21. -X

This option sorts the entry extensions alphabetically.

22. –help

This option displays the help menu of the dir command, as shown below:

help menu of the dir command

23. -Z

This option displays the security context of SE Linux. You can also write this option as “–context.”

24. –scontext

This option displays only the file name and the security context.

25. –version

This option displays the version of the dir command you are using in your system.

version of the dir command

Since we have covered the theory and fundamentals regarding the dir command, let us look at some fantastic examples of the dir command in the terminal of ubuntu 20.04.

Printing directory contents using the dir command

The most basic way to use the dir command is to execute it without any options or filenames. When you execute the dir command, it will list the contents of the directory you are currently in.

printing directory contents using the dir command

Listing hidden files

Most of the time, the dir command produces sufficient. However, it does not show the hidden files. To show these hidden files, run the dir command by pairing it with the option “-a” as shown:

dir -a

listing hidden files

Not displaying “.” and “..” in the output

If you observe the previous screenshot, you will notice that there are two entries for “.” and “..”. These represent current and previous directories. These two entries are present in every directory. Therefore, if you want the dir command to avoid them while displaying the output, pair it with the option “-A” as shown:

dir -A

not displaying dots in the output

Differentiating between files and directories

When executing the dir command, it is difficult to tell which entries are files and which are directories. However, if you use the option “-F”, the dir command will succeed in directories with a slash (/), and the regular files will have nothing succeeding them.

differentiating between files and directories

Printing detailed information

If we want to show detailed information by enabling long format mode, you can execute the dir command by pairing it with the option “-l”. In long format, the first column shows file permissions. The second displays the number of hard links; the third shows the file owner; the fourth column shows the group name; the fifth column shows the file size; the sixth, seventh, and eighth columns show the modification date; and the last column displays the filename.

printing detailed information

Printing directories first

Sometimes, you might need to print the directories at the beginning followed by the regular files. You can use the option “–group-directories-first” to do so. You can also use the option “-F” to distinguish directories and normal files.

printing directories first

Printing filesizes in human-readable format

When we printed the contents of a directory in a long format, the sizes were in a format we could not understand. Luckily, we can change that by using the option “-h” as shown:

dir -l -h

printing filesizes in human readable format

Displaying the output in reverse order

We can see that the dir command automatically sorts the entire directory alphabetically. However, if you want to reverse this order (sort it from Z-A), you can use the option “-r” as shown:

dir -r

displaying the output in reverse order

Displaying the content of subdirectories recursively

If you want to print even the contents of the subdirectories in the directory you are in; you can use the option “-R” to print the content recursively. Use the command shown below:

dir -R

Sorting with respect to the file size

There may come many scenarios where you have to sort the output based on the sizes of the files and directories. To do so, execute the dir command by pairing it with the option “-S” as shown: “dir -S.”

sorting with respect to the file size

These are only some examples of the dir command. Since you have yet to get the basic ideas and working of the dir command, the rest is for you to explore by using the numerous options of the dir command. In some scenarios, the dir command may even prove superior to the ls command.

Summary

As you have seen, the dir command is a utility that lists the contents of a directory. You have now understood what the dir command is, its uses, syntax, and its various options. We have also seen ten fascinating examples of using the dir command by pairing it with various options.

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

follow dataflair on YouTube

1 Response

  1. tyba suleman says:

    I found your article on the “dir” command in Linux with examples to be incredibly helpful and insightful. Your clear explanations and practical examples showcase your expertise in navigating the Linux file system efficiently. As I was reading, I couldn’t help but think about the potential applications of this knowledge for enhancing website development services. The ability to efficiently manage and navigate directories and files is crucial for web developers working with Linux-based servers. By utilizing the “dir” command and its various options, developers can streamline their workflow and easily locate, organize, and manipulate files for website development projects. Your article provides valuable insights that can greatly benefit web development teams and agencies, helping them optimize their processes and deliver high-quality websites more effectively.

Leave a Reply

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