0

I have a script that saves to DB content of piped to it file.

cat /mailpath/Maildir/cur/mailfile | php -q /scriptpath/mailPipe.php

I need a shell script or oneliner to find all files in Maildir, pipe them to php script and delete every file after that.

I know how to find and pipe every single file to PHP

find /mailpath/Maildir/cur/ -type f -printf "cat %p | php -q /scriptpath/mailPipe.php\n" | sh

But don't know how to delete files after—I don't won't to experiment with rm

P.S. If someone needs similar script: https://github.com/stuporglue/mailreader

2
  • Frankly, if you aren't comfortable "experimenting" with rm, you shouldn't be doing this--unless the data you're messing with can be lost without consequences. rm is just a command that deletes files; whether you do it with find -delete or rm or something else, the end result is the same. Make--and test!--your backups first. Commented Sep 12, 2015 at 1:25
  • the data I'm messing with can be lost without consequences ;-) but thanks for the care. Commented Sep 12, 2015 at 12:04

1 Answer 1

0

This command should fails because the -delete is executed before the exec and also because in anycase your script only read input from stdin.

find /mailpath/Maildir/cur/ -type f -delete -exec php -q /scriptpath/mailPipe.php {} \;

I suggest you this one now : find . -type f -exec sh -c "php -q /scriptpath/mailPipe.php < '{}'" \; -delete

Which produce this :

for i in `seq -w 1 10`; do echo $i > nonce-$i; done; ls 
nonce-01  nonce-02  nonce-03  nonce-04  nonce-05  nonce-06  nonce-07  nonce-08  nonce-09  nonce-10

Executing the find :

find . -type f -exec sh -c "wc -l < '{}'" \; -delete ; ls
1
1
1
1
1
1
1
1
1
1

Can you try that ? (make a backup of /mailpath/Maildir/cur/ before)

It's an all in one command, without piping find with other commands, which can be pretty confusing.

It's even better because if the return of sh is error, then it won't delete the file at all, the order of command in find are dependent.

Also i really advise your to read this http://mywiki.wooledge.org/UsingFind

7
  • mywiki.wooledge.org/UsingFind you will find usefull information there Commented Sep 11, 2015 at 15:57
  • I've tried -delete with -printf but it deleted files before cat. Your code was strangely long executed and nothing happened. I will give it a try once more but I don't understand how can it work when you pipe a filename, not a file content to the script - I think "cat" is missing. Commented Sep 11, 2015 at 21:18
  • find . -type f -exec sh -c "cat '{}' |wc -l" \; -delete worked fine to me try the other way around find . -type f -exec sh -c "cat '{}' | php -q /scriptpath/mailPipe.php" \; -delete Commented Sep 11, 2015 at 21:38
  • and again backup your server before testing it please ! :) Commented Sep 11, 2015 at 21:41
  • And please don't wait 6 hours to give me a feedback ! Commented Sep 11, 2015 at 21:53

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.