Find the answer to your Linux question:
Results 1 to 5 of 5
I accidentally copied bout 90GB worth of data without the old modification/creation times. I can't recopy the files so I am trying to write a script that will just change ...
  1. #1
    Just Joined!
    Join Date
    May 2007
    Posts
    2

    modification time bash script

    I accidentally copied bout 90GB worth of data without the old modification/creation times.
    I can't recopy the files so I am trying to write a script that will just change the new files to have the old modification/creation dates. I am pretty new to bash scripting.

    Here is what I have but it's not really working...

    Is there a better way to do this or can anyone tell me what is wrong with my script?

    Code:
    #!/bin/bash -
    ##timetouching
    
    newfolder=`/ki/2newdate`
    oldfolder=`/ki/1olddate`
    
    for i in `ls $oldfolder`; do
            for p in `ls $newfolder`; do
                    touch -r $i -ma $p
            done
    done

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Hi,

    Use double quotes instead of backticks here:

    Code:
    newfolder="/ki/2newdate"
    oldfolder="/ki/1olddate"
    But wait, the second loop isn't properly, I'll try to check it out.

    Regards

  3. #3
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    OK, this must do the job:

    Code:
    #!/bin/sh
    
    newfolder="/ki/2newdate"
    oldfolder="/ki/1olddate"
    
    cd $oldfolder
    
    for i in *; do
        ls $newfolder/"$i" > /dev/null 2>&1
        if [ $? -eq 0 ]
        then
    	touch -r $oldfolder/"$i" -ma $newfolder/"$i"
        else
    	echo $newfolder/"$i" not exists
        fi
    done
    Regards

  4. #4
    tpl
    tpl is offline
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    452
    welcome to the forum

    Here's another way, supposing you have the old files
    available--otherwise you'd not know their dates:

    drwxrwsr-x 2 tpl tpl 4096 2007-05-24 18:34 first
    drwxrwsr-x 2 tpl tpl 4096 2007-05-24 18:34 fourth
    drwxrwsr-x 2 tpl tpl 4096 2007-05-24 18:34 second
    drwxrwsr-x 2 tpl tpl 4096 2007-05-24 18:34 third

    In that directory,

    ls -l | awk '{print $6$7*$8}' | sed 's/-//g;s/://g' >data

    gives a file "data" like this: <date><time>*<filename>

    200705241834*first
    200705241834*fourth
    200705241834*second
    200705241834*third

    the date-time string is in the correct form for "touch." Now

    awk 'BEGIN { FS = "*"} {print "touch -t "$1" "$2}' <data >change

    gives the file "change" containing the time-changing commands:

    touch -t 200705241834 first
    touch -t 200705241834 fourth
    touch -t 200705241834 second
    touch -t 200705241834 third

    so now move "change" to the directory where the copies are
    and in that directory "sh change"

    I assume no '-'s or ':'s in the filenames. If such there be,
    then instead of "sed" use "cut" to make the file "data."
    the sun is new every day (heraclitus)

  5. #5
    Just Joined!
    Join Date
    May 2007
    Posts
    2

    Unhappy date match script

    These are great for one to one folder matching thanks but I have like 90Gb of files inside folders so the script needs to work recursively
    I need to only change files from a specific date because the user has been working on the files and doesn't want me to change the newer files. It seems like mtime could accomplish this.

    Also, the user has poorly named files with spaces and wierd characters in the names so it's a bit more complex than I thought it was going to be.

    Do you think find would work for recursive trawling? I am not sure how to do a double find to sync the old folder file with the new file.

    Code:
    #!/bin/sh
    
    newfolder="/ki/2newdate"
    oldfolder="/ki/1olddate"
    
    cd $oldfolder
    
    for i in *; do
        ls $newfolder/"$i" > /dev/null 2>&1
        if [ $? -eq 0 ]
        then
    	find $newfolder -mtime 25 -name "$i" -exec touch -r $oldfolder/"$i" -ma {} \; 
        else
    	echo $newfolder/"$i" not exists
        fi
    done

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...