dir command in Linux with examples
Last Updated :
10 Oct, 2024
The dir command in Linux is used to list the contents of a directory, providing an overview of the files and folders within it.
How is the dir command different from ls?
dir command differs from the ls command in the format of listing contents that is in default listing options. By default, dir command lists the files and folders in columns, sorted vertically and special characters are represented by backslash escape sequences. But unlike ls, when the output is on the terminal, it does not produce colored output as ls does.
Syntax
dir [OPTION] [FILE]
where,
- OPTION: Various flags can be used to modify the output, such as -a for showing hidden files or -l for a long listing format.
- FILE: The directory or file to list. If not specified, the current directory is used.
Commonly Used Options with the dir Command
1. Display All Files Including Hidden Files (-a or –all option):
It displays all the hidden files(starting with `.`) along with two files denoted by `.` and `..` which signals for current and previous directory respectively.
dir -a
Example:

2. Show Almost All Files (-A or –almost-all option):
It is similar to -a option except that it does not display files that signals the current directory and previous directory.
dir -A
Example:

3. Display Author Information (-l –author option):
Displays author of all the files. -l is required to display the contents in the form of a list.
dir -l --author
Example:

4. Ignore Backup Files (-B or –ignore-backups option):
Ignores listing of backed up files. These files end with a `~`.
dir -B
5. Enable Colored Output (–color option):
It is used to colorize the output (can be followed by =[TIME]). In the absence of time specification which can be auto, never or always, it colorizes the output always (default behavior).
dir --color
Example:

6. Classify File Types (-F, –classify option):
Append indicator (one of */=>@|) to the file names which classifies them into their type. The meanings of symbols are as follows:
- A slash (`/`) indicates a directory.
- An asterisk (`*`) indicates an executable.
- An at sign (`@`) indicates a symbolic link.
- A percent sign (`%`) indicates a whiteout.
- An equal sign (`=`) indicates a socket.
- A vertical bar (`|`) indicates a FIFO.
dir -F
Example:

7. Show File Types Without Executable Indicator (–file-type option):
It is same as -F option, except that it does not append `*` to the executables.
dir --file-type
Example:

8. Format Output (–format=WORD option):
It formats the listing of entries. The WORD can take the following values: across, commas, horizontal, long, single-column, verbose, vertical. The same can be achieved by passing -x, -m, -x, -l, -1, -l, -C options to dir command for each of the respective values.
dir --format=WORD
Example 1:

Example 2:

9. Ignore Files Matching a Pattern (–hide=PATTERN or –ignore=PATTERN option):
It ignores files described by shell PATTERN while listing the contents of a directory.
dir --hide=PATTERN
Example:

10. Display Numeric User and Group IDs (-n, –numeric-uid-gid option):
This option is similar to the long listing that is -l option except that it lists numeric user and group IDs.
dir -n
11. List Files in Reverse Order (-r, –reverse option):
Use this option to list the files in reverse order based on the sorting criteria.
dir -r
Example:

12. List Subdirectories Recursively (-R, –recursive option):
The -R option lists all subdirectories recursively, making it useful for exploring directory structures.
dir -R
Example:

13. Sort Files by a Specified Parameter (–sort=PARAMETER):
To list files in a sorted manner described by the PARAMETER. The PARAMETER can take the following values: none (-U), size (-S), time (-t), version (-v), extension (-X). Instead of passing –sort option, the flags indicated in the brackets can directly be passed as options to sort the listing.
dir --sort=PARAMETER
Example 1:

Example 2:

14. Display Help Information (–help option):
Use the –help option to display a list of all available options and exit.
dir --help
Example:

15. Show Version Information (–version option):
The –version option displays the version of the dir command.
dir --version

Conclusion
The dir command in Linux is a versatile and valuable tool for listing directory contents. It offers various options for displaying file information, sorting, and filtering results, making it a useful alternative to the ls command. By mastering the dir command, you can effectively navigate and manage your Linux filesystem.
dir command in Linux – FAQs
What is the dir command used for?
The dir command is used to list the contents of a directory, similar to the ls command, but with different default formatting.
How does the dir command differ from ls?
The dir command lists contents in columns without colored output, while ls provides colored output by default. dir also uses different options for formatting and sorting.
Can I list files in a specific order with the dir command?
Yes, you can use the –sort option to sort files by various parameters, such as size, time, or extension.
How can I exclude certain files from the dir output?
Use the –ignore or –hide option with a pattern to exclude files matching that pattern.
Similar Reads
cupsd command in Linux with examples
cupsd is a type of scheduler for CUPS (Common Unit Printing System). It implements the printing system on the basis of the Internet Printing Protocol(Version 2.1). If no options is being specified on the command-line then the default configuration file /etc/cups/cupsd.conf will be automatically be u
3 min read
curl Command in Linux with Examples
In the realm of Linux, mastering the command line is essential for efficient and powerful usage of the operating system. Among the number of command line tools available, `curl` stands out as one of the most versatile and powerful utilities. Originally developed by Daniel Stenberg, `curl` is a comma
6 min read
cut command in Linux with examples
The cut command in linux is a command for cutting out the sections from each line of files and writing the result to standard output. It can be used to cut parts of a line by byte position, character, and field. The cut command slices a line and extracts the text. It is necessary to specify an optio
9 min read
cvs command in Linux with Examples
In today's digital era, where file modifications and version control are essential, the Concurrent Versions System (CVS) command in Linux emerges as a powerful tool. CVS allows users to store and track the history of files, enabling easy retrieval of previous versions and restoring corrupted files.
6 min read
How to Display and Set Date and Time in Linux | date Command
The date command in Linux is one of the most basic and powerful tools that enable users to display the current date and time, as well as set the date and time on a Linux system. The date command in Linux can be used for the following: Synchronizing System TimeCustomizing Date FormatsData manipulatio
8 min read
dc command in Linux with examples
The dc command is a versatile calculator found in Linux systems, operating using reverse Polish notation (RPN). This command allows users to perform arithmetic calculations and manipulate a stack, making it ideal for complex mathematical tasks directly from the command line. SyntaxThe basic syntax f
3 min read
'dd' command in Linux
dd is a command-line utility for Unix and Unix-like operating systems whose primary purpose is to convert and copy files. On Unix, device drivers for hardware (such as hard disk drives) and special device files (such as /dev/zero and /dev/random) appear in the file system just like normal files.dd c
5 min read
declare command in Linux with Examples
The built-in is a powerful built-in feature of the Bash shell. It allows users to declare and set attributes for variables and functions, enabling better control over their behavior. By understanding how to use declare, you can manage variables and functions more effectively in your shell scripts. T
2 min read
depmod command in Linux with examples
depmod(Dependency Modules) command is used to generate a list of dependency description of kernel modules and its associated map files. This analyzes the kernel modules in the directory /lib/modules/kernel-release and creates a "Makefile"-like dependency file named modules.dep based on the symbols p
7 min read
df Command in Linux with examples
There might come a situation while using Linux when you want to know the amount of space consumed by a particular file system on your LINUX system or how much space is available on a particular file system. LINUX being command friendly provides a command line utility for this i.e. 'df' command that
8 min read