Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I would like to write a small script copying from a directory A to directory B all the files with the .log extension. So in my directory A, I've :

ls : 
a.log
b.log
c.log

Here is the pseudo-code I would like to implement :

foreach *.log x do :
    if [stat -c %s pk_copylogs < 10485760]; then 
          cp  A/x  B/x       
    else
          read vANSWER?" >> File x is bigger than 10 MB, would you like to copy it anyway ? Type YES or NO : "
          if [ $vANSWER = "YES"]; then
              cp A/x B/x
          fi
    fi

My main problem here, is to find a way to implement my foreach *.log. How can I do that ?

share|improve this question
    
Why don't you use rsync? –  Jiri Xichtkniha Nov 21 '13 at 8:32
    
It looks like cp -i will copy them, and interactively prompting only to overwrite. In my case, it's both for overwriting and writing. –  user1058398 Nov 21 '13 at 13:04

1 Answer 1

You can do this in two commands:

find A -name "*.log" -size -10485760c -exec cp {} B/ +
find A -name "*.log" -size +10485760c -exec cp -i {} B/ +

The first finds files smaller than 10485760 and copies indiscriminately. The second finds files larger than 10485760 and copies interactively, prompting for each copy.

Put them into a shell script or function so that you can execute it with a single command, if you like.

share|improve this answer
    
Actually I tried : find . -name "*.log" -size +10485760c -exec cp -i {} $3/$2/ \; --> didn't work, copy file with size higher than 10 mb without asking. Looking into man page, I guess I've forget something here –  user1058398 Nov 21 '13 at 12:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.