update files and preserve date


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users update files and preserve date
# 1  
Old 01-20-2010
update files and preserve date

I have a bunch of historical files that need to be modified, as the file source announced there is an error in them which should be corrected.

I am planning to use sed to do the mass update, but I would like to know if there is a way to preserve files last modified date/time, so later ls -ltr lists will show me when files were obtained, not when I updated them.

Any idea?
# 2  
Old 01-20-2010
cp --preserve=timestamps
# 3  
Old 01-20-2010
One way:
Code:
filetime()
{
perl -e '      
      $mtime = (stat("$ARGV[0]"))[9];

      # time structure into variables

      ($sec,$min,$hr,$day,$mon,$yr,$wday,@dntcare) = localtime($mtime);
      $yr = ($yr>=70) ? $yr+1900 : $yr+2000;

      printf ("%d%02d%02d%02d%02d", $yr,$mon,$day,$hr,$min); ' "$1"
}
for fname in *.dat
do
# get file time in YYYYMMddhhmm format
   oldtime=$(filetime $fname)
   ls -l $fname
# sed your file here
#  sed 's/  / /g' $fname > tmp; mv tmp $fname
# put filetime back to where it was
  touch -t $oldtime $fname
  ls -l $fname
done

# 4  
Old 01-20-2010
Purfect, thanks a million!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Replace with last date update

Dear Masters, I have file input 20140901|Cross Nation|Asia 20140908|Cross Region|Europe 20140905|Cross Nation|Europe I want to replace column 1 with the last date update, so my output should be like 20140908|Cross Nation|Asia 20140908|Cross Region|Europe 20140908|Cross Nation|Europe ... (7 Replies)
Discussion started by: radius
7 Replies

2. UNIX for Dummies Questions & Answers

How to update a date dynamically using sed?

hi guys, i need to update a date in my config file via shell file M using this statement but it is not working ...any suggestions cat $CONFIG_FILE | sed -e 's/START_RUN_DATE=$START_RUN_DATE/START_RUN_DATE=$NEW_END_DATE/g' > srap_config_new.txt (1 Reply)
Discussion started by: ravidwivedi2288
1 Replies

3. Shell Programming and Scripting

How to list files that are not first two files date & last file date for every month-year?

Hi All, I need to find all files other than first two files dates & last file date for month and month/year wise list. lets say there are following files in directory Mar 19 2012 c.txt Mar 19 2012 cc.txt Mar 21 2012 d.txt Mar 22 2012 f.txt Mar 24 2012 h.txt Mar 25 2012 w.txt Feb 12... (2 Replies)
Discussion started by: Makarand Dodmis
2 Replies

4. Fedora

Unable to Update Date

Hi everyone, Thanks for your help with this. I'm running an AWS EC2 instance (ami-2bc05345) and the time has been drifting. I have a different server using the same instance where the time is fine. NTP seems to be installed and be running (I uninstalled it and reinstalled it via Yum, just to... (4 Replies)
Discussion started by: AaronLS2
4 Replies

5. Shell Programming and Scripting

Date Format - preserve whitespace

I am trying to change the date format for the following: YESTER=`TZ=aaa24 date +%b" "%d | sed 's/0/ /'` TraceList=$(ls -ltR /pdt/logs | grep "$YESTER" | awk '{print $9}') CMD2=$(find /disk/dan/dansFiles/pass/logs/$TList -name cmd\* -prune) what I am trying to do in the above... (1 Reply)
Discussion started by: ther2000
1 Replies

6. Shell Programming and Scripting

how to update date part with new increment date time

hi experts, my requirement is like this i need to develop a shell script to update date part with new incremental date time in file some 'X' which is kept at some server location incrementing every two hours.as i am new to this scripting i need support from u people,thanx in advance (1 Reply)
Discussion started by: amanmro
1 Replies

7. Shell Programming and Scripting

Sorting Files by date and moving files in date order

I need to build a k shell script that will sort files in a directory where files appear like this "XXXX_2008021213.DAT. I need to sort by date in the filename and then move files by individual date to a working folder. concatenate the files in the working folder then start a process once... (2 Replies)
Discussion started by: rebel64
2 Replies

8. UNIX for Dummies Questions & Answers

File update date

I am facing a peculiar problem with file update dates. In my network, if i telnet to machine A and check the files in dir test of machine A, the dates for the last update shown are true dates and upto date. If the same folder is checked through some other machine by sharing the test folder and... (2 Replies)
Discussion started by: amne
2 Replies

9. UNIX for Dummies Questions & Answers

ftp copy: preserve source file date stamp

Is there any way to preserve a file's create/mod date stamp when it is "put" via ftp (or even using Fetch) to a Win2K server and a SunOS 5.8 server? For example, if I copy a file with a create/mod date of "01/15/2005 3:36 PM" to my Win2K or SunOS ftp server, the date stamp will change to the... (5 Replies)
Discussion started by: cassj
5 Replies
Login or Register to Ask a Question