Unix Shell - Recursively delete all subdirectories

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > July 2005 > Recursively delete all subdirectories





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Recursively delete all subdirectories
Mr. Question

2005-07-30, 8:47 pm

Emacs is set (on my system) to place all backups in a .backups directory.

How can I recursively delete every .backup subdirectory in /home/test?

Thanks.
Alan Connor

2005-07-31, 2:48 am

On comp.unix.shell, in <dchcpm$juj$1@skeeter.ucdavis.edu>,
"Mr. Question" wrote:

> Emacs is set (on my system) to place all backups in a .backups
> directory.
>
> How can I recursively delete every .backup subdirectory in
> /home/test?
>
> Thanks.


rm -rf $(find /home/test -type d -name "*.backup")

Replace $(...) with `...` if you are not using a POSIX shell.
Those are backticks, not single quotes.

HTH,

AC


Chris F.A. Johnson

2005-07-31, 2:48 am

On 2005-07-30, Mr. Question wrote:
> Emacs is set (on my system) to place all backups in a .backups directory.
>
> How can I recursively delete every .backup subdirectory in /home/test?


rm -rf $(find /home/test -type d -name .backups -print)

find /home/test -type d -name .backups -print | xargs rm -rf

find /home/test -type d -name .backups -exec rm -rf {} \;

find /home/test -type d -name .backups -exec rm -rf {} +



--
Chris F.A. Johnson <http://cfaj.freeshell.org>
========================================
==========================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>
Bill Seivert

2005-07-31, 5:55 pm



Mr. Question wrote:
> Emacs is set (on my system) to place all backups in a .backups directory.
>
> How can I recursively delete every .backup subdirectory in /home/test?
>
> Thanks.


find /home/test -depth -name .backups -type d -exec rm -rf {} \;

Bill Seivert

Mario Domgörgen

2005-07-31, 5:55 pm

Bill Seivert <seivert@pcisys.not> schrieb:
> find /home/test -depth -name .backups -type d -exec rm -rf {} \;


I would use -exec rm -rf '{}' \; if there is the slightest chance of
some nasty whitespace or even newlines in the directory names. And why
do you use -depth if you use -rf afterwards? If there
are a lot of dirs maybe he should use the following:

find /home/test -name .backups -type d -print0 | xargs -0 rm -r

Best regards,

Mario

--
Chaos reigns within.
Reflect, repent, and reboot.
Order shall return.
Chris F.A. Johnson

2005-07-31, 5:55 pm

On 2005-07-31, Mario Domgörgen wrote:
> Bill Seivert <seivert@pcisys.not> schrieb:
>
> I would use -exec rm -rf '{}' \; if there is the slightest chance of
> some nasty whitespace or even newlines in the directory names.


That is not necessary. Each filename is passed as a separate
argument.

> And why
> do you use -depth if you use -rf afterwards?


Because you will generate errors when a parent directory has
already been removed.

> If there are a lot of dirs maybe he should use the following:
>
> find /home/test -name .backups -type d -print0 | xargs -0 rm -r


If his versions of find and xargs have -print0 and -0.

--
Chris F.A. Johnson <http://cfaj.freeshell.org>
========================================
==========================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>
Mario Domgörgen

2005-07-31, 5:55 pm

Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
>
> That is not necessary. Each filename is passed as a separate
> argument.


Oh, i never knew that. Is it just because of some historic reasons or
are there good occasions to use '{}'?

>
>
> Because you will generate errors when a parent directory has
> already been removed.


Ups, i should habe tested it. To be honest i've never seen nor used
-depth, so i learned a new lesson today...

Best regards

Mario


--
Chaos reigns within.
Reflect, repent, and reboot.
Order shall return.
Bill Seivert

2005-07-31, 8:51 pm



Mario Domgörgen wrote:
> Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
>
>
>
> Oh, i never knew that. Is it just because of some historic reasons or
> are there good occasions to use '{}'?
>


IIRC, using '{}' should NOT work, since the man page for find says
that {} will be replaced by the current path name only when it is
surrounded by whitespace. Try it with echo instead of rm -rf.

Bill Seivert

Robert Bonomi

2005-07-31, 8:51 pm

In article <42ED69E5.3040500@pcisys.not>,
Bill Seivert <seivert@pcisys.net> wrote:
>
>
>Mario Domgörgen wrote:
>
>IIRC, using '{}' should NOT work, since the man page for find says
>that {} will be replaced by the current path name only when it is
>surrounded by whitespace. Try it with echo instead of rm -rf.


BUT, unless there is 'special' parsing going on within the shell, for the
the 'find' line, find sees what appears on the command-line as {}, or
as '{}', *or (for that matter) as "{}" the simple 2-character token {}.

Find, or any application, has no way of telling whether the original command-
line contained (using "`" as meta-delimiter , ` {} `, ` '{}' `, or
` "{}" ` when it sees an argv[] element consisting solely of the 2-character
sequence {}.

Presumably find works by examining each element of the argv[] array for
s _complete_ match against {}, and ignores occurrences of {} that are
embedded in longer strings.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com