Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

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

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

1  
did you mean to say -type d instead of -type f in the "or" portion? – glenn jackman Aug 18 '11 at 19:20
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
share|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
find . -name *.Log -print 

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

share|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

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$
share|improve this answer
    
current directory is missing – Dima Aug 18 '11 at 19:35

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).

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .