Solved

find a file which matches

Posted on 2006-06-13
11
209 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now