28

I have compared the man pages of dir and ls and they seem to be exactly the same. Both are part of GNU coreutils and "list directory contents".

The only difference I've seen so far is that dir doesn't colorize the output.

So why do two commands exist? Is there a difference I missed? Why would one prefer dir over ls?

Martin Thoma
  • 2,802
  • 5
  • 34
  • 45

2 Answers2

25

I would be inclined to think that dir is there just for backwards compatibility.

From GNU Coreutils:

dir is equivalent to ls -C -b; that is, by default files are listed in columns, sorted vertically, and special characters are represented by backslash escape sequences.

By the way, ls doesn't colorize the output by default: this is because most distros alias ls to ls --color=auto in /etc/profile.d. For a test, type unalias ls then try ls: it will be colorless.

Renan
  • 16,976
  • 8
  • 69
  • 88
  • 10
    `dir` is not provided for backwards compatibility. Instead, `ls` works the way it does for backwards compatibility (and because lots of users want it to work that way), and `dir` is provided as an alternative, which the GNU developers consider generally preferable because its output is device-independent. See [4.5 Standards for Interfaces Generally](https://www.gnu.org/prep/standards/standards.html#User-Interfaces) in the [GNU coding standards](https://www.gnu.org/prep/standards/) and [my answer](https://askubuntu.com/a/615946) to [that AU question](https://askubuntu.com/q/103913) for details. – Eliah Kagan Apr 29 '15 at 16:19
11

dir isn't a Unix standard command (i.e. it is not specified by POSIX) but just a kind of alias/clone to provide some familiarity with Windows (DOS, CMD) users (and possibly VAX-VMS and other OSes where dir was used to list files).

jlliagre
  • 60,319
  • 10
  • 115
  • 157