0

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.

1
  • 1
    did you mean to say -type d instead of -type f in the "or" portion? Aug 18, 2011 at 19:20

4 Answers 4

1

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
2
  • ahh, we have ksh88 and it -E doesn't work, any advice?
    – Dima
    Aug 18, 2011 at 21:49
  • Try find . -type f -name "*Log*" -print | awk 'BEGIN { FS="/"; OFS="/"; } { $NF = ""; print; }' | sort | uniq
    – beny23
    Aug 18, 2011 at 22:34
0
find . -name *.Log -print 

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

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

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$
1
  • current directory is missing
    – Dima
    Aug 18, 2011 at 19:35
0

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

You must log in to answer this question.

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