通过26个实例掌握 linux find 命令

find 命令有什么用 ?

find 命令在 Linux 和 UNIX 系统中用于查找文件和目录,并可对其执行后续操作。

在 find 命令的帮助下,系统管理员可以根据一些搜索条件查找所需的文件。我们可以使用单个或组合多个标准,然后可以对获得的结果执行操作。可选搜索条件:文件名称、目录名称、创建日期、修改日期、属主、权限。

find 命令语法格式:

$ find <path> {file-or-directory-name} <options> <action-on-result>

<action-on-result> 可选项:

  • – delete : 删除文件或目录
  • -exec command {}; : 根据 find 命令的结果执行命令
  • -ok command : 它将运行与 -exec 相同的命令,但它将在实际执行之前提示

(1) 查找当前工作目录下的所有文件和目录

若要只查找目录,请运行

$ find . -type d

若要只查找文件,请运行

$ find . -type f

(2) 列出特定目录下的所有文件

假设我们要列出 /home/linuxtechi/Downlods 目录下的所有文件和目录,运行

$ find /home/linuxtechi/Downloads

如果只查找文件,请运行

$ find /home/linuxtechi/Downloads -type f

如果只查找目录,请运行

$ find /home/linuxtechi/Downloads -type d

find-files-directories-particular-folder-linux

(3) 从目录中按文件名查找文件

要在特定目录中按名称查找文件,请运行

$ sudo find /home -type f -name cleanup.sh

上面的命令将在 /home 文件夹中查找 cleanup.sh 文件。我们还可以在 /var/log 目录中查找扩展名为 .log 的所有文件,运行

$ sudo find /var/log -type f -name *.log

find-files-based-on-exetension-linux

(4) 在多个目录中查找文件

假设我们想在 /home 和 /root 文件夹中找到 .sh 扩展名的文件,请运行

$ sudo find /home /root -type f -name '*.sh'

find-files-from-multiple-directories-linux

(5) 忽略文件名大小写

为了查找文件名不受大小写影响,我们可以在 find 命令中使用 -iname 选项

$ sudo find /home -type f -iname CleanUP.SH
/home/linuxtechi/automation/cleanup.sh
$

(6) 查找除上述类型之外的所有文件类型

假设我们要查找所有不是上述类型的文件,为此可以在 find 命令中使用 -not 选项,如下所示

$ sudo find /home -type f -not -name '*.mp3'

(7) 使用多个条件查找文件

我们还可以组合多个条件来使用正则表达式搜索文件。假设我们想在主目录中搜索扩展名为 .sh 和 .mp3 的文件,运行

$ find $HOME -regex ".*\.\(sh\|mp3\)$"

Regular-Experssion-find-command

(8) 使用 OR 条件查找文件

我们还可以组合多个搜索条件,然后使用 OR 操作符根据一个条件中的任何一个条件的满足来查找文件

$ find $HOME -name "*.sh" -o -name "jumpscripts"
/home/linuxtechi/automation/cleanup.sh
/home/linuxtechi/dumpdata.sh
$

9) 根据权限查找文件

要根据权限查找文件,请在查找命令中使用 - perm 选项。

例如在 /home 目录中查找具有 0777 权限的所有文件,运行

$ sudo find /home -type f -perm 0777

在用户目录查找所有可执行文件

$ find $HOME -type f -perm /a+x

(10) 查找所以隐藏文件

要搜索用户主目录中的所有隐藏文件,请运行命令

$ find $HOME -type f -name ".*"

(11) 找到所有具有 SGID 的文件

要定位所有具有 SGID 位的文件,我们可以使用

$ sudo find / -perm /g=s

(12) 找到所有带有 SUID 的文件

要定位所有 SUID 位的文件,运行

$ sudo find / -perm /u=s

(13) 查找所有可读但没有执行权限的文件

若要只查找所有人都可读但任何人都不能执行的文件,请运行

$ find $HOME -perm -a+r \! -perm /a+x

(14) 搜索几种文件类型

在一个查找命令中,我们可以搜索多种文件类型

$ find $HOME -type f,d,l

(15) 查找用户拥有的所有文件

To locate all the file that are owned by a particular user in /home directory, run following command,

要找到 /home 目录中属于特定用户的所有文件,请运行以下命令

$ sudo find $HOME -user linuxtechi

(16) 找到一个组拥有的所有文件

下面的命令将搜索 apache 组拥有的所有文件。

$ sudo find / -group apache

(17) 按文件大小查找所有文件

Use ‘-size’ option in find command to search files based on the size.
Run following command to find all files whose size is exactly 50MB.

使用 -size 选项根据文件大小进行搜索

查找等于 50MB 的文件

$ find $HOME -size 50M
/home/linuxtechi/dbstuff
$

查找大于 50MB 的文件

$ find $HOME -size +50M

查找小于 50MB 的文件

$ find $HOME -size -50M

查找大小在 40MB 到 500MB 之间的文件

$ find $HOME -size +40M -size -500M

(18) 不下行目录到其他文件系统

-xdev 选项列出了另一个文件系统中的挂载点或分区,但它不会向下查找它们。

下面的命令将在 / 目录中搜索大于 100MB 的所有文件,排除其他挂载的文件系统,重定向错误消息到 /dev /null

$ find / -xdev -size +100M 2>/dev/null

xdev-option-find-command

(19) 查找 N 天前修改过的文件

假设我们想要定位 10 天前修改过的所有文件。我们可以使用 -mtime 选项来实现这一点

$ sudo find / -mtime 10 2>/dev/null

(20) 找出 N 天前被访问过的文件

假设我们想要定位 10 天前被访问过的所有文件。我们可以使用 -atime 选项来实现这一点

$ sudo find / -atime 30 2>/dev/null

(21) 找到所有的空文件和目录

要在用户主目录中搜索所有空文件,请运行

$ find $HOME -type f -empty
or 
$ find $HOME -type f -size 0

类似地,定位所有空目录

$ find $HOME -type d -empty

(22) 搜索和删除文件

使用 find 命令,我们在单个命令中搜索和删除文件,-delete 选项可以删除文件。

在下面的例子中,我们正在从用户的主目录中搜索并删除 mp3 文件

$ find $HOME -type f -name "*.mp3" -delete

Search-and-delete-files-find-command-linux

注意: 上面是破坏性命令,执行它时要小心。

(23) 查找最大和最小的文件

To find largest and smallest file, we will combine sort command with find command & if we further want to list top three of those largest files, we will combine head

为了找到最大和最小的文件,我们将结合 sort 命令和 find 命令,如果我们进一步想列出最大文件中的前三个,我们将结合 head 命令列出用户主目录中前三个文件

$ find $HOME -type f -exec ls -s {} \; | sort -n -r | head -3
51200 /home/linuxtechi/dbstuff
8276 /home/linuxtechi/.cache/gnome-software/appstream/components.xmlb
2764 /home/linuxtechi/.local/share/gnome-photos/tracker3/private/meta.db-wal
$

我们同样可以在用户的主目录中找到最小的文件

$ find $HOME -type f -exec ls -s {} \; | sort -n | head -3

(24) 找到所有日志文件并将它们重定向到一个文件

To run command on find command result use -exec option, it’s syntax given below,

要在查找结果上运行命令,使用 -exec 选项,其语法如下所示

$ find -exec {} ;

以下命令将找到所有文件,并将其名称重定向到 /tmp/logsfiles.txt

$ sudo find /var -type f -name '*.log' -exec ls -lah {} \; > /tmp/logfiles.txt

find-log-files-redirect-them-linux

(25) 搜索文件并更改其权限

假设我们想要搜索权限为 777 的所有文件,并将其权限更改为 644

$ find $HOME -type f -perm 777 -exec chmod 644 {} \;

(26) 从文件中搜索文本

Let’s assume we want to search error word in all log files, run following command

假设我们要在所有日志文件中搜索带有 error 关键字的文件,运行以下命令

$ sudo find /var -type f -name '*.log' -exec grep -i 'error' {} \;

在上面的命令中,我们结合了 find 和 grep 命令来完成任务。

我的开源项目

酷瓜云课堂-开源网校系统

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值