Skip to content

Instantly share code, notes, and snippets.

@southp
Last active May 11, 2017 03:40
Show Gist options
  • Save southp/44ee63174b9a4e9debe3eac5fe2b2718 to your computer and use it in GitHub Desktop.
Save southp/44ee63174b9a4e9debe3eac5fe2b2718 to your computer and use it in GitHub Desktop.
Group files under the target directory into yyyy-mm date directories.
#!/bin/sh
target_dir=$1
if [ ! "$target_dir" ]; then
echo "Usage: $0 <target dir>"
exit 0
fi
if [ ! -d "$target_dir" ]; then
echo "The target dir $target_dir does not exist."
exit 0
fi
cd "$target_dir"
file_list=$(find "$PWD" -type f)
IFS=$'\n'
for file in $file_list; do
if [ "$file" == "$0" ]; then
continue
fi
date=$(stat -f "%Sm" -t "%Y-%m" "$file")
mkdir -p $date
cp -pv "$file" $date
rm "$file"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment