Linux下find命令在目录结构中搜索文件,并执行指定的操作。Linux下find命令提供了相当多的查找文件,功能很强大。由于find具有强大的功能,所以它的选项也很多,需要我们花时间来了解。
命令格式:
find pathname -options [-print -exec -ok ...]
命令功能:
用于在文件树种查找文件,并作出相应的处理(可能访问磁盘)
命令参数:
pathname: find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。
-print: find命令将匹配的文件输出到标准输出。
-exec: find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' { } \;,注意{ }和\;之间的空格。
-ok: 和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行 每一个命令之前,都会给出提示,让用户来确定是否执行。
#-print 将查找到的文件输出到标准输出
#-exec command {} \; —–将查到的文件执行command操作,{} 和 \;之间有空格
#-ok 和-exec相同,只不过在操作前要询用户
执行命令操作时,我先在Linux中建立一个name文件。
[ningning@bogon ~]$ mkdir name [ningning@bogon ~]$ ls Desktop Documents Downloads Music name Pictures Public Templates Videos
-name 按照文件名来查找文件。
[ningning@bogon ~]$ ls Desktop Documents Downloads Music name Pictures Public Templates Videos [ningning@bogon ~]$ find -name name //查找文件名为name的文件 ./name [ningning@bogon ~]$ find . -name "*.txt" -print //在当前目录查找.txt文件并显示 ./test.txt [ningning@bogon ~]$ find ~ -name "*.xml" -print //在$ HOME中查找.xml文件并显示 /home/ningning/.gconf/desktop/%gconf.xml /home/ningning/.gconf/desktop/gnome/accessibility/%gconf.xml /home/ningning/.gconf/desktop/gnome/accessibility/keyboard/%gconf.xml /home/ningning/.gconf/desktop/gnome/%gconf.xml /home/ningning/.gconf/apps/nautilus/%gconf.xml [ningning@bogon ~]$ find -name "[A-Z]*" -print //查找以大写字母开头的文件 ./Downloads ./.gconf/apps/gnome-terminal/profiles/Default ./Templates ./Documents ./Music ./Pictures ./Public ./Videos ./Desktop [ningning@bogon ~]$ find -name "P*" -print //查找以大写字母P开头的文件 ./Pictures ./Public [ningning@bogon ~]$ find -name "*s" -print //查找以小写字母s结尾的文件 ./.xsession-errors ./.gvfs ./.gnome2/keyrings ./.gnome2/panel2.d/default/launchers ./.gnome2/nautilus-scripts ./.dbus ./.dbus/session-bus ./.gnote/addins ./.mozilla/plugins ./.mozilla/extensions ./Downloads ./.gtk-bookmarks ./.local/share/applications ./.local/share/.converted-launchers ./.nautilus ./.gconf/apps ./Templates ./Documents ./Pictures ./Videos [ningning@bogon ~]$ find -name "[a-z]*" -print //查找以小写字母开头的文件 ./.gnome2/keyrings ./.gnome2/keyrings/login.keyring ./.dbus/session-bus ./.dbus/session-bus/dcd26acd87eabb5cc6f2133500000028-0 ./.gnote/addins ./.mozilla/plugins ./.mozilla/extensions ./.gnupg/pubring.gpg ./.cache/event-sound-cache.tdb.dcd26acd87eabb5cc6f2133500000028.i386-redhat-linux-gnu ./.pulse/dcd26acd87eabb5cc6f2133500000028-card-database.tdb ./name ./.gconf/desktop ./.gconf/desktop/gnome ./.gconf/desktop/gnome/accessibility
-perm 按照文件的权限来查找文件
[ningning@bogon ~]$ find . -perm 755 -print //查找权限755的文件 ./.gnome2 ./.gnome2/nautilus-scripts ./.fontconfig ./.mozilla ./.mozilla/plugins ./.mozilla/extensions ./Downloads ./.cache ./.local ./.local/share ./.local/share/applications ./.nautilus ./.config ./.config/gnome-session ./.config/gnome-session/saved-session ./.config/gnome-disk-utility ./.config/gnome-disk-utility/ata-smart-ignore ./Templates ./Documents ./Music ./Pictures ./Public ./Videos ./Desktop //注意ok和exec的用法 [ningning@bogon ~]$ find -perm -007 -ok -l{} \; // #查所有用户都可读写执行的文件同-perm 777 < -l{} ... ./.pulse/dcd26acd87eabb5cc6f2133500000028-runtime > ? y find: `-l./.pulse/dcd26acd87eabb5cc6f2133500000028-runtime': No such file or directory [ningning@bogon ~]$ find -perm -007 -exec -l{} \; find: `-l./.pulse/dcd26acd87eabb5cc6f2133500000028-runtime': No such file or directory
-user 按照文件属主来查找文件
[ningning@bogon ~]$ find / -user ningning /proc/14301/task/14301/cwd /proc/14301/task/14301/root /proc/14301/task/14301/exe /proc/14301/task/14301/mounts /proc/14301/task/14301/mountinfo /proc/14301/task/14301/clear_refs /proc/14301/task/14301/smaps /proc/14301/task/14301/pagemap /proc/14301/task/14301/attr /proc/14301/task/14301/attr/current /proc/14301/task/14301/attr/prev /proc/14301/task/14301/attr/exec /proc/14301/task/14301/attr/fscreate /proc/14301/task/14301/attr/keycreate /proc/14301/task/14301/attr/sockcreate /proc/14301/task/14301/wchan /proc/14301/task/14301/stack /proc/14301/task/14301
-group 按照文件所属的组来查找文件
find: `/proc/27825/fd/5': No such file or directory /proc/27825/fdinfo /proc/27825/fdinfo/0 /proc/27825/fdinfo/1 /proc/27825/fdinfo/2 /proc/27825/fdinfo/3 /proc/27825/fdinfo/4 find: `/proc/27825/fdinfo/5': No such file or directory /proc/27825/ns /proc/27825/ns/net /proc/27825/ns/uts /proc/27825/ns/ipc /proc/27825/ns/mnt /proc/27825/ns/pid /proc/27825/net /proc/27825/environ /proc/27825/auxv
find /home -nouser 列出/home内不属于本地用户的文件或目录
find /home -nogroup 列出/home内不属于本地组的文件或目录
find /home -used -2 列出文件或目录被改动过之后,在2日内被存取过的文件或目录
find /home -user cnscn 列出/home目录内属于用户cnscn的文件或目录
find / -nouser 查找在系统中属于作废用户的文件
[ningning@bogon ~]$ find / -nouser find: `/proc/1356/fd': Permission denied find: `/proc/1356/fdinfo': Permission denied find: `/proc/1356/ns': Permission denied find: `/proc/1357/task/1357/fd': Permission denied find: `/proc/1357/task/1357/fdinfo': Permission denied find: `/proc/1357/task/1357/ns': Permission denied find: `/proc/1357/fd': Permission denied find: `/proc/1357/fdinfo': Permission denied find: `/proc/1357/ns': Permission denied find: `/proc/1382/task/1382/fd': Permission denied
-type 查找某一类型文件
b 块设备文件
d 目录
c 字符设备文件
p 管道文件
l 链接字符文件
f 普通文件
[ningning@bogon ~]$ find -type d -print . ./.gvfs ./.gnome2 ./.gnome2/keyrings ./.gnome2/panel2.d ./.gnome2/panel2.d/default ./.gnome2/panel2.d/default/launchers ./.gnome2/nautilus-scripts ./.dbus ./.dbus/session-bus ./.gnote ./.gnote/addins ./.ssh ./.fontconfig ./.mozilla ./.mozilla/plugins ./.mozilla/extensions ./Downloads ./.gnupg ./.cache [ningning@bogon ~]$ find -type f -exec -l{} \; //查看目录下所有普通文件 find: `-l./.dmrc': No such file or directory find: `-l./.xsession-errors': No such file or directory find: `-l./.gnome2/keyrings/login.keyring': No such file or directory find: `-l./.dbus/session-bus/dcd26acd87eabb5cc6f2133500000028-0': No such file or directory find: `-l./.gnote/36595867-4cc7-4b1f-b298-b25e9c2080ac.note': No such file or directory find: `-l./.gnote/9d4d7c5c-d46e-42eb-8d7f-ed5921568770.note': No such file or directory find: `-l./.bash_profile': No such file or directory find: `-l./.fontconfig/3830d5c3ddfd5cd38a049b759396e72e-le32d4.cache-3': No such file or directory find: `-l./.bashrc': No such file or directory find: `-l./.pulse-cookie': No such file or directory
-size n:[c] 查找文件长度为n块的文件,带有c时表示文件以字节计。
[ningning@bogon ~]$ find . -size 1000c -print //查找长度为1000c的文件 [ningning@bogon ~]$ find . -size +1000c -print //查找长度大于1000c的文件 . ./.xsession-errors ./.gvfs ./.gnome2 ./.gnome2/keyrings ./.gnome2/panel2.d ./.gnome2/panel2.d/default ./.gnome2/panel2.d/default/launchers ./.gnome2/nautilus-scripts ./.dbus ./.dbus/session-bus ./.gnote ./.gnote/36595867-4cc7-4b1f-b298-b25e9c2080ac.note ./.gnote/addins ./.gnote/9d4d7c5c-d46e-42eb-8d7f-ed5921568770.note ./.ssh ./.fontconfig ./.fontconfig/3830d5c3ddfd5cd38a049b759396e72e-le32d4.cache-3 ./.mozilla ./.mozilla/plugins ./.mozilla/extensions ./Downloads ./.gnupg [ningning@bogon ~]$ find . -size +10 -print //查长度超过期作废10块的文件(1块=512字节) ./.gnupg/gpg.conf ./.cache/event-sound-cache.tdb.dcd26acd87eabb5cc6f2133500000028.i386-redhat-linux-gnu ./.pulse/dcd26acd87eabb5cc6f2133500000028-device-volumes.tdb ./.pulse/dcd26acd87eabb5cc6f2133500000028-stream-volumes.tdb ./.local/share/gvfs-metadata/home-6751745f.log ./.gconfd/saved_state ./.gstreamer-0.10/registry.i686.bin ./Desktop/gnome-terminal.desktop
查找一个文件大小为5556字节的文件
[ningning@bogon ~]$ find /ningning -size -5557c -size +5555c -exec ls -ld{} \;