Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

find a file which matches

Posted on 2006-06-13
11
Medium Priority
?
244 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
[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
  • 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
Congratulations! You’re Certified – Now What?

Starting a new career can be overwhelming. Becoming certified in your field of expertise is a great start, but where do you go from here?  Here are some tips to help you on your career journey.

 

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 2000 total points
ID: 16892985
It's typo - change to: -type f
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

721 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