LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-24-2009, 09:00 AM   #1
bufo
LQ Newbie
 
Registered: Sep 2009
Posts: 7

Rep: Reputation: 0
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

Last edited by bufo; 10-24-2009 at 09:04 AM.
 
Old 10-24-2009, 10:31 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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
 
Old 10-24-2009, 10:41 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by bufo View Post
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).
 
Old 10-25-2009, 02:31 AM   #4
bufo
LQ Newbie
 
Registered: Sep 2009
Posts: 7

Original Poster
Rep: Reputation: 0
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Get directory time stamp in c++ craigorymaas Programming 11 04-17-2009 12:23 AM
Using time-stamp in emacs dualcore75 Programming 1 02-08-2007 06:41 AM
Time stamp Kalyani1 Linux - Software 0 11-07-2005 02:58 PM
set up the Time stamp Kalyani1 Linux - Software 3 11-06-2005 02:36 PM
Time stamp in Samba is 11 hours behind time stamp in Linux Linh Linux - General 3 09-04-2003 12:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration