LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 04-29-2005, 06:57 PM   #1
liren
LQ Newbie
 
Registered: Apr 2005
Posts: 2

Rep: Reputation: 0
find shell script help


Hi,

I'm totally new at shell scripting and I've been Googling all day for examples on how to do what I'm trying to do. I also looked at the find Man pages but I still need help.

I need to find all unused *.php* files. So, what I want to do is first run a find that lists all the files and then do a grep to see if that file name is contained within the *.php* files and then print a list of files that are not called in any of the files.

I tried

find . -name '*.php*' -exec grep {} \; -print | grep :0

but that didn't work.

then i tried to declare an array

declare -a files
files=`find . -name '*.php*'`
echo ${files[0]}

but that just dumped the entire list into the first element.

then I tried in the command line

find . -name '*.php*' > files.txt
find . -name '*.php*' -exec grep < files.txt {} \; > files2.txt

but that just wrote the first file name from the first file into the second file.

Please, help I don't know what else to do.

Thanks.
 
Old 04-29-2005, 07:53 PM   #2
YetAnotherDave
Member
 
Registered: Feb 2005
Posts: 94

Rep: Reputation: 17
How about this? Output should be a list of all '*.php*' files that are not included in another one. The "shortName" variable is used to trim the leading path off of "name" and might not be necessary ( depending on the specifics of your situation ).

Code:
#! /bin/bash
FILES=`find . -name '*.php*'`
for name in $FILES; do
    shortName=${name##*/}
    USAGE=`grep -l $shortName $FILES`
    if [ "$USAGE" = "" ] ; then
        echo $name
    fi
done
- Dave
 
Old 04-29-2005, 08:13 PM   #3
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86; Gentoo PPC; Gentoo Sparc64; FreeBSD; OS X; Solaris
Posts: 3,731
Blog Entries: 4

Rep: Reputation: 66
For something like this, a more powerful language such as python seems to do the trick...

Code:
#!/usr/bin/python

import os
from fnmatch import filter

def readInfile(infile):
    f = open(infile, "r")
    text = f.readlines()
    f.close()
    return text

def searchFile(files, file, text):
    for page in files:
        for line in text:
            if page in line:
                if not page in keep:
                    keep.append(page)
            else:
                if not page in delete:
                    delete.append(page)

keep, delete = [], []
php_pages = filter(os.listdir(os.getcwd()), '*.php*')
for php_page in php_pages:
    searchFile(php_pages, php_page, readInfile(php_page))

print "Keep these:"
for file in keep:
    print "\t %s" % file
print "Delete these:"
for file in delete:
    if not file in keep:
        # os.remove(file)
        print "\t %s" % file
This seems to work well on my system with a few scratch files I created. If you are happy with the results then you can get the script to actually just delete the files for you...

Sorry, the script should actually work now

Last edited by bulliver; 04-30-2005 at 03:25 AM.
 
Old 05-02-2005, 03:05 PM   #4
liren
LQ Newbie
 
Registered: Apr 2005
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks for your replies. I decided to keep it simple and use the first example. It worked great!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script to find a particular string Prasun1 Linux - General 5 08-30-2005 09:23 AM
How to find out kernel version in a shell script? ta0kira Slackware 3 10-05-2004 04:54 AM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 06:25 PM
Linux can't find a shell script?? jt1020 Linux - General 4 04-27-2003 08:27 AM
shell script to find user(s) profile j-me Programming 2 01-31-2003 11:16 AM


All times are GMT -5. The time now is 03:22 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration