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.
$ dir [option] [file]
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
If we used the ls command, we could run it as follows.
$ ls
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.
You can also specify a target directory by adding its path.
$ dir /var/log/
2. dir Command with Colored Output
By default, running the dir command will give uncolored output. However, you can add the –-color option.
$ dir --color
Adding the color helps identify the type of files contained in the directory.
3. Display the Output Per Line
With the dir command, you can add the -1 option to show one output per line.
$ dir -1
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 -a
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.
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.
$ dir -l Desktop/
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 -ld Desktop/
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 -i1 Desktop/
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.
$ dir -F
Notice how the symbols when running the -F option represent what we get when we color the output.
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 -R
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.
$ dir -s
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 -sh
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.
$ dir --help
You will get a page showing the different options and their description. With this, you won’t get stuck using the command in Linux.
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 –help | less
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.