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.
|Root or Sudo Privileges||No|
|Host System and Architecture||Ubuntu 22.10 (x64)|
|OS Compatibility||Ubuntu, Manjaro, Fedora, etc.|
|Discussed Tools in this Article|
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.
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
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
Use the “
-h” or “
--human-readable” flag to list the file size in human-readable format.
$ dir -lh
-l” with the “
--author” flag will display the author of all the files (“
-l” is required).
$ dir -l --author
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
Use the “
-al” flag to list all the files and directories in long listing format.
$ dir -al
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
Display File Type Indicators
-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
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
--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"
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 “
Ignoring the Backed-up Files (entries ending with “~”)
-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
Listing the Entries with UID and GID
-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
Display the Inode Number of Each Entry
-i” or “
--inode” flag will display the inode number for each entry on the output.
$ dir -i
Listing Files in Reverse Order
Use the “
-r” flag to reverse the entries’ order from descending to ascending while sorting.
$ dir -r
Also Read: What is Exit Status Code of Last Command in Linux
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.