[命令] Linux 命令 ls (显示文件或目录) (转载)

Linux ls命令

ls命令
1、 介绍
ls - list directory contents
ls命令是Linux中最常用的命令之一。ls是list的缩写
2、 命令格式
ls [选项] [文件名]
3、 命令功能
用来打印当前目录清单或者打印出指定目录下的文件及文件清单。ls命令在打印文件清单时,还可以查看文件权限、目录信息等等。
4、 命令参数
可以使用帮助命令查看详细命令参数:man ls或ls --help
-a, --all
do not ignore entries starting with .(列出目录下的所有文件,包括以.开头的隐含文件)

-A, --almost-all
do not list implied . and …(列出除了.及…以外的所有文件)

–author
with -l, print the author of each file(打印出每一个文件的作者)

-b, --escape
print octal escapes for nongraphic characters(不能输出的字符用反斜杠加字符编号的形式输出)

–block-size=SIZE
use SIZE-byte blocks.(使用SIZE-byte的大小的字节为单位)

-B, --ignore-backups
do not list implied entries ending with ~(不列出任何以波浪号结束的项目)

-c with -lt: sort by, and show, ctime (time of last modification of file status information) with -l: show ctime and sort by name otherwise: sort by ctime
(输出文件的ctime(文件状态最后更改的时间),并根据ctime排序)

-C list entries by columns(由上至下的列出项目)

–color[=WHEN]
colorize the output. WHEN defaults to ‘always’ or can be ‘never’ or ‘auto’. More info below(控制是否使用色彩分辨文件。WHEN可以是always、never或者auto其中一个)

-d, --directory
list directory entries instead of contents, and do not dereference symbolic links
(将目录像文件一样显示,而不显示目录下面的内容)

-D, --dired
generate output designed for Emacs’ dired mode(产生适合Emacs的dired模 式使用的结果)

-f do not sort, enable -aU, disable -ls --color(对输出的文件不进行排序,-aU选线生效,-ls选项失效)

-F, --classify
append indicator (one of /=>@|) to entries(加上文件类型的指示符号(/=@| 其中一个))

–file-type
likewise, except do not append ‘’(和-F一样,除了不追加“”)

–format=WORD
across -x, commas -m, horizontal -x, long -l, single-column -1, verbose -l, vertical -C(–format=关键之,关键字可以是“across -x, commas -m, horizontal -x, long -l, single-column -1, verbose -l, vertical -C”)

–full-time
like -l --time-style=full-iso(即 -l --time-style=full-iso)

-g like -l, but do not list owner(像-l,但是不列出所有者)

–group-directories-first
group directories before files.(组目录在文件目录之前)

augment with a --sort option, but any use of --sort=none (-U) disables grouping
(用-sort选项进行扩展,但任何使用-sort=none(-U)禁用组)

-G, --no-group
in a long listing, don’t print group names(不列出任何有关于组的信息)

-h, --human-readable
with -l, print sizes in human readable format (e.g., 1K 234M 2G)(类似于-l,以人容易理解的格式列出文件大小)

–si likewise, but use powers of 1000 not 1024(像-h,但是文件大小取1000而不是1024)

-H, --dereference-command-line
follow symbolic links listed on the command line(使用命令列中的符号链接指示的 真正目的地)

–dereference-command-line-symlink-to-dir
follow each command line symbolic link that points to a directory(遵循指向目录的 每个命令行符号链接)

–hide=PATTERN
do not list implied entries matching shell PATTERN (overridden by -a or -A)(不 要列出与shell模式匹配的隐含条目(由-a或-A重写))

–indicator-style=WORD
append indicator with style WORD to entry names: none (default), slash (- p), file-type (–file-type), classify (-F)(指定在每个项目名称后加上指示符号<方 式>:none (default), slash (-p), file-type (–file-type), classify (-F))

-i, --inode
print the index number of each file(输出每个文件的inode号)

-I, --ignore=PATTERN
do not list implied entries matching shell PATTERN(不列出任何符合sell万用字 符<样式>的项目)

-k like --block-size=1K(像 --block-size=1K,以k字节的形式表示文件的大小)

-l use a long listing format(使用长格式,即列出文件详细信息)

-L, --dereference
when showing file information for a symbolic link, show information for the file the link references rather than for the link itself(当显示符号连接的文件信息时,显示符号链接所指示的对象而并非符号链接本身的信息)

-m fill width with a comma separated list of entries(所有项目以逗号隔开,并填满整行行宽)

-n, --numeric-uid-gid
like -l, but list numeric user and group IDs(类似-l,用数字UID和GID代替名称)

-N, --literal
print raw entry names (don’t treat e.g. control characters specially)(印出未经处理 的项目名称,(例如不特别处理控制字符))

-o like -l, but do not list group information(类似-l,但是不现实组信息)

-p, --indicator-style=slash
append / indicator to directories(向目录追加/指示器)

-q, --hide-control-chars
print ? instead of non graphic characters(以?字符代替无法打印的字符)

–show-control-chars
show non graphic characters as-is (default unless program is ‘ls’ and output is a terminal)(直接显示无法打印的字符(这是默认方式,除非调用的程序名称时‘ls’而且在终端机画面输出结果))

-Q, --quote-name
enclose entry names in double quotes(将项目名称打上双引号)

–quoting-style=WORD
use quoting style WORD for entry names: literal, locale, shell, shell-always, c, escape(使用指定的quotin方式类型:literal, locale, shell, shell-always, c, escape)

-r, --reverse
reverse order while sorting(以相反次序排列)

-R, --recursive
list subdirectories recursively(同时列出所有子目录层)

-s, --size
print the allocated size of each file, in blocks(以块大小为单位列出所有文件的大小)

-S sort by file size(根据文件大小排序)

–sort=WORD
sort by WORD instead of name: none -U, extension -X, size -S, time -t, version -v(根据关键词代替名字排序:none -U, extension -X, size -S, time -t, version -v)

–time=WORD
with -l, show time as WORD instead of modification time: atime -u, access -u, use -u, ctime -c, or status-c; use specified time as sort key if --sort=time(类似于-l,根据关键词显示时间代替默认的修改时间:atime -u, access -u, use -u, ctime -c, or status)

–time-style=STYLE
with -l, show times using style STYLE: full-iso, long-iso, iso, locale, +FORMAT. FORMAT is interpreted like ‘date’; if FORMAT is FORMAT1FORMAT2, FORMAT1 applies to non-recent files and FORMAT2 to recent files; if STYLE is prefixed with ‘posix-’, STYLE takes effect only outside the POSIX locale(显示时间使用style:…………)

-t sort by modification time(以文件修改时间排序)

-T, --tabsize=COLS
assume tab stops at each COLS instead of 8(显示我文件或目录最后被访问的时间)

-u with -lt: sort by, and show, access time with -l: show access time and sort by name otherwise: sort by access time(-u配合-lt:显示访问时间而依访问时间排序;
配合-l:显示访问时间但根据名称排序;否则,根据访问时间排序)

-U do not sort; list entries in directory order(不进行排序,依系统原有的次序列出项目)

-v natural sort of (version) numbers within text(根据文件版本进行排序)

-w, --width=COLS
assume screen width instead of current value(自行指定屏幕的宽度而不使用目前的数值)

-x list entries by lines instead of by columns(逐行列出项目而不是逐栏列出)

-X sort alphabetically by entry extension(根据扩展名排序)

-1 list one file per line(每行只列出一个文件)

–help 显示此帮助信息并退出

-version 显示版本信息并退出

5、命令范例
由于ls选项过多,这里仅使用一些常用选项做举例
实例1:查看当前目录下的文件或目录
[root@lianxi tmp]# ls
1d 2d 3d 4d TEST

实例2:使用长清单模式,-l参数
[root@lianxi tmp]# ls -l
total 20
drwxr-xr-x. 2 root root 4096 Dec 8 00:44 1d
drwxr-xr-x. 2 root root 4096 Dec 8 00:44 2d
drwxr-xr-x. 2 root root 4096 Dec 8 00:44 3d
drwxr-xr-x. 2 root root 4096 Dec 8 00:44 4d
drwxr-xr-x. 6 root root 4096 Dec 8 00:46 TEST

注:第1列
第一个字母d意味着内容是目录或者文件。如果是’-‘(减号),这意味着它的内容是文件。当它是l(小写l字符),意味这内容是链接文件。
下面的9个字符是关于文件权限。前3个rwx字符是文件的拥有者的权限,第二组3rwx是文件的所有组的权限,最后的rwx是对其他人访问文件的权限。
第2列 这行告诉我们有多少链接指向这个文件。
第3列 这行告诉我们谁是这个文件/文件夹的所有者。
第4列 这行告诉我们谁是这个文件/文件夹的所有组。
第5列 这行告诉我们这个文件/文件夹的以字节为单位的大小。 目录的大小总是4096字节。
第6列 这告诉我们文件最后的修改时间。
第7列 这告诉我们文件名或者目录名。

实例3:显示文件大小
① 以便于人阅读的方式显示文件大小,-h参数
[root@lianxi tmp]# ls -lh
total 20K
drwxr-xr-x. 2 root root 4.0K Dec 8 00:44 1d
drwxr-xr-x. 2 root root 4.0K Dec 8 00:44 2d
drwxr-xr-x. 2 root root 4.0K Dec 8 00:44 3d
drwxr-xr-x. 2 root root 4.0K Dec 8 00:44 4d
drwxr-xr-x. 6 root root 4.0K Dec 8 00:46 TEST
② 使用–si参数,此参数和-h类似,但-si是以1000为单位,而-h是以1024为单位
[root@lianxi tmp]# ls -l --si
total 21k
drwxr-xr-x. 2 root root 4.1k Dec 8 00:44 1d
drwxr-xr-x. 2 root root 4.1k Dec 8 00:44 2d
drwxr-xr-x. 2 root root 4.1k Dec 8 00:44 3d
drwxr-xr-x. 2 root root 4.1k Dec 8 00:44 4d
drwxr-xr-x. 6 root root 4.1k Dec 8 00:46 TEST

实例4:排序文件大小,-S参数
[root@lianxi tmp]# ls -lhS /etc/
total 1.7M
-rw-r–r--. 1 root root 626K Oct 2 2013 services
-rw-r–r--. 1 root root 150K Dec 8 03:14 prelink.cache
-rw-r–r--. 1 root root 87K Dec 7 14:44 termcap
-rw-r–r--. 1 root root 43K Sep 23 2011 mime.types
drwxr-xr-x. 3 root root 4.0K Dec 7 14:43 abrt
drwxr-xr-x. 4 root root 4.0K Dec 7 14:44 acpi

实例5:测量大小,使用–block-size=key
ls可以通过使用–block-size=SIZE改单位大小。这里的SIZE是:
K = Kilobyte M = Megabyte G = Gigabyte T = Terabyte
P = Petabyte E = Exabyte Z = Zettabyte Y = Yottabyte
比如,我们希望使用KB作为单位大小。所以语法就会像这样:
[root@lianxi tmp]# ls -l --block-size=K /etc/
total 1680K
drwxr-xr-x. 3 root root 4K Dec 7 14:43 abrt
drwxr-xr-x. 4 root root 4K Dec 7 14:44 acpi
-rw-r–r--. 1 root root 1K Dec 7 23:31 adjtime
-rw-r–r--. 1 root root 2K Jan 12 2010 aliases
-rw-r–r--. 1 root root 12K Dec 7 14:45 aliases.db
drwxr-xr-x. 2 root root 4K Dec 7 14:44 alsa
drwxr-xr-x. 2 root root 4K Dec 7 14:43 alternatives

实例6:显示隐藏文件,即以.开头的文件,使用-a参数
[root@lianxi tmp]# ls -a
. … 1d 2d 3d 4d .ICE-unix TEST

实例7:只列出目录,使用-d参数
[root@lianxi etc]# ls -ld */
drwxr-xr-x. 3 root root 4096 Dec 7 14:43 abrt/
drwxr-xr-x. 4 root root 4096 Dec 7 14:44 acpi/
drwxr-xr-x. 2 root root 4096 Dec 7 14:44 alsa/
drwxr-xr-x. 2 root root 4096 Dec 7 14:43 alternatives/
drwxr-x—. 3 root root 4096 Dec 7 14:44 audisp/
drwxr-x—. 2 root root 4096 Dec 7 14:44 audit/
drwxr-xr-x. 2 root root 4096 Dec 7 14:44 bash_completion.d/
drwxr-xr-x. 2 root root 4096 Dec 7 14:45 blkid/

实例8:不打印所有者信息,使用-g参数
[root@lianxi etc]# ls -g
total 1680
drwxr-xr-x. 3 root 4096 Dec 7 14:43 abrt
drwxr-xr-x. 4 root 4096 Dec 7 14:44 acpi
-rw-r–r--. 1 root 46 Dec 7 23:31 adjtime
-rw-r–r--. 1 root 1512 Jan 12 2010 aliases
-rw-r–r--. 1 root 12288 Dec 7 14:45 aliases.db
drwxr-xr-x. 2 root 4096 Dec 7 14:44 alsa
drwxr-xr-x. 2 root 4096 Dec 7 14:43 alternatives
-rw-------. 1 root 541 Nov 23 2013 anacrontab
-rw-r–r--. 1 root 148 May 15 2009 asound.conf
-rw-r–r--. 1 root 1 Jan 30 2012 at.deny

实例9:不打印组信息,使用-G参数
[root@lianxi etc]# ls -lG
total 1680
drwxr-xr-x. 3 root 4096 Dec 7 14:43 abrt
drwxr-xr-x. 4 root 4096 Dec 7 14:44 acpi
-rw-r–r--. 1 root 46 Dec 7 23:31 adjtime
-rw-r–r--. 1 root 1512 Jan 12 2010 aliases
-rw-r–r--. 1 root 12288 Dec 7 14:45 aliases.db
drwxr-xr-x. 2 root 4096 Dec 7 14:44 alsa
drwxr-xr-x. 2 root 4096 Dec 7 14:43 alternatives
-rw-------. 1 root 541 Nov 23 2013 anacrontab
-rw-r–r--. 1 root 148 May 15 2009 asound.conf
-rw-r–r--. 1 root 1 Jan 30 2012 at.deny

实例10:打印UID和GID代替用户名和组名,使用-n参数
[root@lianxi etc]# ls -n
total 1680
drwxr-xr-x. 3 0 0 4096 Dec 7 14:43 abrt
drwxr-xr-x. 4 0 0 4096 Dec 7 14:44 acpi
-rw-r–r--. 1 0 0 46 Dec 7 23:31 adjtime
-rw-r–r--. 1 0 0 1512 Jan 12 2010 aliases
-rw-r–r--. 1 0 0 12288 Dec 7 14:45 aliases.db
drwxr-xr-x. 2 0 0 4096 Dec 7 14:44 alsa
drwxr-xr-x. 2 0 0 4096 Dec 7 14:43 alternatives
-rw-------. 1 0 0 541 Nov 23 2013 anacrontab
-rw-r–r--. 1 0 0 148 May 15 2009 asound.conf
-rw-r–r--. 1 0 0 1 Jan 30 2012 at.deny
drwxr-x—. 3 0 0 4096 Dec 7 14:44 audisp

实例11:不带颜色打印,使用–color=never参数
[root@lianxi etc]# ls -l --color=never
total 1680
drwxr-xr-x. 3 root root 4096 Dec 7 14:43 abrt
drwxr-xr-x. 4 root root 4096 Dec 7 14:44 acpi
-rw-r–r--. 1 root root 46 Dec 7 23:31 adjtime
-rw-r–r--. 1 root root 1512 Jan 12 2010 aliases
-rw-r–r--. 1 root root 12288 Dec 7 14:45 aliases.db
drwxr-xr-x. 2 root root 4096 Dec 7 14:44 alsa
drwxr-xr-x. 2 root root 4096 Dec 7 14:43 alternatives
-rw-------. 1 root root 541 Nov 23 2013 anacrontab
-rw-r–r--. 1 root root 148 May 15 2009 asound.conf
在这里插入图片描述
在这里插入图片描述

实例12:打印每个文件的索引号,使用-i参数
[root@lianxi etc]# ls -li
total 1680
918741 drwxr-xr-x. 3 root root 4096 Dec 7 14:43 abrt
791386 drwxr-xr-x. 4 root root 4096 Dec 7 14:44 acpi
790584 -rw-r–r--. 1 root root 46 Dec 7 23:31 adjtime
784914 -rw-r–r--. 1 root root 1512 Jan 12 2010 aliases
791636 -rw-r–r--. 1 root root 12288 Dec 7 14:45 aliases.db
791495 drwxr-xr-x. 2 root root 4096 Dec 7 14:44 alsa
785057 drwxr-xr-x. 2 root root 4096 Dec 7 14:43 alternatives
790861 -rw-------. 1 root root 541 Nov 23 2013 anacrontab
786368 -rw-r–r--. 1 root root 148 May 15 2009 asound.conf

实例13:增加/(斜线)标记目录,-p参数
[root@lianxi etc]# ls -lp
total 1680
drwxr-xr-x. 3 root root 4096 Dec 7 14:43 abrt/
drwxr-xr-x. 4 root root 4096 Dec 7 14:44 acpi/
-rw-r–r--. 1 root root 46 Dec 7 23:31 adjtime
-rw-r–r--. 1 root root 1512 Jan 12 2010 aliases
-rw-r–r--. 1 root root 12288 Dec 7 14:45 aliases.db
drwxr-xr-x. 2 root root 4096 Dec 7 14:44 alsa/
drwxr-xr-x. 2 root root 4096 Dec 7 14:43 alternatives/
-rw-------. 1 root root 541 Nov 23 2013 anacrontab
-rw-r–r--. 1 root root 148 May 15 2009 asound.conf

实例14:排序时反转顺序,使用-r参数
[root@lianxi etc]# ls -lr
total 1680
drwxr-xr-x. 2 root root 4096 Dec 7 14:43 yum.repos.d
-rw-r–r--. 1 root root 969 Feb 22 2013 yum.conf
drwxr-xr-x. 5 root root 4096 Dec 7 14:43 yum
drwxr-xr-x. 2 root root 4096 Dec 7 14:43 xinetd.d
drwxr-xr-x. 3 root root 4096 Dec 7 14:42 xdg
drwxr-xr-x. 4 root root 4096 Dec 7 14:43 X11
-rw-r–r--. 1 root root 0 Dec 7 14:47 wvdial.conf
-rw-r–r--. 1 root root 4479 Oct 10 2012 wgetrc
-rw-r–r--. 1 root root 3008 Nov 22 2013 warnquota.conf
-rw-r–r--. 1 root root 1962 Feb 17 2012 virc

实例15:递归列出子目录,使用-R参数
[root@lianxi etc]# ls -R /tmp/
/tmp/:
1d 2d 3d 4d TEST
/tmp/1d:
/tmp/2d:
/tmp/3d:
/tmp/4d:
/tmp/TEST:

实例16:扩展名排序,使用-X参数或者–sort=extension
[root@lianxi etc]# ls -lrX
total 1680
drwxr-xr-x. 2 root root 4096 Sep 27 2011 cron.weekly
-rw-r–r--. 1 root root 1112 Mar 31 2003 minicom.users
-rw-r–r--. 1 root root 43591 Sep 23 2011 mime.types
lrwxrwxrwx. 1 root root 15 Dec 7 14:43 rc.sysinit -> rc.d/rc.sysinit
-rw-r–r--. 1 root root 1 Dec 7 23:23 resolv.conf.save
-rw-r–r--. 1 root root 1909 Aug 1 2013 mail.rc
lrwxrwxrwx. 1 root root 56 Dec 7 14:42 favicon.png -> /usr/share/icons/hicolor/16x16/apps/system-logo-icon.png
-rw-r–r--. 1 root root 46 Nov 27 2013 issue.net
drwxr-xr-x. 2 root root 4096 Dec 7 14:44 cron.monthly

实例17:通过修改时间列出,比较新的会在前面,使用-t参数
[root@lianxi etc]# ls -lt
total 1680
-rw-r–r--. 1 root root 152927 Dec 8 03:14 prelink.cache
-rw-r–r--. 1 root root 264 Dec 7 23:31 mtab
-rw-r–r--. 2 root root 53 Dec 7 23:31 resolv.conf
-rw-r–r--. 1 root root 46 Dec 7 23:31 adjtime
drwxr-xr-x. 7 root root 4096 Dec 7 23:29 sysconfig
-rw-r–r--. 2 root root 165 Dec 7 23:28 hosts
-rw-r–r--. 1 root root 1 Dec 7 23:23 resolv.conf.save

实例18:列出主目录
[root@lianxi etc]# ls ~
anaconda-ks.cfg install.log install.log.syslog

实例19:列出父目录
[root@lianxi etc]# ls …/
bin boot data dev etc home lib lib64 lost+found media mnt opt proc root sbin selinux srv sys tmp usr var

实例20:打印ls命令版本
[root@lianxi etc]# ls --version
ls (GNU coreutils) 8.4
Copyright © 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Richard M. Stallman and David MacKenzie.
————————————————
版权声明:本文为CSDN博主「micjlxx」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/micjlxx/article/details/84889322

注明:所有转载内容皆直接从被转载文章网页的标题和内容的文本中复制而来

CC 4.0 BY-SA 版权协议网址:https://creativecommons.org/licenses/by-sa/4.0/deed.z

[实验] NFS 远程共享服务的搭建 (openSUSE Leap 15 版)

纪念:站主于 2020 年 1 月完成了此开源实验,并将过程中的所有命令经过整理和注释以后,形成以下教程

步骤一:规划拓扑

1.1 服务器列表

服务端 192.168.1.20
客户端 192.168.1.21

1.2 服务器列表简介

1) 服务器提供 NFS 服务将自己的目录分享
2) 客户端挂载和使用 NFS 服务将服务端分享的目录挂载在自己的目录上

步骤二:系统环境要求

1) 所有服务器的系统都需要是 openSUSE 15.1 版本
2) 所有服务器都要关闭防火墙
3) 所有服务器系统都要配置好可用的软件源(最好是软件数量最多的官方版本)
4) 需要按照拓扑图给对应的服务器配置好 IP 地址和主机名
5) 所有服务器都要可以相互 ping 通自己和对方的 IP 地址和主机名

步骤三:所有服务器安装 NFS 服务

3.1 所有服务器安装 NFS 服务

(分别在服务端和客户端上执行以下步骤)

# zypper install nfs-kernel-server
# zypper install nfs-client

3.2 设置所有服务器开机自启 NFS 服务

(分别在服务端和客户端上执行以下步骤)

# systemctl enable rpcbind
# systemctl enable nfsserver

3.3 所有服务器启动 NFS 服务

(分别在服务端和客户端上执行以下步骤)

# systemctl start rpcbind
# systemctl start nfsserver

步骤四:配置 NFS 服务

4.1 创建用于 NFS 服务的目录

4.1.1 创建被 NFS 服务共享的目录

(只在服务端上执行以下步骤)

# mkdir /nfsserver
4.1.2 创建用于挂载 NFS 服务分享目录的目录

(只在客户端上执行以下步骤)

# mkdir /nfsclient

4.2 配置服务端的 NFS 服务配置文件

4.2.1 在服务端上添加可被 NFS 服务挂载的选项

(只在服务端上执行以下步骤)

# vi /etc/exports

添加以下内容:

......
/nfsserver 192.168.1.21(rw,no_root_squash,no_subtree_check)

(补充:这里的 192.168.1.21 是客户端的 IP 地址)

4.2.2 让刚刚修改的 NFS 服务配置文件生效

(只在服务端上执行以下步骤)

# exportfs -a

4.3 配置客户端的 NFS 服务挂载文件

4.3.1 在客户端上添加开机挂载 NFS 的选项

(只在客户端上执行以下步骤)

# vi /etc/fstab

添加以下内容:

......
192.168.1.20:/nfsserver /nfsclient nfs  timeo=120,rw,soft,nolock  0 0
4.3.2 让刚刚修改的 NFS 服务挂载文件生效

(只在客户端上执行以下步骤)

# mount -a

步骤五:确认 NFS 服务是否搭建成功

(只在客户端上执行以下步骤)

# df -h | grep 192.168.1.20

(补充:如果出现了类似 “192.168.1.20:/nfsserver 38G 5.5G 31G 16% /nfsclient”,则代表 NFS 搭建成功了)

[命令] Linux 命令 find (查找文件或目录)

内容一:find 格式

# find <directory> <parameter> <parameter> ...... -print <-exec|-ok> <command|file> {<command>} \;

内容二:find 选项

1) 目录 指需要查找的目录
2) 参数 限制匹配的条件,可以有多个
3) -print 将匹配的结果进行标准输出
4) -exec 让后面的命令执行查找到的内容
5) -ok 和 -exec 的作用相同,只不过每执行一步就需要用户确认一下
6) command 对匹配的结果执行命令
7) file 对匹配的结果进行文件种类分析,后面的 {} 里的命令必须为空
8) 命令 对匹配结果要执行的命令

(注意:<-exec|-ok> {} \;,这是一个固定格式,谁也不能被缺少。并且 {<命令>} 和 \ 之间有一个空格)

9) -xdev 不显示警告,只显示查找到的内容,这样显示起来简单美观

内容三:find 参数

3.1 匹配名称的参数

1) -name n 名称是 n 的文件或目录
2) -iname n 名称是 n 的文件或目录,并且忽略大小写
3) -lname n 名称是 n 的链接

(注意:在 3) 参数中,n 是被链接文件的名称而不是链接文件本身的名称)

3.2 匹配属性的参数

-type n :文件类型是 n 的文件

(补充:这里的 n 可以是 d、f、l,它们分别代表目录、文件、链接)

3.3 匹配所有者的参数

1) -uid n 所属 gid 是 n 的文件或目录
2) -user n 所属主名称为 n 的文件或目录
3) -gid n 所属 gid 是 n 的文件或目录
4) -group n 所属组名称为 n 的文件或目录
5) -nouser 匹配无所属主的文件或目录
6) -nogroup 匹配无所属组的文件或目录

3.4 匹配权限的参数

1) -perm /u=n 所属主权限是 n 的文件或目录
2) -perm /u=n 所属组权限是 n 的文件或目录
3) -perm /o=n 所其他的权限是 n 的文件或目录

(补充:在以上 1) 到 3) 参数中 n 可以是 r、w、x 它们分别代表读权限、写权限、执行权限)

4) -perm -n 所属主、所属组、其他的权限是 n 的文件或目录

(补充:在 (4 参数中 n 是可以是 1、2、4)

5) -perm nnn 权限是 nnn 的文件或目录


补充:
在 5) 参数中:
n 是数字
第一个 n 是特殊权限
第二个 n 是所属主的权限
第三个 n 是所属组的权限
第四个 n 是其他的权限
例:600

6) -perm nnnn 权限是 nnnn 的文件或目录


补充:
在 6) 参数中:
n 是数字
第一个 n 是特殊权限
第二个 n 是所属主的权限
第三个 n 是所属组的权限
第四个 n 是其他的权限
例:0600

3.5 匹配时间的参数

1) -amin n 在过去 n 分钟内被读取过的文件或目录
2) -anewer file 比文件 file 更晚被读取过的文件或目录
3) -atime n 在过去 n 天内被读取过的文件或目录
4) -mmin n 在过去 n 分钟内被修改过内容的文件或目录
5) -mnewer file 比文件 file 更晚被修改过内容的文件或目录
6) -mtime n 在过去 n 天内被修改过内容的文件或目录
7) -cmin n 在过去 n 分钟内被修改过属性(比如新建)的文件或目录
8) -cnewer file 比文件 file 更晚被修改过属性(比如新建)的文件或目录
9) -ctime n 在过去 n 天内被修改过属性(比如新建)的文件或目录

(补充:在以上 1) 到 9) 参数中 n 是数字, +n 表示 n 以前,-n 表示 n 以内,n 表示第 n)

3.6 匹配大小的参数

1) -empty 为空的文件或目录
2) -size n 文件大小是 n 的文件或目录

(补充:在 2) 参数中 n 是数字, +n 表示比 n 大,-n 表示比 n 小,单位有 b、m、g 等)

3.7 匹配目录的参数

1) -path ‘n’ 路径名称符合 n 的文件或目录
2) -ipath ‘n’ 路径名称符合 n 的文件或目录,并且忽略大小写
3) -depth n 路径深度为 n 的文件或目录
4) -maxdepth n 最大路劲深度为 n 的文件或目录

(补充:在以上 1) 到 4) 参数中 n 是数字)

3.8 匹配格式化方式的参数

1) -fstype n 匹配硬盘格式为 n 文件或目录

(补充:1) 参数中 n 可以是 ext3、ext4、xfs 等硬盘格式)

2) -mount 是被挂载的文件或目录

3.9 逻辑匹配参数

1) -not 不匹配后面的参数,需要放在其他参数前面
2) ! 不匹配后面的参数,需要放在其他参数前面
3) -a 和,需要放在两个参数之前
4) -and 和,需要放在两个参数之前
5) -o 或,需要放在两个参数之前
6) -or 或,需要放在两个参数之前
7) <parameter> ( <parameter> n <parameter> ) 指定将某两个参数作为一个整体和另一个逻辑参数进行匹配
8) <parameter> -prune -n <parameter> <parameter> 指定某两个参数进行逻辑匹配

(补充:在以上 7) 到 8) 参数中 n 可以是 -a、-and、-o、-or 以上 3) 到 6) 参数)

内容四:find 的使用案例

4.1 案例一:匹配名称的参数

4.1.1 在当前目录查找以 txt 结尾的文件和目录
# find . -name "*\.txt"
4.1.2 在当前目录查找以 txt 结尾,但名称不为 test1.txt 和 test2.txt 的文件和目录
# find . -name "*\.txt" -not -name "test1.txt" -not -name "test2.txt"
4.1.3 在 /var/log 目录查找所有名称不为 wtmp、btmp 和 lastlog 的文件,并去掉它们的其它权限的读、写和执行
# find /var/log -type f -not -name "wtmp" -not -name "btmp" -not -name "lastlog" -exec chmod o-rwx {} +

4.2 案例二:匹配属性的参数

4.2.1 在当前目录查找目录
# find . -type d
4.2.2 在 /tmp 目录查找 1 级目录,并把当前目录下的 .htaccess 文件拷贝到查找到的目录下,并重新命名为 .htaccess.orig
# find /tmp/ -maxdepth 1 -type d -exec cp -p -f ./.htaccess.orig '{}'/.htaccess \;

4.3 案例三:匹配所有者的参数

4.3.1 在当前目录查找所属主是 root 的文件和目录
# find . -user root
4.3.2 在当前目录查找没有所属主和所属组的文件和目录
# find . -nouser -o -nogroup
4.3.3 查找所有所属主是 natasha 的文件并将它们复制到 /root/findfiles 目录下
# find / -user natasha -exec cp -rp {} /root/findfiles/ \;

4.4 案例四:匹配权限的参数

4.4.1 在当前目录查找所有权限为 0777 的文件,并将其显示出来
# find . -type f -perm 0777 -exec ls -l {}\;
4.4.2 在当前目录查找权限为 222 的文件
# find . -type f -perm -2
4.4.3 在当前目录查找有任何执行权限的文件和目录
# find . -perm /a=x
4.4.4 查找所有包含特殊权限的文件和目录
# find / -type f \( -perm -1000 -o -perm -2000 -o -perm -4000 \) -print

4.5 案例五:匹配时间的参数

4.5.1 在当前目录查找 10 天内被修改过的文件
# find . -ctime -10
4.5.2 在 /var/log/mysql 目录查找在过去 3 天被修过的文件和目录,并在确认后将其删除
# find /var/log/mysql -type f -mtime +3 -ok rm {}\;

(注意:此方法会出现交互界面,需要在交互界面确认以后才会对找到的文件进行操作)

4.6 案例六:匹配大小的参数

4.6.1 在当前目录查找所有大小为 0 的文件,并将其显示出来
# find . -type f -size 0 -exec ls -l {} \;
4.6.2 在当前目录查找所有大小为 0 的文件
# find . -type f -empty

4.7 案例七:匹配目录的参数

在 / 目录和 /proc/ 目录查找文件格式为 ext3 无所属主或所属组的文件和目录

# find / -path '/proc/*' -prune -o -fstype ext3 -nouser -o -nogroup

4.8 案例八:匹配格式化方式的参数

在 \ 目录查找以

# find . -type f

4.9 案例九:逻辑匹配参数

4.9.1 在当前目录查找以 “.txt” 结尾和以 “.dox” 结尾的文件
# find . -type f \( -name "*.txt" -or -name "*.dox" \)
4.9.2 在当前目录查找权限不是 777 的文件
# find . -type f ! -perm 777
4.9.3 在当前目录查找权限为 2000 和 4000 的文件,并分析文件属性
# find . -type f \( -perm -2000 -o -perm -4000 \) -exec file {} \;
4.9.4 在根 “/” 目录查找没有所属主和所属组的文件和目录
# find / -nouser -o -nogroup