Simply Bash Script to Confirm that there are 2 Files in Same Folder

Im looking for 2 Files -> *.WAV and *.wav with

find . -name 'unavail.wav' -o -name 'unavail.WAV' 

I need to list the Folders that contains these 2 Files?

Thx in advance

Maybe like this?

find . -iname 'unavail.wav' | tr A-Z a-z | sort | uniq -cd

That won't work as expected if there are folders that only differ in case too.

yeah, seems to working

XXX:[XXX]/voip/old # find . -name 'unavail.wav' -o -name 'unavail.WAV' | tr A-Z a-z | sort | uniq -cd
      2 ./2309/unavail.wav
      2 ./2315/unavail.wav
      2 ./33010/unavail.wav
      2 ./8336/unavail.wav
XXX:[XXX]/voicemail/voip/old # 

thank you very much. otherwise i would have to check more then 600 folders manually.

Hi,

You could also use;

find . -name 'unavail.wav' -o -name 'unavail.WAV' -printf %h

This should print only directory names where matching files exist.

Regards

Gull04

1 Like

Should there be directories that differ in case only (the caveat that stomp mentioned, which is negligible with your numeric directories obviously), you could use

find . -iname 'unavail.wav' | rev | sort -t"/" -k2 | cut -d"/" -f2- | rev | uniq -cd

on the other hand, with a looong pipeline like that, you might be better off using e.g. awk .

this doesnt work for me. I find many folders but not those where both files (unavail.WAV & unawail.wav) were contained.

XXX:[XXX/voip/old # find . -name 'unavail.wav' -o -name 'unavail.WAV' -printf %h 
./7325./6657./2309./5863./3339./4045./96964./5867./3336./98764./33010./5891./2312./2057./2352./5861./7322./7332./8336./5890./2311./2128./2346./5880./2333./2300./5553./5881./5882./2313./2304./2360./2339./2335./5883./2369./2308./98765./2129./2325./96962./5252./7333./5371./4950./2329./6606./3333./2344./2306./5859./5898./5830./5897./5860./2321./2347./2316./5862./5864./2310./2307./34699./7331./2345./5896./5870./2399./2371./7320./3338./2356./2368./5866./2323./2380./2318./5868./2330./5872./5601./5833./2365./5894./5865./5885./2361./5843./2338./9015./2328./5869./2301./5886./2341./2354./2353./4946./2367./5893./2317./1234./2320./2303./2357./2319./2315XXX:[XXX]/voip/old # 

when i look into the found foldes I see just one file

XXX:[XXX]/voip/old # ll 7325
insgesamt 12
drwxr-xr-x 2 root root 4096  5. Aug 2011  INBOX
-rw-r--r-- 1 root root 6040  5. Aug 2011  unavail.WAV
XXX:[XXX]/voip/old # 

but I need those Folders where both Files (unavail.wav & unawail.WAV) are in it.

XXX:[XXX]/voip/old # find . -name 'unavail.wav' -o -name 'unavail.WAV' | tr A-Z a-z | sort | uniq -cd
      2 ./2309/unavail.wav
      2 ./2315/unavail.wav
      2 ./33010/unavail.wav
      2 ./8336/unavail.wav
XXX:[XXX]/voip/old # 


So when I look into these folders:

XXX:[XXX]/voip/old # ll 2309
insgesamt 164
-rwx------ 1 root root    386  5. Aug 2011  busy.WAV
drwx------ 2 root root   4096  5. Aug 2011  Cust5
-rwx------ 1 root root   1750  5. Aug 2011  greet.WAV
drwxr-xr-x 2 root root   4096  5. Aug 2011  INBOX
drwx------ 2 root root   4096  5. Aug 2011  Old
-rw-r--r-- 1 root root 134198  5. Aug 2011  unavail.wav
-rwx------ 1 root root   2010  5. Aug 2011  unavail.WAV
drwx------ 2 root root   4096  5. Aug 2011  Work
XXX:[XXX]/voip/old # 


thx anyway

Hi,

Sorry, I assumed that you would have a bit more knowledge of find so for completeness;

find ./ -name 'unavail.wav' -o -name 'unavail.WAV' -printf '%#m %u %h\n'

Give that a try - you should read the find man page and adjust the required output on the printf option.

Regards

Gull04

1 Like

Hi Gull04,

I tested your variant with this...

stomp@box:~/tmp$ find a
a
a/a.wav
a/A.wav
stomp@box:~/tmp$ find ./ -name 'a.wav' -o -name 'A.wav' -printf '%h\n'
./a
stomp@box:~/tmp$ 

...and I'm wondering why I have only 1 result. I would expect 2 results, as I understand the command like this: "Search for a file name a.wav or A.wav and print the corresponding parent directory". So why only 1 result here?

hehe, no i dont use linux for almost 10 years now.

thx for your answer, but still same result:

XXX:[XXX]/voip/old # find ./ -name 'unavail.wav' -o -name 'unavail.WAV' -printf '%#m %u %h\n'
0644 root ./7325
0644 root ./6657
0700 root ./2309
0644 root ./5863
0700 root ./3339
0644 root ./4045
0700 root ./96964
0644 root ./5867
0700 root ./3336
0644 root ./98764
0700 root ./33010
0644 root ./5891
0644 root ./2312
0644 root ./2057
0700 root ./2352
0644 root ./5861
0644 root ./7322
0644 root ./7332
0700 root ./8336
0700 root ./5890
0644 root ./2311
0644 root ./2128
0644 root ./2346
0644 root ./5880
0644 root ./2333
0644 root ./2300
0700 root ./5553
0644 root ./5881
0644 root ./5882
0644 root ./2313
0644 root ./2304
0644 root ./2360
0644 root ./2339
0644 root ./2335
0644 root ./5883
0644 root ./2369
0644 root ./2308
0700 root ./98765
0700 root ./2129
0644 root ./2325
0700 root ./96962
0700 root ./5252
0644 root ./7333
0644 root ./5371
0644 root ./4950
0644 root ./2329
0644 root ./6606
0700 root ./3333
0644 root ./2344
0644 root ./2306
0644 root ./5859
0644 root ./5898
0644 root ./5830
0644 root ./5897
0644 root ./5860
0644 root ./2321
0644 root ./2347
0644 root ./2316
0644 root ./5862
0644 root ./5864
0644 root ./2310
0644 root ./2307
0644 root ./34699
0700 root ./7331
0644 root ./2345
0644 root ./5896
0644 root ./5870
0700 root ./2399
0644 root ./2371
0644 root ./7320
0700 root ./3338
0644 root ./2356
0644 root ./2368
0644 root ./5866
0644 root ./2323
0644 root ./2380
XXX:[XXX]/voip/old # 

in none of the founded folders are both files together: unavail.wav & unavail.WAV.
just one of them exist in them:

XXX:[XXX]/voip/old # ll 7325
insgesamt 12
drwxr-xr-x 2 root root 4096  5. Aug 2011  INBOX
-rw-r--r-- 1 root root 6040  5. Aug 2011  unavail.WAV
XXX:[XXX]/voip/old # ll 5863
insgesamt 12
drwxr-xr-x 2 root root 4096  5. Aug 2011  INBOX
-rw-r--r-- 1 root root 6040  5. Aug 2011  unavail.WAV
XXX:[XXX]/voip/old # ll 3339
insgesamt 20
drwx------ 2 root root  4096  5. Aug 2011  INBOX
-rwx------ 1 root root 12670  5. Aug 2011  unavail.WAV
XXX:[XXX]/voip/old # ll 96964
insgesamt 8
drwxr-xr-x 2 root root 4096  5. Aug 2011  INBOX
-rwx------ 1 root root 3310  5. Aug 2011  unavail.WAV
XXX:[XXX]/voip/old # 

so

find . -name 'unavail.wav' -o -name 'unavail.WAV' | tr A-Z a-z | sort | uniq -cd

worked fine

thx alot : )

Hi,

I'll suggest that it's the way your search parameters are configured, as a simple example see below;

/-> touch a.txt A.txt
 /-> find / -name "[a,A].txt" -printf '%h\n'
/home/e434069
/home/e434069
 /-> find / -name a.txt -o -name A.txt -printf '%h\n'
/home/e434069
/->

As you can see there are implications for how you configure the find with the or option.

Regards

Gull04

thank you very much. i will work on it =)

Hi,

You can change the find to;

find / -name "unavail.[W,w][A,a][V,v]" -printf '%#m %u %h\n'

A little bit messy maybe, I'm sure someone will know a better way of doing this.

Regards

Gull04

The reason for above single item output is "operator precedence". man find :

So above command reads find / -name a.txt -o \(-name A.txt -printf '%h\n' \) as (implied) -a precedes -o . Run

$ find  \( -name a.txt -o -name A.txt \) -printf '%f\n'
A.txt
a.txt

to overcome the problem.

Thanks Rudi,

I finally understood this now, so I'm at last able to use that find ... -prune thing now after 25 years(was a great mystery to me) :wink: