Shell script to create a folder daily with a time stamp and stamps generated using timestamps

I have a cron job that runs every 30 minutes to generate time stamped log files as follows:

test20130215100531.log, 
test20130215102031.log  

I would like to create one folder daily with a date stamp and click the log files in the corresponding date folder when creating.

I need to achieve this on an AIX server using bash.

+5
source share
2 answers

Perhaps you are looking for a script as follows:

#!/bin/bash

shopt -s nullglob  # this line is so that it does not compain when no logfiles are found
for filename in test*.log; do # Files considered are the ones startign with test and ending in .log
    foldername=$(echo "$filename" | awk '{print (substr($0, 5, 8));}'); # The foldername is characters 5 to 13 from the filename (if they exist)
    mkdir -p "$foldername"  # -p so that we dont get "folder exists" warning
    mv "$filename" "$foldername"
    echo "$filename $foldername" ;
done

I tested only your sample, so do the proper testing before using it in a catalog that contains important materials.

Edit in response to comments:

Change the source script to:

foldername=$(date +%Y%m%d)
mkdir -p  /home/app/logs/"$foldername"
sh sample.sh > /home/app/logs/"$foldername"/test$(date +%Y%m%d%H%M%S).log

- , :

sh sample.sh > /home/app/logs/$(date +%Y%m%d)/test$(date +%Y%m%d%H%M%S).log
+9

All Articles