Solved

perl script to find all files with certain extensions

Posted on 2010-08-23
25
709 Views
Last Modified: 2013-12-20
I am trying to write a perl script which finds all files on a server excluding certain directories and only including certain file types..

I have the following command.

find /opt/lampp/htdocs/ -name '*' -path '/opt/lampp/htdocs/some/folder' -prune -o -path '/opt/lampp/htdocs/another/folder' -prune -o -print;

This works fine and returns all files on the server.

I have two issues.

1. This runs fine from the console... but when run from inside my perl script I get the following error.

find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]


2. I need to only return files with certain extensions, this includes .pl, .txt, .php, .html, .xml, .js . I know I could run multiple commands using '*.php" but can this be incorporated into a single command?

Thanks
0
Comment
Question by:Snarfles
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 6
  • 4
  • +2
25 Comments
 
LVL 11

Expert Comment

by:Pieter Jordaan
ID: 33501927

Hi

Try it with double quotes.

find /opt/lampp/htdocs/ -name "*" -path "/opt/lampp/htdocs/some/folder" -prune -o -path "/opt/lampp/htdocs/another/folder" -prune -o -print;

If that fails, escape the double quotes

find /opt/lampp/htdocs/ -name \"*\" -path \"/opt/lampp/htdocs/some/folder\" -prune -o -path \"/opt/lampp/htdocs/another/folder\" -prune -o -print;

I hope that helps
0
 
LVL 8

Assisted Solution

by:mustaccio
mustaccio earned 166 total points
ID: 33502157
To answer your question #1 we'll need to see how you invoke find in your perl program. As to the question #2, try replacing

-name "*"

with the list of patterns:

-name "*php" -o -name "*js"

etc
0
 
LVL 9

Author Comment

by:Snarfles
ID: 33502198
Awesome that did help. It now executes!

Do you know about part 2?

Also it includes directories as well... can I exclude those from the results?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:wilcoxon
ID: 33502232
You don't need -name if you want all files.

I think the simplest thing to do since you say it's in a perl script is to do the find in perl rather than using the find command.  This should do what you want...
#!/usr/bin/perl
    eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
        if 0; #$running_under_some_shell

use strict;
use warnings;
use File::Find ();

# Set the variable $File::Find::dont_use_nlink if you're using AFS,
# since AFS cheats.

# for the convenience of &wanted calls, including -eval statements:
use vars qw/*name *dir *prune/;
*name   = *File::Find::name;
*dir    = *File::Find::dir;
*prune  = *File::Find::prune;

sub wanted {
    #my ($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_);
    if ($name =~ /^\/opt\/lampp\/htdocs\/some\/folder\z/s or
        $name =~ /^\/opt\/lampp\/htdocs\/another\/folder\z/s) {
    	$prune = 1
    } elsif ($name =~ /\.(?:pl|txt|php|html|xml|js)$/) {
        print "$name\n";
    }
}


# Traverse desired filesystems
File::Find::find({wanted => \&wanted}, '/opt/lampp/htdocs');

Open in new window

0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 33502246
To only include files in find, you can do -type f.
0
 
LVL 9

Author Comment

by:Snarfles
ID: 33502287
Hmm so I have ...

find /opt/lampp/htdocs/ -name \"*.php\" -o -name \"*.js\" -o -name \"*.html\" -o -name \"*.pl\" -o -name \"*.xml\" -o -name \"*.txt\" -path \"/opt/lampp/htdocs/some/folder\" -prune -o -path \"/opt/lampp/htdocs/another/folder\" -prune -o -print;

That seemed to cut down my results from almost 10000 to about 6000 but I am still seeing lots of image files and pdf's and also the directories.

0
 
LVL 11

Expert Comment

by:Pieter Jordaan
ID: 33502343

Just use grep to remove the last results:

find /opt/lampp/htdocs/ -name \"*.php\" -o -name \"*.js\" -o -name \"*.html\" -o -name \"*.pl\" -o -name \"*.xml\" -o -name \"*.txt\" -path \"/opt/lampp/htdocs/some/folder\" -prune -o -path \"/opt/lampp/htdocs/another/folder\" -prune -o -print | grep -v \".unwanted\" | grep -v \".moreunwanted\"
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 33502355
Did you look at the perl solution I posted?  For complex finds, I find it much simpler to write it in perl where it can be much clearer what is going on and much easier to modify it (add more file types to include dirs to exclude, etc).
0
 
LVL 11

Expert Comment

by:Pieter Jordaan
ID: 33502383

This is correct usage for multiple patterns.
find . -type f \( -name \"*.java\" -o -name \"*.xml\" -o -name \"*.html\" \)
0
 
LVL 9

Author Comment

by:Snarfles
ID: 33502397
Yep, sorry I did, but was trying the other suggestions. The script I have already uses a lot of the find commands already so it would be preferable to stick with that... even though this command is just getting longer.

Does that perl find script of yours allow you to search on different systems as I am ssh ing to other boxes to do searches as well as the local one.... just to complicate things :/
0
 
LVL 26

Accepted Solution

by:
wilcoxon earned 167 total points
ID: 33502441
Have you looked at taking multiple of the find system calls in your perl script and combining them into fewer (possibly as few as one) File::Find call?

No.  File::Find does not support ssh (then again, neither does find directly).  It may be possible to write something in perl to ssh and find but that would likely be very complicated.  If doing find over ssh, I would either just use find or write a (some?) perl script(s) and put them on the remote boxes (if not a shared fs) and execute them over ssh (rather than find).
0
 
LVL 9

Author Comment

by:Snarfles
ID: 33502606
So I now have

$command = "find /opt/lampp/htdocs/ -type f -o \( -name \"*.php\" -o -name \"*.js\" -o -name \"*.html\" -o -name \"*.pl\" -o -name \"*.xml\" -o -name \"*.txt\" \) -o -path '/opt/lampp/htdocs/some/folder' -prune -o -path '/opt/lampp/htdocs/another/folder/' -prune -o -print;";

Which I feel is pretty close... except I am getting this error when run

syntax error near unexpected token `('

i've tried
with and without the -o after the -find f
with and without the -o after the end bracket
with and without the slashes for the brackets...

Little confused :S
0
 
LVL 9

Author Comment

by:Snarfles
ID: 33502655
Sorry that error line is incomplete

bash: -c: line 0: syntax error near unexpected token `('
0
 
LVL 11

Expert Comment

by:Pieter Jordaan
ID: 33502675

OK, brackets wont work.
Sorry.

Try escaping the  * too.
-name \"\*.php\"
0
 
LVL 9

Author Comment

by:Snarfles
ID: 33502739
Oh you know what... its actually now doing the opposite of what I want... so its excluding all the file types I want and returning the ones I dont

$command = "find /opt/lampp/htdocs/ -type f -name \"*.php\" -o -name \"*.js\" -o -name \"*.html\" -o -name \"*.pl\" -o -name \"*.xml\" -o -name \"*.txt\" -path '/opt/lampp/htdocs/some/folder' -prune -o -path '/opt/lampp/htdocs/another/folder/' -prune -o -print;";
0
 
LVL 9

Author Comment

by:Snarfles
ID: 33502927
Hmm this command didnt work BitFreeze

print $command = "find /opt/lampp/htdocs/ -type f -name \"*.php\" -o -name \"*.js\" -o -name \"*.html\" -o -name \"*.pl\" -o -name \"*.xml\" -o -name \"*.txt\" -path '/opt/lampp/htdocs/some/folder/' -prune -o -path '/opt/lampp/htdocs/another/folder' -prune -o -print | grep -v \"*.png\" | grep -v \"*.jpg\";";
0
 
LVL 11

Assisted Solution

by:Pieter Jordaan
Pieter Jordaan earned 167 total points
ID: 33502960
$command = "find /opt/lampp/htdocs/ -type f -name \"*.php\" -or -name \"*.js\" -or -name \"*.html\" -or -name \"*.pl\" -or -name \"*.xml\" -or -name \"*.txt\" -prune -o -path '/opt/lampp/htdocs/another/folder/' -prune -o -print;";
0
 
LVL 9

Author Comment

by:Snarfles
ID: 33528935
Hi Everyone

Thanks for all your suggestions...

With the help of a colleague I have found a solution

$command = "find /opt/lampp/htdocs -nowarn -path \"*directory_1*\" -prune -o -path \"*Directory_2*\" -prune -o -regextype posix-awk -regex \"(.*.html|.*.js|.*.pl|.*.php|.*.txt|.*.xml)\" -type f -print";

The regextype list can be used to add whatever files I actually require.

The prune path needs to be at the front of the command or it breaks everything.

The -nowarn is needed because the way the command works it gives a warning message at the start even though it works perfectly.

This did take a while to work out... you wouldn't think it would be that hard. The prune is very finicky and if you use it in the wrong place it completely destroys your command which I think is the hardest part of this problem.
0
 
LVL 11

Expert Comment

by:Pieter Jordaan
ID: 33532516
-or does the same thing.
0
 
LVL 9

Author Comment

by:Snarfles
ID: 33632446
Hi Vee Mod

I closed this and awarded points and it appears to me to have done the same thing. It looks like I have not awarded points to the experts but I have.

Cheers

Snarfles
0
 

Expert Comment

by:South Mod
ID: 33645150
Objecting on behalf of author.

Snarfles: Please let us know which comments you'd like to accept and the number of points you'd like to award to each and we'll manage this for you. EE is making changes to the closure process and there are still some glitches to work out.

SouthMod
Community Support Moderator
0
 
LVL 9

Author Comment

by:Snarfles
ID: 33656947
Hey, I'm actually traveling atm so bit hard to make comments, but the comments you selected as answers were close to what I was going to select anyway so thanks for closing this.
Cheers
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question