Skip to main content
Commands

The Lesser Known Dir Command in Linux

Learn about the dir command, the less known but identical cousin of the popular ls command.

LHB Community

Warp Terminal

How do you see the contents of a folder in the Linux terminal? You use the ls command.

In fact, the ls command is so popular that many Linux users don't even know about dir.

Yes, there exists a dir command with the sole purpose of showing you the directory contents. And in this tutorial, I'll show you how to use it.

Using dir command to list directory contents

The basic syntax for the dir command is as follows:

dir [options] [Directory] [Files]

Where:

  • options: These are optional parameters that can be used to control the output of the dir command.
  • Directory: This is the directory whose contents need to be listed.
  • Files: This is a list of specific files that need to be listed.

The most basic use of the dir command is to list the contents of the current directory.

To do this, simply type dir without any options and press enter. If no directory is specified, the dir command will list the contents of the current working directory.

dir

The default behavior is to sort the output in alphabetical order.

Default dir Command

Displaying Color Output

Unlike the ls command, the dir command doesn’t display output in colors by default. If you want, you can enable color output for the dir command by using the --color option.

dir --color

This option is useful when you want to identify certain files or folders based on their color quickly.

dir Command with Colored Output

There are more colors available that are used to differentiate between different file types.

  • Green: Regular files
  • Blue: Directories
  • Cyan: Symbolic links
  • Red: Broken links
  • Yellow: Device files

Using Long Listing Format

You can use the -l option with the dir command to display output in a long listing format.

This displays information like permissions, ownership, timestamps, etc.

dir -l
Long Listing Format

The dir command also supports other listing format options:

  • -1: list entries in a single column
  • -C: list entries in columns
  • -m: list entries in a comma-separated format
Display Entries as Comma Separated

The -m option is especially useful when you want to quickly generate a list of files that other programs or scripts can use. Machine learning is an example of where this might be useful.

Listing Hidden Files

The files beginning with a dot (.) are hidden files in Linux and they are not displayed by default.

Similar to ls, you use the option -a (short for all) to list all the files including hidden ones:

dir -a
Display Hidden Files

In the output above, you might notice that there are two special entries - . (current directory) and .. (parent directory). These are called pseudo-files.

If you don’t want to see these special entries in the output, you can use the -A option (short for almost-all).

dir -A
Listing Without Special Entries

Displaying File Type Indicators

The -F option, short for “classify”, appends a character after each entry to indicate the type of file. This information can be very useful, especially when you want to identify certain types of files quickly.

dir -F
File Type Indicators

As you can see in the screenshot above, the -F option appends a ‘/’ character after each directory. Other indicators are

  • - \ for directories
  • - @ for soft links
  • - * for executable files
  • - | for FIFO (named pipes)

There is a similar option called --file-type that does the same except showing * for executable files.

dir --file-type
Displaying Extensions of Files

I like to use it for ignoring files of certain types from the display.

As you can see, all the files that start with “temp” and all the files that end with “.txt” are ignored.

Ignoring Specified Entries

Displaying Output in Human Readable Format

The dir command also supports the -h option, short for “human-readable”.

If a file is of size 1024 bytes, it will be displayed as 1K. Similarly, if a file is of size 1048576 bytes, it will be displayed as 1M. This option uses 1000 as the base for file sizes, not 1024.

This can come in handy when you want to see the file sizes quickly.

You should use the -l option along with -h.

dir -h -l
Listing in Human Readable Form

Displaying Files by Size

The dir command also supports the -S option to sort files by size. This can come in handy when you want to see which files are taking up the most space.

dir -S -l

As you can see in the screenshot below, the most space-consuming files are displayed on top.

Displaying Files by Size

Conclusion

You might have realized that the dir command is no different than the ls command. ls is more popular and almost every Linux user knows about it. Since dir command doesn't offer anything special, it doesn't give reasons to ditch ls command.

LHB Community