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 #查找当前目录下,所有文件,然后查看这些文件的文件类型。
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 #查找当前目录下,所有文件,然后查看这些文件的文件类型。
相关文章推荐
- 编写个shell脚本将/home/test 目录下大于10K的文件转移到/tmp目录下
- shell type使用
- 手把手教你编写一个具有基本功能的shell(已开源)
- bash的配置文件类型
- 学习笔记——shell命令(1)GREP
- 多功能mysql备份shell
- more than one device/emulator问题解决方法
- shell变量的组合变量
- git bash 出现vim的时候怎么退出
- 常用shell指令
- shell脚本参数传递和main函数参数传递方式类似
- shell string wrap
- shell中的内容变量
- shell练习003--获取eth0的MAC并转化成10进制
- HBASE启动脚本/Shell解析
- 使用Shell脚本查找程序对应的进程ID,并杀死进程
- 几点简单的Webshell隐藏技巧
- 完美实现GIF动画缩略图(GIF压缩使用C++、shell命令、php)
- Shell字符操作命令——grep、sed、awk
- Shell Debug