Beginners Guide for Dir Command on Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

The ls command is famous for showing the content of the current working directory and has become so popular that it outranks other commands like the dir command.

The dir command is rarely used today except for shell script; apart from that, both commands have no differences and provide no extra features.

In this article, you will learn how to use the β€œdir” command in the most practical way.

Tutorial Details

DescriptionDir
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitesdir
Internet RequiredNo

Syntax of the Dir Command

The dir command takes two arguments: one is the option, and the other is the file name or directory path.

$ dir [OPTION] [FILE NAME/DIRECTORY PATH]

Listing the Content of the Directory

When you execute the command without specifying any options or locations, it will list the content of the current working directory.

$ dir

Output:

Listing content using dir

Displaying the Output in Color

The dir command does not display the output in color by default; use the β€œ--color” flag to display the color in the output.

$ dir --color

Output:

Listing the content in color

Long Listing Format

Use the β€œ-l” flag to list each file and directory in newlines with extra information like permissions, ownership, size, etc.

$ dir -l

Output:

Listing each file or directory on a new line with extra information

Use the β€œ-h” or β€œ--human-readable” flag to list the file size in human-readable format.

$ dir -lh

Output:

Show entries' sizes in human-readable format

The β€œ-l” with the β€œ--author” flag will display the author of all the files (β€œ-l” is required).

$ dir -l --author

Output:

Listing files with author name

Listing All Files, Including Hidden Files

The hidden files are not listed without any flags (identical to those in the ls command) in the dir command. Use the β€œ-a” or β€œ--all” flag to list all the files and directories, including the hidden.

$ dir -a

Output:

Listing all files and directories, including hidden ones

Use the β€œ-al” flag to list all the files and directories in long listing format.

$ dir -al

Output:

Listing all content of the directory in long listing format

As you can see in the above picture, there are two more entries: β€œ.” (current directory) and β€œ..” (parent directory), also known as pseudo-files.

If you don’t want to see them in the output, use the β€œ-A” flag to ignore them.

$ dir -A

#OR

$ dir -Al

Output:

Listing all content except for special files

Display File Type Indicators

The β€œ-F” flag will append an indicator after each entry based on the file type to help you distinguish between the files and directories.

$ dir -F

Output:

Appending an indicator after each entry

The following indicator will be used based on file type:

  • β€œ*” – Executable files
  • β€œ/” – Directories
  • β€œ@” – Soft links
  • β€œ|” – FIFO (named pipes)
  • β€œ%” – Whiteout
  • β€œ=” – Socket

Note that you can also use the β€œ--file-type” flag to list all the files except β€œ*” for executable files.

Ignoring the Entries Matching the Pattern

The β€œ--ignore” flag can be used to ignore certain entries based on their file type in the output. For example, the following command will list all the files and directories except those ending with β€œ*.txt” and β€œ*.sh” extensions.

$ dir --ignore="*.txt" --ignore="*.sh"

Output:

Ignoring entries based on their file type

Note that there is an alternative β€œ--hide” flag that behaves similarly to the β€œ--ignore” flag, except the β€œ-a” or β€œ-A” flag will override the result for the β€œ--hide” option.

Ignoring the Backed-up Files (entries ending with β€œ~”)

The β€œ-B” or β€œ--ignore-backups” flag will hide all the backed up files from the output (basically the files ending with the β€œ~” tilde symbol).

$ dir -B

Output:

Hiding the backed up files from output

Listing the Entries with UID and GID

The β€œ-n” flag is identical to the β€œ-l” flag except it will list all the files and directories with their numeric user ID and group ID.

$ dir -n

Output:

Listing entries with their UID and GID

Display the Inode Number of Each Entry

The β€œ-i” or β€œ--inode” flag will display the inode number for each entry on the output.

$ dir -i

Output:

List the entry with their inode number

Listing Files in Reverse Order

Use the β€œ-r” flag to reverse the entries’ order from descending to ascending while sorting.

$ dir -r

Output:

Reverse the entries order while sorting

Exit Status

The dir command has three exit statuses:

  • 0: Okay
  • 1: Minor problems (ex: cannot access a sub-directory)
  • 2: Serious problems (ex: specified option or path is invalid)

That was all about this command.

If you have questions or confusion related to this command, let us know in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.