* listing/deleting empty directories recursively
@ 2005-11-19 9:19 zzapper
2005-11-19 9:57 ` Christian Taylor
0 siblings, 1 reply; 13+ messages in thread
From: zzapper @ 2005-11-19 9:19 UTC (permalink / raw)
To: zsh-users
Hi
ls -ld *(/^F)
Will list any empty directories, but only for one level of subdirectories, how to make it delete
empty directories at any level.
(2nd posting, couldn't see the first)
--
zzapper
Success for Techies and Vim,Zsh tips
http://SuccessTheory.com/
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: listing/deleting empty directories recursively
2005-11-19 9:19 listing/deleting empty directories recursively zzapper
@ 2005-11-19 9:57 ` Christian Taylor
2005-11-19 11:28 ` zzapper
0 siblings, 1 reply; 13+ messages in thread
From: Christian Taylor @ 2005-11-19 9:57 UTC (permalink / raw)
To: zsh-users
> ls -ld *(/^F)
>
> Will list any empty directories, but only for one level of subdirectories,
> how to make it delete empty directories at any level.
ls -ld **/*(/^F)
(will list empty directories at any level in the current working directory)
Christian
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: listing/deleting empty directories recursively
2005-11-19 9:57 ` Christian Taylor
@ 2005-11-19 11:28 ` zzapper
2005-11-19 11:56 ` Thor Andreassen
` (2 more replies)
0 siblings, 3 replies; 13+ messages in thread
From: zzapper @ 2005-11-19 11:28 UTC (permalink / raw)
To: zsh-users
On Sat, 19 Nov 2005 10:57:25 +0100, wrote:
>> ls -ld *(/^F)
>>
>> Will list any empty directories, but only for one level of subdirectories,
>> how to make it delete empty directories at any level.
>
>ls -ld **/*(/^F)
>(will list empty directories at any level in the current working directory)
>
>Christian
Thanx works just dandy,
Now I'd like to rm those directories, and log which ones were deleted.
--
zzapper
Success for Techies and Vim,Zsh tips
http://SuccessTheory.com/
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: listing/deleting empty directories recursively
2005-11-19 11:28 ` zzapper
@ 2005-11-19 11:56 ` Thor Andreassen
2005-11-19 11:59 ` Thor Andreassen
2005-11-19 14:39 ` Lloyd Zusman
2005-11-19 16:09 ` Bart Schaefer
2 siblings, 1 reply; 13+ messages in thread
From: Thor Andreassen @ 2005-11-19 11:56 UTC (permalink / raw)
To: zsh-users
On Sat, Nov 19, 2005 at 11:28:15AM +0000, zzapper wrote:
> On Sat, 19 Nov 2005 10:57:25 +0100, wrote:
>
> >> ls -ld *(/^F)
> >>
> >> Will list any empty directories, but only for one level of subdirectories,
> >> how to make it delete empty directories at any level.
> >
> >ls -ld **/*(/^F)
> >(will list empty directories at any level in the current working directory)
> >
> >Christian
> Thanx works just dandy,
> Now I'd like to rm those directories, and log which ones were deleted.
This should do the trick:
for dir in `ls -ld **/*(/^F)`; do echo $dir > LOGFILE; rmdir $dir; done
--
mvh
Thor Andreassen
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: listing/deleting empty directories recursively
2005-11-19 11:56 ` Thor Andreassen
@ 2005-11-19 11:59 ` Thor Andreassen
2005-11-19 12:48 ` Christian Taylor
0 siblings, 1 reply; 13+ messages in thread
From: Thor Andreassen @ 2005-11-19 11:59 UTC (permalink / raw)
To: zsh-users
On Sat, Nov 19, 2005 at 12:56:53PM +0100, Thor Andreassen wrote:
[snip]
> This should do the trick:
>
> for dir in `ls -ld **/*(/^F)`; do echo $dir > LOGFILE; rmdir $dir; done
You should use double quotes (") around the dir variable if you have
dirnames with spaces (and some other chars), i.e. echo "$dir" and rmdir
"$dir".
--
Thor Andreassen
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: listing/deleting empty directories recursively
2005-11-19 11:59 ` Thor Andreassen
@ 2005-11-19 12:48 ` Christian Taylor
2005-11-19 14:54 ` Thorsten Kampe
2005-11-19 15:59 ` Stephane Chazelas
0 siblings, 2 replies; 13+ messages in thread
From: Christian Taylor @ 2005-11-19 12:48 UTC (permalink / raw)
To: zsh-users
Thor Andreassen wrote:
> > This should do the trick:
> >
> > for dir in `ls -ld **/*(/^F)`; do echo $dir > LOGFILE; rmdir $dir; done
>
> You should use double quotes (") around the dir variable if you have
> dirnames with spaces (and some other chars), i.e. echo "$dir" and rmdir
> "$dir".
That probably wouldn't work, since ls -ld prints a whole lot of additional
information that will be interpreted as directories to log and delete. It's
far simpler to do away with ls:
for dir in **/*(/^F); do print $dir >> LOGFILE; rmdir $dir; done
or with the alternative syntax I really like:
for dir (**/*(/^F)) { print $dir >> LOGFILE; rmdir $dir }
As far as I can see, filenames generated this way are already quoted
correctly, so spaces and special characters shouldn't be an issue.
Christian
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: listing/deleting empty directories recursively
2005-11-19 11:28 ` zzapper
2005-11-19 11:56 ` Thor Andreassen
@ 2005-11-19 14:39 ` Lloyd Zusman
2005-11-19 16:09 ` Bart Schaefer
2 siblings, 0 replies; 13+ messages in thread
From: Lloyd Zusman @ 2005-11-19 14:39 UTC (permalink / raw)
To: zsh-users
zzapper <david@tvis.co.uk> writes:
> On Sat, 19 Nov 2005 10:57:25 +0100, wrote:
>
>>> ls -ld *(/^F)
>>>
>>> Will list any empty directories, but only for one level of subdirectories,
>>> how to make it delete empty directories at any level.
>>
>>ls -ld **/*(/^F)
>>(will list empty directories at any level in the current working directory)
>>
>>Christian
> Thanx works just dandy,
> Now I'd like to rm those directories, and log which ones were deleted.
How about this?
print **/*(/^F) | xargs -n1 -t rmdir
The xargs command dates back to something like 1977, in the infancy of
Unix. It's still quite useful for tasks such as this one.
--
Lloyd Zusman
ljz@asfast.com
God bless you.
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: listing/deleting empty directories recursively
2005-11-19 12:48 ` Christian Taylor
@ 2005-11-19 14:54 ` Thorsten Kampe
2005-11-19 15:59 ` Stephane Chazelas
1 sibling, 0 replies; 13+ messages in thread
From: Thorsten Kampe @ 2005-11-19 14:54 UTC (permalink / raw)
To: zsh-users
* Christian Taylor (2005-11-19 12:48 +0100)
> Thor Andreassen wrote:
>>> This should do the trick:
>>>
>>> for dir in `ls -ld **/*(/^F)`; do echo $dir > LOGFILE; rmdir $dir; done
>>
>> You should use double quotes (") around the dir variable if you have
>> dirnames with spaces (and some other chars), i.e. echo "$dir" and rmdir
>> "$dir".
>
> That probably wouldn't work, since ls -ld prints a whole lot of additional
> information that will be interpreted as directories to log and delete. It's
> far simpler to do away with ls:
>
> for dir in **/*(/^F); do print $dir >> LOGFILE; rmdir $dir; done
Exactly. Using "for x in $(ls)" or "for x in $(ls *)" instead of "for
dir in *" is a common mistake (mentioned in "From bash to zsh").
> or with the alternative syntax I really like:
>
> for dir (**/*(/^F)) { print $dir >> LOGFILE; rmdir $dir }
>
> As far as I can see, filenames generated this way are already quoted
> correctly, so spaces and special characters shouldn't be an issue.
Sorry, but it completely the other way round: the filenames are not
split (shwordsplit) so there's no need to quote them.
That's one major reason for the superiority of zsh to other shells
like bash.
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: listing/deleting empty directories recursively
2005-11-19 12:48 ` Christian Taylor
2005-11-19 14:54 ` Thorsten Kampe
@ 2005-11-19 15:59 ` Stephane Chazelas
1 sibling, 0 replies; 13+ messages in thread
From: Stephane Chazelas @ 2005-11-19 15:59 UTC (permalink / raw)
To: zsh-users
On Sat, Nov 19, 2005 at 01:48:29PM +0100, Christian Taylor wrote:
[...]
> for dir in **/*(/^F); do print $dir >> LOGFILE; rmdir $dir; done
>
> or with the alternative syntax I really like:
>
> for dir (**/*(/^F)) { print $dir >> LOGFILE; rmdir $dir }
>
> As far as I can see, filenames generated this way are already quoted
> correctly, so spaces and special characters shouldn't be an issue.
They will as you forgot the "-r" for print, and to mark the end
of options with "--" for both print and rmdir.
Moreover, note that that won't remove directories that only
contain empty directories, you may prefer:
zargs rmdir -- ./**/*(/od) 2> /dev/null
as rmdir won't remove non-empty dirs anyway.
--
Stéphane
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: listing/deleting empty directories recursively
2005-11-19 11:28 ` zzapper
2005-11-19 11:56 ` Thor Andreassen
2005-11-19 14:39 ` Lloyd Zusman
@ 2005-11-19 16:09 ` Bart Schaefer
2005-11-19 17:07 ` zzapper
2 siblings, 1 reply; 13+ messages in thread
From: Bart Schaefer @ 2005-11-19 16:09 UTC (permalink / raw)
To: zzapper, zsh-users
On Nov 19, 11:28am, zzapper wrote:
} Subject: Re: listing/deleting empty directories recursively
}
} >ls -ld **/*(/^F)
} >
} >Christian
}
} Now I'd like to rm those directories, and log which ones were deleted.
On a GNU-ish system?
rmdir --verbose **/*(/^F)
You can work out the stderr redirections yourself.
Of course you realize that this only finds/removes empty leaf directories.
Every time you remove a directory you might discover that its parent has
also become empty. For that reason you might be better off doing a depth-
first traversal attempting to remove *all* directories, and simply allow
rmdir to fail if the directory isn't empty.
rmdir --verbose **/*(/od)
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: listing/deleting empty directories recursively
2005-11-19 16:09 ` Bart Schaefer
@ 2005-11-19 17:07 ` zzapper
2005-11-19 19:39 ` Bart Schaefer
0 siblings, 1 reply; 13+ messages in thread
From: zzapper @ 2005-11-19 17:07 UTC (permalink / raw)
To: zsh-users
On Sat, 19 Nov 2005 16:09:31 +0000, wrote:
>On Nov 19, 11:28am, zzapper wrote:
>} Subject: Re: listing/deleting empty directories recursively
>}
>} >ls -ld **/*(/^F)
>} >
>} >Christian
>}
>} Now I'd like to rm those directories, and log which ones were deleted.
>
>On a GNU-ish system?
>
> rmdir --verbose **/*(/^F)
>
>You can work out the stderr redirections yourself.
>
>Of course you realize that this only finds/removes empty leaf directories.
>Every time you remove a directory you might discover that its parent has
>also become empty. For that reason you might be better off doing a depth-
>first traversal attempting to remove *all* directories, and simply allow
>rmdir to fail if the directory isn't empty.
>
> rmdir --verbose **/*(/od)
I presume this can be xarg?
eg
print **/*(/od) | xargs -n1 -t rmdir
The disadvantage of this is that it produces a flood of warning messages which may prevent you
spotting something untoward.
--
zzapper
Success for Techies and Vim,Zsh tips
http://SuccessTheory.com/
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: listing/deleting empty directories recursively
2005-11-19 17:07 ` zzapper
@ 2005-11-19 19:39 ` Bart Schaefer
0 siblings, 0 replies; 13+ messages in thread
From: Bart Schaefer @ 2005-11-19 19:39 UTC (permalink / raw)
To: zzapper, zsh-users
On Nov 19, 5:07pm, zzapper wrote:
} Subject: Re: listing/deleting empty directories recursively
}
} > rmdir --verbose **/*(/od)
} I presume this can be xarg?
Or zargs, as Stephane suggested.
} The disadvantage of this is that it produces a flood of warning
} messages which may prevent you spotting something untoward.
I guess the question is whether you want a short command or minimal
output. You can do the recursive descent yourself, of course.
function rmemptydir() {
emulate -RL zsh
local here=$REPLY
local -a deep
deep=( $here/*(N/+rmemptydir) )
[[ -n $deep ]] && rmdir --verbose $deep
reply=( $here(N^F) )
}
: *(N/+rmemptydir)
This all assumes a version of zsh recent enough that recursive calls
to the (e) glob qualifier don't cause a crash, and so that you have
the (+) qualifier shortcut.
^ permalink raw reply [flat|nested] 13+ messages in thread
* listing/deleting empty directories recursively
@ 2005-11-18 19:49 zzapper
0 siblings, 0 replies; 13+ messages in thread
From: zzapper @ 2005-11-18 19:49 UTC (permalink / raw)
To: zsh-users
Hi
ls -ld *(/^F)
Will list any empty directories, but only for one level of subdirectories, how to make it delete
empty directories at any level.
--
zzapper
Success for Techies and Vim,Zsh tips
http://SuccessTheory.com/
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2005-11-20 21:16 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-19 9:19 listing/deleting empty directories recursively zzapper
2005-11-19 9:57 ` Christian Taylor
2005-11-19 11:28 ` zzapper
2005-11-19 11:56 ` Thor Andreassen
2005-11-19 11:59 ` Thor Andreassen
2005-11-19 12:48 ` Christian Taylor
2005-11-19 14:54 ` Thorsten Kampe
2005-11-19 15:59 ` Stephane Chazelas
2005-11-19 14:39 ` Lloyd Zusman
2005-11-19 16:09 ` Bart Schaefer
2005-11-19 17:07 ` zzapper
2005-11-19 19:39 ` Bart Schaefer
-- strict thread matches above, loose matches on Subject: below --
2005-11-18 19:49 zzapper
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).