哔哔one
dir 和 ls 都是软件包 coreutils 的一部分还有一个 vdir 也是一样。这两个命令几乎相同,只是具有不同的默认选项,可以通过 info 指令获得它们的信息:10.1 ‘ls’: List directory contents==================================The ‘ls’ program lists information about files (of any type, includingdirectories). Options and file arguments can be intermixed arbitrarily,as usual.For non-option command-line arguments that are directories, bydefault ‘ls’ lists the contents of directories, not recursively, andomitting files with names beginning with ‘.’. For other non-optionarguments, by default ‘ls’ lists just the file name. If no non-optionargument is specified, ‘ls’ operates on the current directory, acting asif it had been invoked with a single argument of ‘.’.By default, the output is sorted alphabetically, according to thelocale settings in effect.(1) If standard output is a terminal, theoutput is in columns (sorted vertically) and control characters areoutput as question marks; otherwise, the output is listed one per lineand control characters are output as-is.Because ‘ls’ is such a fundamental program, it has accumulated manyoptions over the years. They are described in the subsections below;within each section, options are listed alphabetically (ignoring case).The division of options into the subsections is not absolute, since someoptions affect more than one aspect of ‘ls’’s operation.10.2 ‘dir’: Briefly list directory contents===========================================‘dir’ is equivalent to ‘ls -C -b’; that is, by default files are listedin columns, sorted vertically, and special characters are represented bybackslash escape sequences.10.3 ‘vdir’: Verbosely list directory contents==============================================‘vdir’ is equivalent to ‘ls -lb’; that is, by default files are listedin long format and special characters are represented by backslashescape sequences.大概意思就是说 dir 和 vdir 两个指令相当于 ls 指令加上一些其他参数,没有其他明显区别。特别的,许多人认为 dir 是 ls 的别名,但事实并非如此。这两个命令都不是另一个的别名,默认情况下,在 Ubuntu 中, dir 根本不是别名。 ls 和 dir 由单独的 non-identical 可执行文件提供。单独的 dir 实用程序的基本原理在 GNU coding standards 的 4.5 Standards for Interfaces Generally 中给出。Please don’t make the behavior of a utility depend on the name used to invoke it….Instead, use a run time option or a compilation switch or both to select among the alternate behaviors….Likewise, please don’t make the behavior of a command-line program depend on the type of output device….Compatibility requires certain programs to depend on the type of output device. It would be disastrous if ls or sh did not do so in the way all users expect. In some of these cases, we supplement the program with a preferred alternate version that does not depend on the output device type. For example, we provide a dir program much like ls except that its default output format is always multi-column format.大概意思是说 ls 指令由于兼容性需要,无法作为独立运行设备编写,故另外单独编写了 dir 和 vdir 指令作为 device-independent 输出(而 ls 指令是 device-dependent 类型的)。相比于 ls 来说,这两个指令虽然在默认情况下调用和 ls 的带参数调用相当,但是这两个指令提供了一些额外的选项组合。大多数人觉得 ls 比 dir 好用的原因是在默认情况下很多 linux 分发版的 .bashrc 将 ls 定义为 ls --color=auto 的别名,这样造成了 ls 的输出带有不同的颜色标识而 dir 与 vdir 没有。下面是 Ubuntu 的 .bashrc 文件片段:if [ -x /usr/bin/dircolors ]; thentest -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"alias ls='ls --color=auto'#alias dir='dir --color=auto'#alias vdir='vdir --color=auto'alias grep='grep --color=auto'alias fgrep='fgrep --color=auto'alias egrep='egrep --color=auto'fi# some more ls aliasesalias ll='ls -alF'alias la='ls -A'alias l='ls -CF'可以看到 .bashrc 文件中贴心地为 ls 指令添加了很多别名,但是 dir 和 vdir 的别名被注释掉了。这也就造成了为什么 ls 比 dir 好用的原因。要使用 dir 启用彩色输出,只需在主目录中编辑 .bashrc 文件,然后通过删除前导 # 取消注释 #alias dir='dir --color=auto' 行。在更改后启动的 shell 中, dir 将成为别名,输入 dir 指令也可以产生不同的色彩高亮标识。而当作为外部命令调用时,例如在脚本中或通过运行 \dir 或 command dir 覆盖别名时, dir 仍将产生 device-independent 输出。这就是说,将 dir 别名化为 dir --color=auto 并不会真正破坏 dir 。