请关注本头条号,每天坚持更新原创干货技术文章。
如需学习视频,请在微信搜索公众号“智传网优”直接开始自助视频学习
1. 前言
本文主要详细讲解Linux系统中的dir命令。
本文展示了使用dir命令列出目录内容的一些示例。dir命令不是Linux中常用的命令。尽管它的工作方式不太像大多数Linux用户喜欢使用的ls命令。我们将讨论dir命令,并在其中查看如何使用不同的选项和参数。
dir命令的一般语法如下所示。
dir [OPTION] [FILE]
Linux dir 命令详解
2. dir命令用法与示例
dir命令用法与示例
简单的dir命令输出
dir /
[root@la ~]# dir /
bin boot dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
带有/etc目录文件的dir命令的输出如下所示。从输出中可以看到,/etc目录中并没有列出所有的文件。
dir /etc
[root@la ~]# dir /etc
adjtime DIR_COLORS host.conf lynx-site.cfg passwd- rpm sudoers.daliases DIR_COLORS.256color hostname machine-id pkcs11 rsyncd.conf sudo-ldap.confaliases.db DIR_COLORS.lightbgcolor hosts machine-info pki rsyslog.conf sysconfigalternatives dracut.conf hosts.allow magic plymouth rsyslog.d sysctl.confanacrontab dracut.conf.d hosts.deny makedumpfile.conf.sample pm rwtab sysctl.d
要列出每行一个文件,请使用-1选项,如下所示。
dir dir -1
[root@la ~]# dir -l
total 117236-rw-r--r-- 1 root root 120040373 Sep 25 21:25 go1.13.1.linux-amd64.tar.gzdrwxr-xr-x 4 root root 4096 Oct 5 00:21 speedtest-clidrwxr-xr-x 3 root root 4096 Oct 1 04:39 work
3. 使用`dir`命令列出目录中的所有文件
包括.(隐藏)文件,使用-a选项。可以使用-l选项将输出格式化为列表。
使用dir命令列出目录中的所有文件
dir -a dir -al
[root@la ~]# dir -a
. .acme.sh .bash_logout .bashrc .cshrc .pki speedtest-cli .tcshrc.. .bash_history .bash_profile .cache go1.13.1.linux-amd64.tar.gz .rnd .ssh work
4. 使用`dir`命令查看目录项而不是内容

使用dir命令查看目录项而不是内容
当需要只列出目录条目而不是目录内容时,可以使用-d选项。在下面的输出中,选项-d列出/etc目录的条目。
当您使用-dl时,它会显示一个很长的目录列表,包括所有者、组所有者和权限。
[root@la ~]# dir -d /etc/etc
[root@la ~]# dir -dl /etc
drwxr-xr-x. 85 root root 4096 Oct 5 00:24 /etc
5. 使用`dir`命令查看文件的索引号
如果希望查看每个文件的索引号,请使用选项-i。从下面的输出中,您可以看到第一列显示了数字。这些数字称为索引节点,有时也称为索引节点或索引号。
在Linux系统中,inode是文件系统上的数据存储,它存储关于文件的信息,但不包括文件名及其实际数据。
dir -il
total 1172364509 -rw-r--r-- 1 root root 120040373 Sep 25 21:25 go1.13.1.linux-amd64.tar.gz136213 drwxr-xr-x 4 root root 4096 Oct 5 00:21 speedtest-cli517883 drwxr-xr-x 3 root root 4096 Oct 1 04:39 work
6. 使用`dir`命令以块的形式列出文件及其分配的大小

使用dir命令以块的形式列出文件及其分配的大小
可以使用-s选项查看文件大小。如果需要根据大小对文件进行排序,那么使用-S选项。
在这种情况下,还需要使用-h选项以人类可读的格式查看文件大小。
dir -shl
total 115M115M -rw-r--r-- 1 root root 115M Sep 25 21:25 go1.13.1.linux-amd64.tar.gz4.0K drwxr-xr-x 4 root root 4.0K Oct 5 00:21 speedtest-cli4.0K drwxr-xr-x 3 root root 4.0K Oct 1 04:39 work
在上面的输出中,第一列以千字节为单位显示文件的大小。下面的输出显示了使用-S选项按大小排序的文件列表。
dir -ashlS /etc/
total 1.6M656K -rw-r--r--. 1 root root 655K Jun 7 2013 services156K -rw-r--r-- 1 root root 153K Jun 10 2014 lynx.cfg32K -rw-r--r-- 1 root root 30K Oct 5 00:24 ld.so.cache12K -rw-r--r-- 1 root root 12K Sep 12 21:11 aliases.db12K -rw-r--r--. 1 root root 8.7K Jun 10 2014 nanorc8.0K -rw-r--r-- 1 root root 7.2K Sep 30 15:25 kdump.conf8.0K -rw-r--r--. 1 root root 6.6K Apr 11 2018 screenrc
您还可以根据修改时间排序,最近修改的文件首先出现在列表中。这可以使用-t选项来完成。
[root@la ~]# dir -ashlt /root/
total 115M24K -rw------- 1 root root 19K Nov 21 04:00 .bash_history4.0K dr-xr-x---. 8 root root 4.0K Nov 21 03:43 .4.0K drwxr-xr-x 4 root root 4.0K Oct 5 00:26 .cache4.0K drwxr-xr-x 4 root root 4.0K Oct 5 00:21 speedtest-cli4.0K -rw------- 1 root root 1.0K Oct 4 12:45 .rnd4.0K drwx------ 8 root root 4.0K Oct 4 12:45 .acme.sh4.0K -rw-r--r--. 1 root root 165 Oct 1 08:13 .tcshrc4.0K -rw-r--r--. 1 root root 136 Oct 1 08:13 .cshrc4.0K -rw-r--r--. 1 root root 207 Oct 1 08:13 .bashrc4.0K -rw-r--r-- 1 root root 343 Oct 1 08:13 .bash_profile4.0K drwxr-xr-x 3 root root 4.0K Oct 1 04:39 work4.0K drwx------ 2 root root 4.0K Sep 30 15:22 .ssh115M -rw-r--r-- 1 root root 115M Sep 25 21:25 go1.13.1.linux-amd64.tar.gz4.0K drwxr----- 3 root root 4.0K Sep 12 21:11 .pki4.0K dr-xr-xr-x. 18 root root 4.0K Sep 12 21:08 ..4.0K -rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout
7. 使用`dir`命令列出没有所有者或组所有者的文件
要列出没有所有者的文件,您必须使用-g选项,它与-l选项类似,只是不打印文件所有者。若要列出没有组所有者的文件,请使用-G选项,如下所示。
dir -ahgG /root
total 115Mdr-xr-x---. 8 4.0K Nov 21 03:43 .dr-xr-xr-x. 18 4.0K Sep 12 21:08 ..drwx------ 8 4.0K Oct 4 12:45 .acme.sh-rw------- 1 19K Nov 21 04:00 .bash_history-rw-r--r--. 1 18 Dec 29 2013 .bash_logout-rw-r--r-- 1 343 Oct 1 08:13 .bash_profile-rw-r--r--. 1 207 Oct 1 08:13 .bashrcdrwxr-xr-x 4 4.0K Oct 5 00:26 .cache-rw-r--r--. 1 136 Oct 1 08:13 .cshrc-rw-r--r-- 1 115M Sep 25 21:25 go1.13.1.linux-amd64.tar.gzdrwxr----- 3 4.0K Sep 12 21:11 .pki-rw------- 1 1.0K Oct 4 12:45 .rnddrwxr-xr-x 4 4.0K Oct 5 00:21 speedtest-clidrwx------ 2 4.0K Sep 30 15:22 .ssh-rw-r--r--. 1 165 Oct 1 08:13 .tcshrcdrwxr-xr-x 3 4.0K Oct 1 04:39 work
您可以从上面的输出中注意到,文件所有者和组所有者的名称没有打印出来。还可以使用-author标志查看文件的作者,如下所示。
dir -al --author /root
total 117308dr-xr-x---. 8 root root root 4096 Nov 21 03:43 .dr-xr-xr-x. 18 root root root 4096 Sep 12 21:08 ..drwx------ 8 root root root 4096 Oct 4 12:45 .acme.sh-rw------- 1 root root root 19436 Nov 21 04:00 .bash_history-rw-r--r--. 1 root root root 18 Dec 29 2013 .bash_logout
在上面的输出中,第五列显示了文件作者的名字。文件由用户root拥有,属于组root,由用户root编写。
8. 使用`dir`命令在其他文件之前列出目录
您可能希望在查看所有其他文件之前查看目录,这可以通过使用-group-directories-first标志来实现,如下所示。
dir -l --group-directories-first
total 117236drwxr-xr-x 4 root root 4096 Oct 5 00:21 speedtest-clidrwxr-xr-x 3 root root 4096 Oct 1 04:39 work-rw-r--r-- 1 root root 120040373 Sep 25 21:25 go1.13.1.linux-amd64.tar.gz
当您观察上面的输出时,您可以看到所有目录都列在常规文件之前。权限前面的字母d表示目录,字母a表示常规文件。
还可以递归地查看子目录,这意味着可以使用-R选项列出目录中的所有其他子目录,如下所示。
dir -R
9. 使用`dir`命令查看用户和组id,而不是名称
要查看用户和组id,需要使用-n选项。让我们来观察一下下面两个输出之间的区别。
输出没有-n选项。
[root@la ~]# dir -l --author
total 117236-rw-r--r-- 1 root root root 120040373 Sep 25 21:25 go1.13.1.linux-amd64.tar.gzdrwxr-xr-x 4 root root root 4096 Oct 5 00:21 speedtest-clidrwxr-xr-x 3 root root root 4096 Oct 1 04:39 work
输出与-n选项。
[root@la ~]# dir -nl --author
total 117236-rw-r--r-- 1 0 0 0 120040373 Sep 25 21:25 go1.13.1.linux-amd64.tar.gzdrwxr-xr-x 4 0 0 0 4096 Oct 5 00:21 speedtest-clidrwxr-xr-x 3 0 0 0 4096 Oct 1 04:39 work
10. 使用`dir`命令查看用逗号分隔的条目
这可以通过使用-m选项存档。
dir -am
要查找使用dir命令use -help标志的帮助,并查看dir use -version的版本详细信息。

11. 结论
本文主要讲解Linux系统中的dir命令的作用与详细解释。
这些只是dir命令基本用法的示例,要使用其他许多选项,请参阅系统上dir命令的手动输入。如果您发现其他有趣的选项或使用dir命令的方法,请留下您的评论。希望这篇文章对你有用。
本文已同步至博客站,尊重原创,转载时请在正文中附带以下链接:https://www.linuxrumen.com/rmxx/1533.html 点击了解更多,快速查看更多的技术文章列表。