Monday, 14 September 2009

Handling files in creation order in bourne shell

Sometimes the simplest sounding things get quite complicated. I initially thought listing files (and only files) in a directory in the order they were created would be easy. Turns out I couldn't find a way to sort by creation time, only by modification time (ls -ct). No probs, I did

line=`ls -l | sort -k 6,7`
FILE=`echo $line | sed -r 's/^(.*) (.*)$/\2/'`

Which is nice as long as there are no sub directories in the directory. Getting it right took a bit more time, this is how I did it:

for line in `find $WORK_DIR -maxdepth 1 -type f -printf '%f/%A@' | sort -k 2`
do
FILE=`echo $line | sed -r 's/^(.*)\/(.*)$/\2/'`
# Do what ever you want with the file
done
'ls' just didn't seem to cut it. find did, but it did get a bit complicated for such a simple task. But I'm sure there's an easier way...

0 comments:

Post a Comment