Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Copy timestamps between a file or multiple files UNIX
Sometimes I find the need to copy a file's timestamp to another file. For example, when I do batch conversion of video files (e.g. via VisualHub), the converted files' timestamps are all based on the current date. Subsequently when I import them in iMovie, I have to spend some effort to get them to show the correct date. It would be nicer if the converted files' timestamps were exactly the same as the original ones.

So I created this shell script, which allows you to copy the timestamp from one file to another.
#!/bin/bash

if [ $# -ne 2 ]; then
  echo "Usage: `basename $0` timestamp_src dest_file";
  exit 1;
fi

function printtimestamp()
{
  stat -n -f "%Sm" -t "%Y%m%d%H%M" "$1"
}

SRC=$1
DST=$2

if [ ! -f "${SRC}" ]; then
  echo "Error: ${SRC} does not exist.";
  exit 1;
fi;

if [ ! -f "${SRC}" ]; then
  echo "Error: ${SRC} does not exist.";
  exit 1;
fi;

TIME_SRC=`printtimestamp "${SRC}"`;

touch -t ${TIME_SRC} ${DST}
Just paste into a text editor and save it as a new shell script (cptime.sh, for instance) and run like so:
$ ./cptime.sh timestamp_src dest_file
This will copy the timestamp of the file timestamp_src to the file dest_file.

Even better, I created another script to batch copy timestamps from one directory to the same files in the current directory with a different extension. For example, if I want all the converted *.MOV files in ~/converted to have the same timestamp as the original *.MPG files in ~/orig, I would run this next script like so:
$ cd ~/converted
$ ./modtime.sh MPG MOV ~/orig
The source for this script (modtime.sh) is below.
#!/bin/bash

if [ $# -ne 3 ]; then
  echo "Usage: `basename $0` old_ext new_ext old_dir";
  exit 1;
fi

function printfilename()
{
  echo "$1" | awk -F'.' 'BEGIN { ORS="" } { print $1 }'
}

function printtimestamp()
{
  stat -n -f "%Sm" -t "%Y%m%d%H%M" "$1"
}

EXT_OLD=$1
EXT_NEW=$2
DIR_OLD=$3

for f in `ls *.${EXT_NEW}` ; do

  FILE_OLD=`printfilename "${f}"`
  FILE_OLD=${FILE_OLD}.${EXT_OLD}
  FILE_OLD=${DIR_OLD}/${FILE_OLD}

  if [ ! -f "${FILE_OLD}" ]; then
    echo "Error: ${FILE_OLD} does not exist.";
    exit 1;
  fi;

  TIME_OLD=`printtimestamp "${FILE_OLD}"`;
  TIME_NEW=`printtimestamp "${f}"`;

  echo "Changing ${f} timestamp from ${TIME_NEW} to ${TIME_OLD}";
  touch -t ${TIME_OLD} ${f}
done
Remember to make both scripts executable (chmod +x scriptname). Hope some of you find this hint useful!

[robg adds: Any mistakes in the code are probably my fault -- please let me know if they don't work as described.]
    •    
  • Currently 2.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (9 votes cast)
 
[9,211 views]  

Copy timestamps between a file or multiple files | 7 comments | Create New Account
Click here to return to the 'Copy timestamps between a file or multiple files' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Copy timestamps between a file or multiple files
Authored by: fds on Jul 28, '09 07:56:13AM

The basic touch command already supports copying of the modification time from another file.

touch -r source destination



[ Reply to This | # ]
Copy timestamps between a file or multiple files
Authored by: hopthrisC on Jul 28, '09 02:52:21PM

My thought, exactly.



[ Reply to This | # ]
Copy timestamps between a file or multiple files
Authored by: mementor on Jul 29, '09 06:54:29AM

Can I say 'D-oh'?! :P



[ Reply to This | # ]
Copy timestamps between a file or multiple files
Authored by: DaleMox on Jul 28, '09 01:42:09PM
There's a hanging "Remember" at the end of the second code block (inside the code block). Also your comment, robg, is in that second code block. Finally, there's a mistake in the first code block as there are two:

if [ ! -f "${SRC}" ]; then
  echo "Error: ${SRC} does not exist.";
  exit 1;
fi;

if [ ! -f "${SRC}" ]; then
  echo "Error: ${SRC} does not exist.";
  exit 1;
fi;
(should be ${DST} in one of the cases)

[ Reply to This | # ]
Copy timestamps between a file or multiple files
Authored by: barefootguru on Jul 28, '09 03:13:24PM

It's not mentioned in the article but from looking at the scripts this appears to be changing only the modification timestamp, not the creation timestamp?



[ Reply to This | # ]
Copy timestamps between a file or multiple files
Authored by: mchampag on Jul 29, '09 05:26:58AM

It's my understanding that Unix doesn't jeep track of a file's creation time--only its access and modification times--so there's no mechanism to change it.



[ Reply to This | # ]
Copy timestamps between a file or multiple files
Authored by: francotommasi on Nov 28, '09 04:15:19AM

"It would be nicer if the converted files' timestamps were exactly the same as the original ones" ...

Isn't it enough the following command?

cp -p src dst



[ Reply to This | # ]