Hi can anybody help me with the following line;

find . \( -type d ! -name . -prune \) -o -type f -name "*Log*"

Basically it should find directories where files with "Log" in their name are present.

I have a structure like this:

/logs
  |
  |
  ----folder1
  |       |
  |       |
  |       ---App1LogDate.txt
  |
  ----folder2
  |      |
  |      |
  |      ---App2LogDate.txt
  |
  |
  |--App3LogDate.txt
  |
  |--App4LogDate.txt

So given that I will run this line within /logs directory I should get the following results:

.
./folder1
./folder2

3 directories in total.

link|improve this question
1  
did you mean to say -type d instead of -type f in the "or" portion? – glenn jackman Aug 18 '11 at 19:20
feedback

migrated from stackoverflow.com Aug 18 '11 at 23:25

This question came from our site for professional and enthusiast programmers.

4 Answers

up vote 1 down vote accepted

Slightly left-field, but based on the above description, the following should do exactly what you want:

find . -type f -name "*Log*" -print | sed -E 's/\/[^\/]+$//' | sort | uniq
link|improve this answer
thanks, works like magic! – Dima Aug 18 '11 at 21:34
ahh, we have ksh88 and it -E doesn't work, any advice? – Dima Aug 18 '11 at 21:49
Try find . -type f -name "*Log*" -print | awk 'BEGIN { FS="/"; OFS="/"; } { $NF = ""; print; }' | sort | uniq – beny23 Aug 18 '11 at 22:34
feedback
find . -name *.Log -print 

This will give full path of all the files whose name ends with Log .

link|improve this answer
I only need directories where such files are present – Dima Aug 18 '11 at 19:24
1  
find . -type f -name '*.Log' -print | sed 's,/[^/]*$,,' | sort -u – Keith Thompson Aug 18 '11 at 19:48
beauty ! Works like a charm . – ganguly.sarthak Aug 18 '11 at 19:52
feedback

find . ( -type d ! -name . -prune ) -o -type d -name "Log"

tmp$ ls
App3LogDate.txt  App4LogDate.txt  folder1  folder2
tmp$ ls folder*
folder1:
App1LogDate.txt

folder2:
App2LogDate.txt
tmp$ find . \( -type d ! -name . -prune \) -o -type d -name "*Log*"
./folder2
./folder1
tmp$
link|improve this answer
current directory is missing – Dima Aug 18 '11 at 19:35
feedback

If you have GNU find (as Linux and many other modern Unixes do), you can just use the awesome printf operator and do this:

find -type f -name '*Log*' -printf '%h\n'

You probably also want to pipe that through a sort -u (or through sort | uniq, as appropriate). Note that some commercial UNIX implementations will have that installed as gfind (or have a package that installs it as such).

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.