hayzem
hayzem

Reputation: 67

Getting filenames from a directory and search these filenames in php code

I have more than 500 php files and need to find where there php files are in use. So I need a script or something that could help me to find these usages/mentions in the files. I'm thinking to write an bash script to do that but I'm not sure it is an good idea. Do you have any ideas about it?

Upvotes: 0

Views: 85

Answers (3)

hayzem
hayzem

Reputation: 67

This is what I did at the end. This bash script solved my problem generally. Still there is a problem about file names with space(s). I get errors if file name has space(s) in it. For now it's ok for me.

 #!/bin/bash

DIR="/path/to/dir"

# save and change IFS 
OLDIFS=$IFS
IFS=$'\n'

# read all file name into an array
FILES=$(find $DIR -type f -name "*.php")
DFILES=$FILES

#General report file
ofile="general-report.txt"

#Unused files report file
nuffile="unused-files-report.txt"

#count unused files
yok=0

echo "Files have found. Proccessing..."

#Title for general report
echo "GENERAL REPORT FOR X " >> "$ofile"

#Title for unused files report
echo "UNUSED FILES FOR X" >> "$nuffile"

for i in $FILES 
do
    bulundu=0
    dosyaadi=$(basename $i)
    echo "###########################################################################################">> "$ofile"
    echo "###########################################################################################"
    echo "------>>>>> Looking for $dosyaadi ..."
    echo "------>>>>> Looking for $dosyaadi ..." >> "$ofile"
    for j in $DFILES 
    do
        if grep -rnH --color=auto "$dosyaadi" "$j"
            then
            grep -rnH --color=auto "$dosyaadi" "$j" >> "$ofile"
            bulundu=$((bulundu+1))
        fi
    done
    if [ $bulundu -eq 0 ]
        then 
        yok=$((yok+1))
        echo "DOSYA $yok -->> $dosyaadi dosyasi hic bir php kodunda bulunamadi. Kullanilmiyor olmasi muhtemel.">> "$nuffile"
        echo "Dosyanin konumu: $i">> "$nuffile"
        echo -e '\n\n'>> "$nuffile"
    fi
    echo "__________________________________________________________________________________________">> "$ofile"
    echo "__________________________________________________________________________________________"
    echo "$dosyaadi dosyasinin diger dosyalarda $bulundu sayida eslesme bulundu.">> "$ofile"
    echo "$dosyaadi dosyasinin diger dosyalarda $bulundu sayida eslesme bulundu."
    echo "__________________________________________________________________________________________">> "$ofile"
    echo "__________________________________________________________________________________________"
    echo -e '\n\n\n\n'>> "$ofile"
    echo -e '\n\n\n\n'
done
echo "TOPLAM $yok tane dosya kullanilmiyor gorunuyor!">> "$nuffile"
echo "Heyyo! Tüm tarama bitti $ofile dosyasına yazıldı."
# restore it 
IFS=$OLDIFS

Upvotes: 0

Rangad
Rangad

Reputation: 2150

You could append a script writing the output of get_included_files() to a file using auto_append_file

Upvotes: 0

hek2mgl
hek2mgl

Reputation: 157967

With grep on the command line:

grep -rnH --color=auto 'require "test.php"' SOURCE_FOLDER

The command will print every occurance of require "test.php" together with file name and line number (colorized)

Upvotes: 1

Related Questions