Skip to content

Instantly share code, notes, and snippets.

@Korko
Created April 15, 2013 21:37
Show Gist options
  • Save Korko/5391465 to your computer and use it in GitHub Desktop.
Save Korko/5391465 to your computer and use it in GitHub Desktop.
Rename all files in a directory to year_month_day_hour_minutes.ext from date of last modification.
#! /bin/ksh
if [ $# -ne 1 ] ;then
echo "USAGE: $0 <directoryToScan>"
exit 1;
fi
for file in "$1"/* ;do
extension=${file##*.}
fulldate=`ls -al "$file" | tr -s ' ' | cut -d' ' -f6-7`
date=${fulldate%% *}
fulltime=${fulldate##* }
year=${date:0:4}
month=${date:5:2}
day=${date:8:2}
hour=${fulltime:0:2}
minutes=${fulltime:3:2}
echo "mv \""$file"\" \""$year"_"$month"_"$day"_"$hour"_"$minutes"."$extension"\""
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment