Bash 命令以查找常见文件名并对其内容进行分类
Bash command in order to find common filename and cat their content
您好,我有 3 个目录,例如:
/path1/path2/pathA/path4/
1_to_45.txt
ABC
1_to_46.txt
DEF
1_to_47.txt
GHI
/path1/path2/pathB/path4/
1_to_45.txt
JKL
1_to_48.txt
MNO
1_to_47.txt
PQR
/path1/path2/pathC/path4/
1_to_45.txt
STW
1_to_49.txt
XYZ
1_to_47.txt
AUI
1_to_41.txt
IOP
而且我想将 3 个目录之间共有的所有文件 cat 到 Merged_directory
目录中,并创建以与 comme filename 相同的方式命名的文件
例如,3 个目录之间有两个共同的文件:
/path1/path2/Merged_directory
1_to_45.txt
ABC
JKL
STW
1_to_47.txt
GHI
PQR
AUI
有人有想法吗?
遍历一个目录中的文件名。检查其他两个目录中是否也存在相应的名称。如果是这样,将它们全部cat到合并目录中。
dir1=/path1/path2/pathA/path4
dir2=/path1/path2/pathB/path4
dir3=/path1/path2/pathC/path4
merged=/path1/path2/Merged_directory
for file in "$dir1"/*.txt
do
fn=${file##*/}
if [ -f "$dir2/$fn" ] && [ -f "$dir3/$fn" ]
then
cat "$fn" "$dir2/$fn" "$dir3/$fn" > "$merged/$fn"
fi
done
您好,我有 3 个目录,例如:
/path1/path2/pathA/path4/
1_to_45.txt
ABC
1_to_46.txt
DEF
1_to_47.txt
GHI
/path1/path2/pathB/path4/
1_to_45.txt
JKL
1_to_48.txt
MNO
1_to_47.txt
PQR
/path1/path2/pathC/path4/
1_to_45.txt
STW
1_to_49.txt
XYZ
1_to_47.txt
AUI
1_to_41.txt
IOP
而且我想将 3 个目录之间共有的所有文件 cat 到 Merged_directory
目录中,并创建以与 comme filename 相同的方式命名的文件
例如,3 个目录之间有两个共同的文件:
/path1/path2/Merged_directory
1_to_45.txt
ABC
JKL
STW
1_to_47.txt
GHI
PQR
AUI
有人有想法吗?
遍历一个目录中的文件名。检查其他两个目录中是否也存在相应的名称。如果是这样,将它们全部cat到合并目录中。
dir1=/path1/path2/pathA/path4
dir2=/path1/path2/pathB/path4
dir3=/path1/path2/pathC/path4
merged=/path1/path2/Merged_directory
for file in "$dir1"/*.txt
do
fn=${file##*/}
if [ -f "$dir2/$fn" ] && [ -f "$dir3/$fn" ]
then
cat "$fn" "$dir2/$fn" "$dir3/$fn" > "$merged/$fn"
fi
done