find
功能最强的查找文件命令
-size
按文件大小查找,第一个参数为查询目录
可以不写查询目录,默认从当前所在目录递归查找:
mcdx@ubuntu:~$ sudo find /etc -size +333k
/etc/apparmor.d/cache/usr.bin.evince
/etc/brltty/Contraction/zh-tw-ucb.ctb
/etc/brltty/Contraction/zh-tw.ctb
mcdx@ubuntu:~$ cd /etc
mcdx@ubuntu:/etc$ sudo find -size +333k
./apparmor.d/cache/usr.bin.evince
./brltty/Contraction/zh-tw-ucb.ctb
./brltty/Contraction/zh-tw.ctb
-name
按照文件名查找,-iname
忽略大小写,参数可接受通配符
*
任意数量任意字符
?
0 个或 1 个任意字符
[asdf]
字符集,类似正则里的概念,匹配任意一个里面包含的字符
举个例子:
mcdx@ubuntu:~$ find test -name '*.py'
test/shiyan/shiyan/middlewares.py
test/shiyan/shiyan/pipelines.py
test/shiyan/shiyan/items.py
test/shiyan/shiyan/settings.py
test/shiyan/shiyan/__init__.py
test/shiyan/shiyan/spiders/image.py
test/shiyan/shiyan/spiders/haha.py
test/shiyan/shiyan/spiders/__init__.py
mcdx@ubuntu:~$ find git/pc -name app.py
git/pc/37_代码框架及数据库设计/jobplus/jobplus/app.py
git/pc/29_增加用户页面/simpledu/simpledu/app.py
git/pc/38_首页和注册登录功能/jobplus/jobplus/app.py
git/pc/08_为文章增加标签/news/app.py
git/pc/07_从数据库中读取内容/news/app.py
git/pc/06_一个简单的资讯网站/news/app.py
对软链接的查找用 -lname
,对硬链接的查找用 -samefile
,查找节点号用 -inum
:
mcdx@ubuntu:~/test$ ll -i
总用量 12
1460981 drwxrwxr-x 3 mcdx mcdx 4096 Jul 19 10:29 ./
1485401 drwxr-xr-x 20 mcdx mcdx 4096 Jul 19 08:59 ../
1494630 -rw-rw-r-- 1 mcdx mcdx 0 Jul 13 17:33 0001.c
1495120 -rw-rw-r-- 1 mcdx mcdx 0 Jul 13 17:33 0002.c09
1495232 -rw-rw-r-- 1 mcdx mcdx 0 Jul 13 17:33 0003.c
1483901 -rw-rw-r-- 3 mcdx mcdx 0 Jul 19 10:29 a
1483901 -rw-rw-r-- 3 mcdx mcdx 0 Jul 19 10:29 a1
1483901 -rw-rw-r-- 3 mcdx mcdx 0 Jul 19 10:29 a2
1472327 drwxr-sr-x 3 mcdx mcdx 4096 Jun 8 10:00 shiyan/
mcdx@ubuntu:~/test$ find -inum 1483901
./a
./a1
./a2
-links
查找指定链接数的文件:
mcdx@ubuntu:~/test$ find -links 3
.
./shiyan
./shiyan/shiyan/image
./a
./a1
./a2
-exec
对查询结果进一步操作,格式:find [条件] [参数] -exec [命令] {} \;
Mcdx@haha~$ find . -size +111M
./Library/Caches/com.apple.helpd/SDMHelpData/AppleMain/zh_CN/lsm_map
./Library/Caches/com.apple.helpd/SDMHelpData/Other/zh_CN/lsm_map
./Library/Caches/com.apple.helpd/SDMHelpData/AppleExtra/zh_CN/lsm_map
Mcdx@haha~$ find . -size +111M -exec ls -l {} \;
-rw-r--r-- 1 mcdx staff 124169136 9 14 19:10 ./Library/Caches/com...
-rw-r--r-- 1 mcdx staff 164184036 9 14 19:10 ./Library/Caches/com...
-rw-r--r-- 1 mcdx staff 214697712 9 18 10:19 ./Library/Caches/com...
locate
实现快速查找此命令在一个存有系统中全部文件名和目录名的数据库中去查找
此数据库不会实时更新,可以手动更新:sudo updatedb
-i
参数忽略大小写
它是按字符串进行查询,查询的结果是完整的文件路径:
mcdx@ubuntu:~$ locate readme.md
/home/mcdx/git/xiangmuxiezuo/dazuoye/36_热门职位标签/seiya/readme.md
mcdx@ubuntu:~$ locate -i readme.md
/home/mcdx/Downloads/redis-stable/README.md
/home/mcdx/Downloads/redis-stable/deps/README.md
/home/mcdx/git/xiangmuxiezuo/README.md
/home/mcdx/git/xiangmuxiezuo/dazuoye/36_热门职位标签/seiya/readme.md
mcdx@ubuntu:~$ locate /usr/bin/python3
/usr/bin/python3
/usr/bin/python3-config
/usr/bin/python3.5
/usr/bin/python3.5-config
/usr/bin/python3.5m
/usr/bin/python3.5m-config
/usr/bin/python3m
/usr/bin/python3m-config
mcdx@ubuntu:~$ locate ~/a
/home/mcdx/a.py
-l
限制打印查询结果的数量:
mcdx@ubuntu:~$ locate -l 2 README.md
/home/mcdx/Downloads/redis-stable/README.md
/home/mcdx/Downloads/redis-stable/deps/README.md
-c
打印查询数量
mcdx@ubuntu:~$ locate -c app.py
47
如果查询结果中有目录,自然也会打印目录以及目录中的全部子目录以及文件
这就是所谓全库搜索按字符串查询,没有目录的概念,十分强硬:
mcdx@ubuntu:~/test/shiyan/shiyan$ locate /home/mcdx/test/shiyan/shiyan/i
/home/mcdx/test/shiyan/shiyan/image
/home/mcdx/test/shiyan/shiyan/items.py
/home/mcdx/test/shiyan/shiyan/image/full
/home/mcdx/test/shiyan/shiyan/image/full/1cea...7452aca3e756a44e.jpg
/home/mcdx/test/shiyan/shiyan/image/full/1f1a...9b62f08a50145016.jpg
/home/mcdx/test/shiyan/shiyan/image/full/223d...f662e849e08fd855.jpg
/home/mcdx/test/shiyan/shiyan/image/full/26dd...9a1a6215da800f8.jpg
/home/mcdx/test/shiyan/shiyan/image/full/2c50...225726fbce3aba31.jpg
whereis
跟前面的 locate 类似也是在那个存储字符串的库里查找字符串
不过它只查找二进制文件(也就是可执行文件)、源、man 手册
这段待补充,先写个例子:
mcdx@ubuntu:~$ whereis flask
flask: /usr/local/bin/flask
mcdx@ubuntu:~$ whereis pip3
pip3: /usr/bin/pip3 /usr/local/bin/pip3.5 /usr/share/man/man1/pip3.1.gz
which
用于在 PATH 环境变量中查找 shell 命令的完整路径shell 命令其实也是一个可执行文件的文件名
此命令默认打印第一个符合的结果:
-a
参数打印全部结果
mcdx@ubuntu:~$ which tree
/usr/bin/tree
mcdx@ubuntu:~$ which python
/usr/bin/python