Solved

find a file which matches

Posted on 2006-06-13
11
226 Views
Last Modified: 2010-04-22
hello

i would like to use a shell script which

searches a directory path recursively eg.

home/oracle

for files which match...

if you find a match print the directory paths to a file
if a match is not found then print no match
0
Comment
Question by:sma3092
  • 6
  • 5
11 Comments
 
LVL 43

Expert Comment

by:ravenpl
ID: 16892745
PATTERN='backup*.bck'
MATCH=$( find /the/dir/to/search -type f -name $PATTERN -printf "%h\n" )
[ -z "$MATCH" ] && echo "no match" || echo $MATCH
0
 

Author Comment

by:sma3092
ID: 16892756
whats does the PATTERN variable do in the above script?

i just want to search a directory recursively.... and if there is a match print both the directory paths to a txt file
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 16892782
It does nothing, but You have to match against some pattern. You may do it this way as well

find /the/dir/to/search -type f -name 'backup*.bck' -printf "%h\n" > result.file.txt
[ -s result.file.txt ] || echo "no match" > result.file.txt # we checking if the file has some size

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:sma3092
ID: 16892811
i want to match against files having the same name..

so for example

/home/oracle/andrew.txt
/home/oracle/test/andrew.txt

should match because the file names exist twice

these paths should then be printed to a file.
if they do not exist then the script should do nothing
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 16892830
Do You want: for each file within directory, check if there is another one with same name within same(another) directory.
If duplicate is fine print all occurances to a result.file (full path, or just directories) ?
0
 

Author Comment

by:sma3092
ID: 16892861
i want to start a say this directory point /home/oracle... search recusively through this directory matching file names...

so a match would be

/home/oracle/test.txt
/home/oracle/andrew/test.txt

matches get written to a text file so the txt file will have

the following files /home/oracle/test.txt and /home/oracle/andrew/test.txt match
0
 

Author Comment

by:sma3092
ID: 16892865
yes please the full path of duplicate files
0
 

Author Comment

by:sma3092
ID: 16892870
with in same & diferent directories so the following will be a match

/home/oracle/test.txt
/home/oracle/andrew/test.txt
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 16892922
find /home/oracle -type f -printf "%f\n" | sort | uniq -c | while read line; do
 COUNT=$( echo $line | cut -d' ' -f1 )
 [ $COUNT -gt 1 ] && find /home/oracle -type -f -name $( echo $line | cut -d' ' -f2 )
done

will dump all matches into stdout, modify to have it in file

done > results.txt
[ -s results.txt ] || echo "no match" > results.txt
0
 

Author Comment

by:sma3092
ID: 16892965
Hi

I get the following error message when i run the script

find: invalid argument `-f' to `-type'
0
 
LVL 43

Accepted Solution

by:
ravenpl earned 500 total points
ID: 16892985
It's typo - change to: -type f
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

830 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