1. Introduction

In command-line interfaces, it’s often important to be able to inspect files and subdirectories of the current directory. Two common commands for this are dir and ls. Although they seem similar, they do differ in several key aspects.

In this tutorial, we’ll cover the differences between the dir and ls terminal commands.

First, we’ll do an overview of both commands. Subsequently, we’ll discuss misconceptions about the dir command. Lastly, we’ll elaborate on the differences between the two commands.

2. Overview of the dir and ls Commands

When compared to the dir command, the ls command is more widely used in Linux. However, at their core, both dir and ls serve the fundamental function of listing the contents of directories. When looking at examples of navigating through directories in the terminal, we can often see either command used. In other words, both commands provide a comprehensive inventory of files and subdirectories within the current directory.

Let’s look at the output of both dir and ls:

ls and dir output

From the output, we can see that both commands show the same output on the terminal. However, ls has a default color-coded and formatted representation, while dir displays a fairly plain output.

3. Misconceptions About the dir Command

ls and dir are separate programs that behave similarly. However, there are some common misconceptions related to the dir command. Let’s go over two of them.

3.1. Alias Misconception

To begin with, the first misconception is about dir being the alias of the ls command. Contrary to popular belief, dir is rarely an alias of ls, and vice versa. Moreover, these commands are separate entities, each governed by its executable. In addition, in default Linux configurations, dir isn’t configured as an alias.

To verify this, we execute the alias command with ls:

$ alias ls
alias ls='ls --color=auto'

Similarly, we check for the dir command:

$ alias dir
-bash: alias: dir: not found

Thus, we can see that dir isn’t an alias of ls in this particular environment.

While there are platforms where this is indeed the case, most major Linux distributions don’t exhibit the behavior.

3.2. Historical Notion

It’s often thought that dir is there for compatibility reasons, but that’s rarely true. Unlike ls, which adjusts to work with other systems, dir doesn’t have to because it’s not a standard Linux command.

In addition, unlike ls, which is tailored to provide output suited for specific reasons, dir is designed to maintain a uniform output format across all devices. For instance, dir shows the default multi-column format output, irrespective of the output device.

4. Comparing the dir and ls Commands

Although ls and dir may seem similar, it’s important to understand that they are separate programs created for similar purposes. The dir command is primarily intended to offer functionality similar to ls but with consistent output regardless of whether it’s displayed in a terminal or directed elsewhere. This requires formatting the output in a way that’s suitable for viewing on a terminal and for redirection to files or pipes.

4.1. Output Structure

ls typically displays filenames in vertically sorted columns when the output goes to a terminal. However, when we redirect the output to another command or file, it lists one filename per line.

Let’s pipe the ls command to more:

$ ls | more
LinuxWork
conver.sh
csv-to-excel
directory1
example
example.sh
exampleFile.txt
file1.pdf
file1.ps
file2-pdfjam.pdf
file2-pdfjam.ps
file2.pdf
file2.ps
...

On the other hand, dir consistently presents filenames in vertically sorted columns, regardless of whether the output is displayed in a terminal or redirected elsewhere.

Now, let’s see the piped version of dir:

$ dir | more

We can achieve the same result by using the -C option with ls.

4.2. Presentation

When we use the ls command, it often presents directory contents in a visually pleasing format, displaying file sizes, timestamps, and color-coded distinctions for different file types.

For instance, running ls -l provides a detailed listing with file permissions, ownership, size, and modification timestamps:

$ ls -l
total 39834
drwxrwxrwx 1 sidrah sidrah 512 Jul 5 2023 LinuxWork
-rwxrwxrwx 1 sidrah sidrah 431 Mar 9 10:03 conver.sh
drwxrwxrwx 1 sidrah sidrah 512 Sep 8 2023 csv-to-excel
drwxrwxrwx 1 sidrah sidrah 512 Jul 29 2023 directory1
-rwxrwxrwx 1 sidrah sidrah 16 Dec 7 20:32 example
-rwxrwxrwx 1 sidrah sidrah 16 Dec 7 20:32 example.sh
…

With dir, we get the list in columns if we run it on Linux.

Notably, if we run dir in a Windows environment, we get an output similar output to ls -l:

C:\Users\USER>dir
Volume in drive C has no label.
Volume Serial Number is F2E2-959A
Directory of C:\Users\USER
03/06/2024 11:07 PM <DIR> .
06/01/2023 05:04 PM <DIR> ..
03/06/2024 10:13 PM 573 .bash_history
03/06/2024 11:06 PM 83 .gitconfig
03/06/2024 11:07 PM 20 .lesshst
03/06/2024 10:09 PM 14 .minttyrc
05/19/2023 10:31 AM <DIR> .ms-ad
10/16/2023 10:52 AM 0 .node_repl_history
08/06/2023 07:10 PM <DIR> .vscode
03/15/2023 04:10 PM <DIR> 3D Objects
...

Hence, dir output may exhibit variations in formatting, often tailored to suit Windows environments, but may lack some of the visual enhancements offered by ls.

4.3. Handling Control Characters

When encountering control characters in filenames, ls may replace them with a question mark or any other symbol when the output is directed to a terminal. However, if the output isn’t a terminal, ls prints control characters as-is.

To test this option, we create a file with a control character using the touch command:

$ touch "file_with_control_character$(printf '\t')name"

Next, we check it using the ls command:

$ ls
exampleFile.txt 'file_with_control_character'$'\t''name' 'new dir' sample-directory

Depending on our terminal settings, the output may contain a question mark or tab character. Moreover, we can verify this ls behavior by using the cat command:

$ ls --show-control-chars | cat -A
file_with_control_character^Iname$

In contrast, dir utilizes backslash sequences to represent control or special characters. This ensures consistent output regardless of the output device. To verify, we use the –quoting-style=escape with dir:

$ dir --quoting-style=escape
file_with_control_character\tname

Hence, ls behaves similarly to ls -q when printing filenames on the terminal. When its standard output isn’t a terminal, ls prints control characters as-is. Conversely, dir utilizes backslash sequences to represent control characters or other characters. This includes characters below the space character on the ASCII table and ensures consistent output regardless of the output device.

5. Produce Color-Coded Output With dir

In many Linux distributions, users often have an alias called ls configured to run ls –color=auto. This alias enables color-coded output for ls when the output is a terminal.

To enable color-coded output for dir, we can uncomment the dir –color=auto line in the .bashrc file or simply create the alias if the line doesn’t exist:

$ cat .bashrc
...

When we uncomment these lines in .bashrc, we set up an alias for dir similar to the one for ls. This enables the dir command to produce color-coded output when the terminal supports it. However, it’s worth noting that this approach contradicts the principle that dir should produce consistent output regardless of the output device.

6. Conclusion

In this article, we covered the differences between the dir and ls terminal commands.

First, we went through both the dir and ls commands. After that, we explored the misconceptions related to dir, followed by the differences between ls and dir. Lastly, we discussed how to produce color-coded output using dir.