歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> Linux/Unix Shell的find用法筆記

Linux/Unix Shell的find用法筆記

日期:2017/3/1 10:00:59   编辑:SHELL編程

Linux/Unix Shell的find用法筆記

參數解釋:

find用來查詢某個目錄下的文件幾種常用的參數如下

-path 後面跟著要查詢的路徑

-prune 表示不再指定目錄下查詢,如果同時使用-depth,那麼-prune命令將會被忽略

-name 要查詢的文件名,可以使用正則表達式來查詢

-mtime 查詢近n天更改過的文件+表示大於n,-n表示n天以內

-另外還有-atime和-ctime用法類似

-newer file1 ! -newer file2 表示查詢比file1新,比file2舊的文件

-exec 表示對查詢出來的文件進行某些命令操作,如 -exec ls -l {} \;

-type 要查詢的文件類型

! -type d 表示除了類型為目錄的

-size 指定大小的文件+10c或-10c:大於10個字節的,或者小於10個字節的

演示:

[Oracle@localhost testDir]$ cat find.sh
echo "prune:" >> find.out
find . -path "./folder" -prune -o -name "file*" >> find.out
echo "-name:" >> find.out
find . -name "[a-z][a-z]*" >> find.out
echo "-mtime: +3" >> find.out
find . -mtime +3 >> find.out
echo "-mtime: -3" >> find.out
find . -mtime -3 >> find.out
echo "find newer then 1 and older then 2 files:">> find.out
find . -newer "test1.sh" ! -newer "test2.sh" -exec ls -l {} \; >> find.out
echo "-type:" >> find.out
find . -type d >> find.out
echo "except type:" >> find.out
find . ! -type d >> find.out
echo "-size +10c :" >> find.out
find . -size +10c >> find.out
echo "-size -10c :" >> find.out
find . -size -10c >> find.out輸出結果:

[oracle@localhost testDir]$ cat find.out
prune:
./folder
-name:
./blank_file
./find.sh
./shelltest.sh
./folder
./folder/file1
./test1.log
./test1.sh
./test2.sh
./main.sh
./find.out
-mtime: +3
./find.sh
./shelltest.sh
./folder
./folder/file1
./test1.sh
./test2.sh
./main.sh
-mtime: -3
.
./blank_file
./test1.log
./find.out
find newer then 1 and older then 2 files:
-rwxr-xr-x 1 oracle oinstall 100 Nov 10 23:08 ./test2.sh
-rwxr-xr-x 1 oracle oinstall 26 Nov 10 22:30 ./main.sh
-type:
.
./folder
except type:
./blank_file
./find.sh
./shelltest.sh
./folder/file1
./test1.log
./test1.sh
./test2.sh
./main.sh
./find.out
-size +10c :
.
./blank_file
./find.sh
./shelltest.sh
./folder
./test1.log
./test1.sh
./test2.sh
./main.sh
./find.out
-size -10c :
./folder/file1

Copyright © Linux教程網 All Rights Reserved