List file in Dir and then match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting List file in Dir and then match
# 1  
Old 09-20-2008
Data List file in Dir and then match

Hi ALL,

I am making a script that search all then worldwriteable dir in documentroot.I have manage to find all the dir now i want to match each file extension in worldwriteable against a list of array which contain file like php html etc,if i find any file then print dirname. Dont see to get any idea how i do that.i past my code for reiew and comments



#!/bin/bash

list[0]="php"
list[1]="html"
Docroot[0]="/var/www/html/"

export IFS=$'\n'

# Loop through our array.
for x in ${Docroot[@]}
do
# Find all directories & subdirectories
for i in $(find $x -type d -perm /o=w )
do



echo $i

done
done


This is a small output
/var/www/html/test3
/var/www/html/test3/abc
/var/www/html/test3/gallery
/var/www/html/test/abc
# 2  
Old 09-20-2008
Hi now i can also print the file inside all dir,Plz help me with matching,Thnks

#!/bin/bash

list[0]="php"
list[1]="html"
Docroot[0]="/var/www/html/"

export IFS=$'\n'

# Loop through our array.
for x in ${Docroot[@]}
do
# Find all directories & subdirectories
for i in $(find $x -type d -perm /o=w )
do

for j in $(ls -l $i)
do


echo $j

done
done
done


Output

total 8
drwxrwxrwx 2 root root 4096 2008-09-19 18:44 abc
-rw-r--r-- 1 root root 0 2008-09-10 17:21 abc2.php
-rw-r--r-- 1 root root 0 2008-09-10 17:21 abc3.php
-rw-r--r-- 1 root root 0 2008-09-10 17:21 abc.php
drwxrwxrwx 2 root root 4096 2008-09-19 01:53 gallery
-rw-r--r-- 1 root root 0 2008-09-19 18:43 abc.php
# 3  
Old 09-21-2008
what do u mean by matching, pls explain, match files from where to where.

what you want to match, give some example, so can be think on it and put suggestion on it.

Thanks,
Bash
# 4  
Old 09-21-2008
Error

Hi

I mean comparing,I have change the code now i can do comparing , i am facing a new problem ,One is that i m using concatenation to print the total dir out side the loop but its not working as i want my be i m missing the point,I will explain how i need below,Please see example,The number in example is count like if i find any file with php or html extension i incrses the count.Give a soulation to current code i left pervious becaus it was hard and difficult to manage.Thx


Code

find /var/www/html -type d -perm /o=w | while read DIR
do
test=$(find "$DIR" -type f \( -name "*.html" -o -name "*.php" \) -print )


$test2="$test2 $test"
done

print $test2


I want (Example)
/var/www/htm/test 3

Current Out put

/var/www/html/phpBB3/includes/utf/data/recode_cjk.php
/var/www/html/phpBB3/includes/utf/data/search_indexer_20.php
/var/www/html/phpBB3/includes/utf/data/search_indexer_95.php
/var/www/html/phpBB3/includes/utf/utf_tools.php
# 5  
Old 09-21-2008
You're nearly there, you just need to count the number of lines from your find command:

Code:
find /var/www/html -type d -perm /o=w | while read DIR
do 
    echo $DIR $(find "$DIR" -type f \( -name "*.html" -o -name "*.php" \) -print | wc -l)
done

# 6  
Old 09-21-2008
Thx its work but giving extra count look at output,It also giving count of the dir inside other dir.Look ls -l output,Any quic and dirty idea,and how can i do like this /var/www/abc 3 ??.Becz still caught in same problem to get one dir not dir + file name.
drwxrwxrwx 2 root root 4096 2008-09-19 18:44 abc
-rw-r--r-- 1 root root 0 2008-09-10 17:21 abc2.php
-rw-r--r-- 1 root root 0 2008-09-10 17:21 abc3.php
-rw-r--r-- 1 root root 0 2008-09-10 17:21 abc.php
drwxrwxrwx 2 root root 4096 2008-09-19 01:53 gallery

Code output.
/var/www/html/test3/abc.php
/var/www/html/test3/abc3.php
/var/www/html/test3/abc/abc.php
/var/www/html/test3/abc/abc2.php
/var/www/html/test3/abc2.php 5
# 7  
Old 09-21-2008
What operating system are you using? As far as I know -perm /o=w is not valid syntax, but I presumed you have tested this? Try -perm -020 instead maybe.

Also I made an error in the above, if you run find again to count files it will count those in subdirectories - do you want that? If not, try:

Code:
find /var/www/html -type d -perm -020 | while read DIR
do 
    echo $DIR $(cd "$DIR"; ls *.html *.php 2>/dev/null | wc -l)
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename files to match file list pattern

Hi All, I have 100 folders with the first delimiter has a unique name i.e (123_hello and 575_hello) and each folder have atlist 1000 plus files with naming convention i.e (575_hello_1.iso ... 575_hello_1000.iso). 575_hello/575_hello_1.iso 575_hello/575_hello_2.iso 575_hello/575_hello_3.iso... (8 Replies)
Discussion started by: lxdorney
8 Replies

2. Shell Programming and Scripting

Unable to list particular file in dir.

Hi i am trying to list all the file starting with CSA.CHENAISCP* i am using below command to do that ls -lrt | grep CSA.CHENAISCP*however i am getting below error. 0403-027 The parameter list is too long.please suggest me some alternative way. scriptor (3 Replies)
Discussion started by: scriptor
3 Replies

3. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

4. Shell Programming and Scripting

Create file Dir and Sub Dir same time

Hi Guys , I want create files Dire and Sub Dire. as same time using variable. EX: x1="/hk/Pt/put/NC/R1.txt" x2="/hk/pt/Put/Ot/NC/RN.txt" And i want delete all after done with my script. Thanks (2 Replies)
Discussion started by: pareshkp
2 Replies

5. Shell Programming and Scripting

List all file that match user input perl

Hi, I want to list all file that match user input ( specified shell wildcard) but when I compile it dont list me #!/usr/bin/perl -w print "Enter Advance Search Function: "; chomp ($func = <STDIN>); my @files = glob("$func"); foreach my $file (@files) { print "$file\n";... (1 Reply)
Discussion started by: guidely
1 Replies

6. UNIX for Dummies Questions & Answers

problem to determine all files and dir match up on 2 different unix boxes

Hi Friends I have 2 solaris boxes and I need to check certain directories (on local filesystem and mounted nfs) to make sure that they match up on both boxes and to delete or move the other mismatches to elsewhere on the local filesystem. I investigated for unix commands like rsync, and tree... (1 Reply)
Discussion started by: mpc8250
1 Replies

7. Shell Programming and Scripting

how to list of file in home dir ?

I want to print all list of file in the home dir of the user in $i with full bath an owner and group and permissions (1 Reply)
Discussion started by: testman84
1 Replies

8. UNIX for Dummies Questions & Answers

How to list all files in dir and sub-dir's recursively along with file size?

I am very new to unix as well as shell scripting. I have to write a script for the following requirement. In have to list all the files in directory and its sub directories along with file path and size of the file Please help me in this regard and many thanks in advance. (3 Replies)
Discussion started by: nmakkena
3 Replies

9. UNIX for Dummies Questions & Answers

Searching list of entries in file for actual files in dir

Hi all, I have a file entries.txt that contains a number of entries all on seperate lines e.g. test1 test2 test3 test4 Then in a directory called /TestFiles I have a number of files that could contain the above text in the file name e.g. qwertytest1.csv qwertytest2.csv... (2 Replies)
Discussion started by: not4google
2 Replies

10. Shell Programming and Scripting

how to create new dir fro a file list

Hi, What will be the best way to do the follwing: i have a file calld dir.list /cav /cav/brif /usr/main /cat i want to run a script that will take each of the item in the file and create a new dir in a location that i'll choose it nee to do mkdir cav mkdir cav cd cav mkdir brif... (8 Replies)
Discussion started by: banjo
8 Replies
Login or Register to Ask a Question