shell 腳本攻略-find 文件查找總結

對 find 命令的總結

先看看 cat 命令

ld@ubuntu:/mnt/hgfs/haShare/test$ ls -la
total 6
drwxrwxrwx 1 root root    0 Jul 11 05:54 .
drwxrwxrwx 1 root root 4096 Jul 11 05:36 ..
-rwxrwxrwx 1 root root   79 Jul 11 05:48 file1.txt
-rwxrwxrwx 1 root root   76 Jul 11 05:47 file2.txt
-rwxrwxrwx 1 root root   76 Jul 11 05:48 file3.txt
-rwxrwxrwx 1 root root    0 Jul 11 05:54 note

拼接顯示

ld@ubuntu:/mnt/hgfs/haShare/test$ cat file1.txt file2.txt file3.txt
content of file 1
content of file 1
content of file 1
content of file 1
content of file 2
content of file 2
content of file 2
content of file 2
content of file 3
content of file 3
content of file 3
content of file 3
ld@ubuntu:/mnt/hgfs/haShare/test$ cat file*
content of file 1
content of file 1
content of file 1
content of file 1
content of file 2
content of file 2
content of file 2
content of file 2
content of file 3
content of file 3
content of file 3
content of file 3

連接 stdin 和 已存在的文本

'cat - file1.txt' 中的 - 被作爲來自 std 文本的文件名
ld@ubuntu:/mnt/hgfs/haShare/test$ ls -la | cat - file1.txt
total 6
drwxrwxrwx 1 root root    0 Jul 11 05:47 .
drwxrwxrwx 1 root root 4096 Jul 11 05:36 ..
-rwxrwxrwx 1 root root   79 Jul 11 05:48 file1.txt
-rwxrwxrwx 1 root root   76 Jul 11 05:47 file2.txt
-rwxrwxrwx 1 root root   76 Jul 11 05:48 file3.txt
content of file 1
content of file 1
content of file 1
content of file 1

顯示行號

ld@ubuntu:/mnt/hgfs/haShare/test$ cat -n file*
1 content of file 1
2 content of file 1
3 content of file 1
4 content of file 1
5 content of file 2
6 content of file 2
7 content of file 2
8 content of file 2
9 content of file 3
10 content of file 3
11 content of file 3
12 content of file 3

find 命令基礎

#分隔符類型
-print :'\n'
-print0 :'\0' #在ASCII字符集中對應空字符NULL

根據文件名查找

-name

-iname 不區分大小寫

ld@ubuntu:/mnt/hgfs/haShare$ tree tmp/
tmp/
├── flaskr.db
├── flaskr.py
├── flaskr.pyc
├── schema.sql
├── setup.cfg
├── setup.py
├── static
│   └── style.css
└── templates
    ├── layout.html
    ├── login.html
    └── show_entries.html

2 directories, 10 files
ld@ubuntu:/mnt/hgfs/haShare$ find ./tmp/  \( -name "*.py" -o -name "*.html" \) -print
./tmp/flaskr.py
./tmp/setup.py
./tmp/templates/layout.html
./tmp/templates/login.html
./tmp/templates/show_entries.html
ld@ubuntu:/mnt/hgfs/haShare$

根據路徑信息匹配

-path

ld@ubuntu:/mnt/hgfs/haShare$ find ./ -path '*static*' -print
./py/docs/_static
./py/docs/_static/debugger.png
./py/docs/_static/flask-favicon.ico
./py/docs/_static/flask.png
./py/docs/_static/flaskr.png
./py/docs/_static/logo-full.png
./py/docs/_static/no.png
./py/docs/_static/touch-icon.png
./py/docs/_static/yes.png
./py/docs/_themes/flask/static
./py/docs/_themes/flask/static/flasky.css_t
./py/docs/_themes/flask/static/small_flask.css
./py/docs/_themes/flask_small/static
./py/docs/_themes/flask_small/static/flasky.css_t
./py/examples/flaskr/static
./py/examples/flaskr/static/style.css
./py/examples/minitwit/static
./py/examples/minitwit/static/style.css
./py/examples/persona/static
./py/examples/persona/static/persona.js
./py/examples/persona/static/spinner.png
./py/examples/persona/static/style.css
./py/tests/static
./py/tests/static/config.json
./py/tests/static/index.html
./py/tests/test_apps/blueprintapp/apps/admin/static
./py/tests/test_apps/blueprintapp/apps/admin/static/css
./py/tests/test_apps/blueprintapp/apps/admin/static/css/test.css
./py/tests/test_apps/blueprintapp/apps/admin/static/test.txt
./py/tests/test_apps/subdomaintestmodule/static
./py/tests/test_apps/subdomaintestmodule/static/hello.txt
./tmp/static
./tmp/static/style.css

用正則表達式匹配路徑或者文件名 -regex

-regex
ld@ubuntu:/mnt/hgfs/haShare$ find . -iregex ".*\(\.py\|\.css\)"
./microblog-0.5/app/forms.py
./microblog-0.5/app/models.py
./microblog-0.5/app/views.py
./microblog-0.5/app/__init__.py
./microblog-0.5/config.py
./microblog-0.5/db_create.py
./microblog-0.5/db_downgrade.py
./microblog-0.5/db_migrate.py
./microblog-0.5/db_repository/manage.py
./microblog-0.5/db_repository/versions/001_migration.py
./microblog-0.5/db_repository/versions/002_migration.py
./microblog-0.5/db_repository/versions/__init__.py
./microblog-0.5/db_repository/__init__.py
./microblog-0.5/db_upgrade.py
./microblog-0.5/run.py
./microblog-0.5/setup.py
./microblog-0.5/virtualenv.py
./py/docs/conf.py
./py/docs/flaskdocext.py
./py/docs/flaskext.py
./py/docs/_themes/flask/static/small_flask.css
./py/docs/_themes/flask_theme_support.py
./py/examples/blueprintexample/blueprintexample.py
./py/examples/blueprintexample/simple_page/simple_page.py
./py/examples/blueprintexample/simple_page/__init__.py
./py/examples/blueprintexample/test_blueprintexample.py
./py/examples/flaskr/flaskr.py
./py/examples/flaskr/static/style.css
./py/examples/flaskr/test_flaskr.py
./py/examples/jqueryexample/jqueryexample.py
./py/examples/minitwit/minitwit.py
./py/examples/minitwit/static/style.css
./py/examples/minitwit/test_minitwit.py
./py/examples/persona/persona.py
./py/examples/persona/static/style.css

.....

否定參數

ld@ubuntu:/mnt/hgfs/haShare/test$ ls -la
total 6
drwxrwxrwx 1 root root    0 Jul 11 06:03 .
drwxrwxrwx 1 root root 4096 Jul 11 05:36 ..
-rwxrwxrwx 1 root root   79 Jul 11 05:48 file1.txt
-rwxrwxrwx 1 root root   76 Jul 11 05:47 file2.txt
-rwxrwxrwx 1 root root   76 Jul 11 05:48 file3.txt
-rwxrwxrwx 1 root root  106 Jul 11 06:09 multi_blanks
-rwxrwxrwx 1 root root    0 Jul 11 05:54 note
ld@ubuntu:/mnt/hgfs/haShare/test$ find . !  -name '*.txt' -print
.
./multi_blanks
./note

定義深度,mindepth=1 則從當前路徑開始查找,1 代表當前路徑。

考慮到性能問題,深度參數必須前置,否則影響效率

ld@ubuntu:/mnt/hgfs/haShare/test$ tree .
.
├── file1.txt
├── file2.txt
├── file3.txt
├── multi_blanks
├── note
└── tow
    └── three
        └── four
            └── five

4 directories, 5 files
ld@ubuntu:/mnt/hgfs/haShare/test$ touch ./tow/tow.txt;
ld@ubuntu:/mnt/hgfs/haShare/test$ touch ./tow/three/three.txt;
ld@ubuntu:/mnt/hgfs/haShare/test$ touch ./tow/three/four/four.txt;
ld@ubuntu:/mnt/hgfs/haShare/test$ touch ./tow/three/four/five/five.txt;
ld@ubuntu:/mnt/hgfs/haShare/test$ find -mindepth 1 -type f -print
./file1.txt
./file2.txt
./file3.txt
./multi_blanks
./note
./tow/three/four/five/five.txt
./tow/three/four/four.txt
./tow/three/three.txt
./tow/tow.txt
ld@ubuntu:/mnt/hgfs/haShare/test$ find -mindepth 1 -maxdepth 3 -type f -print
./file1.txt
./file2.txt
./file3.txt
./multi_blanks
./note
./tow/three/three.txt
./tow/tow.txt
ld@ubuntu:/mnt/hgfs/haShare/test$ find -mindepth 2 -maxdepth 4 -type f -print
./tow/three/four/four.txt
./tow/three/three.txt
./tow/tow.txt
ld@ubuntu:/mnt/hgfs/haShare/test$

根據文件類型查找

文件類型有 fldcbsp

ld@ubuntu:/mnt/hgfs/haShare/test$ find . -type d -print
.
./tow
./tow/three
./tow/three/four
./tow/three/four/five
ld@ubuntu:/mnt/hgfs/haShare/test$ find . -type f -print
./file1.txt
./file2.txt
./file3.txt
./multi_blanks
./note
./tow/three/four/five/five.txt
./tow/three/four/four.txt
./tow/three/three.txt
./tow/tow.txt

其他選項

根據文件大小搜索

-size +2M
-size -200k

刪除

-delete

根據權限值查找

-perm 664

根據所有者查找

-user username
-prune 忽略某些目錄
-newer filename 比某文件新
-atime -amin
-mtime -mmin
-ctime -cmin 元數據被更改(如權限、所有者等信息)
time 是天爲單位,min 爲單位

-exec 結合find執行命令或操作

ld@ubuntu:/mnt/hgfs/haShare/tmp$ find . -type f -atime +2 -name ‘*.py’ -exec cp -v {} ../test/ \;
‘./flaskr.py’ -> ‘../test/flaskr.py’
‘./setup.py’ -> ‘../test/setup.py’

執行多個命令的技巧
-exec ./cmds.sh {} \;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章