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
Description | Dir |
Difficulty Level | Low |
Root or Sudo Privileges | No |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | dir |
Internet Required | No |
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:
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:
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:
Use the “-h
” or “--human-readable
” flag to list the file size in human-readable format.
$ dir -lh
Output:
The “-l
” with the “--author
” flag will display the author of all the files (“-l
” is required).
$ dir -l --author
Output:
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:
Use the “-al
” flag to list all the files and directories in long listing format.
$ dir -al
Output:
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:
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:
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:
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:
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:
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:
Listing Files in Reverse Order
Use the “-r
” flag to reverse the entries’ order from descending to ascending while sorting.
$ dir -r
Output:
Exit Status
The dir command has three exit statuses:
0
: Okay1
: 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.