Master the dir Command and How to Use It – 12 Examples

dir-command-in-linux

Most Linux and UNIX users run the ls command when they want to view the contents of a directory. But did you know you can use the dir command in Linux to achieve this?

Unfortunately, the dir command is the unpopular version of the ls command. The dir command is specifically designed to display directory contents, unlike the ls command, which has other functionalities. If you’ve just heard about the dir Linux command, we will thoroughly review it to ensure you understand it. Moreover, we’ve presented its basic usage and advanced functionalities. Read on!

What is the Linux dir Command?

The dir command helps list directory contents and only differs from the ls command in how it displays the listing. For instance, with the ls command, your output will be colored. However, the dir command only shows the colors if you specify.

The command follows the below syntax.

The dir command displays the directory contents in columns, and the output is sorted alphabetically.

Here’s the basic way you run the dir command.

dir-command-linux

If we used the ls command, we could run it as follows.

how-to-use-the-ls-command

Notice the difference in output between the two commands.

Related: Linux rm Command with Examples

With this introduction, let’s now cover examples of using the Linux dir command to see its advanced functionalities.

Linux dir Command Examples

The dir command is handy for any Linux user. You can use it to quickly know what is contained in the current working directory or another directory.

1. Basic Usage

When you run the dir command with no option, it will list the directory contents. However, the output won’t be colored.

how-to-use-the-dir-command

You can also specify a target directory by adding its path.

dir-command-add-path

2. dir Command with Colored Output

By default, running the dir command will give uncolored output. However, you can add the –-color option.

Adding the color helps identify the type of files contained in the directory.

dir-command-with-color

3. Display the Output Per Line

With the dir command, you can add the -1 option to show one output per line.

dir-command-show-output-per-line

4. Show Hidden Files and Directories

Like the ls command, the dir Linux command doesn’t include the hidden files and directories in its output. However, you can include them by adding the -a option.

dir-command-show-hidden-files

In the above output, the ‘.’ and ‘..’ represent the current and previous directories. However, you can omit them from the output using the -A instead of -a option.

dir-command-hidden-files

5. How to Long List Using the dir Command

Suppose you want to view detailed information about the contents of your directory. Adding the -l option will show details such as timestamps, owners, etc.

long-list-with-dir-command

6. Only Display Directory Entries

So far, you’ve noticed that the dir command will display all the contained entries. Assuming you are only interested in viewing the contained directories. Add the -d option to filter the output.

We’ve combined it with the -l for long listing for this example.

dir-command-only-show-directories

7. Display the inodes

Every Linux file has an inode number containing information about the file. To view the inode numbers for all the contained entries, add the -i option. We’ve also included the -1 to show the output in one column.

dir-command-display-inodes

8. Show the File Type

You can show the file type by adding the -F option when displaying the directory contents. For directories, they will have a ‘\’ after the name. Soft links will have ‘@’, and executable files will have an ‘*’ after their names.

Notice how the symbols when running the -F option represent what we get when we color the output.

dir-command-show-file-type

9. dir Command Display Contents Recursively

Suppose you want to view the contents of a directory recursively. The –R option will also display the contents of any subdirectories that are contained.

Here’s an example.

dir-command-display-recursively

10. Show Files by Size

How about showing directory contents while displaying the total size they occupy? Add the -s option, and each entry will be displayed with its size on the right.

show-file-sizes-with-dir-command

11. Display Output in Human-Readable Format

The output we get in the previous example isn’t human-friendly. Luckily, we can add the -h option to get more human-readable output and learn the file sizes and format used. For this case, the sizes are in kilobytes.

dir-command-with-human-readable-format

12. View the Help Menu

Like all Linux commands, you can access the dir Linux command help page for more details. Run the below command.

You will get a page showing the different options and their description. With this, you won’t get stuck using the command in Linux.

dir-command-help-page

The help page is large, but you can split it into pages for better readability and navigation. To do so, combine it with the Linux less command.

dir-command-with-the-less-command

Conclusion

The Linux dir command is a reliable way of viewing directory contents. However, the command is unpopular; instead, people are conversant with using the Linux ls command. Hopefully, this post has discussed the dir command in-depth, and you can now use it where you would otherwise use the less command.

Found this helpful? - Share it!

Leave a Comment

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

Scroll to Top