Last active
November 18, 2023 20:22
-
-
Save makbeta/27d0ab6493a901dc9895f8cd9a190208 to your computer and use it in GitHub Desktop.
Organize files with date name format into subfolders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Handy script to help move images created with a phone camera into date-based folders | |
# Accepts files in the format yyyymmddhhjjss.* | |
# creates subdirectories yyyy-mm-dd | |
# moves files into subdirectories renames them to yyyy-mm-dd-hh-jj-ss.* (extension is preserved) | |
# Tested against Bash v3.2.57, the expressions are overly verbose as the compact expressions | |
# do not produce the same results, in this environment | |
if [[ $(ls | grep -E "[0-9]{8}_.*") ]]; then | |
for name in [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]_*.*; do | |
echo "$name" | |
new_dir=$(echo $name | sed -e 's/\([0-9][0-9][0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9]\)_\(.*\)/\1-\2-\3/g') | |
new_name=$(echo $name | sed -e 's/\([0-9][0-9][0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9]\)_\([0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9]\)/\1-\2-\3-\4-\5-\6/g') | |
echo "new dir $new_dir" | |
echo "new name $new_name" | |
mkdir -p "$new_dir" | |
mv -v "$name" "$new_dir/$new_name" | |
done | |
else | |
echo "No files in this directory match the expected pattern, no action taken." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment