shell习题第5题:批量更改文件后缀名

【题目要求】

找到123/目录下所有后缀名为.txt的文件

1. 批量修改.txt为.txt.bak

2. 把所有.bak文件打包压缩为123.tar.gz

3. 批量还原文件的名字,即把增加的.bak再删除

【核心要点】

find 用来查找所有的.txt文件

tar 打包一堆文件

还原文件名用for循环

【脚本】

#!/bin/bash
find /123/ -type f -name "*.txt" > /tmp/txt.list 
for f in `cat /tmp/txt.list`
do
    mv $f  $f.bak
done

#find /123/ -type f -name *.txt |xargs -i mv {} {}.bak 
#find /123/ -type f -name *.txt -exec mv {} {}.bak ;

for f in `cat /tmp/txt.list`
do
    echo $f.bak
done > /tmp/txt.bak.list 

#备份
tar -czvf 123.tar.gz `cat /tmp/txt.bak.list |xargs ` for f in `cat /tmp/txt.list` do mv $f.bak $f done
原文地址:https://www.cnblogs.com/dingzp/p/10768379.html