"daniel" <[EMAIL PROTECTED]> writes:

> i'm a perlgeek
> so i'm familiar with its style of regular expressions
> but when i'm trying to use one of those regular expressions in a find
> command,
> i'm not having much luck
> here's what i want to do:
>
>
> find /home/ -name "(.Apple(.*))|(Network Trash
> Folder)|(TheVolumeSettingsFolder)" -print0 | rm -rf
>
>
> to get rid of all the stuff placed by netatalk before i back it up
> now this works:
>
>
> find /home/ -name ".Apple*" -print0 | rm -rf
>
>
> so it looks like i'm not understanding bash's use of regexps
> anyone care to help out here?

If you want to use perl regex then use perl, otherwise you'll need old
fashioned file globbing, which is much more limited than regular
expressions, and has different rules.

Since you already know perl, I'd suggest writing a small script like
the one below, that will be usefull in may places.

I didn't bother with lots of tests and safegaurds so you will want to
doctor it up as needed.  This script expects you to feed it a regex
and a directory to search.  This script only prints the file names it
finds.  To make it do what you want you'll need to lookup the perl
module File::Find and the perl function `unlink'
    

    perldoc -f unlink
    perldoc File::Find 

(if its installed this will bring up the info on how to use either.)
Follow the example script below, but when you perldoc File::Find
scroll down to a line that begins like this:

 `The wanted() function does whatever verifications you'

That section explains what builtin variables will contain.

 Like  "$File::Find::dir" contains the current directory name

So you'll need to add that in your unlink (in place of print below) so
the pathname is available for the unlinking to be accurate

NOTE:  This script only prints filenames it finds... Its left to the
       reader to make it `unlink' them.

In one of my directories ~/no_bak running it like this:

   ./find.pl '(file|9)$' /home/reader/no_bak

You can see the results:
    file
    nmap_file
    19
    9
    refile
And how they match the regex in cmdline arg $1 '(file|9)$'

cat my_find.pl
========================================
#!/usr/bin/perl -w

## get this script name
($myscript = $0) =~ s:^.*/::;

## declare variables
$regex = '';
$directory = '';

use File::Find;

if (!$ARGV[1]){
   print "Whoa, two cmdline args are required'\n";
   print "Example: \`$myscript REGEX DIRECTORY'\n";
   exit;
}
if (! -d $ARGV[1]){
   print "Sorry .. no such directory as $ARGV[1]\n";
   exit;
}
## Shift the cmdline args into variables
$regex = shift;
$directory = shift;

File::Find::find({wanted => \&wanted}, "$directory");
exit;

sub wanted() {
## Look for files matching our REGEX
   if ($_ =~ /$regex/){
      print "$_\n";
   }
## ========================================



_______________________________________________
Redhat-list mailing list
[EMAIL PROTECTED]
https://listman.redhat.com/mailman/listinfo/redhat-list

Reply via email to