ファイルを2つずつ圧縮

ファイルを2つずつ圧縮

次の800個のファイルを含むディレクトリがあります。

file1.png
[email protected]

file2.png
[email protected]

file3.png
[email protected]

... etc

このようなzipファイルを作成する必要があります

file1.zip (containing file1.png and [email protected])
file2.zip (containing file2.png and [email protected])
file3.zip (containing file3.png and [email protected])

魔法コマンドを使ってこれを行うことはできますか?

ベストアンサー1

もしいつも二人います。、非常に簡単です。

for f in file+([0-9]).png; do zip "${f%png}zip" "$f" "${f/./@2x.}"; done

上記の内容は、コマンドラインでそのまま実行されます。スクリプトでそれを使用する予定の場合は、shopt -s extglobこの行の前のどこかにこの行を入れてください。 (extglob対話型シェルではデフォルトで有効になっています。)

拡張を処理しない以前のbashモードでは、この醜い回避策は機能しますが、ロジックを変更する方が良いです。他の回答で提案したように著者: レオニド:

for f in file[0-9].png file*[0-9][0-9].png; do zip "${f%png}zip" "$f" "${f/./@2x.}"; done

おすすめ記事