[Charlug] command line help please

Clark Hale xlark at sdf.lonestar.org
Tue Dec 22 04:07:05 EST 2009

On Tue, Dec 22, 2009 at 1:17 AM, Mark <a.booker4 at ntlworld.com> wrote:
> Hi there, I'm trying to think of an easy way to do this, basically I have a
> bunch of files with this naming format:
> filename_YYYYMMDDhhmm.ext
> I want to extract the YYYYMMDDhhmm part of each filename to use with 'touch'
> to change the datestamp:
> eg:
> touch filename_200912220115.jpg -t 200912220115
> Can someone suggest a way to automate this? I would like to be able to
> process a whole directory at once, preferably a one-line command using
> pipes.

So long as you're using a Bourne-type shell (e.g. bash), probably
something like this would do the trick.

for i in filename_*.ext
touch $i -t `echo $i | sed -e 's/filename_\([0-9]\).ext/\1/'`

Depeding on how many files there are, your shell might barf.  I know
on AIX, there's an upper limit that's obscenely low (512 character
maximum for the filename_*.ext glob.  I think), but I'm not sure about
Linux these days.

Basic caveat emptor:  YMMV, test first, &c.



More information about the CharLUG mailing list