Visit The New, Modern Unix Linux Community


Changing File Time Stamp (Bash Script)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Changing File Time Stamp (Bash Script)
# 1  
Changing File Time Stamp (Bash Script) [SOLVED]

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?

Last edited by chj; 01-06-2010 at 01:57 PM..
# 2  
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.
# 3  
I have a script working for the timestamp change, need a good way to run it on all files

Code:
#!/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


Last edited by jim mcnamara; 01-06-2010 at 12:42 PM.. Reason: complete code tags
# 4  
Code:
#!/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
# 5  
Almost there, but I need to remove "./" from the $fname variable. The first 2 characters.

Last edited by chj; 01-06-2010 at 01:25 PM..
# 6  
Why do you need to remove the ./

/path/./to/./somewhere

is valid and is the same as

/path/to/somewhere


example:
Code:
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


Last edited by jim mcnamara; 01-06-2010 at 01:41 PM..
# 7  
Solved

You are quite correct. Got the script working.

Thanks for the support.

Got it working. Thanks for the support.

Code:
#!/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


Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #657
Difficulty: Easy
The last retail version of MS-DOS was MS-DOS 7.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script | Parse log file after a given date and time stamp

I am developing one script which will take log file name, output file name, date, hour and minute as an argument and based on these inputs, the script will scan and capture all the error(s) that have been triggered from a given time. Example: script should capture all the error after 13:50 on Jan... (2 Replies)
Discussion started by: ROMA3
2 Replies

2. Shell Programming and Scripting

Changing time-stamp with sed

Hi ! I try to change a time-stamp hh:mm:ss allways to full ten-minutes. example: 12:51:03 to 12:50:03 sed 's/::/:{0-5}0:/g' file.txt but it will not work propperly, because the minute-decade will be replaced with the bracket-term {0-5}. Can someone please give me a hint? Thanks in... (6 Replies)
Discussion started by: IMPe
6 Replies

3. Shell Programming and Scripting

file time stamp

Hi All, I am facing small problem. i want to print file time stamp on which date file has placed in the server. i have given some code but its not giving the year. any help appreciated. regards rajesh. (4 Replies)
Discussion started by: rajesh_pola
4 Replies

4. Shell Programming and Scripting

creating a file with time stamp

Hi guys, Here my scenario is to find the files of previous days if the previous day load had not done. for that i created a file with time stamp and this file is created after the load completes. so every dau i search for the this file with previous days time stamp. i want to create a file... (1 Reply)
Discussion started by: apple2685
1 Replies

5. Shell Programming and Scripting

Run a script when a file is modified/time stamp changed

Hi, I need to run a script file which uses a file and that file is modified as and when some alarms generated, it is not based on any fixed time period.. it may be modified even once in a minute for some time and once in 30 min or once in 20 min. Hence i need to watch for the timestamp change of... (3 Replies)
Discussion started by: aemunathan
3 Replies

6. Shell Programming and Scripting

Change time stamp of a file

Hi, As i know , we can change the time stamp of a file by touch command, i did change in a file and it is looking as given # ls -l abcd -rw-r--r-- 1 batsoqa sicusers 0 Feb 17 2010 abcd actually i want to see the output like this -rw-r--r-- 1 batsoqa sicusers ... (3 Replies)
Discussion started by: apskaushik
3 Replies

7. Shell Programming and Scripting

copying a file without changing date stamp.

Hi, I am using the below copy command, to copy the file sbn to sbn1, cp sbn sbn1 but its changing the date stamp of file sbn1, but i dont want to change the date stamp of sbn1. Could you please help me out in this. (3 Replies)
Discussion started by: shivanete
3 Replies

8. Shell Programming and Scripting

how do i put a time stamp in a file name

i want to copy a filea.dat to a file name in the format of filea_yyyymmdd_hhmi.dat using something like DTSTAMP=$(date "+%Y%m%d"), which puts it in format filea_yyyymmdd.dat (5 Replies)
Discussion started by: jhamm
5 Replies

9. UNIX for Dummies Questions & Answers

How to MV without changing Time Stamp

Hi, I need to move the set of files, and it should be same time stamp as previous. How to do this? (3 Replies)
Discussion started by: redlotus72
3 Replies

10. UNIX for Dummies Questions & Answers

File date and time stamp

I have to capture the creation date and time stamp for a file. The ls command doesn't list all the required information. I need year, month, day, hour, minute and second. Any ideas... (1 Reply)
Discussion started by: Xenon
1 Replies

Featured Tech Videos