Skip to content

Instantly share code, notes, and snippets.

@jadiunr
Last active December 2, 2017 12:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jadiunr/af5ecca10a7f67181c09a7a8d30b74ac to your computer and use it in GitHub Desktop.
Save jadiunr/af5ecca10a7f67181c09a7a8d30b74ac to your computer and use it in GitHub Desktop.

画像整理するだけのShellscript

ITC Advent Calendar(1) 2日目

https://adventar.org/calendars/2563

ブログに書くほどでもないのでGistに書こうと思う

Introduction

twitterとかpixivとかでかき集めたえろ画像を1個のフォルダに突っ込みまくってたらウン万件くらいいってたので
画像の更新日時を見て月毎(YYMM)にフォルダ分けするスクリプトを書きました。
Xubuntuでしか動作確認してません。
Windowsは?知るか

Script

#!/bin/bash

exts=("jpg" "png" "jpeg" "gif")
    for ext in "${exts[@]}";do
    find . -maxdepth 1 -name "*.${ext}" | while read file;do
        date=$(ls -l --time-style=long-iso "${file}" | sed -e 's/  */ /g' | cut -d " " -f 6 | cut -c 3-7 | sed -e 's/-//g')
        if [ ! -e "${date}" ];then
            mkdir "${date}"
        fi
        mv "${file}" "${date}"
    done
done

Description

for ext in "${exts[@]}";do

find . -maxdepth 1 -name "*.${ext}" | while read file;do

  • -maxdepthで階層の最大深度を指定できる
  • findコマンドの出力は1行毎に${file}に格納されてforeachみたいな感じになる

ls -l --time-style=long-iso "${file}" | sed -e 's/ */ /g' | cut -d " " -f 6 | cut -c 3-7 | sed -e 's/-//g'

  • --time-styleで更新日時の書式を指定できる  - long-isoだと数字(とハイフン)だけのアレになる
  • sed -e 's/ */ /g' | cut -d " " -f 6 | cut -c 3-7 | sed -e 's/-//gでめちゃクソ整形しまくったらYYMMの形になる
if [ ! -e "${date}" ];then
    mkdir "${date}"
  • ${date}ディレクトリが存在しなかった時に${date}ディレクトリを作る

っていう感じで整理しました。

おわり

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment