Shell脚本第二篇(find)


Shell脚本第二篇(find)
 
001
#cat 命令
002
cat -n /usr/local/php/lib/php.ini  #显示添加行号
003
cat -s  cat.txt    #压缩多个空行为一个空行
004
 
005
#find 命令
006
 
007
find  /home/wangdk -iname "*.txt"  -print    # -print 指名打印匹配文件名,换行符\n  -i 忽略大小写
008
 
009
find  . \( -name "*.txt" -o -name "*.pdf" \) -print  # -o 是逻辑或
010
 
011
# -name  对一个文件名进行匹配
012
# -path 对一个文件路径进行匹配
013
# -regex 用一个正则表达式匹配文件路径
014
 
015
#支持正则
016
# find . -regex ".*\(\.sh\)$"
017
 
018
#否定参数
019
find . ! -name "*.txt" -print
020
 
021
#基于目录的深度搜索
022
#maxdepth 最大搜索深度
023
#mindepth  最小搜索深度
024
 
025
find . -maxdepth 1 -type f -print
026
find . -mindepth 2 -type f -print
027
 
028
# type  f  普通文件
029
# type  l   链接符号
030
# type  d 目录
031
#type  c  字符设备
032
#type  b  块设备
033
#type  s  套接字
034
#type p  Fifo
035
 
036
 
037
#根据文件时间进行搜索
038
# 访问时间 -atime
039
# 修改时间 -mtime
040
# 变化时间 -ctime
041
 
042
# -atime -mtime -ctime 单位为天,支持 + - 大于,小于
043
 
044
#打印出最近7天内被访问过的所有文件
045
find . -type  f -atime  -7 -print
046
 
047
#打印出正好7前内访问过的文件
048
find . type f -atime 7 -print
049
 
050
#超过7天
051
find . -type f -atime  +7 -print
052
 
053
#基于时间搜索,分钟
054
# -amin -mmin -cmin
055
 
056
#基于文件大小的搜索
057
find . -type f -size +2k   #大于2k的文件
058
 
059
find . -type f -size -2k   #小于2k的文件
060
 
061
# 除了k 以外还有其他单位
062
# b   块(512字节)
063
# c   字节
064
# w   字
065
# k   千字节
066
# M  MB
067
#G  GB
068
 
069
#删除匹配的文件
070
 
071
find  . -type f -name "*.swp" -delete
072
 
073
#基于文件权限的匹配
074
find  .-type f -perm 644 -print
075
 
076
# 以apache 为例子,web服务器上的PHP需要合适的执行权限,我们可以用下面来搜索
077
find . -type f -name "*.php" | -perm 644 -print
078
 
079
#基于文件所有者的文件
080
find . -type f -user wangdk -print
081
 
082
#结合find 执行命令或动作
083
find . -type -user root  -exec chown wangdk {} \;  #在这个命令中{}是一个特殊字符串与 -exec 选项结合是使用,对每个匹配的文件替换成相应的文件, 例如
084
# find 命令找到test1.txt 和 test2.txt 其所有者均为wangdk 那么find 将会执行 chown wangk{} 他会被解析为  chown wangdk test1.txt  chown wangdk test2.txt
085
 
086
find . -type f -name "*.c" -exec cat {} \; > all_c._files.txt
087
 
088
#-exec 之后可以接任何命令 { }表示一个匹配,匹配任意文件名
089
 
090
find . -type f  -mtime  +10 -a "*.txt" -exec cp { } OLD \;
091
 
092
# exec 无法执行多个命令,但是可以执行shell脚本  -exec ./commands.sh { } \;
093
 
094
find . -type f -name "*.txt" -exec printf "text file: %s\n" { } \;
095
# -exec 能够同printf结合起来生成有用的输出信息
096
 
097
# 让 find 跳过指定目录
098
find deve1//source_path \ ( -name ".git" -prune \) -o \ (-type f -print \)
099
#不使用 \( -type -print \) 二十选择需要的过滤器
100
#这里,\( -name ".git -prune \) 的作用是用于进行排序,她只ing了.git目录应该被排斥外,而\( -type f -print \) 只指名了需要执行的动作
 

相关内容

    暂无相关文章