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.
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
curl is a command-line utility for transferring data to or from a server, employing a range of internet protocols such as HTTP, HTTPS, FTP, SCP, and SFTP. Whether you want to download a file, test a REST API, or simply verify that a website is up and running, curl is your best friend. It is accessed
5 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
Unlock the full potential of the date command in Linuxâa versatile tool that does more than just show the current date and time. With this command, you can set your systemâs clock, synchronize time across networks, and even calculate past or future dates for tasks like scheduling or logging. In this
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: Explained
The dd command in Linux is a powerful utility for low-level data copying and conversion, primarily used for disk cloning, creating disk images, partition backups, and writing ISO files to USB drives. Mastering the dd command is essential for Linux system administrators, as it enables precise control
6 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
9 min read