shell 批量替换文件名,实现目录合并
使用shell脚本替换 文件名,并把两个目录合并
源文件目录结果如下:
ls -l /temp/filelist/2001/
-rw-r–r– 1 test test 6472 03-28 19:19 100-2001-9-2-0.png
-rw-r–r– 1 test test 5860 03-28 19:19 100-2001-9-2-1.png
-rw-r–r– 1 test test 6472 03-28 19:20 100-2001-9-3-0.png
-rw-r–r– 1 test test 5860 03-28 19:20 100-2001-9-3-1.png
-rw-r–r– 1 test test 6472 03-28 19:20 100-2001-9-4-0.png
-rw-r–r– 1 test test 5860 03-28 19:20 100-2001-9-4-1.png
-rw-r–r– 1 test test 6472 03-28 19:20 100-2001-9-5-0.png
-rw-r–r– 1 test test 5860 03-28 19:20 100-2001-9-5-1.png
ls -l /temp/filelist/2003/
-rw-r–r– 1 test test 6472 03-28 19:19 100-2003-8-2-0.png
-rw-r–r– 1 test test 5860 03-28 19:19 100-2003-8-2-1.png
-rw-r–r– 1 test test 6472 03-28 19:20 100-2003-8-3-0.png
-rw-r–r– 1 test test 5860 03-28 19:20 100-2003-8-3-1.png
-rw-r–r– 1 test test 6472 03-28 19:20 100-2003-8-4-0.png
-rw-r–r– 1 test test 5860 03-28 19:20 100-2003-8-4-1.png
-rw-r–r– 1 test test 6472 03-28 19:20 100-2003-8-5-0.png
-rw-r–r– 1 test test 5860 03-28 19:20 100-2003-8-5-1.png
要求把2003目录中的“2003”替换为“2001”,并且把2003目录中修改文件名后的文件合并到2001目录中。
[local#2hei.net]# more ChangeName.sh
#!/bin/sh
# Edit By 2hei
Path=”/temp/filelist”
ls -l $1 |awk ‘{if($8>0) print $8}’ > $Path/list
while read Line
do
Name=`echo $Line|sed ‘s/2003/2001/g’`
cp $Path/2003/$Line $Path/2001/$Name
done < $Path/list
Usage: sh ChangeName.sh /temp/filelist/2003
本文固定链接: https://www.2hei.net/2008/03/30/shell_rename_file_join_dir/ | 2hei.net
最活跃的读者