find命令

find命令

一、简介

find命令是Linux中常用的用来查找的命令,来看看man手册中是怎么形容它的: find - search for files in a directory hierarchy
在目录层,或者说是目录树上寻找文件。也就是说find是按照当前的目录结构一层一层的寻找文件。

二、命令语法参数

[root@redhatclient ~]# find --help
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

default path is the current directory; default expression is -print
expression may consist of: operators, options, tests, and actions:

operators (decreasing precedence; -and is implicit where no others are given):
      ( EXPR )   ! EXPR   -not EXPR   EXPR1 -a EXPR2   EXPR1 -and EXPR2
      EXPR1 -o EXPR2   EXPR1 -or EXPR2   EXPR1 , EXPR2

positional options (always true): -daystart -follow -regextype

normal options (always true, specified before other expressions):
      -depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf
      --version -xdev -ignore_readdir_race -noignore_readdir_race

tests (N can be +N or -N or N): -amin N -anewer FILE -atime N -cmin N
      -cnewer FILE -ctime N -empty -false -fstype TYPE -gid N -group NAME
      -ilname PATTERN -iname PATTERN -inum N -iwholename PATTERN -iregex PATTERN
      -links N -lname PATTERN -mmin N -mtime N -name PATTERN -newer FILE
      -nouser -nogroup -path PATTERN -perm [+-]MODE -regex PATTERN
      -readable -writable -executable
      -wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N
      -used N -user NAME -xtype [bcdpfls]
      -context CONTEXT


actions: -delete -print0 -printf FORMAT -fprintf FILE FORMAT -print 
      -fprint0 FILE -fprint FILE -ls -fls FILE -prune -quit
      -exec COMMAND ; -exec COMMAND {} + -ok COMMAND ;
      -execdir COMMAND ; -execdir COMMAND {} + -okdir COMMAND ;

Report (and track progress on fixing) bugs via the findutils bug-reporting
page at http://savannah.gnu.org/ or, if you have no web access, by sending
email to <bug-findutils@gnu.org>.

三、命令实践

1.使用find打印当前目录下面的所有文件夹和文件:
[root@redhatclient ~]# find .
[root@redhatclient ~]# find
[root@redhatclient ~]# find . -print

这三条命令的效果是一样的,在find –help中可以看到,find默认的目录是指定的当前目录,默认的表达方式是-print。
其中,-print还可以使用-print0来替换,替换之后就会使用’/0’来作为每一次输出的分隔符:

[root@redhatclient ~]# find . -print0
../.bash_profile./.bash_logout./Desktop./.pki./.pki/nssdb./.ssh./.gnote./.gnote/addins./.gnote/67eef83c-6613-449f-bd87-ccb1a538dcf6.note./.gnote/db428298-f8be-4c39-bb1b-11f9f5df9b1e.note./.xsession-errors./install.log.syslog./.esd_auth./test./test/output.session./test/test./test/f1./test/t1.data./test/log./test/check./test/timing.log./.gnome2./.gnome2/nautilus-scripts./.gnome2/keyrings./.gnome2/keyrings/login.keyring./.gnome2/panel2.d./.gnome2/panel2.d/default./.gnome2/panel2.d/default/launchers./Music./.ICEauthority./.gconfd./.gconfd/saved_state./.tcshrc./.bashrc./.bash_history./Pictures./.cache./.cache/event-sound-cache.tdb.25f1c4e7d13ee56480f6a52700000017.x86_64-redhat-linux-gnu./.cache/matplotlib./.cache/matplotlib/tex.cache./.cache/matplotlib/fontList.cache./.cshrc./.gvfs./.gtk-bookmarks./anaconda-ks.cfg./.pulse-cookie./install.log./Public./.gnupg./.gnupg/pubring.gpg./.gnupg/gpg.conf./.gnupg/trustdb.gpg./.gnupg/secring.gpg./.veritas./.veritas/java./.veritas/java/770000./.veritas/java/770000/_vrts_nbu_client_JBP_JVTable_vrts.nbu.client.JBP.ProgressPanel$TaskTableModel_Status_prefs.properties./.veritas/java/770000/_vrts_nbu_client_JBP_JVTable_RestoreTableFilter_prefs.properties./.veritas/java/770000/_vrts_nbu_client_JBP_JVTable_vrts.nbu.client.JBP.ProgressPanel$TaskTableModel_prefs.properties./.veritas/java/770000/_vrts_nbu_client_JBP_JVTable_RestoreTableFilter_Quiesced_prefs.properties./.veritas/java/770000/_vrts_nbu_client_JBP_JVTable_vrts.nbu.client.JBP.ProgressPanel$TaskTableModel_Job Id_prefs.properties./.veritas/java/770000/_vrts_nbu_client_JBP_JVTable_BackupTableModel_Modified_prefs.properties./.veritas/java/770000/_vrts_nbu_client_JBP_JVTable_RestoreTableFilter_Modified_prefs.properties./.veritas/java/770000/_vrts_nbu_client_JBP_JVTable_RestoreTableFilter_Policy_prefs.properties./.veritas/java/770000/_vrts_nbe_NBELoginLists_vrts-nbe-AdminConsole_prefs.properties./.veritas/java/770000/_vrts_nbu_client_JBP_JVTable_RestoreTableFilter_Backup Date_prefs.properties./.veritas/java/770000/_vrts_nbu_client_JBP_JVTable_RestoreTableFilter_Data Mover_prefs.properties./.veritas/java/770000/_vrts_nbu_client_JBP_JVTable_RestoreTableFilter_Schedule Type_prefs.properties./.veritas/java/770000/_vrts_nbu_client_JBP_JVTable_BackupTableModel_Name_prefs.properties./.veritas/java/770000/_vrts_nbu_client_JBP_JVTable_RestoreTableFilter_Size(Bytes)_prefs.properties./.veritas/java/770000/_vrts_nbu_client_JBP_JVTable_BackupTableModel_Size(Bytes)_prefs.properties./.veritas/java/770000/_vrts_nbe_NBEWindowPreferences_vrts-nbe-AdminConsole_prefs.properties./.veritas/java/770000/_vrts_nbu_client_JBP_JVTable_RestoreTableFilter_Image Format_prefs.properties./.veritas/java/770000/_vrts_nbu_client_JBP_JVTable_BackupTableModel_prefs.properties./.veritas/java/770000/_vrts_nbu_client_JBP_JVTable_RestoreTableFilter_Name_prefs.properties./.veritas/java/770000/_vrts_nbu_client_JBP_JVTable_vrts.nbu.client.JBP.ProgressPanel$TaskTableModel_Date_prefs.properties./.veritas/java/770000/_vrts_nbu_client_JBP_JVTable_vrts.nbu.client.JBP.ProgressPanel$TaskTableModel_Task_prefs.properties./.gconf./.gconf/apps./.gconf/apps/%gconf.xml./.gconf/apps/nautilus./.gconf/apps/nautilus/%gconf.xml./.gconf/apps/nautilus/desktop-metadata./.gconf/apps/nautilus/desktop-metadata/directory./.gconf/apps/nautilus/desktop-metadata/directory/%gconf.xml./.gconf/apps/nautilus/desktop-metadata/%gconf.xml./.gconf/apps/nautilus/desktop-metadata/home./.gconf/apps/nautilus/desktop-metadata/home/%gconf.xml./.gconf/apps/nautilus/desktop-metadata/trash./.gconf/apps/nautilus/desktop-metadata/trash/%gconf.xml./.gconf/apps/nautilus/desktop-metadata/computer./.gconf/apps/nautilus/desktop-metadata/computer/%gconf.xml./.gconf/apps/panel./.gconf/apps/panel/%gconf.xml./.gconf/apps/panel/applets./.gconf/apps/panel/applets/clock./.gconf/apps/panel/applets/clock/%gconf.xml./.gconf/apps/panel/applets/clock/prefs./.gconf/apps/panel/applets/clock/prefs/%gconf.xml./.gconf/apps/panel/applets/%gconf.xml./.gconf/apps/panel/applets/window_list./.gconf/apps/panel/applets/window_list/%gconf.xml./.gconf/apps/panel/applets/window_list/prefs./.gconf/apps/panel/applets/window_list/prefs/%gconf.xml./.gconf/apps/panel/applets/workspace_switcher./.gconf/apps/panel/applets/workspace_switcher/%gconf.xml./.gconf/apps/panel/applets/workspace_switcher/prefs./.gconf/apps/panel/applets/workspace_switcher/prefs/%gconf.xml./.gconf/apps/gnome-session./.gconf/apps/gnome-session/%gconf.xml./.gconf/apps/gnome-session/options./.gconf/apps/gnome-session/options/%gconf.xml./.gconf/apps/gnote./.gconf/apps/gnote/%gconf.xml./.gconf/apps/gnome-terminal./.gconf/apps/gnome-terminal/%gconf.xml./.gconf/apps/gnome-terminal/profiles./.gconf/apps/gnome-terminal/profiles/%gconf.xml./.gconf/apps/gnome-terminal/profiles/Default./.gconf/apps/gnome-terminal/profiles/Default/%gconf.xml./.gconf/apps/nm-applet./.gconf/apps/nm-applet/%gconf.xml./.gconf/desktop./.gconf/desktop/%gconf.xml./.gconf/desktop/gnome./.gconf/desktop/gnome/%gconf.xml./.gconf/desktop/gnome/accessibility./.gconf/desktop/gnome/accessibility/%gconf.xml./.gconf/desktop/gnome/accessibility/keyboard./.gconf/desktop/gnome/accessibility/keyboard/%gconf.xml./.xsession-errors.old./sh1./.local./.local/share./.local/share/.converted-launchers./.local/share/applications./.local/share/applications/preferred-mail-reader.desktop./.local/share/applications/preferred-web-browser.desktop./.dbus./.dbus/session-bus./.dbus/session-bus/25f1c4e7d13ee56480f6a52700000017-0./.config./.config/user-dirs.dirs./.config/user-dirs.locale./.config/gnome-disk-utility./.config/gnome-disk-utility/ata-smart-ignore./.config/gnome-session./.config/gnome-session/saved-session./.config/matplotlib./.lesshst./.Xauthority./Templates./Documents./.pulse./.pulse/25f1c4e7d13ee56480f6a52700000017-stream-volumes.tdb./.pulse/25f1c4e7d13ee56480f6a52700000017-runtime./.pulse/25f1c4e7d13ee56480f6a52700000017-device-volumes.tdb./.pulse/25f1c4e7d13ee56480f6a52700000017-default-source./.pulse/25f1c4e7d13ee56480f6a52700000017-card-database.tdb./.pulse/25f1c4e7d13ee56480f6a52700000017-default-sink./Downloads./sh2./Videos./.nautilus[root@redhatclient ~]#
2.使用find搜索指定文件名的文件
[root@redhatclient ~]# find -name "*.log"
./test/timing.log
./install.log

在find后接-name参数即可搜索指定文件名的文件,*.log代表了所有以.log结尾的文件夹和文件。还有一个参数-iname,指的是在匹配名字的时候忽略大小写。

[root@redhatclient ~]# find -name "*.LOG"
[root@redhatclient ~]# find -iname "*.LOG"
./test/timing.log
./a.log
./install.log
[root@redhatclient ~]# 
3.使用find中的操作符(operators)
[root@redhatclient ~]# find . -name "*.txt"
./test/1.txt
[root@redhatclient ~]# find . -name "*.log"
./test/timing.log
./a.log
./install.log
[root@redhatclient ~]# find . \( -name "*.txt" -o -name "*.log" \)
./test/timing.log
./test/1.txt
./a.log
./install.log
[root@redhatclient ~]# find . \( -name "*.txt" -or -name "*.log" \)
./test/timing.log
./test/1.txt
./a.log
./install.log

\(以及\)将其中的内容作为一个整体,-o或者-or指逻辑或的意思,会将符合至少两个条件中一个的文件或者文件夹打印出来。同类的操作符还有-a或者-and。

4.使用-type搜索指定文件类型的文件
文件类型命令参数
普通文件f
符号链接l
目录d
字符设备c
块设备b
套接字s
FIFOp

比如,搜索所有的目录

[root@redhatclient test]# mkdir a
[root@redhatclient test]# find . -type d
.
./a
[root@redhatclient test]# 

或者搜索普通文件

[root@redhatclient test]# find . -type f
./output.session
./test
./f1
./t1.data
./log
./check
./timing.log
./1.txt
[root@redhatclient test]# 
5.根据文件时间搜索

可以根据文件的访问时间或者修改时间或者变化时间来搜索符合条件的文件。

时间类型说明
访问时间(-atime)用户最近一次访问文件的时间。
修改时间(-mtime)文件内容最后一次被修改的时间。
变化时间(-ctime)文件元数据(matedata,如权限或所有权)最后一次改变的时间。

如:
查看最近七天和一天内被访问的文件或者目录

[root@redhatclient test]# find . -atime -7
.
./f1
./a
./1.txt
[root@redhatclient test]# find . -atime -1
.
./a

查看今天被访问的文件或者目录

[root@redhatclient test]# find . -atime 0
.
./a
[root@redhatclient test]# 

查看访问时间超过3天的文件或者目录

[root@redhatclient test]# find . -atime +3
./output.session
./test
./f1
./t1.data
./log
./check
./timing.log
[root@redhatclient test]# 

-mtime和-ctime的用法相同,可以看到,搜索都是以天为单位,可以基于分钟来作为搜索单位,-amin,-mmin和-cmin。用法相同。
-newer参数可以找出比指定文件更新(修改时间更短)的文件

[root@redhatclient test]# find . -newer f1
.
./a
./1.txt
6.根据文件大小来搜索

比如分别查找文件大小大于小于和等于4k的文件。

[root@redhatclient test]# ls -al
total 88
drwxr-xr-x   3 root root  4096 Feb 20 04:11 .
dr-xr-x---. 27 root root  4096 Feb 16 06:41 ..
-rw-r--r--   1 root root     0 Feb 16 06:49 1.txt
drwxr-xr-x   2 root root  4096 Feb 20 04:11 a
-rwxr--r--   1 root root 26486 Feb  8 23:30 check
-rw-r--r--   1 root root    48 Feb 15 00:40 f1
-rw-r--r--   1 root root   121 Jan 31 18:19 log
-rw-r--r--   1 root root 26318 Feb  4 18:56 output.session
-rw-r--r--   1 root root  1024 Feb  5 23:12 t1.data
-rwxr--r--   1 root root    45 Feb  7 22:17 test
-rw-r--r--   1 root root  1115 Feb  4 18:56 timing.log
[root@redhatclient test]# find . -size +4k
./output.session
./check
[root@redhatclient test]# find . -size -4k
./test
./f1
./t1.data
./log
./timing.log
./1.txt

[root@redhatclient test]# find . -size 4k
.
./a
[root@redhatclient test]# 

除了指定k这个单位之外,还可以指定M(大写)兆以及G(大写)等单位。

7.其它参数

可以接-delete参数来删除find命令查找到的匹配的文件

[root@redhatclient test]# ls
1.txt  a  check  f1  log  output.session  t1.data  test  timing.log
[root@redhatclient test]# find . -size 4k
.
./a
[root@redhatclient test]# find . -size 4k -delete
[root@redhatclient test]# ls
1.txt  check  f1  log  output.session  t1.data  test  timing.log
[root@redhatclient test]# 

可以接-perm根据文件的权限进行匹配

[root@redhatclient test]# ls -al
total 84
drwxr-xr-x   2 root root  4096 Feb 20 04:47 .
dr-xr-x---. 27 root root  4096 Feb 16 06:41 ..
-rw-r--r--   1 root root     0 Feb 16 06:49 1.txt
-rwxr--r--   1 root root 26486 Feb  8 23:30 check
-rw-r--r--   1 root root    48 Feb 15 00:40 f1
-rw-r--r--   1 root root   121 Jan 31 18:19 log
-rw-r--r--   1 root root 26318 Feb  4 18:56 output.session
-rw-r--r--   1 root root  1024 Feb  5 23:12 t1.data
-rwxr--r--   1 root root    45 Feb  7 22:17 test
-rw-r--r--   1 root root  1115 Feb  4 18:56 timing.log
[root@redhatclient test]# find . -perm 644
./output.session
./f1
./t1.data
./log
./timing.log
./1.txt
[root@redhatclient test]# 

还可以根据user或者group来搜索

[root@redhatclient test]# find . -group root
.
./output.session
./test
./f1
./t1.data
./log
./check
./timing.log
./1.txt
[root@redhatclient test]# find . -user root
.
./output.session
./test
./f1
./t1.data
./log
./check
./timing.log
./1.txt
[root@redhatclient test]# 
8.结合find执行命令

find后面跟-exec参数可以对搜索出来的文件执行相应的命令

[root@redhatclient test]# find -type f -exec file {} \;
./output.session: ASCII text, with CRLF, LF line terminators, with escape sequences, with overstriking
./test: UTF-8 Unicode text
./f1: ASCII text
./t1.data: data
./log: UTF-8 Unicode text
./check: Bourne-Again shell script text executable
./timing.log: ASCII text
./1.txt: empty
[root@redhatclient test]# 

其中{}与-exec结合使用。{}会被替代成每一个寻找出来的文件。

9.结合正则表达式使用

find后跟-regex,可以指定正则表达式来匹配

[root@redhatclient test]# ls
1.txt  a  check  f1  log  output.session  t1.data  test  timing.log
[root@redhatclient test]# find . -regex ".*\.log$"
./timing.log
[root@redhatclient test]# 

正则表达式被单引号或者双引号括起,其中开头的.*代表任意多个字符,\.代表转义的字符.,$代表字符串结尾。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值