Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Since this is more of a Bash question I think this is better suited for serverfault than superuser, even though I'm talking about media files.

I had to make a batch conversion of several media files, and all the output files have a new timestamp.

    file1.mpg     (correct timestamp)
    file1.mpg.m4v (wrong timestamp)
    file2.mpg     (correct timestamp)
    file2.mpg.m4v (wrong timestamp)

What I want to do is a batch copy of the timestamps so that each *.mpg.m4v gets the timestamp of the corresponding *.mpg file.

From my searching, it looks like I need to use touch -r and some regex with substitution to handle this.

Am I on the right track, and does anyone have any suggestions for how I need to do the regex (my regex knowledge, is bad, bad, bad) to handle this?

share|improve this question
I point out that bash is the 36th top tag on SuperUser. There is a lot of Linux over there. – sysadmin1138 Aug 26 '10 at 22:35
And it could very well belong there since it doesn't seem server-specific, but well it's ok here, too :-) – ℝaphink Aug 26 '10 at 22:47
up vote 5 down vote accepted

Let's see, how about...

for f in *.mpg; do
   touch -r "$f" "${f}.m4v"

No need for regexps, just to take the problem from the right side. It's much easier to circle through *.mpg and add .m4v to them than the contrary, although you could also write it the other way without regexps (just for fun):

for f in *.m4v; do
   touch -r "${f%\.m4v}" "$f"

If you want it done with one command, you could do that with find (restricting it to one level for safety):

find . -maxdepth 1 -name '*.mpg' -exec touch -r {} {}.m4v \;
share|improve this answer
That worked perfectly, thanks! There's useful knowledge in each of your three examples. – user52487 Aug 26 '10 at 22:59
The ${foo%bar} is part of Bash's (and other shells') parameter expansion capability and is not regular expressions (it's more closely related to globbing). Otherwise +1. – Dennis Williamson Aug 27 '10 at 1:05
@Dennis: I never said I was using regexps, I actually emphasized that it was not necessary to use regexps for any of these solutions. – ℝaphink Aug 27 '10 at 6:56
Sorry, I misread that line. – Dennis Williamson Aug 27 '10 at 9:13
No problem at all. – ℝaphink Aug 27 '10 at 9:50

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.