Changing File Time Stamp (Bash Script)

I need some help recovering from a "slight" screwup. We just moved 3 TB of data from one RAID Array to another. Low lever archive files. This was done with a regular cp (for some reason) and now we have lost all the timestamps on the files, and we urgently need to get the timestamps back on these files.

We are running Ubuntu 9.10 Server and we have mounted the following

  1. /mnt/old-raid ##Old raid from the old server
  2. /mnt/new-raid ##New raid on the server

I know we can read out the timestamp on the old server using the command stat -c '%Y' <<filname>>

I know we can change the timestamp of the file, using the command touch -d '<<date>>'

To get from the stat -c date to the input date in touch we need to use date -d @<<timestamp>> +'%d %b %Y %R'

So my question is, how can I create a loop that will list all files in a folder, get their timestamp and update the old timestamp with the new?

Clear as mud?

find /path/to/directory will list all files and directories under /path/to/directory including /path/to/directory itself. You can either read the filenames into your script and handle it that way, or have find call your script using the -exec parameter.

I have a script working for the timestamp change, need a good way to run it on all files

#!/bin/bash
#
BASE_OLD=/mnt/old-raid
BASE_NEW=/mnt/new-raid

TS=$(stat -c '%Y' "${BASE_OLD}${*}")

TIMESTAMP=$(date -d @${TS} +'%d %b %Y %R')

touch -d "${TIMESTAMP}" "${BASE_NEW}${*}"
exit
#!/bin/bash
#
BASE_OLD=/mnt/old-raid
BASE_NEW=/mnt/new-raid

cd $BASE_OLD
find . -type f |
while read fname 
do
touch -t $(date -r $fname  +%Y%m%d%H%M%S)  $BASE_NEW/${fname}
done

If you have lots of files this will take a while to run

Almost there, but I need to remove "./" from the $fname variable. The first 2 characters.

Why do you need to remove the ./

/path/./to/./somewhere

is valid and is the same as

/path/to/somewhere

example:

appworx> ls -l  /./etc/./passwd
-r--r--r--   1 root     sys         4081 Aug 26 22:11 /./etc/./passwd
appworx> ls -l /etc/passwd
-r--r--r--   1 root     sys         4081 Aug 26 22:11 /etc/passwd

You are quite correct. Got the script working.

Thanks for the support.

Got it working. Thanks for the support.

#!/bin/bash
#
BASE_OLD=/mnt/old-raid
BASE_NEW=/mnt/new-raid

cd $BASE_OLD
find . -type f |
while read fname
do
$BASE_NEW/${fname}

TS=$(stat -c '%Y' "${BASE_OLD}/${fname}")


TIMESTAMP=$(date -d @${TS} +'%d %b %Y %R')

touch -d "${TIMESTAMP}" "${BASE_NEW}/${fname}"

echo "${BASE_NEW}/${fname}"

done

Jim,

We have an exact requirement, but it is not like chj's where it is on two different mountpoints. The files are on one directory and one mount point. All we need to do is update the timestamp of the file so they don't automatically get purged due to aging by our retention policy.

I used the exact code for the same requirement...but it does not work. We have AIX box and use ksh shell. I believe it is due to that...

Instead I was trying to use it this way. It does change the timestamp, but I would like to pass the timestamp as a parameter like you suggested

(+%Y%m%d%H%M%S)

so when we run this on a monthly basis it will change the timestamp as of the run date.

#!/usr/bin/ksh
find . -type f |
while read fname
do
touch -t 201003050525.26 $SEQ_DIR/${fname}
done

In additon to that, how can we also make the code work so it only picks the files that we need? Should we pass a list file as a parameter which has the list of files that need to be touched with the new timestamp?

Please advise on the best way to approach this.

Regards,
MK