I've been trying to find the difference between using the dir
and ls
commands in terminal. I know ls
is the traditional UNIX method of viewing the files in a directory, and that dir
is the windows command prompt equivalent, but both commands work in terminal.
If I type in dir
, it displays the files and folders in the directory, and if I type ls
, it does the same, except with content highlighting. Both commands accept options (i.e. ls -a
and dir -a
both return all files and folders and hidden files.
So does anyone know what the difference is and why both dir
and ls
are used?
dir
andls
are part ofcoreutils
anddir
is almost the same asls
, just with different default options.info dir
says:Oh and there is also
vdir
!info vdir
says:Most likely
dir
exists for backwards compatibility or due to historical reasons.The Relationship Between
ls
anddir
ls
anddir
are separate programs that behave similarly. As explained and referenced below, the purpose ofdir
is to provide a command likels
whose output does not vary depending on whether or not it is going to a terminal. To achieve this helpfully,dir
must format its output in a way that is reasonable and useful both for viewing in a terminal and for writing to a file or pipe.There are two common misconceptions about
dir
:dir
is an alias ofls
, but that is not the case. Neither command is an alias of the other, and by default in Ubuntu,dir
is not an alias at all.ls
anddir
are provided by separate, non-identical executables.dir
exists for obscure historical reasons or to provide compatibility with some standard or some other OS. That is not the case either.ls
behaves the way it does for compatibilty.dir
, which doesn't have to be compatible because it's not a standard Unix command, behaves in an alternate way that the developers consider valuable in its own right and possibly even preferable.OK, but exactly how do
ls
anddir
differ?Both
ls
anddir
list the contents of directories. Two specific differences in their default behaviors distinguish them.When its standard output is a terminal,
ls
lists filenames in vertically sorted columns (likels -C
). When its standard output is not a terminal (for example, a file or pipe),ls
lists filenames one per line (likels -1
).Whether or not its standard output is a terminal,
dir
lists filenames in vertically sorted columns (likels -C
).For both
ls
anddir
, these defaults may be overridden by the--format=
flag and by the-1
,-C
,-m
, and-x
flags, which abbreviate particular--format=
options. See 10.1.4 General output formatting in the GNU coreutils reference manual for details.When its standard output is a terminal and a filename to be listed contains control characters,
ls
prints?
instead of each control character (likels -q
). When its standard output is not a terminal,ls
prints control characters as-is (likels --show-control-chars
).Whether or not its standard output is a terminal, when
dir
encounters a control character or any other character that would be interpreted specially if entered into a shell, it prints backslash sequences for the characters. This includes even relatively common characters like spaces. For example,dir
will list an entry calledDocuments backups
asDocuments\ backups
. This is likels -b
.For both
ls
anddir
, these defaults may be overridden by the flags listed in 10.1.7 Formatting the file names in the GNU coreutils reference manual. This includes-b
,-q
,--quoting-style=
, and some others.Sources: ls invocation and dir invocation, in the GNU coreutils reference manual.
Why have
dir
?The rationale for a separate
dir
utility is given in 4.5 Standards for Interfaces Generally of the GNU coding standards. I recommend reading that whole section to understand the developers' reasoning, but here are the highlights as applicable tols
/dir
:The GNU Project considers it undesirable, from a technical perspective, for a utility to produce different output depending on what kind of device it is writing to (at least in the utility's default configuration). For some utilities, including
ls
, device-dependent output is necessary for compatibility and so it works the way users expect. Some users do also specifically prefer this device-dependent behavior.While
ls
could not reasonably be written to behave device independently, a separatedir
utility was created to achieve this. Thusdir
is not the utility that behaves strangely for reasons of historical compatibility--ls
is.To see how
ls
,dir
, and the relatedvdir
utility are implemented in the coreutils source code without needless code duplication, seels-dir.c
,ls-ls.c
,ls-vdir.c
,ls.h
, andls.c
.Is
dir
really useful?If you've ever wished
ls
produced multi-column output even when you piped it toless
(ls | less
) or redirected it to a file (ls > out.txt
), you can usedir
orls -C
.If you've ever wished you could directly copy a filename shown by
ls
and use it as part of a command without worrying about quoting, you can usedir
orls -b
.dir
is equivalent tols -Cb
, so in that sense you don't needdir
. Butdir
provides a combination of options that in practice is often useful (though not widely known about).Why do I get colorized output from
ls
(evenls -Cb
) but notdir
?!Most Ubuntu users have an alias called
ls
which runsls --color=auto
. Whenls
exists both as an alias and an external command, the alias takes precedence in simple, interactive commands.Alias definitions aren't expanded recursively--it's the external
ls
command that thels
alias is calling with--color=auto
. See 6.6 Aliases in the Bash reference manual for more information on how aliases work.When passed to
ls
,dir
, orvdir
(and some other commands, likegrep
),--color=auto
uses color when its output is a terminal, but not otherwise.By default in Ubuntu, user accounts are created with this in
~/.bashrc
:You'll notice the
ls
alias (alias ls='ls --color=auto'
) is uncommented, while those fordir
andvdir
are commented out with#
so they take no effect. That is, whiledir
is not an alias,ls
is (but not todir
).How do I make
dir
produce colored output, too?To enable colored output with
dir
, simply edit.bashrc
in your home directory and uncomment the#alias dir='dir --color=auto'
line by removing the leading#
. In shells started after the change,dir
will be an alias.If you want the change in the current shell, you can run the alias definition as a command, or you can source
.bashrc
by running. ~/.bashrc
.This arguably goes against the main point of
dir
--that it should produce the same sort of output regardless of the output device. However:dir
alias, you should certainly do so.\dir
orcommand dir
,dir
will still produce device-independent output. This is to say that aliasingdir
todir --color=auto
does not really breakdir
.Source: Renan's answer to What's the difference between “dir” and “ls”?
When in doubt, compare
type ls
vstype dir
(see also Difference between ls and la):The difference boils down to different options to
ls
, in my case--color=tty
would be the most visible, your system may differ.Short Answer : None,
dir
is the same source code thanls
,ls
binary have--color
by default. (1 line of code diff)