Re: Reliable/safe way of removing empty maildirs?
On 07Dec2007 08:12, Patrick Shanahan [EMAIL PROTECTED] wrote: | * Cameron Simpson [EMAIL PROTECTED] [12-07-07 01:38]: | Yeah, but without even invoking find: | |rmdir dir/new dir/tmp dir/cur dir \ ||| mkdir -p dir/new dir/tmp dir/cur | | Robust, safe, trivial. | | People always seem to forget that rmdir is perfectly safe, in that it | won't remove empty directories. | ^^^ | *only* removes empty directories :^) | ^^^ Thank you Todd and Patrick! Code correct, documentation busted! -- Cameron Simpson [EMAIL PROTECTED] DoD#743 http://www.cskk.ezoshosting.com/cs/ I wish it need not have happened in my time, said Frodo. So do I, said Gandalf, and so do all who live to see such times. But that is not for them to decide. All we have to decide is what to do with the time that is given us.
Re: Reliable/safe way of removing empty maildirs?
On Thu, Dec 06, 2007 at 01:03:13PM +, Chris G wrote: What's a reliable way of removing empty maildirs? my /etc/crontab entry: for i in ~/Maildir/.*; do if [ -d $i ] [ ! $(find $i -mindepth 2 -type f) ]; then rm -rf $i ; fi ; done
Re: Reliable/safe way of removing empty maildirs?
On Sun, Dec 09, 2007 at 05:59:40PM +0100, Louis-David Mitterrand wrote: On Thu, Dec 06, 2007 at 01:03:13PM +, Chris G wrote: What's a reliable way of removing empty maildirs? my /etc/crontab entry: for i in ~/Maildir/.*; do if [ -d $i ] [ ! $(find $i -mindepth 2 -type f) ]; then rm -rf $i ; fi ; done Which is OK until some mail arrives between the find and the rm surely?! -- Chris Green
Re: Reliable/safe way of removing empty maildirs?
On Sun, Dec 09, 2007 at 08:41:07PM +, Chris G wrote: On Sun, Dec 09, 2007 at 05:59:40PM +0100, Louis-David Mitterrand wrote: On Thu, Dec 06, 2007 at 01:03:13PM +, Chris G wrote: What's a reliable way of removing empty maildirs? my /etc/crontab entry: for i in ~/Maildir/.*; do if [ -d $i ] [ ! $(find $i -mindepth 2 -type f) ]; then rm -rf $i ; fi ; done Which is OK until some mail arrives between the find and the rm surely?! No, it is still OK: 1) if any maildir is missing the mda (procmail) will instantly recreate them 2) one should only prune empty maildirs for low-priority list/bulk mail
Re: Reliable/safe way of removing empty maildirs?
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 * Cameron Simpson [EMAIL PROTECTED] [12-07-07 01:38]: Yeah, but without even invoking find: rmdir dir/new dir/tmp dir/cur dir \ || mkdir -p dir/new dir/tmp dir/cur Robust, safe, trivial. People always seem to forget that rmdir is perfectly safe, in that it won't remove empty directories. ^^^ *only* removes empty directories :^) ^^^ - -- Patrick Shanahan Plainfield, Indiana, USAHOG # US1244711 http://wahoo.no-ip.org Photo Album: http://wahoo.no-ip.org/gallery2 Registered Linux User #207535@ http://counter.li.org -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.2 (GNU/Linux) iD8DBQFHWUbaClSjbQz1U5oRAhPrAKCKQrp33JBhC8FfaAXL8Geg5tJn3wCgoDkd j0gOrWyGMG/8Qy6kb0OV3pA= =0tkT -END PGP SIGNATURE-
Re: Reliable/safe way of removing empty maildirs?
On Thu, Dec 06, 2007 at 09:05:32PM -0700, Michael Endsley wrote: On Thu, Dec 06, 2007 at 10:28:26PM +, Chris G wrote: On Thu, Dec 06, 2007 at 09:15:10PM +, A Darren Dunham wrote: chmod a-w dir/new if [ `find dir -type f` ] ; then You have to do something like this instead: [snip other responses] Perhaps I've misunderstood the reason for doing this, but I would just ask find to do a rmdir, and let it fail if the directory isn't empty. find dir -depth -type d -exec rmdir {} \; If 'dir' is still around when that finishes, it's probably because there's a file in there now. In the meantime, it's removed all empty subtrees. ... and left an *awful* mess, a maildir mailbox is a directory with *three* sub-directories in it, you need to check that all three are empty before removing them. I needed to empty some subdirectories and this is what I did: du test 4 test/cur 4 test/tmp 4 test/new 16 test Nothing in the test directory, so I deleted it. Er, and? I want a command which safely deletes empty maildirs without me having to inspect them myself. -- Chris Green
Re: Reliable/safe way of removing empty maildirs?
On Fri, Dec 07, 2007 at 12:18:03PM +, Chris G wrote: On Thu, Dec 06, 2007 at 09:05:32PM -0700, Michael Endsley wrote: On Thu, Dec 06, 2007 at 10:28:26PM +, Chris G wrote: On Thu, Dec 06, 2007 at 09:15:10PM +, A Darren Dunham wrote: chmod a-w dir/new if [ `find dir -type f` ] ; then You have to do something like this instead: [snip other responses] Perhaps I've misunderstood the reason for doing this, but I would just ask find to do a rmdir, and let it fail if the directory isn't empty. find dir -depth -type d -exec rmdir {} \; If 'dir' is still around when that finishes, it's probably because there's a file in there now. In the meantime, it's removed all empty subtrees. ... and left an *awful* mess, a maildir mailbox is a directory with *three* sub-directories in it, you need to check that all three are empty before removing them. I needed to empty some subdirectories and this is what I did: du test 4 test/cur 4 test/tmp 4 test/new 16 test Nothing in the test directory, so I deleted it. Er, and? I want a command which safely deletes empty maildirs without me having to inspect them myself. -- Chris Green Ok, sorry, I missed (or forgot) the original question.
Re: Reliable/safe way of removing empty maildirs?
On Fri, Dec 07, 2007 at 05:37:45PM +1100, Cameron Simpson wrote: On 06Dec2007 21:15, A Darren Dunham [EMAIL PROTECTED] wrote: | chmod a-w dir/new | if [ `find dir -type f` ] ; then | | You have to do something like this instead: | [snip other responses] | | Perhaps I've misunderstood the reason for doing this, but I would just | ask find to do a rmdir, and let it fail if the directory isn't empty. | | find dir -depth -type d -exec rmdir {} \; | | If 'dir' is still around when that finishes, it's probably because | there's a file in there now. In the meantime, it's removed all empty | subtrees. Yeah, but without even invoking find: rmdir dir/new dir/tmp dir/cur dir \ || mkdir -p dir/new dir/tmp dir/cur Robust, safe, trivial. People always seem to forget that rmdir is perfectly safe, in that it won't remove empty directories. Now that's quite clever! :-) Can anyone see anything wrong with it? -- Chris Green
Re: Reliable/safe way of removing empty maildirs?
=- Chris G wrote on Thu 6.Dec'07 at 13:03:13 + -= What's a reliable way of removing empty maildirs? Presumably it's possible but you'd have to follow some protocol that wouldn't interfere with the proper writing of messages to the maildir. chmod a-w dir/new rm -rf dir -- © Rado S. -- You must provide YOUR effort for your goal! EVERY effort counts: at least to show your attitude. You're responsible for ALL you do: you get what you give.
Re: Reliable/safe way of removing empty maildirs?
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On Thursday, December 6 at 04:46 PM, quoth Rado S: =- Chris G wrote on Thu 6.Dec'07 at 13:03:13 + -= What's a reliable way of removing empty maildirs? Presumably it's possible but you'd have to follow some protocol that wouldn't interfere with the proper writing of messages to the maildir. chmod a-w dir/new rm -rf dir Well, that's not *quite* safe, now is it? There's a race condition between deciding that a maildir is empty and then changing the permissions such that no one can deliver mail to it. It would have to be more like this: chmod a-w dir/new if [ `find dir -type f` ] ; then echo Not empty! chmod a+w dir/new else rm -rf dir fi ~Kyle - -- I think we ought always to entertain our opinions with some measure of doubt. I shouldn't wish people dogmatically to believe any philosophy, not even mine. -- Bertrand Russell -BEGIN PGP SIGNATURE- Comment: Thank you for using encryption! iD8DBQFHWB1IBkIOoMqOI14RAp8LAJ0YXKrpUr9TCwaswVpky/MF8UH6rACgiECX 1CKaU1As7wX9YrRyQPy50Cw= =ibUG -END PGP SIGNATURE-
Re: Reliable/safe way of removing empty maildirs?
On Thu, Dec 06, 2007 at 10:03:20AM -0600, Kyle Wheeler wrote: On Thursday, December 6 at 04:46 PM, quoth Rado S: =- Chris G wrote on Thu 6.Dec'07 at 13:03:13 + -= What's a reliable way of removing empty maildirs? Presumably it's possible but you'd have to follow some protocol that wouldn't interfere with the proper writing of messages to the maildir. chmod a-w dir/new rm -rf dir Well, that's not *quite* safe, now is it? There's a race condition between deciding that a maildir is empty and then changing the permissions such that no one can deliver mail to it. It would have to be more like this: chmod a-w dir/new if [ `find dir -type f` ] ; then You have to do something like this instead: found=`find dir -type f` if -n $found ; then At least on my system (Mac OS X 10.3 = Darwin 7.9.0), where find(1) exits with status 0 even if nothing is found. echo Not empty! chmod a+w dir/new else rm -rf dir fi ~Kyle Paul. -- Paul Hoffman [EMAIL PROTECTED]
Re: Reliable/safe way of removing empty maildirs?
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On Thursday, December 6 at 11:40 AM, quoth Paul Hoffman: chmod a-w dir/new if [ `find dir -type f` ] ; then You have to do something like this instead: found=`find dir -type f` if -n $found ; then At least on my system (Mac OS X 10.3 = Darwin 7.9.0), where find(1) exits with status 0 even if nothing is found. That's why I used the form that I used. Consider what I said; I said if [ `find dir -type f` ] ; then I did not say: if find dir -type f ; then The reason is because the former relies on the output of find, and the latter relies on the exit status of find. In other words, I had already addressed this issue. At worst, some shells might require that to be rewritten like this: if [ $( find dir -type f ) ] ; then The reason for that is that [ (also known as /bin/test, and frequently a builtin shell command) returns true if it has any arguments of any non-zero length, and returns false if it does not (i.e. the -n argument is the default behavior). My example is equivalent to this: found=`find dir -type f` if [ $found ] ; then ...but does not require the extraneous variable definition. Your alternative suggestion: if -n $found ; then will, unfortunately, not work in many situations because in many POSIX shells (including bash), -n is not a valid argument to the if keyword, nor is -n a recognized executable program or command. Perhaps in your system you have a /bin/-n program, but most folks don't. ~Kyle - -- Many who claim to have been transformed by Christ's love are deeply, even murderously, intolerant of criticism. -- Sam Harris -BEGIN PGP SIGNATURE- Comment: Thank you for using encryption! iD8DBQFHWDXJBkIOoMqOI14RAj1CAJsETNaVb1dj9M9kNyvsy1tKjcHD2QCfRZVu +DLO3JgxGFwm5u70aTy7BU4= =Xgwv -END PGP SIGNATURE-
Re: Reliable/safe way of removing empty maildirs?
On Thu, Dec 06, 2007 at 11:40:04AM -0500, Paul Hoffman wrote: On Thu, Dec 06, 2007 at 10:03:20AM -0600, Kyle Wheeler wrote: On Thursday, December 6 at 04:46 PM, quoth Rado S: =- Chris G wrote on Thu 6.Dec'07 at 13:03:13 + -= What's a reliable way of removing empty maildirs? Presumably it's possible but you'd have to follow some protocol that wouldn't interfere with the proper writing of messages to the maildir. chmod a-w dir/new rm -rf dir Well, that's not *quite* safe, now is it? There's a race condition between deciding that a maildir is empty and then changing the permissions such that no one can deliver mail to it. It would have to be more like this: chmod a-w dir/new if [ `find dir -type f` ] ; then You have to do something like this instead: found=`find dir -type f` if -n $found ; then Ack! Sorry, I meant: if [ -n $found ]; then At least on my system (Mac OS X 10.3 = Darwin 7.9.0), where find(1) exits with status 0 even if nothing is found. echo Not empty! chmod a+w dir/new else rm -rf dir fi ~Kyle Paul. Paul. -- Paul Hoffman [EMAIL PROTECTED]
Re: Reliable/safe way of removing empty maildirs?
On Thu, Dec 06, 2007 at 11:47:53AM -0600, Kyle Wheeler wrote: On Thursday, December 6 at 11:40 AM, quoth Paul Hoffman: chmod a-w dir/new if [ `find dir -type f` ] ; then You have to do something like this instead: found=`find dir -type f` if -n $found ; then At least on my system (Mac OS X 10.3 = Darwin 7.9.0), where find(1) exits with status 0 even if nothing is found. That's why I used the form that I used. Consider what I said; I said if [ `find dir -type f` ] ; then I did not say: if find dir -type f ; then The reason is because the former relies on the output of find, and the latter relies on the exit status of find. In other words, I had already addressed this issue. Right, sorry. I was going to say to use the exit status of find(1), but then realized that wouldn't help. At worst, some shells might require that to be rewritten like this: if [ $( find dir -type f ) ] ; then Or using backticks: if [ `find dir -type f` ]; then I don't know if that's any more portable, though. The reason for that is that [ (also known as /bin/test, and frequently a builtin shell command) returns true if it has any arguments of any non-zero length, and returns false if it does not (i.e. the -n argument is the default behavior). Thanks -- I didn't know that. My example is equivalent to this: found=`find dir -type f` if [ $found ] ; then ...but does not require the extraneous variable definition. Hmm, my test (builtin, bash 2.05b.0) can't be relied upon: $ PS1='\n\$ ' $ mkdir foo; cd foo /User/nkuitse/dt/foo $ builtin test `find . -type f` || echo No files OK-- No files $ touch bar $ builtin test `find . -type f` || echo No files OK-- $ touch baz $ builtin test `find . -type f` || echo No files bash: test: ./bar: unary operator expected Oops! -- No files /bin/test messes up in the same way, just with a different error message. YMMV of course. ~Kyle Paul. -- Paul Hoffman [EMAIL PROTECTED]
Re: Reliable/safe way of removing empty maildirs?
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On Thursday, December 6 at 03:31 PM, quoth Paul Hoffman: Or using backticks: if [ `find dir -type f` ]; then I don't know if that's any more portable, though. Backticks aren't any more portable, I don't think... but it doesn't matter too much. However the double-quotes there solve the problem that you note: Hmm, my test (builtin, bash 2.05b.0) can't be relied upon: $ PS1='\n\$ ' $ mkdir foo; cd foo /User/nkuitse/dt/foo $ builtin test `find . -type f` || echo No files OK-- No files $ builtin test `find . -type f` || echo No files No files $ touch bar $ builtin test `find . -type f` || echo No files OK-- $ touch bar $ builtin test `find . -type f` || echo No files $ touch baz $ builtin test `find . -type f` || echo No files bash: test: ./bar: unary operator expected Oops! -- No files $ touch baz $ builtin test `find . -type f` || echo No files The reason for the problem is that the shell is evaluating things twice. In essence, the output of find is substituted for `find . -type f` in its raw form. You'd get the same problem if you did this: $ foo= $ builtin test $foo echo full || echo empty empty $ foo=one $ builtin test $foo echo full || echo empty full $ foo=one two $ builtin test $foo echo full || echo empty -bash: test: one: unary operator expected empty That last command is equivalent, to the shell, to the following command: $ builtin test one two echo full || echo empty In order to get the result you want, i.e. to test whether foo contains text, you have to quote it: $ builtin test $foo echo full || echo empty This specifies that no matter what's in foo, it's part of a single quoted string that is passed as the only argument to test. The same thing is true of anything that the shell evaluates, including backtick expansion. In other words, the following two commands behave the same way: $ test $foo echo full || echo empty $ test `echo $foo` echo full || echo empty ... and they both have the same solution: encapsulate whatever it is inside quotes: $ test $foo echo full || echo empty $ test `echo $foo` echo full || echo empty Heh, ain't the shell grand? ~Kyle - -- Nonsense. Space is blue and birds fly through it. -- Heisenberg -BEGIN PGP SIGNATURE- Comment: Thank you for using encryption! iD8DBQFHWGUJBkIOoMqOI14RAt59AKCZOYtUjkEemFpLm2lZK1vr6XCmTgCcDEOe w+PrVIPulkGo+xatxk5x8g4= =m/9g -END PGP SIGNATURE-
Re: Reliable/safe way of removing empty maildirs?
chmod a-w dir/new if [ `find dir -type f` ] ; then You have to do something like this instead: [snip other responses] Perhaps I've misunderstood the reason for doing this, but I would just ask find to do a rmdir, and let it fail if the directory isn't empty. find dir -depth -type d -exec rmdir {} \; If 'dir' is still around when that finishes, it's probably because there's a file in there now. In the meantime, it's removed all empty subtrees. -- Darren Dunham [EMAIL PROTECTED] Senior Technical Consultant TAOShttp://www.taos.com/ Got some Dr Pepper? San Francisco, CA bay area This line left intentionally blank to confuse you.
Re: Reliable/safe way of removing empty maildirs?
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On Thursday, December 6 at 09:15 PM, quoth A Darren Dunham: Perhaps I've misunderstood the reason for doing this, but I would just ask find to do a rmdir, and let it fail if the directory isn't empty. find dir -depth -type d -exec rmdir {} \; If 'dir' is still around when that finishes, it's probably because there's a file in there now. In the meantime, it's removed all empty subtrees. That would work flawlessly on MH directories, but Maildir's are slightly more complicated---they consist of several directories. Your find command will indeed not erase mail that was delivered right when you wanted it to be, however it will corrupt the Maildir. For example, if I think I have an empty Maildir named dir, then on-disk it should look like this: dir/ +- cur/ +- new/ `- tmp/ If, without my knowledge, email was delivered to it, it will look like this: dir/ +- cur/ +- new/ | `- a_message_file `- tmp/ If I then run your find command, it will delete the two empty components (tmp and cur) and will leave me with an unreadable corrupted Maildir that looks like this: dir `- new `- a_message_file I would have to realize what has happened and then re-create the missing directories in order to make things behave properly. So, you're right that your command doesn't irrecoverably destroy email... but it's still not very convenient. ~Kyle - -- We are all worms, but I do believe I am a glow worm. -- Winston Churchill -BEGIN PGP SIGNATURE- Comment: Thank you for using encryption! iD8DBQFHWG6GBkIOoMqOI14RAgA0AJ95IP1zV0NNy0LZGvn6QOHOweS3GgCgoPrw 8qI9KmQwaWDPT804NGTI5Wk= =BUoH -END PGP SIGNATURE-
Re: Reliable/safe way of removing empty maildirs?
On Thu, Dec 06, 2007 at 09:15:10PM +, A Darren Dunham wrote: chmod a-w dir/new if [ `find dir -type f` ] ; then You have to do something like this instead: [snip other responses] Perhaps I've misunderstood the reason for doing this, but I would just ask find to do a rmdir, and let it fail if the directory isn't empty. find dir -depth -type d -exec rmdir {} \; If 'dir' is still around when that finishes, it's probably because there's a file in there now. In the meantime, it's removed all empty subtrees. ... and left an *awful* mess, a maildir mailbox is a directory with *three* sub-directories in it, you need to check that all three are empty before removing them. -- Chris Green
Re: Reliable/safe way of removing empty maildirs?
I needed to empty some subdirectories and this is what I did: du test 4 test/cur 4 test/tmp 4 test/new 16 test Nothing in the test directory, so I deleted it. On Thu, Dec 06, 2007 at 10:28:26PM +, Chris G wrote: On Thu, Dec 06, 2007 at 09:15:10PM +, A Darren Dunham wrote: chmod a-w dir/new if [ `find dir -type f` ] ; then You have to do something like this instead: [snip other responses] Perhaps I've misunderstood the reason for doing this, but I would just ask find to do a rmdir, and let it fail if the directory isn't empty. find dir -depth -type d -exec rmdir {} \; If 'dir' is still around when that finishes, it's probably because there's a file in there now. In the meantime, it's removed all empty subtrees. ... and left an *awful* mess, a maildir mailbox is a directory with *three* sub-directories in it, you need to check that all three are empty before removing them. -- Chris Green
Re: Reliable/safe way of removing empty maildirs?
On 06Dec2007 21:15, A Darren Dunham [EMAIL PROTECTED] wrote: | chmod a-w dir/new | if [ `find dir -type f` ] ; then | | You have to do something like this instead: | [snip other responses] | | Perhaps I've misunderstood the reason for doing this, but I would just | ask find to do a rmdir, and let it fail if the directory isn't empty. | | find dir -depth -type d -exec rmdir {} \; | | If 'dir' is still around when that finishes, it's probably because | there's a file in there now. In the meantime, it's removed all empty | subtrees. Yeah, but without even invoking find: rmdir dir/new dir/tmp dir/cur dir \ || mkdir -p dir/new dir/tmp dir/cur Robust, safe, trivial. People always seem to forget that rmdir is perfectly safe, in that it won't remove empty directories. Cheers, -- Cameron Simpson [EMAIL PROTECTED] DoD#743 http://www.cskk.ezoshosting.com/cs/ Thousands at his bidding speed, And post o'er land and ocean without rest - Milton
Re: Reliable/safe way of removing empty maildirs?
Cameron Simpson wrote: Yeah, but without even invoking find: rmdir dir/new dir/tmp dir/cur dir \ || mkdir -p dir/new dir/tmp dir/cur Robust, safe, trivial. Hooray for simplicity. :) People always seem to forget that rmdir is perfectly safe, in that it won't remove empty directories. I'm sure you meant just the opposite. It won't remove non-empty dirs. -- ToddOpenPGP - KeyID: 0xBEAF0CE3 | URL: www.pobox.com/~tmz/pgp ~~ That men do not learn very much from the lessons of history is the most important of all the lessons of history. -- Aldous Huxley Collected Essays, 1959 pgp0zq36D0iq5.pgp Description: PGP signature