Find *.tar files under all subdirectories

Hi there,

I'm new to shell scripting...
I've a situation like to find *.tar files under all subdirectories in "/home/abcd" and i used the below,

find /opt/lhapp ! -name "temp" | more

the above works fine.. Now don't need search few direcotries like "/home/abcd/aaaa", "/home/abcd/bbbb", "/home/abcd/cccc". So i need to exclude these directories. How??? any ideas???

Thanks in advance!
Vasanth.

Please search the forum, this question has been asked and answered for many times.

I tried with the post using "-path" option but I'm using AIX and it says -path is not supported. Any other way???

find  /home -type d \( -name aaaa -o -name bbbb -o -name cccc \)  \
  -prune -o -name '*.tar' -print

use the -prune option
you can also have look at -depth option

Hi Radoulov,

I tried with below,

find  /home -type d \( -name aaaa -o -name bbbb -o -name cccc \)  \  -prune -o -name '*.tar' -print 

But getting missing conjunction error:confused:

can you correct me ?

The third backslash in my post is used to escape a newline.
If you don't have embedded newlines, remove it:

find  /home -type d \( -name aaaa -o -name bbbb -o -name cccc \)  -prune -o -name '*.tar' -print 

Thanks Radoulov,

It works fine.. If i give only the directory.. for examaple here "abcd". But my situation is like "/abc/defh/abcd" means i just wanted to eliminate this complete path not all the abcd directory.

I tried giving the complete path but its not excluding ""/abc/defh/abcd" directory.

find  /home -type d \( -name /abc/defh/abcd -o -name bbbb -o -name cccc \) -prune -o -name '*.tar' -print

I tried with single & double qoutes as well its not working.
Any option i can exclude entire path ?

I don't understand what you mean,
could you give an example of the desired output?

Hi,

It work fine.. But it's not excluding when i use the entire path for example(/home/cccc) and it works when i give only the directory as cccc. I just wanted to exclude the entire directory path and I need xxxx to be searched when it is not in(/home/cccc). Below is code the used which was not excluding the /home/cccc.

find /home -type d \( -name /home/cccc -o -name -o -name aaaa \) -prune -o -name '*.tar' -print

But it still searching under(/home/cccc) where it should exclude but aaaa got completely excluded.

Thanks in advance!
Vasanth

---------- Post updated at 10:30 PM ---------- Previous update was at 10:03 PM ----------

Hi,

It work fine.. But it's not excluding when i use the entire path for example(/home/cccc) and it works when i give only the directory as cccc. I just wanted to exclude the entire directory path and I need xxxx to be searched when it is not in(/home/cccc). Below is code the used which was not excluding the /home/cccc.

find /home -type d \( -name /home/cccc -o -name -o -name aaaa \) -prune -o -name '*.tar' -print

But it still searching under(/home/cccc) where it should exclude but aaaa got completely excluded. Can anyone help with the above situation ???

Thanks in advance!
Vasanth

---------- Post updated at 11:50 PM ---------- Previous update was at 10:30 PM ----------

Plz see the above conversation.

Thanks,
Vasanth.

I still don't understand. Consider the following example:

zsh-4.3.10[sysadmin]% find /home/sysadmin/t/abcd -name '*.tar'
/home/sysadmin/t/abcd/aaaa/pippo.tar
/home/sysadmin/t/abcd/bbbb/pippo.tar
/home/sysadmin/t/abcd/cccc/pippo.tar
/home/sysadmin/t/abcd/dddd/pippo.tar
/home/sysadmin/t/abcd/pippo.tar

If have .tar files in:

/home/sysadmin/t/abcd
/home/sysadmin/t/abcd/aaaa
/home/sysadmin/t/abcd/bbbb
/home/sysadmin/t/abcd/cccc
/home/sysadmin/t/abcd/dddd

The following command skips any aaaa, bbbb and cccc directory under /home/sysadmin/t/abcd/:

zsh-4.3.10[sysadmin]% find  /home/sysadmin/t/abcd -type d \( -name aaaa -o -name bbbb -o -name cccc \)  -prune -o -name '*.tar' -print
/home/sysadmin/t/abcd/dddd/pippo.tar
/home/sysadmin/t/abcd/pippo.tar

I suppose that skipping, for example, /home/sysadmin/t/abcd/dddd/aaaa could be an issue for you, but your problem seems to be different, could you please clarify providing an example (files that should/shouldn't be included in the output)?

Hi Radoulov,
Exactly what you guessed below is right!
Thanks for your help Radoulov!

I used 2 separate find command for source and copybook :

/home/apps/**/source 

where it always searches in the test source/copybook.Sorry for the unwanted thought of mine :slight_smile:
Anyways below is my requirement. Hope you will understand. Thanks again.

My requirement :
my test environment path example: /home/apps/xx/source where xx is the test region.
my production path example : /home/apps/source

Now i need to find like :

 find /home/apps -name "1234"

The above will search for the 1234 under /home/apps.. here i need to skip the /home/apps/source which was production and i need to search on all test region. If i skip "source" directory in the below code it will skip both production and test regions source directory well where my requirement is it should not skip the test environment.

find  /home -type d \( -name /home/apps -o -name  -o -name xxxx \)  -prune -o -name '*.tar' -print

So to skip the production directory I used the complete path /home/apps which was ignored in my code. Hope you understand my situation now.

-Vasanth

You could try something like this:

perl -MFile::Find -le'
  BEGIN {
    $skip = {
      "/home/apps/source" => 1,
      };
    $pat = qr/\.tar$/;  
    }
  find { 
    wanted => sub {
      $File::Find::prune = 1 and return 
        if  exists $skip->{$File::Find::dir};   
      /$pat/o or return;
      print $File::Find::name;
      }
    }, shift    
  ' /home/apps

---------- Post updated at 08:20 PM ---------- Previous update was at 07:51 PM ----------

The code is easily extensible, you could skip more directories just by adding them to the hash skip in the BEGIN block:

$skip = {
      "/home/apps/source" => 1,
      "/dir/2"            => 1,
      "/dir/3/1"          => 1,
      };
1 Like

Hi, I think I have a similar problem.

find /storage/cas/extracts/ -name "usage_1_*" -type f -mtime +7

I want to limit the search to /storage/cas/extracts only.

Thanks in advance!

find /storage/cas/extracts/. ! -name . -prune -name 'usage_1_*' -type f -mtime +7

Or just use the maxdepth option if it's available:

find /storage/cas/extracts -maxdepth 1 -name 'usage_1_*' -type f -mtime +7
1 Like

wow thanks radoulov! it works!