> ## Content Index
> Fetch the complete content index at: https://linuxhandbook.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# The Lesser Known Dir Command in Linux
- URL: https://linuxhandbook.com/dir-command/
- Published: 2022-10-27T14:54:58.000Z
- Updated: 2024-09-08T10:34:21.000Z
- Description: Learn about the dir command, the less known but identical cousin of the popular ls command.
- Author: LHB Community
- Tags: Commands

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](https://linuxhandbook.com/content/images/2022/10/default-dir-command.png)

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](https://linuxhandbook.com/content/images/2022/10/dir-command-with-colored-output.png)

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 with dir command](https://linuxhandbook.com/content/images/2022/10/dir-command-long-listing-format.png)

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 With dir Command in Linux](https://linuxhandbook.com/content/images/2022/10/display-entries-as-comma-separated.png)

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](https://linuxhandbook.com/ls-show-hidden-files/):

```
dir -a

```

![Display Hidden Files with Dir Command](https://linuxhandbook.com/content/images/2022/10/dir-comamnd-listing-hidden-files.png)

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

```

![](https://linuxhandbook.com/content/images/2022/10/dir-command-listing-files-without-special-entries.png)

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 with dir command](https://linuxhandbook.com/content/images/2022/10/dir-command-file-type-indicators.png)

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 with dir command](https://linuxhandbook.com/content/images/2022/10/showing-files-by-type.png)

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 with dir command](https://linuxhandbook.com/content/images/2022/10/dir-command-ignoring-specified-entries-1.png)

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](https://linuxhandbook.com/show-file-size-linux/).

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

```
dir -h -l
```

![list in human readable format with the dir command in Linux](https://linuxhandbook.com/content/images/2022/10/listing-in-human-readable-form.png)

Listing in Human Readable Form

### Displaying Files by Size

The `dir` command also supports the `-S` option to [sort files by size](https://linuxhandbook.com/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.

![](https://linuxhandbook.com/content/images/2022/10/display-files-by-size.png)

Displaying Files by Size

## Conclusion

You might have realized that the dir command is no different than the [ls command](https://linuxhandbook.com/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. No wonder dir is often excluded from the [list of common file management commands in Linux](https://linuxhandbook.com/file-commands/).