Short Overview: The two commands are identical, and you can consider that “
dir” is equivalent to the “
ls -Cb” command.
In the Linux world, two common commands, “ls” and “dir“, replicate each other so well that most Linux users think one of them is an alias or symbolic link to the other (it’s not true).
Another mistake most people make is judging the output of ls (which by default displays the color) and dir (which does not without specifying an option) and again failing to judge it because “
ls” is an alias to “
To verify this, execute the “
alias ls” command, or check the result after executing “
unalias ls” (it will remove the color from the output).
Note: Both commands are part of the same Coreutils package and share the same functionality with different options.
Keep in mind that dir is not an alias for the ls command, nor does it exist for any historical backward compatibility; both are independent programs with different purposes.
Difference Between ‘ls’ and ‘dir’ Commands
The existence of dir can be said to be due to the nature of ls, whose output varies depending on whether it’s executed in a terminal or piped with another command.
For example, when you execute the ls and dir commands without any options, both will list the content of the directory in vertically sorted columns, as shown.
However, if you pipe the ls and dir with more command, the ls output will list each file and directory in a new line, while the dir command will keep the output in vertically sorted columns.
Does that mean you cannot achieve the same result as the dir command does? Not true; even if it’s changing its state, you can use the “
-C” flag with the ls command.
Another difference between them is how they treat escape characters in a file or directory name. For example, if you have a “new dir” directory in your system, the ls command will print it as it is, whereas the dir command will print the escape along with the name, defining the space.
Again, you can use the ls command to print the escape character for directories with spaces in their names using the “
Does This Minor Difference Make It Necessary to Have the dir command?
The only difference between them was that the ls command lists the content in newlines when piped with another command and does not print the escape character by default.
And both issues (which they are not) can be easily resolved using “
-C” and “
-b“. You can even hear people say that the dir command is equal to the “
ls -Cb” command (which is totally true).
For me, this was enough to digest, and I totally agree that the dir command is not even worth mentioning (correct me if I’m wrong), and you can easily achieve each thing using the ls command without any problem.
If you are a beginner reading this article, I would suggest that you don’t overexaggerate the uniqueness of this topic and instead focus on other commands that are worth knowing.
That was my point of view. If you have something fresh to say, feel free to drop your opinion in the comment section.
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.