Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.unix.shell > #21466

Re: finding directories based on files within?

From Ed Morton <mortonspam@gmail.com>
Newsgroups comp.unix.shell
Subject Re: finding directories based on files within?
Date 2021-05-13 07:20 -0500
Organization A noiseless patient Spider
Message-ID <s7j5i6$7up$1@dont-email.me> (permalink)
References <eli$2105101745@qaz.wtf>

Show all headers | View raw


On 5/10/2021 5:13 PM, Eli the Bearded wrote:
> I currently have a simple shell script along the lines of:
> 
> 	find "$1" -type d \
> 		-exec test -f {}/SENTINEL-1 \; \
> 		-exec test -f {}/SENTINEL-2 \; \
> 		-print
> 
> The idea is to list directories that satisfy the condition of having two
> exactly named files within them.

FWIW I'd just do something like (untested):

find "$1" -type f |
awk -F'/' '
     { dir=$(NF-1) }
     dir != prev { prt(); prev=dir}
     { files[$NF] }
     END { prt() }
     function prt() {
         if ( ("SENTINEL-1" in files) && ("SENTINEL-2" in files) ) {
             print prev
         }
         delete files
     }
'

so the `find` just prints a list of paths to files, awk is only called 
once and will be extremely efficient so it scales well, it's only using 
mandatory POSIX tools so it'll work on any Unix box, and you can 
trivially write whatever condition you like in the `prt()` function or 
write a generalized prt() function such that you can pass a variable to 
specify different types of comparison and/or a list of files to test for.

Regards

	Ed.

  This code works, but it's not exactly
> fast (two execs for each matched directory), and it doesn't scale well:
> three files ;or has one but NOT the other, has either one of them, has
> sub-directories of particular names etc, all require rewriting the whole
> command. The way I use this tool, it needs to examine both immediate
> subdirectories of "$1" and one level further.
> 
> Before I write myself a custom tool, is there a better way of doing this
> sort of thing? In my case, the directories are all small, with a median
> of under ten items, so spped of readdir() versus stat() isn't a big
> concern (and I'd be comparing the speed to find).
> 
> I already have a Perl script that might be a candidate for replacement
> if there is a better way. That script does not need to recurse, so it
> doesn't use find (or the Perl equivilent module). It takes a list of
> directories and prints the ones that only have files matching a couple of
> globs, specifically (case insenitive) *.txt and *.html. Any directory
> that has a .jpg or other non-txt, non-html suffix, an or a file with no
> suffix, is not printed.
> 
> For those wondering, the commonality is both of these uses are for
> automatic clean-up of directories created by other programs. Directories
> that match are safe to delete without further inspection (subject to
> race conditions).
> 
> Elijah
> ------
> has this set of data been processed? and backed up?
> 

Back to comp.unix.shell | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

finding directories based on files within? Eli the Bearded <*@eli.users.panix.com> - 2021-05-10 22:13 +0000
  Re: finding directories based on files within? Spiros Bousbouras <spibou@gmail.com> - 2021-05-13 02:38 +0000
  Re: finding directories based on files within? Martijn Dekker <martijn@inlv.demon.nl> - 2021-05-13 04:50 +0200
  Re: finding directories based on files within? Ed Morton <mortonspam@gmail.com> - 2021-05-13 07:20 -0500
    Re: finding directories based on files within? Ed Morton <mortonspam@gmail.com> - 2021-05-13 09:04 -0500
  Re: finding directories based on files within? Axel Reichert <mail@axel-reichert.de> - 2021-05-13 14:56 +0200
    Re: finding directories based on files within? Ed Morton <mortonspam@gmail.com> - 2021-05-13 08:44 -0500
      Re: finding directories based on files within? Axel Reichert <mail@axel-reichert.de> - 2021-05-13 21:09 +0200
  Re: finding directories based on files within? merlyn@stonehenge.com (Randal L. Schwartz) - 2021-05-13 07:31 -0700
    Re: finding directories based on files within? Eli the Bearded <*@eli.users.panix.com> - 2021-05-13 17:11 +0000
    Re: finding directories based on files within? Christian Weisgerber <naddy@mips.inka.de> - 2021-05-13 22:04 +0000
      Re: finding directories based on files within? merlyn@stonehenge.com (Randal L. Schwartz) - 2021-05-15 06:45 -0700
      find2perl (Was: finding directories based on files within?) gazelle@shell.xmission.com (Kenny McCormack) - 2021-05-15 17:32 +0000
        Re: find2perl Dan Espen <dan1espen@gmail.com> - 2021-05-15 13:46 -0400
        Re: find2perl (Was: finding directories based on files within?) Christian Weisgerber <naddy@mips.inka.de> - 2021-05-15 20:17 +0000
          Re: find2perl (Was: finding directories based on files within?) gazelle@shell.xmission.com (Kenny McCormack) - 2021-05-16 13:06 +0000
        Re: find2perl merlyn@stonehenge.com (Randal L. Schwartz) - 2021-05-15 15:34 -0700

csiph-web