At any point in time while working on the command line, you are always inside a directory. There may be times when you'd want to list information about all files and subdirectories within a directory. This is where the dir command helps. In this tutorial, we will discuss the basics of this command as well as the features it provides.
All examples mentioned in this tutorial have been tested on Ubuntu 16.04LTS.
Linux dir command
Following is the generic syntax of this command:
dir [OPTION]... [FILE]...
The tool's man page describes it as:
List information about the FILEs (the current directory by default). Sort entries alphabetically
if none of -cftuvSUX nor --sort is specified.
The following Q&A-style examples should give you a better idea on how this tool works.
Q1. How to list directory contents using dir command?
The basic usage of the dir command is very simple, all you have to do is to execute the tool in the following way:
The output produced is alphabetically-sorted. Here's an example:
Q2. How to list hidden files?
When you run the dir command in its most basic form (as described in the previous section), the output produced may be sufficient in most cases, but it's not complete. What I mean to say is, hidden files don't get displayed by default.
In case you want to see them as well, use the -a command line option.
Here's an example:
So as you can see, files with names beginning with a dot (.) are also displayed in the output.
Q3. How to make dir not display . and .. in output?
As you can observe in the example screenshot provided in the previous section, there are entries for . and .. , which represent current and previous directories. These two entries are present in every directory, so you might want dir to avoid them while displaying output.
This can be done using the -A command line option:
Here's the above command in action:
So you can observe that . and .. were not displayed in output this time around.
Q4. How to differentiate between files and directories in dir output?
As you would have noticed, in all the examples we've discussed so far, it's difficult to differentiate between files and directories in the output. If this bothers you as well, you can use the -F command line option.
Here's an example:
The forward slash (/) at the end of a name suggests it's a directory.
Q5. How to make dir print detailed information?
Up until now, we've seen dir printing only names of files and directories in output. However, if you want, the tool can also display detailed information for each entry. This feature - also known as long list format - can be accessed using the -l command line option.
Here's an example of this option:
Column wise, the output shows the following information for each entry: access permissions, number of hardlinks, file owner, file group, file size, modification time, and filename.
Q6. How to make dir display directories first?
There might be situations where-in you'd want dir to display directories first, followed by files. The tool is capable of doing this - you'll have to use the --group-directories-first command line option.
Here is a screenshot showing the aforementioned command in action:
Note that we've used the -F command line option to make sure that directories and files are clearly distinguishable in output.
Q7. How to make dir print sizes in human-readable form?
In the Q5 above, we saw how we can use the -l command line option with dir to get detailed information about directory contents. However, the size figure that gets displayed in that case isn't exactly in human-readable form.
If you want, you can have this information displayed with easy to understand size symbols like 'K', 'M', and 'G' (kilobytes, megabytes, and gigabytes). For this, you need to run the dir command with -h option.
The following screenshot shows the same command that was executed in Q5, but with -h option added.
As you can see, the file sizes in output are now produced in human-readable format.
Q8. How to make dir display reverse sorted output?
As we already know, the dir command sorts the output entries alphabetically. However, in case you want the output to be reverse sorted, you can use the -r command line option.
The following example shows the dir command output when the tool was executed without and then with this option:
Q9. How to make dir display sub-directories recursively?
As you'd have observed by now, the dir command doesn't display contents of subdirectories by default. However, if you want, you can enable this feature using the -R command line option.
Following is an example:
Q10. How to make dir sort output by size of file?
If you want, you can also ask dir to sort output by file sizes. For this use the -S command line option.
Following screenshot makes the functionality clear:
So you can see that the output was sorted by size, with file having largest size appearing first.
We've just scratched the surface here as the dir command offers a plethora of features. Nonetheless, the examples we've discussed in this tutorial should be enough to give you head-start. When you're done trying these out on your machine, head to the tool's man page for more information.