美文网首页
find、grep、awk、sed实例

find、grep、awk、sed实例

作者: 六月天的安静 | 来源:发表于2018-08-07 15:41 被阅读111次

    find命令

    参数:

    pathname: find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。
    -print: find命令将匹配的文件输出到标准输出。
    -exec: find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' { } \;注意{ }和\;之间的空格。
    -ok: 和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。
    -print 将查找到的文件输出到标准输出
    -exec   command   {} \;      —–将查到的文件执行command操作,{} 和 \;之间有空格
    -ok 和-exec相同,只不过在操作前要询用户 。例:find . -name .svn | xargs rm -rf
    -name   filename             #查找名为filename的文件
    -perm                        #按执行权限来查找
    -user    username             #按文件属主来查找
    -group groupname            #按组来查找
    -mtime   -n +n                #按文件更改时间来查找文件,-n指n天以内,+n指n天以前
    -atime    -n +n               #按文件访问时间来查GIN: 0px">
    -ctime    -n +n              #按文件创建时间来查找文件,-n指n天以内,+n指n天以前
    -nogroup                     #查无有效属组的文件,即文件的属组在/etc/groups中不存在
    -nouser                     #查无有效属主的文件,即文件的属主在/etc/passwd中不存
    -newer   f1 !f2              找文件,-n指n天以内,+n指n天以前 
    -ctime    -n +n               #按文件创建时间来查找文件,-n指n天以内,+n指n天以前 
    -nogroup                     #查无有效属组的文件,即文件的属组在/etc/groups中不存在
    -nouser                      #查无有效属主的文件,即文件的属主在/etc/passwd中不存
    -newer   f1 !f2               #查更改时间比f1新但比f2旧的文件
    -type    b/d/c/p/l/f         #查是块设备、目录、字符设备、管道、符号链接、普通文件
    -size      n[c]               #查长度为n块[或n字节]的文件
    -depth                       #使查找在进入子目录前先行查找完本目录
    -fstype                     #查更改时间比f1新但比f2旧的文件
    -type    b/d/c/p/l/f         #查是块设备、目录、字符设备、管道、符号链接、普通文件
    -size      n[c]               #查长度为n块[或n字节]的文件
    -depth                       #使查找在进入子目录前先行查找完本目录
    -fstype                      #查位于某一类型文件系统中的文件,这些文件系统类型通常可 在/etc/fstab中找到
    -mount                       #查文件时不跨越文件系统mount点
    -follow                      #如果遇到符号链接文件,就跟踪链接所指的文件
    -cpio                %;      #查位于某一类型文件系统中的文件,这些文件系统类型通常可 在/etc/fstab中找到
    -mount                       #查文件时不跨越文件系统mount点
    -follow                      #如果遇到符号链接文件,就跟踪链接所指的文件
    -cpio                        #对匹配的文件使用cpio命令,将他们备份到磁带设备中
    -prune                       #忽略某个目录
    
    

    例子:

        1、在$HOME中查找.txt文件并显示
        find ~ -name  "*.txt" -print 
        find . -name "*.txt" -print
        
        2、查找以大写字母开头的文件
        find . -name "[A-Z]*"  -print 
        
        3、查找以host开头的文件
        find /etc -name "host*" -print
        
        4、查以两个小写字母和两个数字开头的txt文件
        find . -name "[a-z][a-z][0-9][0-9].txt" -print
        
        5、查找权限755的文件
        find . -perm 755 -print
        
        6、查所有用户都可读写执行的文件同 -perm 777
         find . -perm 777 -exec ls -l {} \;
    
        7、查找当前目录下所有(非)子目录
         find . -type d -print
         find . ! -type d  -print
         
        8、查长度大于1Mb的文件
        find . -size +1000000c -print
        
        9、查长度为100c的文件
        find . -size 100c -print
        
        10、查看是否有"cnscn"用户
            find /etc -name "passwd*"  -exec grep "cnscn" {} \;
            
        11、查当前目录下的所有普通文件
            find . -type f -exec ls -l {} \;
            
        12、在/logs目录中查找更改时间在5日以前的文件并删除它们
            find /logs -type f -mtime +5 -exec -ok rm -f {} \;
        
        13、查询当天修改过的文件
            find ./ -type f -mtime -1 -exec ls -l {} \;
            
        14、查找磁盘中大于3M的文件
            find . -size +3000k exec ls -l {} \;
            
        15、将查找出的内容copy到另一个地方
            find ./ -name *.c -exec cp '{}' /tmp/ \;
            find ./ -name *.c | xargs -i cp {} /tmp/
            
        16、在当前目录下查找以april开始的文件
            find   -name april*     
            
        17、 在当前目录下查找以april开始的文件,并把结果输出到file中               
            find   -name   april*   fprint file   
                 
        18、查找以ap或may开头的文件
            find   -name ap* -o -name may*              
            
        19、 在/mnt下查找名称为tom.txt且文件系统类型为vfat的文件
            find   /mnt   -name tom.txt   -ftype vfat  
            
        20、 在/mnt下查找名称为tom.txt且文件系统类型不为vfat的文件
            find   /mnt   -name t.txt ! -ftype vfat  
            
        21、在/tmp下查找名为wa开头且类型为符号链接的文件  
            find   /tmp   -name wa* -type l   
    
        22、在/home下查最近两天内改动过的文件          
            find   /home   -mtime   -2                  
            
        23、查1天之内被存取过的文件
            find /home    -atime -1       
    
        24、在/home下查60分钟前改动过的文件  
            find /home -mmin    +60      
        
        25、查最近30分钟前被存取过的文件               
            find /home   -amin   +30    
        
        26、在/home下查更新时间比tmp.txt近的文件或目录                
            find /home   -newer   tmp.txt               
            
        27、在/home下查存取时间比tmp.txt近的文件或目录
            find /home   -anewer   tmp.txt              
            
        28、列出文件或目录被改动过之后,在2日内被存取过的文件或目录
            find   /home   -used   -2          
                     
        29、 列出/home目录内属于用户cnscn的文件或目录
            find   /home   -user cnscn                 
            
        30、列出/home目录内用户的识别码大于501的文件或目录
            find   /home   -uid   +501                  
            
        31、列出/home内组为cnscn的文件或目录
            find   /home   -group   cnscn               
        32、列出/home内组id为501的文件或目录
            find   /home   -gid 501                     
        33、列出/home内不属于本地用户的文件或目录
            find   /home   -nouser                      
        34、列出/home内不属于本地组的文件或目录
            find   /home   -nogroup 
        35、 列出/home内的tmp.txt 查时深度最多为3层                    
            find   /home    -name tmp.txt    -maxdepth   4 
        36、从第2层开始查  
            find   /home   -name tmp.txt   -mindepth   3      
        37、查找大小为0的文件或空目录    
            find   /home   -empty 
        38、查大于512k的文件                            
            find   /home   -size   +512k   
        39、查小于512k的文件                   
            find   /home   -size   -512k  
        40、 查硬连接数大于2的文件或目录                    
            find   /home   -links   +2   
        41、查权限为700的文件或目录                    
            find   /home   -perm   0700    
        42、exec与ok       
            find   /tmp   -name tmp.txt   -exec cat {} \;
            find   /tmp   -name   tmp.txt   -ok   rm {} \;
        43、查找在系统中最后10分钟访问的文件
            find    /   -amin    -10   
        44、查找在系统中最后48小时访问的文件     
            find    /   -atime   -2        
        45、查找在系统中为空的文件或者文件夹 
            find    /   -empty     
        46、 查找在系统中属于 groupcat的文件        
            find    /   -group   cat 
        47、查找在系统中最后5分钟里修改过的文件     
            find    /   -mmin   -5  
        48、查找在系统中最后24小时里修改过的文件       
            find    /   -mtime   -1      
        49、查找在系统中属于作废用户的文件  
            find    /   -nouser  
        50、查找在系统中属于FRED这个用户的文件          
            find    /   -user    fred      
    

    grep 命令

    grep [-acivn] [--color=auto] '搜寻字符串'
        选项与参数:
            -a:将binary文件以text文件的方式搜寻;
            -c:计算到'搜寻字符串'的次数;
            -i: 忽略大小写的不同;
            -n:输出行号
            -v:反向选择
            --color=auto :可以将找到的关键字部分加上颜色显示;
    

    例子

        1、将/etc/passwd有出现root的行取出来
        grep root /etc/passwd
        
        2、将/etc/passwd有出现root的行取出来,同时显示这些行在/etc/passwd的行号
        grep -n root /etc/passwd
        
        3、将/etc/passwd没有出现root的行取出来
        grep -v root /etc/passwd 
        
        4、将/etc/passwd,没有出现root和nologin的行取出来
        grep -v root /etc/passwd|grep -v nologin
        
        5、用demege列出核心信息,在以grep找出内含eth那行,且加上行号来显示
        dmesg|grep -n 'eth'
        
        6、根据文件内容递归查找目录
            1>在当前目录搜索energywise行的文件
            grep energywise .
            2>在当前目录及其子目录搜索energewise行的文件
            grep -r energewise ./ 
            3>在当前目录及其子目录下搜索energywise行的文件,只显示匹配的文件
            grep -r -l energewise ./
            
        7、搜索test和tast字符串
        grep 't[ae]st' regular_express.txt
        
        8、字符串的反向选择[^]:如果想要搜索到有oo的行,但不想要oo前面有g:
        grep -n '[^g]oo' regular_express.txt
        
        9、oo前面不出现小写字节
        grep -n '[^a-z]oo' regular_express.txt 
        
        10、想要取得有数字的行:
        grep -n '[0-9]' regular_express.txt
        
        11、搜索出以the开头的行
        grep -n '^the' regular_express.txt
        
        12、搜索以小写字母开头的行
        grep -n '[a-z]' regular_express.txt
        
        12、搜索出非字母开头的行
        grep -n '[^a-zA-Z]' regular_express.txt
    
        13、末尾结束为小数点(.)的行
        grep -n '\.$' regular_express.txt
        
        14、找出空白行
        grep -n '^$' regular_express.txt
    
        15、找出g??d的字符串
        grep -n 'g??d' regular_express.txt
        
        16、至少出现两个o,就需要ooo* (*代表重复0个或多个前面的字符)
        grep -n 'ooo*' regular_express.txt
        
        17、开头结尾都是g,中间至少一个o
        grep -n 'goo*g' regular_express.txt
        
        18、g开头g结尾,当中的字符有无均可
        grep -n '^g.*g$' regular_express.txt
        
        19、找出任意有数字的行
        grep -n '[0-9][0-9]*' regular_express.txt
        
        20、找到两个连续o的字符串
        grep -n 'o\{2\}' regular_express.txt
        
        21、搜索出g后面接2-5个o,然后再接一个g的字串
        grep -n 'go\{2,5\}g' regular_express.txt
        
        22、两个以上的gooo...g
        grep -n 'go\{2,\}g' regular_express.txt
        grep -n 'gooo*g' regular_express.txt
        
        扩展grep(grep -E或者egrep)
        
        23、打印所有包含NW或EA的行
        egrep 'NW|EA' testfile
        
        24、对于标准grep,如果在扩展字符前面加\,grep会自动启用扩展选项-E
        grep 'NW\|EA' testfile
        
        25、搜索一个或多个w的行
        grep 'w\+' testfile
        grep -E 'w+' testfile
        egrep 'w+' testfile
        
        26、搜索所有包含0个或1个小说点的行
        grep '2\.\?[0-9]' testfile
        grep -E '2\.?[0-9]' testfile
        egrep '2\.?[0-9]' testfile
        
        27、搜索一个或者多个连续的no行
        grep '\(no\)\+' testfile
        grep -E '(no)+' testfile
        egrep '(no)+' testfile
        
        28、匹配上下文分别显示3行
        grep -C 3 'good' testfile
        
        29、只匹配整个单词而不是字符串的一部分(而不是magical)
        grep -w 'magic' testfile 
        
        30、只匹配man
        grep '\<man\>' testfile
    

    awk实例:

            -F指定分隔符,可指定一个或多个
            1、awk -F":" '{print $1}' /etc/passwd
            2、awk -F":" '{print $1 $3}' /etc/passwd
            3、awk -F":" '{print $1 " " $3}' /etc/passwd
            4、awk -F":" '{print "username:" $1 "\t\tuid:" $3}' /etc/passwd
            5、 awk -F'[: ]+' '{print $3;}' /data
        
           6、只查看test.txt文件(100行)内第20到第30行的内容
             awk '{if(NR>=20 &&NR<=30) print $1}' test.txt
           
           7、已知test.txt文件内容为:
           # cat test.txt
             I am Poe,my qq is 33794712
             请从该文件中过滤出'Poe'字符串与33794712,最后输出的结果为:Poe 33794712
             awk -F'[ ,]+' '{print $3" "$7}' test.txt
             
           8、统计/etc/passwd的账户人数
           # awk '{count++;print $0;} END{print "user count is ",count}' /etc/passwd
            root:x:0:0:root:/root:/bin/bash
            ..............................................
            user count is  27
            
          9、显示最近登入的5个账号
            # last -n 5 | awk '{print $1}'
            
          10、显示/etc/passwd的账户
            # cat /etc/passwd | awk -F':' '{print $1}'  
        
          11、显示/etc/passwd的账户和账户对应的shell,而账户与shell之间用tab键分割
            # cat /etc/passwd | awk -F':' '{print $1"\t"$7}'
        
           12、如果只显示/etc/passwd的账户和对应的shell,而账户与shell之间以逗号分割,而且所在的行添加name,shell,在最后一行添加"blue,/bin/bash".
            # cat /etc/passwd|awk -F':' 'BEGIN{print "name,shell"} {print $1","$7} END{print "blue,/bin/bash"}'
        
           13、搜索/etc/passwd有root关键字的所有行
            # awk -F':' '/root/' /etc/passwd
    
          14、统计/etc/passwd:文件名,每行的行号,每行的列数,对应的完整行内容:
            # awk -F':' '{print "filename:" FILENAME,"linenumber:" NR,"columns:" NF,"linecontent:" $0}' /etc/passwd
            # awk -F':' '{printf("filename:%s,linenumber:%s,columns:%s,linecontent:%s\n",FILENAME,NR,NF,$0)}' /etc/passwd
        
        
        15、统计/etc/passwd的账户人数
            awk '{count++;print $0;} END{print "user count is ",count}' /etc/passwd
        
        16、上面没有对count进行初始化
            awk 'BEGIN{count=0;print "[start]user count is ",count} {count=count+1;print $0} END{print "[end]user is ",count }' /etc/passwd
        
        17、统计某个文件夹下的文件占用字节数
            ls -l | awk 'BEGIN{size=0;} {size=size+$5;} END{print "[end]size is ",size}'
            以M为单位显示:
            ls -l | awk 'BEGIN{size=0;} {size=size+$5} END{print "size is ",size/1024/1024,"M"}'
        
        18、统计某个文件夹下的文件占用的字节数,过滤4096大小的文件(一般都是文件夹)
            ls -l | awk 'BEGIN{size=0;print "[start]size is ",size} {if($5!=4096){size=size+$5;}} END{print "[end]size is ",size/1024/1024,"M"}'
            
        19、显示/etc/passwd的账户
            awk -F':' 'BEGIN{count=0;} {name[count]=$1;count++} END{for(i=0;i<NR;i++) print i ,name[i]}' /etc/passwd
        
        
        sed 命令
        1、只查看文件的100行到200行
            sed -n '100,200p' /etc/passwd
        
        2、地址是逗号分隔的,那么需要处理的地址是这两行之间的范围(包括这两行在内)。范围可以用数字、正则表达式、或二者的组合表示。例如:
            删除第二行到第五行:                      sed '2,5d' test.log
            删除包含"My"的行到包含"You"的行之间的行:   sed '/My/,/You/d' test.log
            删除包含"My"的行到第十行之间的内容:            sed '/My/,10d' test.log 
            
        3、打印匹配行
            sed '/My/p' test.log
        
        4、删除最后一行,其它的都会显示
            sed '$d' test.log
        
        5、删除包含My的行,其余的都被显示
            sed '/My/d' test.log
            
        6、替换
            sed 's/My/You/g' test.log
        
        7、取消默认输出,处理1到20行里匹配以My结尾的行,把行内所有的My替换为You,并打印到屏幕上
            sed -n '1,20s/My$/You/gp' test.log
        
        8、多重编辑(第一重编辑删除第1-3行。第二重编辑将出现的所有My替换为Your
            sed -e '1,3d' -e 's/My/You/g' test.log
        
        9、r命令
           如果在文件datafile的某一行匹配到模式My,就在该行后读入文件introduce.txt的内容。如果出现My的行不止一行,则在出现My的各行后都读
           入introduce.txt文件的内容:
            sed 's/My/r introduce.txt' datafile
            
            w命令
            sed -n '/My/w me.txt' test.log
            
            y命令(转换命令)
            将1到20行内,所有的小写hrwang转换成大写,将1转换成^,将2转换成$。
            sed '1,20y/hrwang/HRWANG^$'
    

    相关文章

      网友评论

          本文标题:find、grep、awk、sed实例

          本文链接:https://www.haomeiwen.com/subject/vqlavftx.html