Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 23

Thread: Bash script to find multiples files, find the last modified one and copy/overwrite it to others loca

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Question Bash script to find multiples files, find the last modified one and copy/overwrite it to others loca

    Hi everyone,

    I'm looking to complete my Bash script to find multiples files,
    then find the last modified one and copy/overwrite it to others locations..

    Here's my first attempt .. I can handle the result with php this far, but wanne complete it in order to cronjob update of script serverwide
    find /var/www/ -type f -iregex '.*PIE\.htc' -printf '%T@ %p %s\n'|sort -r -n

    thanks

  2. #2
    Senior Member Rahul.Patil's Avatar
    Join Date
    Feb 2012
    Location
    Mumbai india
    Posts
    485
    Thanks
    10
    Thanked 49 Times in 46 Posts
    Rep Power
    8

    Default

    Hi,

    You can use mtime option to find command
    Code:
    find . -type f -mtime 0 -exec mv {} /tmp/ \;
    use man find
    Rahul Patil <http://www.linuxian.com>

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Right, but I'm still looking for the appropriate syntax to catch the first result, then copy it to others locations
    ( that's why I used the print )

  4. #4
    Senior Member Rahul.Patil's Avatar
    Join Date
    Feb 2012
    Location
    Mumbai india
    Posts
    485
    Thanks
    10
    Thanked 49 Times in 46 Posts
    Rep Power
    8

    Default

    without input sample and desire sample output we can't help you more.
    Rahul Patil <http://www.linuxian.com>

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    right, sorry about this
    there's my output for : #find /var/www/ -type f -iregex '.*PIE\.htc' -printf '%T@ %p\n'|sort -r -n

    1350663880.0000000000 /var/www/site1/PIE.htc
    1350663342.0000000000 /var/www/site2/PIE.htc
    1350318272.0000000000 /var/www/site3/PIE.htc
    1335785728.0000000000 /var/www/site4/PIE.htc
    1313662290.0000000000 /var/www/site5/PIE.htc

    in this example I'd like the first result to be copied accross the 4 lines afterwards

  6. #6
    Senior Member Rahul.Patil's Avatar
    Join Date
    Feb 2012
    Location
    Mumbai india
    Posts
    485
    Thanks
    10
    Thanked 49 Times in 46 Posts
    Rep Power
    8

    Default

    Hi,

    PHP Code:
    #!/bin/bash

    #copy first file from list (you can increase if you want)
    Number_of_files=1

    # get list of file using find (modify find command as per your requirement)
    File_list=(`find /tmp/ -type f -iname '*.conf' -printf '%T@ %p\n' | sort -r -n| cut -d" " -f2`)

    # files to be cp
    Destination_path="/opt/"

    count=0

    while [ "${count}-lt "${Number_of_files}]; do

    cp "${File_list[$count]}"  "${Destination_path}"
    let count=count+1

    done 
    Last edited by Rahul.Patil; 24th October 2012 at 03:16 PM.
    Rahul Patil <http://www.linuxian.com>

  7. #7
    Senior Member
    Join Date
    Aug 2011
    Posts
    447
    Thanks
    0
    Thanked 61 Times in 57 Posts
    Rep Power
    10

    Default

    hi Rahul.Patil,

    you can't build an array this way; what if a filename has, at least, a space in it?
    Code:
    #!/bin/bash
    
    while read nm pathAndFilename
    do if test -z toBeCopied
       then
          toBeCopied="$pathAndFilname"
       else
          echo "cp \"$toBeCopied\" \"$pathAndFilename\""
       fi
    done < <(find /var/www/ -type f -name "PIE.htc" -printf '%T@ %p\n' | sort -r -n)
    what I understand from «copy across» is "overwrite"... is that it, nyx?
    Last edited by Watael; 24th October 2012 at 04:14 PM.

  8. #8
    Junior Member
    Join Date
    Oct 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Sure, rahul script works to copy first file in list to /opt folder ..
    second script gives me a syntax error, I'm running a debian 6 server

  9. #9
    Senior Member
    Join Date
    Aug 2011
    Posts
    447
    Thanks
    0
    Thanked 61 Times in 57 Posts
    Rep Power
    10

    Default

    what's the exact error message?

  10. #10
    Junior Member
    Join Date
    Oct 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    line 10: syntax error close to expected symbol <
    ( I'm not familiar with heredoc .. )

    I'm looking for something close to this

    File_list=`find /z/ -type f -iname '*PIE.htc' -printf '%T@ %p\n' | sort -r -n| cut -d" " -f2`;
    #wanna explode File_list on "\n" or another char .. because it's not an Array this far
    count=0;
    while [ "${count}" -lt "${Number_of_files}" ]; do
    #Foreach(File_list)??
    let count=count+1;
    cp "${File_list[0]}" "${File_list[count]}";
    #Copy (overwrite) first occurence to others one locations
    done;

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 15th August 2012, 12:33 AM
  2. [Solved] Bash shell awk script to find out highest cpu eating apache php process
    By javiercampos in forum Shell scripting
    Replies: 2
    Last Post: 5th August 2012, 11:16 PM
  3. find -exec {} bash example??
    By chinalinix in forum Shell scripting
    Replies: 2
    Last Post: 28th January 2011, 08:12 AM
  4. cp last modified with find command
    By Rahul.Kolan in forum Linux software
    Replies: 2
    Last Post: 19th May 2010, 07:26 PM
  5. Find Unix Linux File / Directory by date And Then Copy / Move File
    By asim.mcp in forum CentOS / RHEL / Fedora
    Replies: 1
    Last Post: 10th August 2008, 03:30 AM

Tags for this Thread

Posting Permissions

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