dir Command in Linux



The dir command in Linux lists the content of a directory and sorts the entries alphabetically. It essentially lists the content of the mentioned directory in the form of columns in a simple format. Moreover, it is quite similar to the Windows dir command, which makes it easier to use for both Windows and Linux users.

Table of Contents

Syntax for the dir Command

The general syntax for using the Linux dir command is as follows −

dir [options] [directory]

In the syntax, the [options] field takes the options that modify the dir command output. While the [directory] field takes the directory name or path.

Options for the dir Command

The options for the dir command are listed below −

Flags Options Description
-a --all To list all entries including .
-A --almost-all It ignores the . and .. entries
--author It displays the owner of each file (used with -l)
--block-size=SIZE It displays file sizes in specific units, e.g. K, M, G (used with -l)
C It lists the entries in column format
--color=WHEN It colors the output (WHEN = always, auto, and never)
-F --classify=WHEN It appends indicators (*/=>@|) to the entries (WHEN = always, auto, and never)
--group-directories-first It prints the directories first and then files
-h --human-readable It prints entry sizes like 5K, 300M (used with -l and -s)
--hide=PATTERN It ignores entries that matches the PATTERN
-i --inode It prints the index number of the files
-l It prints entries in long list format
-m It prints entries separated by commas
-p --indicator-style=slash It appends / to the directories
-q --hide-control-chars It replaces the non-graphic elements with ?
-Q --quote-name It places the entries in quotations
-r --reverse It prints the entries in the reverse order
-R --recursive It lists the subdirectories recursively
-S It sorts by file sizes (large first)
--sort=WORD It sorts the entries by WORD such as time, size, extension, version, and width
-t --time It sorts entries by time (newest first)
-U It ignores the sorting (list in directory order)
-X It sorts entries alphabetically by entry extension
-Z --context It prints any security context
--zero It prints entries separated by NULL
-1 It prints each entry per line
--help It displays help about the command

Using the dir Command in Linux

This section will demonstrate the dir command usage in Linux using various examples −

Listing Contents of a Directory

To list the current directory contents, execute the dir command without options −

dir
Listing Contents of a Directory 1

The contents will be displayed alphabetically.

To display the contents of a specific directory, mention the directory with the command −

dir directory
Listing Contents of a Directory 2

Listing Hidden Contents of a Directory

To list the hidden contents of a directory, the -a option is used −

dir -a
Listing Hidden Contents of a Directory 1

The filenames that start with a dot are hidden files. Moreover, in the output, there are two more entries, "." and "..", they represent current and previous directories respectively. To remove them from the output, use "-A".

dir –A
Listing Hidden Contents of a Directory 2

Note that "." and ".." are present in every directory.

Differentiating the Directory Contents

By default, the dir command lists the content of a directory without specifying which entry is a directory, file, symbolic link, or socket.

dir -F
Differentiating the Directory Contents

The following symbols will be affixed with the corresponding entries.

  • / Directory
  • Executable File
  • @ Symbolic Links
  • | FIFO (named pipe)
  • = Socket

Highlighting the Output

The default dir command output is simple without showing apparent differences between files and directories. To highlight the entries, colors can be enabled using the --color option.

dir --color
Highlighting the Output

Listing Contents with Detailed Information

To display the contents of a directory in a longer format, use the -l flag. It displays additional permissions, owner, size, and modification date details.

dir -l
Listing Contents with Detailed Information

Listing Contents in Human Readable Format

Detailed information about the contents of a directory can be obtained using the -l flag. However, to present that information in human-readable format the -h flag is used. The example is given below −

dir -l -h
Listing Contents in Human Readable Format

Listing Contents in Reverse Order

The order of content listing can be reversed using the -r flag.

dir -r
Listing Contents in Reverse Order

The output shows that the entries are listed in reverse alphabetical order.

Hiding Contents

The entries of specific patterns can also be hidden from the output. For instance, to hide all the text files, use −

dir --hide="*.txt"
Hiding Contents

Highlighting the Directories

To specifically differentiate directories and files, the -p flag or --indicator-style option is used. It appends a / after the entry name if it is a directory.

dir -p 
Highlighting the Directories

Listing Contents Separated by Commas

To list the contents of a directory separated by commas use -m

dir -m
Listing Contents Separated by Commas

This format is easy to interpret in Bash.

Listing the Subdirectories

To list the subdirectories recursively use the -R flag. For example, to list the subdirectories of a directory, run −

dir -R directory
Listing the Subdirectories

Listing Inodes

In Linux, the inodes are data structures that represent file system elements such as files or directories. To list content with their respective inodes, use the -i flag −

dir -i
Listing Inodes

Listing Contents by Creation Time

To list the entries by the time they were created, the -t flag is used −

dir -l -t
Listing Contents by Creation Time

The newly created content comes first.

Listing Directories First

To list the directories before files, --group-directories-first option is used −

dir --group-directories-first
Listing Directories First

Listing Contents by File Size

To display the contents by their sizes, the -S flag is used −

dir -l -h -S
Listing Contents by File Size

The -l and -h flags are used to display the content in human-readable format. While -S is sorting the content by size with large coming first.

How "dir" is Different from "ls" Command?

The dir command is quite similar to the ls command. But ls provides more detailed output. Moreover, when it comes to features, ls holds more output customization options. On the other hand, the dir command provides simple outputs with less focus on color highlights.

Both utilities primarily perform the same functionalities but ls is considered a much more powerful tool compared to dir.

Conclusion

The dir command in Linux is used to list the contents of a directory. It comes with different sorting and listing format options. The dir command is quite similar to the ls utility. But both differ when it comes to displaying content.

By default, the dir lists contents in a simple format while ls highlights the directories, files, and executables by color. Note that the dir command is also capable of displaying contents in a way similar to the ls utility by using various flags and options.

Advertisements