Solved

find a file which matches

Posted on 2006-06-13
11
232 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
Suggested Courses

752 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