It'd be very useful, I'm using it sometimes under Windowze ("FileMenu Tools" application).don570 wrote:For instance does anybody know where there's a script to rename
files in a given folder according to modification date
fileX fileY fileZ to fileX000 fileY001 fileZ003
(the sorting depends on modification date of the file
rather than alphabetical)
Could be something like this?
Code: Select all
#!/bin/bash
# batchaddnum by SFR'2012
# Usage: batchaddnum <folderpath> (DO NOT USE ENDING / PLEASE!)
FOLDERPATH=$1
# Dump all filenames from FOLDERPATH into temporary file
# 'ls -t' option sorts filenames in date order ('-tr' for reversed date order)
# 'tail -n +2' removes "Total..." line
# 'egrep -v '^d' removes directories and '^l' removes links
# next 'cut's to leave only filename
ls "$FOLDERPATH" -A -o -t | tail -n +2 | egrep -v '^d' | egrep -v '^l' | cut -f2- -d ':' | cut -b 4- > /tmp/batchfileslist
# And finally rename all files adding numeric suffix
CNT=1
while read LINE; do
OLDNAME=$FOLDERPATH"/"$LINE
if [ ${#CNT} -eq 1 ]; then CNT="00"$CNT; fi
if [ ${#CNT} -eq 2 ]; then CNT="0"$CNT; fi
NEWNAME=$OLDNAME$CNT
mv "$OLDNAME" "$NEWNAME"
let CNT=10#$CNT+10#1
done < /tmp/batchfileslist
rm -f /tmp/batchfileslist
exit
Also, I've been thinking about using find FOLDERNAME -maxdepth 1 -type f, then stat -c %Y FILENAME and then join times and filenames and use sort...
Hope I saved you some time.
Greetings!