您的位置:首页 > 运维架构 > Shell

linux shell 编程笔记 - 常用的find和xargs

2016-03-03 18:52 686 查看
find命令工具用于在目录下(甚至是整个文件系统),遍历地查找文件;
find path_name -option [-print -exec -ok]    #find的一般格式;
1、find命令的选项(-option)
1.1、根据名称查找文件和目录(-name):
    cb@Standalone14:~/Documents$ find . -name "cb*" -print    #在当前目录下,查找cb开头的文件及目录; 
1.2、根据文件修改时间查找文件和目录(-mtime):
    cb@Standalone14:~/Documents$ find . -mtime -2 -print    #在当前目录下,查找2天内修改的文件及目录;
    cb@Standalone14:~/Documents$ find . -mtime +5 -print    #在当前目录下,查找5天以前修改的文件及目录;
1.3、根据文件类型查找文件或目录(-type):
    find . -type d -print    #在当前目录下,查找所有的目录;
1.4、在find中使用exec和ok来执行shell命令:
    cb@Standalone14:~/Documents$ find . -type f -exec ls
-l {} \;     #在当前目录下,查找所有文件,然后执行 ls -l命令;
    cb@Standalone14:~/Documents$ find . -name "myfile2" -ok rm {} \;    #用安全模式ok,查找文件并删除文件;
    < rm ... ./shellT/myfi
4000
le2 > ? yes
    root@Standalone14:/home/cb/Documents# find /etc -name "passwd*" -type f  -exec grep "cb" {} \;    #查找文件,并通过grep查看文件中是否有包含cb的行。
2、xargs
    xargs把find的结果分批传给shell命令,避免过程占用机器的资源。
    cb@Standalone14:~/Documents$ find . -type f -print | xargs file    #查找当前目录下,所有文件,然后查看这些文件的文件类型。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: