歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

Linux中常見find命令的使用

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{} \;

Copyright © Linux教程網 All Rights Reserved