#!/usr/bin/perl -w # ptouch: change file modtimes to match inputs. # # If we pass in filenames on the command line, they're expected to # start with a number in the form of a Unix epoch time. This number # will be used to set the file modification time. # # If no filenames are on the command line, read lines in the format # # and change the modtime of file to epoch-time. use strict; use File::Basename; use POSIX qw(strftime); my $file; my $t; # utime LIST # Changes the access and modification times on each # file of a list of files. The first two elements of # the list must be the NUMERICAL access and modifica- # tion times, in that order. if (@ARGV) { foreach $file (@ARGV) { next unless -f $file; $_ = basename($file); if (m/^([0-9]*)/) { $t = $1; fixtime($t, $file); } } } else { while (<>) { chomp; s/^\s\s*//g; # use non-greedy matching on first term to allow for # spaces in filenames. if (m/(.*?)\s(.*)/) { $t = $1; $file = $2; next unless -f $file; fixtime($t, $file); } } } exit(0); sub fixtime { my ($t, $f) = @_; if (utime($t, $t, $f)) { my @lt = localtime($t); print strftime("%Y-%m-%d %T", @lt), ": $f\n"; } else { warn "utime: $f: $!\n"; } }