通过比较两个文件夹bash / sed中的文件名来重命名文件

时间:2016-03-16 06:30:23

标签: bash sed

我正在玩bash,我遇到了一个问题。

在第一个文件夹中,我有文件列表

folder1/first_1.txt
folder1/second_2.txt
folder1/third_3.txt
folder1/फाइल_3.txt

在第二个文件夹中,我有文件列表

folder2/1.txt
folder2/2.txt
folder2/3.txt

现在,我需要比较第一个和第二个文件夹中的文件名,并将第二个文件夹的文件名替换为第一个文件夹文件名为

ouputfolder/fist.text
ouputfolder/second.text
ouputfolder/third.text
ouputfolder/फाइल.text

如何使用bash / sed完成?任何建议都表示赞赏。

2 个答案:

答案 0 :(得分:2)

使用find查找folder1while循环中的文件,以检查folder2文件名中每个文件名的相关部分是否存在。如果存在,则完成必要的重命名:

find folder1 -maxdepth 1 -type f -name '*.txt' -print0 | \
  while IFS= read -d $'\0' -r file; do start="${file%%_*}"; end="${file#*_}"; \
   [ -f folder2/"$end" ] && echo mv -i folder2/"$end" folder2/"$start".text; done

对要做出的改变感到满意吗?如果确定,请删除echo以执行操作:

find folder1 -maxdepth 1 -type f -name '*.txt' -print0 | \
   while IFS= read -d $'\0' -r file; do start="${file%%_*}"; end="${file#*_}"; \
     [ -f folder2/"$end" ] && mv -i folder2/"$end" folder2/"$start".text; done

示例:

% tree
.
├── folder1
│   ├── first_1.txt
│   ├── second_2.txt
│   └── third_3.txt
└── folder2
    ├── 1.txt
    ├── 2.txt
    └── 3.txt


% find folder1 -maxdepth 1 -type f -name '*.txt' -print0 | while IFS= read -d $'\0' -r file; do start="${file%%_*}"; end="${file#*_}"; [ -f folder2/"$end" ] && echo mv -i folder2/"$end" folder2/"$start".text; done
mv -i folder2/2.txt folder2/second.text
mv -i folder2/3.txt folder2/third.text
mv -i folder2/1.txt folder2/first.text

% find folder1 -maxdepth 1 -type f -name '*.txt' -print0 | while IFS= read -d $'\0' -r file; do start="${file%%_*}"; end="${file#*_}"; [ -f folder2/"$end" ] && mv -i folder2/"$end" folder2/"$start".text; done 

% tree                                                  
.
├── folder1
│   ├── first_1.txt
│   ├── second_2.txt
│   └── third_3.txt
└── folder2
    ├── first.text
    ├── second.text
    └── third.text

答案 1 :(得分:0)

已编辑,以便在所有文件名中完全正常运行。

sed 可用于搜索和替换文件中的字符串。它不是自然用于文件名。

尝试使用此测试版本(也使用shellcheck检查):

#!/bin/bash --
find folder2 -name \*.txt -printf "%f\n" | while read -r folder2fn ; do
  folder1fn=$(find folder1 -name \*_"${folder2fn}" -printf "%f");
  mv folder2/"${folder2fn}" folder2/"${folder1fn%_${folder2fn}}".text
done

构建测试文件夹:

mkdir folder1
printf "f1 %s\n" "one" > folder1/first_1.txt
printf "f1 %s\n" "two" > folder1/second_2.txt
printf "f1 %s\n" "three" > folder1/third_3.txt
printf "f1 %s\n" "four" > folder1/"$(printf "\0\1\2\3\4\5\6\7\10\11\12\13\14\15\16\17\20\21\22\23\24\25\26\27\30\31\32\33\34\35\36\37\40\42\47\54\77\134\140\177")"_4.txt
mkdir folder2
printf "f2 %s\n" "one" > folder2/1.txt
printf "f2 %s\n" "two" > folder2/2.txt
printf "f2 %s\n" "three" > folder2/3.txt
printf "f2 %s\n" "four" > folder2/4.txt

列出测试元素:

ls -1b folder*/*.txt

folder1/\001\002\003\004\005\006\a\b\t\n\v\f\r\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037\ "',?\\`\177_4.txt
folder1/first_1.txt
folder1/second_2.txt
folder1/third_3.txt
folder2/1.txt
folder2/2.txt
folder2/3.txt
folder2/4.txt

运行脚本:

./newscript.sh 

显示新文件并检查结果:

ls -1b folder2/*.text

folder2/\001\002\003\004\005\006\a\b\t\n\v\f\r\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037\ "',?\\`\177.text
folder2/first.text
folder2/second.text
folder2/third.text

替代一行版本

我发现这很有趣:) 它已经过测试,并通过shellcheck验证。

find folder2 -name \*.txt -printf "%f\0" | xargs -0 -I xxxx bash -c '( folder1fn=$(find folder1 -name \*_xxxx -printf "%f") ; mv folder2/xxxx folder2/"${folder1fn%_xxxx}".text )'