Skip to content

Instantly share code, notes, and snippets.

@muupan
Created September 17, 2013 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muupan/6599706 to your computer and use it in GitHub Desktop.
Save muupan/6599706 to your computer and use it in GitHub Desktop.
$HOME/Downloads直下にあるファイル及びディレクトリを変更日時で整理するシェルスクリプト
#!/bin/sh
dir="$HOME/Downloads"
cd $dir
files=`ls -1`
# Set IFS (Internal Field Separator)
# See http://linux.just4fun.biz/%E9%80%86%E5%BC%95%E3%81%8D%E3%82%B7%E3%82%A7%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88/%E3%82%B9%E3%83%9A%E3%83%BC%E3%82%B9%E3%81%8C%E5%90%AB%E3%81%BE%E3%82%8C%E3%82%8B%E6%96%87%E5%AD%97%E5%88%97%E3%82%921%E8%A1%8C%E3%81%A8%E3%81%97%E3%81%A6%E6%89%B1%E3%81%86%E6%96%B9%E6%B3%95.html
IFS_BACKUP=$IFS
IFS=$'\n'
for file in $files; do
year_month=`stat -f "%m" "$file" | gawk '{print strftime("%Y-%m", $1)}'`
if [ -d "$file" ]; then
match=`echo "$file" | egrep "^[0-9]{4}-[0-9]{2}$"`
if [ -n "$match" ]; then
continue
fi
fi
if [ ! -e "$year_month" ]; then
mkdir "$year_month"
fi
if [ ! -d "$year_month" ]; then
echo "Warning: $year_month is not a directory"
continue
fi
mv "$file" "$year_month"
done
IFS=$IFS_BACKUP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment