shell脚本攻略读书笔记之七&&&find命令精解

前言

  • 文章来源:CSDN@LawsonAbs

. specifies current directory and .. specifies the parent directory。
【译:.指的是当前目录,..指的是当前目录的上层目录】

find . -printPrint lists of files and folders【打印当前目录中的文件和文件夹】

1. 常用参数

-name参数指定和filename匹配的字符串。

[root@server4 hadoop]# ll
total 156
-rw-r--r--. 1  500  500  4113 Mar  8  2016 mapred-queues.xml.template
-rw-r--r--. 1  500  500   865 Jul  4 07:56 mapred-site.xml
-rw-r--r--. 1 root root   535 Jun 18 01:05 out.txt
-rw-r--r--. 1  500  500   930 Jul 13 10:31 yarn-site.xml
[root@server4 hadoop]# find . -name '*.txt' -print
./out.txt

. :当前目录。
-name: 指定查询的文件名
*.txt:通配符
-print:打印出所有结果

  • 打印出指定的文件【使用了\( \)

下面命令执行的当前目录下的文件如下所示:

[root@server4 backup]# ll
total 0
-rw-r--r--. 1 root root 0 Aug 10 19:01 example.txt
-rw-r--r--. 1 root root 0 Aug 10 19:01 EXAMPLE.txt
-rw-r--r--. 1 root root 0 Aug 10 19:01 file.txt
drwxr-xr-x. 2 root root 6 Aug 10 19:07 littlelawson
-rw-r--r--. 1 root root 0 Aug 10 19:07 little.txt
-rw-r--r--. 1 root root 0 Aug 10 19:02 new.txt
-rw-r--r--. 1 root root 0 Aug 10 19:02 some.jpg
-rw-r--r--. 1 root root 0 Aug 10 19:02 text.pdf
  • -iname pattern

Like -name, but the match is case insensitive. For example, the patterns fo*' andF??’ match the file names Foo',FOO’, foo',fOo’, etc. The pattern *foo* will also match a file called ‘.foobar’.
-iname参数类似 -name参数。但是-iname是不区分大小写的。所以fo*可以匹配文件名包括Foo,FOO等等

-o 参数:或者匹配另外一个参数

参看下面这个脚本

[root@server4 backup]# cat rename.sh 
#!/bin/bash
count=1
for img in `find . -iname '*.png' -o -iname '*.jpg' -type f -maxdepth 1`
do
new=image-$count.${img##*.}
echo "Renaming $img to $new"
mv "$img" "$new"
let count++
done

the -o option is used to specify multiple -iname options【这就是或的原理嘛】

其它参数

-delete 参数,将符合搜索结果的文件执行删除操作
-perm 参数,查找符合条件【based on the file permissions and ownership】的文件

[root@server4 backup]# ll
total 0
-rw-r--r--. 1 root root  0 Aug 10 19:01 example.txt
-rw-r--r--. 1 root root  0 Aug 10 19:01 EXAMPLE.txt
-rw-r--r--. 1 root root  0 Aug 10 19:01 file.txt
drwxr-xr-x. 2 root root 23 Aug 10 20:05 littlelawson
-rw-r--r--. 1 root root  0 Aug 10 19:07 little.txt
-rw-r--r--. 1 root root  0 Aug 10 19:02 new.txt
-rw-r--r--. 1 root root  0 Aug 10 19:02 some.jpg
-rw-r--r--. 1 root root  0 Aug 10 19:02 text.pdf

如下这条命令是找出文件类型为file(普通文件),用户是root,然后执行(-exec)操作(将其放到littlelawson文件夹中)。
[root@server4 backup]# find . -type f -user root  -exec chown littlelawson {} \;
[root@server4 backup]# ll
total 0
-rw-r--r--. 1 littlelawson root  0 Aug 10 19:01 example.txt
-rw-r--r--. 1 littlelawson root  0 Aug 10 19:01 EXAMPLE.txt
-rw-r--r--. 1 littlelawson root  0 Aug 10 19:01 file.txt
drwxr-xr-x. 2 root         root 23 Aug 10 20:05 littlelawson
-rw-r--r--. 1 littlelawson root  0 Aug 10 19:07 little.txt
-rw-r--r--. 1 littlelawson root  0 Aug 10 19:02 new.txt
-rw-r--r--. 1 littlelawson root  0 Aug 10 19:02 some.jpg
-rw-r--r--. 1 littlelawson root  0 Aug 10 19:02 text.pdf

2. 常用命令

2.1 查找包含指定文件类型的文件

[root@server4 backup]# find . \( -name "*.txt" -o -name "*.pdf" \) -print
./example.txt
./EXAMPLE.txt
./file.txt
./new.txt
./text.pdf

2.2 查找指定路径下,包括指定字符的文件

[root@server4 backup]# find /root/backup/ -path "*/little*" -print
/root/backup/littlelawson
/root/backup/little.txt

2.3 查找不是以.txt结尾的文件或者是文件夹【使用!】,并输出

[root@server4 backup]# find . ! -name "*.txt" -print
.   #为什么这里会输出.号
./some.jpg
./text.pdf
./littlelawson

2.4 限制最大查找深度(使用-maxdepth)【这里的1就是指当前目录

[root@server4 backup]# find . -maxdepth 1 -name "f*" -print
./file.txt

2.5限制最小查找深度(使用-mindepth)【 当前是第1层,子目录是第2层】

[root@server4 backup]# find . -mindepth 2 -name "f*" -print
./littlelawson/file.test

-maxdepth-mindepth 参数的位置与find的效率有关。比如说,先查找符合name条件的文件,这样找的文件还需要再经过深度测试。这样就会产生很多低效的操作。相反,如果是先搜索深度,再比较name就会高效很多。
By using -type, we can specify to the find command that it should only match files having a specified type.

list only directories including descendants 
find . -type d -print

list only regular files as follows
find . -type f -print

list only symbolic links as follows
find . -type l -print

File type		Type argument
Regular file	 f
Symbolic link 	 l
Directory 		 d
Character special
device			  c
Block device 	  b
Socket   		  s
FIFO 			  p

2.6 根据访问、修改、创建时间查找文件

Print all the files that were accessed within the last seven days as follows:[最近7天内访问的数据]
$ find . -type f -atime -7 -print

Print all the files that are having access time exactly seven-days old as follows:[恰好为7天时访问的数据]
$ find . -type f -atime 7 -print

Print all the files that have an access time older than seven days as follows:[7天之前访问的数据]
$ find . -type f -atime +7 -print

[root@server4 backup]# find . -type f -amin +1 -print
./example.txt
./EXAMPLE.txt
./file.txt
./new.txt
./some.jpg
./text.pdf
./littlelawson/file.test
./little.txt
[root@server4 backup]# find . -type f -amin +100 -print
[root@server4 backup]# find . -type f -amin +10 -print
./example.txt
./EXAMPLE.txt
./file.txt
./new.txt
./some.jpg
./text.pdf
./littlelawson/file.test
./little.txt

2.7 根据文件大小查找数据

based on the file sizes of the files, a search can be performed as follows:
find . -type f -size +2k
find . -type f -size -2k
find . -type f -size 2k  

2.8 查看当前目录下的文件个数

find -type f | wc -l

2.9 查找文件并删除

find . -type f -name "*.txt" -print | xargs rm -f 

上面这个命令很危险,因为没有指定分隔符【我们也不知道这个命令的分割符是什么。】所以建议使用如下语句:
find . -type f -name "*.txt" -print0 | xargs -0 rm -f
This removes all .txt files.
xargs -0 interprets that the delimiting character is \0.

Subshell ( ) tricks can be used in a variety of problematic environments.
()其实是一个subshell,而不是普通的括号。

[root@server4 backup]# cat file.txt | ( while read arg; do echo $arg; done )
hello spark n hello hadoop
hello hive

If cmd1 is cd /, within the subshell, the path of the working directory changes. However, this change resides inside the subshell only。
cd这个命令的使用仅仅只在subshell中生效,在父shell窗口中是不管用的。例如下面这个命令:
[root@server4 backup]# ( cd ;echo sdf;cd /)
sdf
[root@server4 shells]# echo -e "1\nhello\n" ./interactive.sh 
1
hello
 ./interactive.sh
[root@server4 shells]# echo -e "1\nhello\n" | ./interactive.sh 
You have entered 1, hello
[root@server4 shells]# echo -e "1\nhello\n"  > input.data
[root@server4 shells]# cat input.data 
1
hello
   #【这里有一个换行,不能忘记】

This redirects interactive input data from a file.
[root@server4 shells]# ./interactive.sh < input.data 
You have entered 1, hello
-print0 | xargs -0
-0 interprets that the delimiting character is \0.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

说文科技

看书人不妨赏个酒钱?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值