
フォルダが700個ほどあります。各フォルダには、ファイルのペア別の組み合わせが含まれています。各ペアごとの組み合わせに対して1つのファイルだけを維持したいと思います。どちらのファイルにも同じコンテンツが含まれているため、任意のペアのファイルをアーカイブできます。フォルダのファイル名は必ずしもアルファベット順に指定されるわけではありません。
Example:
Folder1:
-> A-B.txt
-> B-A.txt
Folder2:
-> C-D.txt
-> C-E.txt
-> E-C.txt
-> D-E.txt
-> D-C.txt
-> E-D.txt
Final folder structure:
Folder1:
-> A-B.txt (or) B-A.txt
Folder2:
-> C-D.txt (or) D-C.txt
-> C-E.txt (or) E-C.txt
-> D-E.txt (or) E-D.txt
答え1
次のようなことができます
ls *.txt | awk -F '[.-]' '{ if (f[$2,$1]) { print $0; }
else { f[$1,$2] = 1} }' | xargs rm
次のように動作します。そのファイルの名前をに割り当てますawk
。各ファイルに対して、逆の名前のファイルがすでに配列に入力されていることを確認してくださいf
。その場合はファイル名を出力します。そうでない場合は、配列に入れてくださいf
。プログラムの出力を使用してawk
重複ファイルを削除します。
答え2
find
ファイル名でダッシュの前後の部分を使用して抽出し、ペアが存在するかどうかをテストし、存在する場合はそのファイルを削除できます。
find . -name \*-\*.txt -execdir sh -c 'fn=${1##*/};bn=${fn%.*};one=${bn%-*};
two=${bn#*-};pair=${two}-${one}.txt; [[ -f $pair ]] && rm "$1"' boom {} \;
ループを使用して同じことを実行できますfor
(シェルが再帰ワイルドカードをサポートしていると仮定)。
# if you're using bash run
shopt -s globstar
それから
for f in **/*-*.txt; do
dn=${f%/*}; fn=${f##*/}; bn=${fn%.*}; one=${bn%-*}; two=${bn#*-};
pair=${dn}/${two}-${one}.txt; [[ -f $pair ]] && rm -- "$f"; done
答え3
find . -type d -exec \
perl -wMstrict -le '
(local $", my $top) = ("", $ENV{PWD});
for my $curdir ( @ARGV ) {
my %h;
chdir $curdir;
for ( <*.txt> ) {
my @pair = /^([^-]+)-([^.]+)[.]txt$/;
next unless @pair;
$h{ "@pair" }++;
unlink if exists $h{ "@{[reverse @pair]}" };
}
chdir $top;
}
' {} +
sed
/bin/ls -1 |
sed -ne '
1H;1d
G
/^\([^-]*\)-\([^.]*\).txt\n\(.*\n\)\{0,1\}\2-\1.txt$/P
/^\([^-]*\)-\([^.]*\).txt\n\(.*\n\)\{0,1\}\2-\1.txt\n/P
s/\n\n.*//;H
' | xargs rm