HELP!! shell script for recursive file removal..

HELP!! shell script for recursive file removal..

Post by rav » Wed, 06 Oct 1993 00:15:55



HI,
   I want to write a shell script that will remove all copies of a
 file starting from one directory down to its subdirectories.

  Any hints , suggestions ???

ravi.

 
 
 

HELP!! shell script for recursive file removal..

Post by R. Stewart Ell » Wed, 06 Oct 1993 03:20:02



>HI,
>   I want to write a shell script that will remove all copies of a
> file starting from one directory down to its subdirectories.
>  Any hints , suggestions ???
>ravi.

rm -r will remove things recursively, but I have never used it with anything
except the argument * or a dir name to rm a whole directory tree.  You could
give it an explicit file name from the top dir it appears in and see if it
will descend.

The way I would normally do this for a well-constrained filename would be:

find /starting/dir -name filename -exec rm {} \;

If you want to be brave you can try a regex for the filename, but It may
match more than you anticipate.  The \ before the ; is the most important
part of the command above.

--
  R.Stewart(Stew) Ellis, Assoc.Prof., (Off)313-762-9765   ___________________
  Humanities & Social Science,  GMI Eng.& Mgmt. Inst.    /   _____  ______

  Gopher,News and sendmail maintainer, all around hack /________/ /  /  / /

 
 
 

HELP!! shell script for recursive file removal..

Post by Mike Ka » Wed, 06 Oct 1993 08:39:06



|>
|> >HI,
|> >   I want to write a shell script that will remove all copies of a
|> > file starting from one directory down to its subdirectories.
|>
|> >  Any hints , suggestions ???
|>
|>
|> >ravi.
|>
|> rm -r will remove things recursively, but I have never used it with anything
|> except the argument * or a dir name to rm a whole directory tree.  You could
|> give it an explicit file name from the top dir it appears in and see if it
|> will descend.
|>
|> The way I would normally do this for a well-constrained filename would be:
|>
|> find /starting/dir -name filename -exec rm {} \;
|>
|> If you want to be brave you can try a regex for the filename, but It may
|> match more than you anticipate.  The \ before the ; is the most important
|> part of the command above.
|>
If you do want to test the regex option, or just be safe, use the -ok option
instead of -exec.  This will prompt you in /bin/rm -i fashion.

|>
|> --
|>   R.Stewart(Stew) Ellis, Assoc.Prof., (Off)313-762-9765   ___________________
|>   Humanities & Social Science,  GMI Eng.& Mgmt. Inst.    /   _____  ______

|>   Gopher,News and sendmail maintainer, all around hack /________/ /  /  / /

--
+---------------------------------------------------------------+
| Mike D. Kail                      AT&T:   (612) 945-6527      |
| Advanced Technology, IS&T         FAX:    (612) 945-6502      |

+---------------------------------------------------------------+
| "Nothing great was ever achieved without enthusiasm."         |
|  - Ralph Waldo Emerson                                        |
+---------------------------------------------------------------+

 
 
 

HELP!! shell script for recursive file removal..

Post by David W. Tamk » Wed, 06 Oct 1993 12:02:04



written:

E> If you want to be brave you can try a regex for the filename, but it may
E> match more than you anticipate.

No, not a regex but a shell globbing pattern: that's what find uses for
wildcards in the -name option.

GNU find has an additional option (-regex) for using regular expressions,
but it still interprets an argument to the -name option as a shell globbing
pattern.


K> If you do want to test the regex option, or just be safe, use the -ok
K> option instead of -exec.  This will prompt you in /bin/rm -i fashion.

Mike's suggestion to use -ok instead of -exec is a good idea, but unfortu-
nately he said "regex" again, reinforcing the confusion.  Perhaps he meant
not "the regex option" (as in "the option of using a regex instead of one
filename") but rather "the -regex option."

David W. Tamkin  Box 3284  Skokie, Illinois  60076-6284  312-714-5610

 
 
 

HELP!! shell script for recursive file removal..

Post by Andrew E Kellm » Wed, 06 Oct 1993 04:15:35



>   I want to write a shell script that will remove all copies of a
> file starting from one directory down to its subdirectories.

Well, _my_ first response would be to use something like

        rm -r <filename>  ; thee -r indicates recursive

Assuming that the original dir name is not the same, it should remove
all occurrances of the <filename>.  (I might be wrong)

If, on the other hand, you have a dir that you want to completely
remove, then type

        rm -rf <dirname>  ; the -f overrides write-protection

I know this works, I use it all the time.

                                        -=Andrew

--


< "....but somehow G-d to me has never been a little spunky Jew named Nathan >
<   Brazil."  --Serge Ortega (Jack Chalker, _Midnight_at_the_Well_of_Souls_  >

 
 
 

HELP!! shell script for recursive file removal..

Post by Mike Ka » Wed, 06 Oct 1993 20:53:14



|> ...

|>
|> K> If you do want to test the regex option, or just be safe, use the -ok
|> K> option instead of -exec.  This will prompt you in /bin/rm -i fashion.
|>
|> Mike's suggestion to use -ok instead of -exec is a good idea, but unfortu-
|> nately he said "regex" again, reinforcing the confusion.  Perhaps he meant
|> not "the regex option" (as in "the option of using a regex instead of one
|> filename") but rather "the -regex option."
|>
No, sorry, I meant "shell globbing pattern".  A mere oversight on my part... :-(

|> David W. Tamkin  Box 3284  Skokie, Illinois  60076-6284  312-714-5610

--
+---------------------------------------------------------------+
| Mike D. Kail                      AT&T:   (612) 945-6527      |
| Advanced Technology, IS&T         FAX:    (612) 945-6502      |

+---------------------------------------------------------------+
| "Nothing great was ever achieved without enthusiasm."         |
|  - Ralph Waldo Emerson                                        |
+---------------------------------------------------------------+

 
 
 

HELP!! shell script for recursive file removal..

Post by Danny R. Faug » Wed, 06 Oct 1993 22:33:59



about rm -r and find...

These work in most normal circumstances, but they barf on long
pathnames (at least in HP-UX).  We have a testcase that creates a long
pathname to test the ENAMETOOLONG errno, and we had to write a script
that cd's down into the tree and uses relative paths to delete things.
If the testcase panics the system, it will only come up to single user
mode, because fsck chokes on the long pathname!
--
Danny Faught -- Convex rookie -- MPP OS Test Development
"Everything is deeply intertwingled."  (Ted Nelson, _Computer Lib_)

 
 
 

HELP!! shell script for recursive file removal..

Post by S. E. Gro » Thu, 07 Oct 1993 09:49:46


>No, not a regex but a shell globbing pattern: that's what find uses for
>wildcards in the -name option.

>David W. Tamkin  Box 3284  Skokie, Illinois  60076-6284  312-714-5610


Mike;
        Can you explain the difference between the shell globbing
function and 'regex'? I assumed that they meant regular expression
when they said regex. Though I have seen some indication that they
are not the same only similar, I would like a better explanation.
Thank you.
--
        Stephen Grove  Comm. Tech. ESS Pacific Bell

 
 
 

HELP!! shell script for recursive file removal..

Post by Mike Campbe » Thu, 07 Oct 1993 20:58:18



Quote:> E> If you want to be brave you can try a regex for the filename, but it may
> E> match more than you anticipate.

> No, not a regex but a shell globbing pattern: that's what find uses for
> wildcards in the -name option.

> GNU find has an additional option (-regex) for using regular expressions,
> but it still interprets an argument to the -name option as a shell globbing
> pattern.

There is a subtle difference here, (other than the fact that shell
globs are NOT regex's, as you noted.)  The -regex option on GNU find
matches regex's on the full filename, including directory.

--
+--------------------------------------+
| Michael Campbell - Salomon Bros Inc. |

+--------------------------------------+----------------------------------------+
| Quantum physics predicts the past with 80% accuracy                           |
+-------------------------------------------------------------------------------+

 
 
 

HELP!! shell script for recursive file removal..

Post by Eternal The Impossible Dream » Thu, 07 Oct 1993 23:03:43



[stuff deleted]
:       Can you explain the difference between the shell globbing
:function and 'regex'? I assumed that they meant regular expression
:when they said regex. Though I have seen some indication that they
:are not the same only similar, I would like a better explanation.
:Thank you.

Shell globbing and regular expression matching are basically two different
ways of specifying "patterns".

Globbing is generally used for creating patterns that are to be matched
against the filenames in the file system, giving a selection of filenames
that match. It is thus very easy to specify things like every file (*) or
every file ending in .c (*.c) but isn't very good at much else. Globbing
tends only to be used in the UNIX shells (although there is some limited
use for it in perl).

Regular expression is a more general method of pattern matching, it can
give some very precise patterns, because of this it tends to require more
characters and be more difficult to "think about" than a shell glob. It is
generally used as a true or false condition against a single known string,
a sort of more complex equals operator. Regular Expressions  can be found
in the UNIX tools "perl" and "lex".

:--
:        Stephen Grove  Comm. Tech. ESS Pacific Bell

--
Brian Blackmore, The University of Kent at Canterbury, United Kingdom.
Beyond the shadow of a dream, who knows what lies.

 
 
 

HELP!! shell script for recursive file removal..

Post by Christoph Badu » Sun, 10 Oct 1993 22:45:32



Quote:>   I want to write a shell script that will remove all copies of a
> file starting from one directory down to its subdirectories.

Please define "copy" more precisely.  In the meantime you can do
something like:

#!/bin/sh
# rmcopies - remove all copies of a given file in the specified
#               directory tree.
# usage: rmcopies file dir
#
# BUGS: doesn't handle the case were file resides below dir
#
master=$1
find $2 -type f -print |
        while read f ; do
                cmp -s $master $f && rm $f
        done

All the responses I've seen so far tried to remove files with the same
name which is quite different than removing a copy of a file.
--

Personally, I don't care whether someone is cool enough to quote Doug
Gwyn--I only care whether Doug Gwyn is cool enough to quote. -- Larry Wall