LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   need help with time stamp script (https://www.linuxquestions.org/questions/linux-general-1/need-help-with-time-stamp-script-764155/)

bufo 10-24-2009 09:00 AM

need help with time stamp script
 
Hi,

I had to recover log files from a rescue disk, and now all the 1238 files have the same time stamp. I wanted to sort them in chronological order so that I can manage them more better. The files span from 2004-2009 and are of the form

log_102404.txt
log_020407.txt
log_010209.txt

I am trying to get a BASH script going to sort and time stamp them at the same time using the 'touch -m' option, but that did not go so well.

Code:

for f in (ls -t /file/directory)
do
  # any file earlier than today
  if [[ "X$f" < "Xlog_$(date +%m%d%y)" ]] # correct for ascii comparison
  then touch -m $f # will need a magic sort routine here
  fi
done

I can get a file_list with all the files sorted by file name, but getting the command 'touch -m | cat file_list.txt' did not work for me.
So I am a bit of stuck in the mud. Even though I can get a file list as a Perl array, but can't seem to get 'touch' to accept the file descriptors. 'touch' is complaining of indigestion "touch missing file operand."

I am now ripe for a kind suggestion.

Regards,

bufo

catkin 10-24-2009 10:31 AM

See if the script is running the commands you think it is and then try running that exact same command at the command line
Code:

for f in (ls -t /file/directory)
do
  # any file earlier than today
  if [[ "X$f" < "Xlog_$(date +%m%d%y)" ]] # correct for ascii comparison
  then
      echo touch -m $f # will need a magic sort routine here
  fi
done


unSpawn 10-24-2009 10:41 AM

Quote:

Originally Posted by bufo (Post 3730846)
Code:

for f in (ls -t /file/directory)

Hmm. AFAIK "-t" does not work if the files listed are not already timestamped differently but if your "file_list.txt" contains consistent name patterns of
Code:

log_102404.txt
log_020407.txt
log_010209.txt

and if that is a non-ISO-conforming date string I see there then you could
Code:

cat file_list.txt|while read FILENAME; do
 # Could make an array but this explains it better:
 M=${FILENAME:4:2}; D=${FILENAME:6:2}; Y="20${FILENAME:8:2}"
 # Remove the echo and double quotes once you have confirmed
 # this is what you're after:
 echo "touch -acmt ${Y}${M}${D}0000.01 ${FILENAME}"
done

Note that if any files follow a different naming scheme this will fsck up and silently so YMMV(VM).

bufo 10-25-2009 02:31 AM

Thanks for the suggestion unSpawn. I developed the idea a bit using a control script as input. This can be improved a bit further.

Code:

#!/bin/bash
# time_restamp.sh
## update file time stamp after rescue disk time stamp snafu

#-----------------
# utilty functions
#-----------------
function rmv_ctrl_script()
{
        if [ -e "/tmp/log_data.tmp" ]
        then
                rm "/tmp/log_data.tmp"
        fi
}
function gen_ctrl_script()
{
        #----------------------------
        # step into correct dir first
        #----------------------------
        working_dir="/var/logs/"
        if [ $(pwd) != "/var/logs/" ];
        then
                cd $working_dir
        fi
        # our log data spans 2004..2009
        #------------------------------------------
        # kick out ctrl script (most recent first)
        #------------------------------------------
        for f in $(ls -t $working_dir);
        do
                if [ "$f" == "inventory.txt" ] # skip inventory file
                then
                        continue
                # will need to update sel log for future dates
                # elif [[ "${f:11:2}" == "10" # etc.
                elif [[ "${f:11:2}" == "09" ]]
                        then echo $f >> /tmp/log_data.tmp
                elif [[ "${f:11:2}" == "08" ]]
                        then echo $f >> /tmp/log_data.tmp
                elif [[ "${f:11:2}" == "07" ]]
                        then echo $f >> /tmp/log_data.tmp
                elif [[ "${f:11:2}" == "06" ]]
                        then echo $f >> /tmp/log_data.tmp
                elif [[ "${f:11:2}" == "05" ]]
                        then echo $f >> /tmp/log_data.tmp
                elif [[ "${f:11:2}" == "04" ]]
                        then echo $f >> /tmp/log_data.tmp
                else
                        break
                fi
        done
}
function main_loop()
{
        # run script
        cat "/tmp/log_data.tmp" | while read FL; do
                M=${FL:7:2}; D=${FL:9:2}; Y=${FL:11:2}; mm=0000.00;
        touch -acmt 20${Y}${M}${D}${mm} ${FL};
        done
}
rmv_ctrl_script
gen_ctrl_script
main_loop

exit 0


With kind regards,

bufo


All times are GMT -5. The time now is 07:25 AM.