Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

find a file which matches

Posted on 2006-06-13
11
Medium Priority
?
247 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
Industry Leaders: 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 2000 total points
ID: 16892985
It's typo - change to: -type f
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

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 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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

885 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