今天以前的一个同事问我,要把系统中的大量类似er4567io.txt的文件文件重命令为er7890.txt。
开始以为直接用sed就可以搞定,结果却不行,郁闷
# find . -name "*4567*" -exec cp -v {} $(echo {}|sed 's/4567/7890/') \;
cp: `./er4567io.txt' and `./er4567io.txt' are the same file
cp: `./er4567io.txt' and `./er4567io.txt' are the same file
最后,还是用awk才搞定
# for i in `find . -name "*4567*"`;do echo $i| awk -F4567 '{print "cp -v "$1"4567"$2" "$1"7890"$2}';done|sh
`./er4567io.txt' -> `./er7890io.txt'
`./er4567io.txt' -> `./er7890io.txt'
其实有更简单的办法,大家可以参考这个,比我这办法简单多了
http://www.linux-wiki.cn/index.php/%E6%89%B9%E9%87%8F%E9%87%8D%E5%91%BD%E5%90%8D%E6%96%87%E4%BB%B6