0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

xargs + zipコマンドでディレクトリごとにzip化する

Last updated at Posted at 2022-01-31

以下のようなディレクトリがあるとき、photo/2021_01 ディレクトリを zip 化した photo/2021_01.zip を作りたい。

photo
├─ 2021_01
    ├─ photo1.png
    ├─ photo2.png
    ├─ ...
├─ 2021_02
├─ 2021_03
├─ ...

コマンド

# photo のディレクトリで実行するとき
cd photo; ls | sed -e "s|/$||" | xargs -I{} zip -r {}.zip {}

# photo の1つ上のディレクトリで実行するとき
find photo -type d | grep "/" | xargs -I{} zip -r {}.zip {}

補足

zip コマンド

$ zip -r 生成するzipファイル名 zip化するディレクトリ名

xargs コマンド

-I replstr : 後ろのコマンドに含まれる replstr の文字列をパイプの出力から受け取った文字列に置換する。

$ echo "2021_01" | xargs -I{} zip -r {}.zip {}
=> zip -r 2021_01.zip 2021_01
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?