View Single Post
  #1   (View Single Post)  
Old 10th January 2017
gustaf gustaf is offline
Fdisk Soldier
 
Join Date: Dec 2016
Posts: 69
Default OpenBSD find command -- exclude dot directory

How do I tell the OpenBSD find(1) command to exclude the dot directory, but not its contents, from the list of directories and files it generates?

It seems like this should be possible with the not (!) operator, but after searching the Web, reading (and re-reading) the find(1) man page, and trying out numerous combinations of arguments and operators, I still have not found a solution.

Using -prune doesn't work for me in this situation; -prune will cause find to not descend into the pruned directory. If the pruned directory is the dot directory, the contents of the directory will be missing. Effectively, find will generate an empty list. This is obviously not the desired behavior.

I have found ways to exclude:
  1. individual files plus the dot directory, or
  2. individual files and named directories,
but not all three types (the dot directory, named directories, and individual files) in the same command.

I've written a sample script to demonstrate the problem because it's easier to see than on the command line.

If, for example, my home directory is the current directory and contains the following items:

./
../
.Xdefaults
.cache/
.profile
dirA/
dirA/dirB/
dirA/dirB/file1
dirC/
dirC/file2
file3.txt
file4.pdf

and I want the generated list to look like this:

.profile
dirA/
dirA/dirB
dirA/dirB/file1
file3.txt

I can run this script to test the find command -- the results are explained in the comments:

Code:
#!/bin/sh
# findtest

cd $HOME

# Method 1 works to exclude the dot directory and specified files; 
# contents of named directories are ignored by find and end up in the printed list.
METHOD1='( 
            ! -name .
            ! -path ./.Xdefaults
            ! -path ./.cache
            ! -path ./dirC
            ! -path ./file4.pdf
         )'
find . $METHOD1 -print | sort > method1.txt

# Method 2 works to exclude named directories and specified files;
# adding the dot directory to this command results in an empty list.
METHOD2='(
            -path ./.Xdefaults -or
            -path ./.cache -or
            -path ./dirC -or
            -path ./file4.pdf
         )'
find . $METHOD2 -prune -or -print | sort > method2.txt

#Method 3 should exclude the dot directory, named directories, and specified files.
## METHOD3=...
## find . $METHOD3...
Is this an impossible task for OpenBSD find or have I just not figured out the logic?

Any help would be appreciated.

Last edited by gustaf; 11th January 2017 at 09:22 PM. Reason: added link to find(1) man page; corrected comments and code for Method 1
Reply With Quote