What Will We Talk About?
In this guide, we will see how to use the dir command to list the information about a given directory and learn about the different options to sort the items inside it.
Syntax of the Dir Command
The dir command has the following standard syntax:
Use of the Dir Command with Examples
The dir command is most often used to show a directory’s contents in standard alphabetical order.
By default, the dir command lists the files inside the current working directory if no specific directory is specified.
How to List the Files and Directories in a Given Path
If you want a complete list of the contents of a directory, just add the directory’s path to the end of the command. For example, the contents of the home directory of the user LinuxHint directory are detailed as follows:
If you only want to display one file per line, use the -1 parameter as shown in the following:
How to List the Hidden Files and Directories
The -a option makes the directory listing include the hidden files (.) in addition to the regular file names. Additionally, with the -l option, the output is presented in a list format:
$ dir -al
How to List the Directory Entries
The -d option is used to show just the directory entries rather than the contents of a directory. The user home directory’s contents are shown in the following output with the -d option.
The directory’s owner, group owner, and permissions are all shown in a detailed listing when -dl is used.
$ dir -d -l /home/linuxhint
How to List the Index Number of Files
The index node is referred to by the “inode” acronym. An inode is a unique integer that is assigned to every file and directory in a filesystem.
A filesystem’s total inode count is set when it is created and can’t be modified dynamically. Consequently, it’s crucial to often verify the inode usage to make sure it complies with the set limitations.
We can get the index/inode number of each file using the -i option:
How to Sort the Files Based on Size
We can see the size of individual files using the -s option. Furthermore, we can also view the file sizes in a more intuitive manner using the -h option:
As we can see from the output, the file sizes are in Kilobytes (see the first column). Also, to sort the files by size, we can simply add the -S option:
How to Sort the Files Based on Modification Time
Interestingly, we can also order our files according to when they were last modified, with the most recently updated ones appearing at the top. The -t switch comes in handy here:
How to List the Files without Owners
You can use the -g switch like -l to list the files but without their owners. Also, the -G switch can be used to list the files without printing the group names.
It is clear from the preceding result that neither the file owner nor the group owner is displayed. Additionally, the –author option displays the file’s author:
Also, we must use the -l option here to see the author’s name.
How to List the Directories Before Any File
If you want to list the directories prior to any other files, use the –group-directories-first argument:
You can see that the directories are shown before the regular files. Also, for directories, the “d” prefix is used, whereas “-“ is used for regular files. Using the -R option, you can list the subdirectories in a recursive manner:
How to List the Files with the User and Group IDs
You may use the -n option in order to see the user and group IDs. Let’s look at how the output differs with and without -n:
Without the -n option:
With the -n option:
How to List the Files Separated by Commas
The -m option allows you to list the files separated by commas.
Use the –help option to get more information about dir, and the –version flag to see dir’s version information.
$ dir –version
Conclusion
These are just a few instances of how the dir command can be used. There are a wide variety of additional options available. To learn more about the dir command, see dir documentation from the man pages.