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.