๐
Quick 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 โls --color=autoโ.
To verify this, execute the โalias lsโ command, or check the result after executiโฆ
๐
Quick 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 โls --color=autoโ.
To verify this, execute the โalias lsโ command, or check the result after executing โunalias lsโ (it will remove the color from the output).
๐ฎ
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 โ-bโ flag.
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.