making same change to *lots* of files, *without* date changed

Discussion in 'Perl Misc' started by David Combs, Oct 17, 2005.

  1. David Combs

    David Combs Guest

    Well, it's time to bite the bullet, and quit fighting
    the idea (requirement?) that "/home" isn't for my own
    use, but for the automounter's.

    Way back with sunos (before solaris), use rooted a lot
    of our stuff at /home, and refer to it throughout our
    programs.

    But solaris says no can do, unless you hack the automounter
    files.

    Well, the time has come to modify several hundred files,
    changing every /home to /myhome.

    But doing that will screw up the creation dates of the
    files, on which I rely to remind me what I've been
    working on recently.

    Since I want to make this change to just about EVERYTHING,
    I think it best if the dates do NOT change.

    So, my question: how would *you* accomplish this task?

    (maybe via a oneliner, the argument being a list of
    the files that need this minor change?)

    Yes, I've seen the faq, but before I try that,
    I'd ask here if someone had already doine something
    like this.

    (Plus knew the dangers involved of dateless-hacking.)

    Thanks!

    David
    David Combs, Oct 17, 2005
    #1
    1. Advertising

  2. On 2005-10-17, David Combs <> wrote:
    ....

    > Well, the time has come to modify several hundred files,
    > changing every /home to /myhome.
    >
    > But doing that will screw up the creation dates of the
    > files, on which I rely to remind me what I've been
    > working on recently.
    >
    > Since I want to make this change to just about EVERYTHING,
    > I think it best if the dates do NOT change.
    >
    > So, my question: how would *you* accomplish this task?


    Process files one by one. First stat the file, and save the result of
    the stat call, then touch the newly created file with the saved stat
    information.

    You can use either the touch command line utility or File::Touch

    <URL: http://search.cpan.org/~nwetters/File-Touch-0.01/>

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Oct 17, 2005
    #2
    1. Advertising

  3. David Combs

    tester Guest

    David Combs wrote:
    > Well, it's time to bite the bullet, and quit fighting
    > the idea (requirement?) that "/home" isn't for my own
    > use, but for the automounter's.
    >
    > Way back with sunos (before solaris), use rooted a lot
    > of our stuff at /home, and refer to it throughout our
    > programs.
    >
    > But solaris says no can do, unless you hack the automounter
    > files.
    >
    > Well, the time has come to modify several hundred files,
    > changing every /home to /myhome.
    >
    > But doing that will screw up the creation dates of the
    > files, on which I rely to remind me what I've been
    > working on recently.
    >
    > Since I want to make this change to just about EVERYTHING,
    > I think it best if the dates do NOT change.
    >
    > So, my question: how would *you* accomplish this task?
    >
    > (maybe via a oneliner, the argument being a list of
    > the files that need this minor change?)
    >
    > Yes, I've seen the faq, but before I try that,
    > I'd ask here if someone had already doine something
    > like this.
    >
    > (Plus knew the dangers involved of dateless-hacking.)
    >
    > Thanks!
    >
    > David
    >
    >


    I think you can copy files from /home to /myhome with -p option! explore
    man cp

    ~ tester
    tester, Oct 17, 2005
    #3
  4. David Combs wrote:
    > Well, it's time to bite the bullet, and quit fighting
    > the idea (requirement?) that "/home" isn't for my own
    > use, but for the automounter's.
    >
    > Way back with sunos (before solaris), use rooted a lot
    > of our stuff at /home, and refer to it throughout our
    > programs.
    >
    > But solaris says no can do, unless you hack the automounter
    > files.
    >
    > Well, the time has come to modify several hundred files,
    > changing every /home to /myhome.
    >
    > But doing that will screw up the creation dates of the
    > files, on which I rely to remind me what I've been
    > working on recently.


    AFAIK Unix doesn't record the creation dates of files anywhere.


    > Since I want to make this change to just about EVERYTHING,
    > I think it best if the dates do NOT change.
    >
    > So, my question: how would *you* accomplish this task?


    man tar
    man cpio



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Oct 17, 2005
    #4
  5. David Combs

    Tintin Guest

    "David Combs" <> wrote in message
    news:divco0$t3q$...
    >
    > Well, it's time to bite the bullet, and quit fighting
    > the idea (requirement?) that "/home" isn't for my own
    > use, but for the automounter's.


    Only if you have a need for automounter.

    >
    > Way back with sunos (before solaris), use rooted a lot
    > of our stuff at /home, and refer to it throughout our
    > programs.
    >
    > But solaris says no can do, unless you hack the automounter
    > files.


    Or simply disable automounter.

    >
    > Well, the time has come to modify several hundred files,
    > changing every /home to /myhome.
    >
    > But doing that will screw up the creation dates of the
    > files, on which I rely to remind me what I've been
    > working on recently.


    No such thing as a creation date in Unix. You probably meant last
    modification date.

    >
    > Since I want to make this change to just about EVERYTHING,
    > I think it best if the dates do NOT change.
    >
    > So, my question: how would *you* accomplish this task?


    You certainly could do it in Perl, however it's just as easy with simple
    Unix tools (my example references Perl to make it vaguely relevant to this
    group), eg:

    #!/bin/sh
    for file in *.sh
    do
    perl -pe 's:/home:/myhome:g' $file >$file.new && touch -r $file
    $file.new && mv $file.new $file
    done
    Tintin, Oct 17, 2005
    #5
  6. David Combs

    David Combs Guest

    In article <XtH4f.8$>,
    tester <> wrote:
    >David Combs wrote:
    >> Well, it's time to bite the bullet, and quit fighting
    >> the idea (requirement?) that "/home" isn't for my own

    ....
    >>
    >>

    >
    >I think you can copy files from /home to /myhome with -p option! explore
    >man cp


    Copy the files? Really?

    Can't I just rename (mv) the current (via hack to automoount) /home
    to /myhome?

    But that isn't the problem -- the problem is how to change all
    the "/home[^a-zA-Z-]" to "/myhome" -- *within* those files.

    david
    David Combs, Oct 17, 2005
    #6
  7. David Combs

    David Combs Guest

    In article <>,
    A. Sinan Unur <> wrote:
    >
    >Process files one by one. First stat the file, and save the result of
    >the stat call, then touch the newly created file with the saved stat
    >information.
    >
    >You can use either the touch command line utility or File::Touch
    >
    ><URL: http://search.cpan.org/~nwetters/File-Touch-0.01/>
    >
    >Sinan


    One by one, *by hand*?

    Gee, I was hoping for a (if trustworthy!) single cmd based on something
    like this:

    perl -i.bak -pe 's/(?:\015)?\012/\015/g' filenames

    , but with a "don't modify the date/times" (somehow) added in.


    Thanks,

    David


    PS: FEATURE-SUGGESTION: if not already existing, then a *new*
    option for just that purpose?
    David Combs, Oct 17, 2005
    #7
  8. David Combs

    Paul Lalli Guest

    David Combs wrote:
    > In article <>,
    > A. Sinan Unur <> wrote:
    > >
    > >Process files one by one. First stat the file, and save the result of
    > >the stat call, then touch the newly created file with the saved stat
    > >information.
    > >
    > >You can use either the touch command line utility or File::Touch
    > >
    > ><URL: http://search.cpan.org/~nwetters/File-Touch-0.01/>

    >
    > One by one, *by hand*?


    You make it sound like someone told you to dig into your chipset and
    start modifying the bits.

    Can you explain how File::Find and File::Copy do not suit your needs?

    > Gee, I was hoping for a (if trustworthy!) single cmd based on something
    > like this:
    >
    > perl -i.bak -pe 's/(?:\015)?\012/\015/g' filenames


    I don't know what you think that does, but I'm willing to bet it
    doesn't.

    > , but with a "don't modify the date/times" (somehow) added in.


    File::Copy's move() and copy() routines do not modify the last modified
    time.

    > PS: FEATURE-SUGGESTION: if not already existing, then a *new*
    > option for just that purpose?


    I still don't know what "new option" you're talking about, but in Perl,
    we typically call new features "modules". You are, of course, welcome
    to contribute any such thing you find useful to the rest of the Perl
    community.

    Now, taking a step back, perhaps I've misunderstood what you're asking
    for, but why do you believe this needs to be done in Perl at all? What
    is wrong with simply:

    cp -Rp /home/* /myhome/

    ?

    Paul Lalli
    Paul Lalli, Oct 17, 2005
    #8
  9. David Combs

    David Combs Guest

    perldoc -q 'date|time' FAQ -- and -- Re: making same change to *lots* of files, *without* date changed

    In article <dwH4f.29049$Io.3499@clgrps13>,
    John W. Krahn <> wrote:
    >David Combs wrote:

    ....
    >>
    >> But doing that will screw up the creation dates of the
    >> files, on which I rely to remind me what I've been
    >> working on recently.

    >
    >AFAIK Unix doesn't record the creation dates of files anywhere.


    Well, then whatever date it does change upon a "touch" of the file.

    >
    >
    >> Since I want to make this change to just about EVERYTHING,
    >> I think it best if the dates do NOT change.
    >>
    >> So, my question: how would *you* accomplish this task?

    >
    >man tar
    >man cpio


    John, how does that help me *edit* the gillion files?,

    , all the while, *not* changing any of the dates/times associated
    with the files?

    I figured that if anyone could come up with a one-liner,
    it would be *you*! :)


    >John



    FYI: Here's what "perldoc -q 'date|time' >! foo.out" gets me:


    Found in /me-FIRST-in-PATH-bin/perl-5.8.6/lib/5.8.6/pod/perlfaq4.pod
    How can I compare two dates and find the difference?
    If you're storing your dates as epoch seconds then simply subtract one
    from the other. If you've got a structured date (distinct year, day,
    month, hour, minute, seconds values), then for reasons of accessibility,
    simplicity, and efficiency, merely use either timelocal or timegm (from
    the Time::Local module in the standard distribution) to reduce
    structured dates to epoch seconds. However, if you don't know the
    precise format of your dates, then you should probably use either of the
    Date::Manip and Date::Calc modules from CPAN before you go hacking up
    your own parsing routine to handle arbitrary date formats.

    How do I find yesterday's date?
    If you only need to find the date (and not the same time), you can use
    the Date::Calc module.

    use Date::Calc qw(Today Add_Delta_Days);

    my @date = Add_Delta_Days( Today(), -1 );

    print "@date\n";

    Most people try to use the time rather than the calendar to figure out
    dates, but that assumes that your days are twenty-four hours each. For
    most people, there are two days a year when they aren't: the switch to
    and from summer time throws this off. Russ Allbery offers this solution.

    sub yesterday {
    my $now = defined $_[0] ? $_[0] : time;
    my $then = $now - 60 * 60 * 24;
    my $ndst = (localtime $now)[8] > 0;
    my $tdst = (localtime $then)[8] > 0;
    $then - ($tdst - $ndst) * 60 * 60;
    }

    Should give you "this time yesterday" in seconds since epoch relative to
    the first argument or the current time if no argument is given and
    suitable for passing to localtime or whatever else you need to do with
    it. $ndst is whether we're currently in daylight savings time; $tdst is
    whether the point 24 hours ago was in daylight savings time. If $tdst
    and $ndst are the same, a boundary wasn't crossed, and the correction
    will subtract 0. If $tdst is 1 and $ndst is 0, subtract an hour more
    from yesterday's time since we gained an extra hour while going off
    daylight savings time. If $tdst is 0 and $ndst is 1, subtract a negative
    hour (add an hour) to yesterday's time since we lost an hour.

    All of this is because during those days when one switches off or onto
    DST, a "day" isn't 24 hours long; it's either 23 or 25.

    The explicit settings of $ndst and $tdst are necessary because localtime
    only says it returns the system tm struct, and the system tm struct at
    least on Solaris doesn't guarantee any particular positive value (like,
    say, 1) for isdst, just a positive value. And that value can potentially
    be negative, if DST information isn't available (this sub just treats
    those cases like no DST).

    Note that between 2am and 3am on the day after the time zone switches
    off daylight savings time, the exact hour of "yesterday" corresponding
    to the current hour is not clearly defined. Note also that if used
    between 2am and 3am the day after the change to daylight savings time,
    the result will be between 3am and 4am of the previous day; it's
    arguable whether this is correct.

    This sub does not attempt to deal with leap seconds (most things don't).

    How do I validate input?
    The answer to this question is usually a regular expression, perhaps
    with auxiliary logic. See the more specific questions (numbers, mail
    addresses, etc.) for details.

    Found in /me-FIRST-in-PATH-bin/perl-5.8.6/lib/5.8.6/pod/perlfaq5.pod
    Why do I sometimes get an "Argument list too long" when I use <*>?
    The "<>" operator performs a globbing operation (see above). In Perl
    versions earlier than v5.6.0, the internal glob() operator forks csh(1)
    to do the actual glob expansion, but csh can't handle more than 127
    items and so gives the error message "Argument list too long". People
    who installed tcsh as csh won't have this problem, but their users may
    be surprised by it.

    To get around this, either upgrade to Perl v5.6.0 or later, do the glob
    yourself with readdir() and patterns, or use a module like File::KGlob,
    one that doesn't use the shell to do globbing.

    How do I randomly update a binary file?
    If you're just trying to patch a binary, in many cases something as
    simple as this works:

    perl -i -pe 's{window manager}{window mangler}g' /usr/bin/emacs

    However, if you have fixed sized records, then you might do something
    more like this:

    $RECSIZE = 220; # size of record, in bytes
    $recno = 37; # which record to update
    open(FH, "+<somewhere") || die "can't update somewhere: $!";
    seek(FH, $recno * $RECSIZE, 0);
    read(FH, $record, $RECSIZE) == $RECSIZE || die "can't read record $recno: $!";
    # munge the record
    seek(FH, -$RECSIZE, 1);
    print FH $record;
    close FH;

    Locking and error checking are left as an exercise for the reader. Don't
    forget them or you'll be quite sorry.

    How do I get a file's timestamp in perl?
    If you want to retrieve the time at which the file was last read,
    written, or had its meta-data (owner, etc) changed, you use the -M, -A,
    or -C file test operations as documented in perlfunc. These retrieve the
    age of the file (measured against the start-time of your program) in
    days as a floating point number. Some platforms may not have all of
    these times. See perlport for details. To retrieve the "raw" time in
    seconds since the epoch, you would call the stat function, then use
    localtime(), gmtime(), or POSIX::strftime() to convert this into
    human-readable form.

    Here's an example:

    $write_secs = (stat($file))[9];
    printf "file %s updated at %s\n", $file,
    scalar localtime($write_secs);

    If you prefer something more legible, use the File::stat module (part of
    the standard distribution in version 5.004 and later):

    # error checking left as an exercise for reader.
    use File::stat;
    use Time::localtime;
    $date_string = ctime(stat($file)->mtime);
    print "file $file updated at $date_string\n";

    The POSIX::strftime() approach has the benefit of being, in theory,
    independent of the current locale. See perllocale for details.

    How do I set a file's timestamp in perl?
    You use the utime() function documented in "utime" in perlfunc. By way
    of example, here's a little program that copies the read and write times
    from its first argument to all the rest of them.

    if (@ARGV < 2) {
    die "usage: cptimes timestamp_file other_files ...\n";
    }
    $timestamp = shift;
    ($atime, $mtime) = (stat($timestamp))[8,9];
    utime $atime, $mtime, @ARGV;

    Error checking is, as usual, left as an exercise for the reader.

    Note that utime() currently doesn't work correctly with Win95/NT ports.
    A bug has been reported. Check it carefully before using utime() on
    those platforms.

    Found in /me-FIRST-in-PATH-bin/perl-5.8.6/lib/5.8.6/pod/perlfaq8.pod
    How do I set the time and date?
    Assuming you're running under sufficient permissions, you should be able
    to set the system-wide date and time by running the date(1) program.
    (There is no way to set the time and date on a per-process basis.) This
    mechanism will work for Unix, MS-DOS, Windows, and NT; the VMS
    equivalent is "set time".

    However, if all you want to do is change your time zone, you can
    probably get away with setting an environment variable:

    $ENV{TZ} = "MST7MDT"; # unixish
    $ENV{'SYS$TIMEZONE_DIFFERENTIAL'}="-5" # vms
    system "trn comp.lang.perl.misc";

    How can I measure time under a second?
    In general, you may not be able to. The Time::HiRes module (available
    from CPAN, and starting from Perl 5.8 part of the standard distribution)
    provides this functionality for some systems.

    If your system supports both the syscall() function in Perl as well as a
    system call like gettimeofday(2), then you may be able to do something
    like this:

    require 'sys/syscall.ph';

    $TIMEVAL_T = "LL";

    $done = $start = pack($TIMEVAL_T, ());

    syscall(&SYS_gettimeofday, $start, 0) != -1
    or die "gettimeofday: $!";

    ##########################
    # DO YOUR OPERATION HERE #
    ##########################

    syscall( &SYS_gettimeofday, $done, 0) != -1
    or die "gettimeofday: $!";

    @start = unpack($TIMEVAL_T, $start);
    @done = unpack($TIMEVAL_T, $done);

    # fix microseconds
    for ($done[1], $start[1]) { $_ /= 1_000_000 }

    $delta_time = sprintf "%.4f", ($done[0] + $done[1] )
    -
    ($start[0] + $start[1] );

    How do I timeout a slow event?
    Use the alarm() function, probably in conjunction with a signal handler,
    as documented in "Signals" in perlipc and the section on ``Signals'' in
    the Camel. You may instead use the more flexible Sys::AlarmCall module
    available from CPAN.

    The alarm() function is not implemented on all versions of Windows.
    Check the documentation for your specific version of Perl.

    How do I add a directory to my include path (@INC) at runtime?
    Here are the suggested ways of modifying your include path:

    the PERLLIB environment variable
    the PERL5LIB environment variable
    the perl -Idir command line flag
    the use lib pragma, as in
    use lib "$ENV{HOME}/myown_perllib";

    The latter is particularly useful because it knows about machine
    dependent architectures. The lib.pm pragmatic module was first included
    with the 5.002 release of Perl.
    David Combs, Oct 17, 2005
    #9
  10. David Combs

    Guest

    (David Combs) wrote:
    >
    > Well, the time has come to modify several hundred files,
    > changing every /home to /myhome.
    >
    > But doing that will screw up the creation dates of the
    > files, on which I rely to remind me what I've been
    > working on recently.
    >
    > Since I want to make this change to just about EVERYTHING,
    > I think it best if the dates do NOT change.
    >
    > So, my question: how would *you* accomplish this task?


    With utime.

    >
    > (maybe via a oneliner, the argument being a list of
    > the files that need this minor change?)
    >
    > Yes, I've seen the faq, but before I try that,
    > I'd ask here if someone had already doine something
    > like this.


    There are lots of faqs. Which faq are you referring to?

    >
    > (Plus knew the dangers involved of dateless-hacking.)


    Not all combinations of OS/FS/mount options/phase of moon allow you to
    back-date files. So test your program out on test files on the same
    mount-point your real files, otherwise you may permently lose your date
    information if something goes wrong. (Or backup your files in
    data-preserving way before doing anything)


    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Oct 17, 2005
    #10
  11. David Combs

    Guest

    "Paul Lalli" <> wrote:
    >
    > File::Copy's move() and copy() routines do not modify the last modified
    > time.


    They also don't modify the contents of the file, which the OP wants to do.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Oct 17, 2005
    #11
  12. David Combs

    David Combs Guest

    In article <15K4f.974$>,
    Tintin <> wrote:
    >
    >"David Combs" <> wrote in message
    >news:divco0$t3q$...
    >>
    >> Well, it's time to bite the bullet, and quit fighting
    >> the idea (requirement?) that "/home" isn't for my own
    >> use, but for the automounter's.

    >
    >Only if you have a need for automounter.
    >
    >>
    >> Way back with sunos (before solaris), use rooted a lot
    >> of our stuff at /home, and refer to it throughout our
    >> programs.
    >>
    >> But solaris says no can do, unless you hack the automounter
    >> files.

    >
    >Or simply disable automounter.
    >
    >>
    >> Well, the time has come to modify several hundred files,
    >> changing every /home to /myhome.
    >>
    >> But doing that will screw up the creation dates of the
    >> files, on which I rely to remind me what I've been
    >> working on recently.

    >
    >No such thing as a creation date in Unix. You probably meant last
    >modification date.
    >
    >>
    >> Since I want to make this change to just about EVERYTHING,
    >> I think it best if the dates do NOT change.
    >>
    >> So, my question: how would *you* accomplish this task?

    >
    >You certainly could do it in Perl, however it's just as easy with simple
    >Unix tools (my example references Perl to make it vaguely relevant to this
    >group), eg:
    >
    >#!/bin/sh
    >for file in *.sh
    >do
    > perl -pe 's:/home:/myhome:g' $file >$file.new && touch -r $file
    >$file.new && mv $file.new $file
    >done



    AH HA! COOL -- THANK YOU!

    Please, (other) perl/unix gurus, visually/mentally verify the
    above nifty-trick` (obvious only once you see it!) and
    RSVP here -- before I go try the thing!

    (Of course I'll try it on some test files first --
    but maybe some of you will see some inherent problems,
    eg if a file has certain attributes ...?)


    THANKS TO ONE AND ALL!

    David

    PS: again, please say something, pos or neg, about
    the above super-concise scheme.

    And thanks for *that*, too!

    D.
    David Combs, Oct 17, 2005
    #12
  13. David Combs

    Paul Lalli Guest

    wrote:
    > "Paul Lalli" <> wrote:
    > >
    > > File::Copy's move() and copy() routines do not modify the last modified
    > > time.

    >
    > They also don't modify the contents of the file, which the OP wants to do.


    Ahhh, that's the part of the OP's requirements that I didn't
    understand. Thanks, Xho.

    Paul Lalli
    Paul Lalli, Oct 17, 2005
    #13
  14. On 2005-10-17, David Combs <> wrote:
    > In article <>,
    > A. Sinan Unur <> wrote:
    >>
    >>Process files one by one. First stat the file, and save the result of
    >>the stat call, then touch the newly created file with the saved stat
    >>information.
    >>
    >>You can use either the touch command line utility or File::Touch
    >>
    >><URL: http://search.cpan.org/~nwetters/File-Touch-0.01/>
    >>
    >>Sinan

    >
    > One by one, *by hand*?


    I don't understand what you don't understand. I am assuming you have
    some idea how to get (in Perl) a list of files which need to be changed.
    Open each file in that list, save the stat information, open a file to
    write to, write the changed contents to that file, and, after closing
    that file, touch with the cached stat information.

    It is possible to write that script in the time it took you to come up
    with this genius response.

    Alternatively, you can use the shell script provided by Tintin.
    Personally, that's what I would have done, but you asked on a Perl
    group.

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Oct 17, 2005
    #14
  15. Re: perldoc -q 'date|time' FAQ -- and -- Re: making same change to *lots* of files, *without* date changed

    > FYI: Here's what "perldoc -q 'date|time' >! foo.out" gets me:

    What is your point?

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Oct 17, 2005
    #15
  16. David Combs <> wrote:
    > In article <15K4f.974$>,
    > Tintin <> wrote:
    >>"David Combs" <> wrote in message
    >>news:divco0$t3q$...



    >>> Well, the time has come to modify several hundred files,
    >>> changing every /home to /myhome.
    >>>
    >>> But doing that will screw up the creation dates of the
    >>> files,



    I'll assume you meant "modification timestamp" where you
    said "creation date".


    >>You certainly could do it in Perl, however it's just as easy with simple
    >>Unix tools (my example references Perl to make it vaguely relevant to this
    >>group), eg:
    >>
    >>#!/bin/sh
    >>for file in *.sh
    >>do
    >> perl -pe 's:/home:/myhome:g' $file >$file.new && touch -r $file
    >>$file.new && mv $file.new $file
    >>done

    >
    >
    > AH HA! COOL -- THANK YOU!
    >
    > Please, (other) perl/unix gurus, visually/mentally verify the
    > above nifty-trick` (obvious only once you see it!) and
    > RSVP here -- before I go try the thing!
    >
    > (Of course I'll try it on some test files first --
    > but maybe some of you will see some inherent problems,
    > eg if a file has certain attributes ...?)



    What will it do to a directory named

    /homebase

    ??

    A: change it to

    /myhomebase


    So, we'd better make that:

    s:/home/:/myhome/:g


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Oct 17, 2005
    #16
  17. David Combs

    Guest

    > >#!/bin/sh
    > >for file in *.sh
    > >do
    > > perl -pe 's:/home:/myhome:g' $file >$file.new && touch -r $file
    > >$file.new && mv $file.new $file
    > >done


    > AH HA! COOL -- THANK YOU!


    > PS: again, please say something, pos or neg, about
    > the above super-concise scheme.


    Only a minor point - if you have a "gillion" files, you may find that
    the "*.sh" overflows your shell's command buffer, especially if the
    filenames are long (depending on your OS).
    , Oct 17, 2005
    #17
  18. David Combs

    Guest

    wrote:
    > Only a minor point - if you have a "gillion" files, you may find that
    > the "*.sh" overflows your shell's command buffer...


    One other minor point - if your filenames contain "goofy" characters it
    can confuse the shell. Filenames can contain "&" and ">" and
    whitespace and linefeeds (really!) and all sorts of other things that
    are meaningful in shell commands.

    It's a possible security risk also. If you have a file named "rm -rf
    /&" (or a hax0r is able to create such a file) then you might regret
    running this script.
    , Oct 17, 2005
    #18
  19. Tad McClellan <> wrote:
    > David Combs <> wrote:
    >> In article <15K4f.974$>,
    >> Tintin <> wrote:
    >>>"David Combs" <> wrote in message
    >>>news:divco0$t3q$...

    >
    >
    >>>> Well, the time has come to modify several hundred files,
    >>>> changing every /home to /myhome.


    >>>#!/bin/sh
    >>>for file in *.sh
    >>>do
    >>> perl -pe 's:/home:/myhome:g' $file >$file.new && touch -r $file
    >>>$file.new && mv $file.new $file
    >>>done



    > What will it do to a directory named
    >
    > /homebase
    >
    > ??
    >
    > A: change it to
    >
    > /myhomebase
    >
    >
    > So, we'd better make that:
    >
    > s:/home/:/myhome/:g



    Which will, of course, not do the right thing with something like:

    /usr/home/somedir

    ....


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Oct 17, 2005
    #19
  20. Re: perldoc -q 'date|time' FAQ -- and -- Re: making same change to*lots* of files, *without* date changed

    David Combs wrote:
    > In article <dwH4f.29049$Io.3499@clgrps13>,
    > John W. Krahn <> wrote:
    >>David Combs wrote:

    > ...
    >>>But doing that will screw up the creation dates of the
    >>>files, on which I rely to remind me what I've been
    >>>working on recently.

    >>AFAIK Unix doesn't record the creation dates of files anywhere.

    >
    > Well, then whatever date it does change upon a "touch" of the file.
    >
    >>
    >>>Since I want to make this change to just about EVERYTHING,
    >>>I think it best if the dates do NOT change.
    >>>
    >>>So, my question: how would *you* accomplish this task?

    >>man tar
    >>man cpio

    >
    > John, how does that help me *edit* the gillion files?,


    Sorry, I misread the question.

    > , all the while, *not* changing any of the dates/times associated
    > with the files?
    >
    > I figured that if anyone could come up with a one-liner,
    > it would be *you*! :)


    UNTESTED:

    perl -MFile::Find -e'find sub {
    my ( $atime, $mtime ) = ( stat )[ 8, 9 ];
    local ( @ARGV, $^I ) = $_;
    s!/home!/myhome!g while <>;
    close ARGV;
    utime $atime, $mtime, $_;
    }, "."'



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Oct 18, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    664
    Rosanne
    Oct 11, 2005
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,179
    Michael Borgwardt
    May 30, 2004
  3. brad
    Replies:
    9
    Views:
    349
    Bruno Desthuilliers
    Jun 19, 2008
  4. coolneo
    Replies:
    9
    Views:
    175
    coolneo
    Jan 30, 2007
  5. mxbrunet
    Replies:
    1
    Views:
    195
Loading...

Share This Page