2つのディレクトリ間のファイル名の違いを見つける(ファイル拡張子を無視)

2つのディレクトリ間のファイル名の違いを見つける(ファイル拡張子を無視)

同期を維持する必要があるファイルがたくさんあります。たとえば、次のようになります。

./regular/*.txt
./compressed/*.txt.bz2

./regularにファイルをアップロードするときに、まだ圧縮されていないファイルを定期的にチェックしてbzip2で圧縮するスクリプトを作成したいと思います。

私の考えにはまるで...

ls ./regular/*.txt as A
ls ./compressed/*.txt* as B

for each in A as file
    if B does not contain 'file' as match
        bzip2 compress and copy 'file' to ./compressed/

これを実行できるプログラムはありますか?または、誰かがcoreutils / bashでこの種の操作をどのように実行するかを示すことができますか?

ベストアンサー1

zsh代わりに使用してくださいbash

regular=(regular/*.txt(N:t))
compressed=(compressed/*.txt.bz2(N:t:r))
print -r Only in regular: ${regular:|compressed}
print -r Only in compressed: ${compressed:|regular}

これにより、次のことができます。

for f (${regular:|compressed}) bzip2 -c regular/$f > compressed/$f.bz2

${A:|B}これは、配列減算演算子(要素に拡張)を使用して行われます。A バー(除く)B)彼。

bashGNUツールの使用:

(
  export LC_ALL=C
  shopt -s nullglob
  comm -z23 <(cd regular && set -- *.txt && (($#)) && printf '%s\0' "$@") \
            <(cd compressed && set -- *.txt.bz2 && (($#)) &&
               printf '%s\0' "${@%.bz2}")
) |
  while IFS= read -rd '' f; do
    bzip2 -c "regular/$f" > "compressed/$f.bz2"
  done

その後、減算はコマンドによって実行されますcomm。ここでNUL区切り文字を使用すると、zshソリューションと同様に任意のファイル名を処理できます。

おすすめ記事