Linux Commands

The Linux Equivalent of the Windows DIR Command and How to Use It

In order to see the details about the files in a directory, you can use the dir command. The default behavior of the dir command is to list the information of the current directory. If neither -cftuvSUX nor –sort options are used, the items are sorted alphabetically.

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:

$ dir <OPTION>... <FILE>...

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.

$ dir

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:

$ dir /home/linuxhint

If you only want to display one file per line, use the -1 parameter as shown in the following:

$ dir -1 /home/linuxhint

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 -a

$ 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 /home/linuxhint

$ 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:

$ dir -i

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:

$ dir -shl

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:

$ dir -ashlS

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:

$ dir -ashlt

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.

$ dir -ahgG /home/linuxhint

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:

$ dir -al --author /home/linuxhint

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:

$ dir -l --group-directories-first

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:

$ dir -R -l --group-directories-first

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:

$ dir -l --author

With the -n option:

$ dir -nl --author

How to List the Files Separated by Commas

The -m option allows you to list the files separated by commas.

$ dir -am

Use the –help option to get more information about dir, and the –version flag to see dir’s version information.

$ dir --help

$ 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.

About the author

Ali Imran Nagori

Ali imran is a technical writer and Linux enthusiast who loves to write about Linux system administration and related technologies. You can connect with him on LinkedIn
.