linux/unix shell的find用法小练

8 篇文章 0 订阅

好久之前的笔记了,搬出来和大家共享:

参数解释:

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值