本文共 1128 字,大约阅读时间需要 3 分钟。
找到 /123 目录下所有后缀名为 .txt 的文件
在脚本总已经加进了我的分析,可以看看
[root@cenvm71 work]# cat file_tar.sh #!/bin/bash# 将符合条件的,以.txt 结尾的文件,保存到 /tmp/file.txt find /usr/local/sbin/work/ -maxdepth 1 -type f -name "*.txt" > /tmp/file.txt# 用循环逐行读取 /tmp/file.txt 文件,修改文件名为 .txt.bakwhile read line ; do mv $line $line.bakdone
【分析】
find /usr/local/sbin/work -type f -name "*.txt" -print0 | xargs -d '\0' mv {} {}.bak
要注意,find 命令的查找路径需要使用绝对路径,不要用相对路径。如果用 xargs 命令接在后面,则用 -print0 选项,将某些包含空格的特殊文件名,也包含在内,不会处理错误。
如果你没有 .txt 结尾的文件,可以用下面的命令生成一堆,用来做实验:
for i in `seq 30`;do touch $i.txt;done
这样就可以生成30个 .txt 结尾的文件用来做实验了。
今天的习题帮大家复习了find命令,xargs 命令,还有for 循环,while 循环的常见用法,关键是学习那种处理问题的思路。
转载于:https://blog.51cto.com/hellocjq/2111993