How to exclude the list of full directory paths in the find command in Solaris

I have a very specific need to search for files and directories in Solaris using a script and should be able to exclude all directory paths from the path, since they contain potentially thousands of files that were not saved (and this is normal, because they are files hosted on other servers). I don’t even want to find a search in these directories, because it will hang on the server (cpu spiking up to 99% for a long time), so displaying search results in egrep to filter these directories is not an option.

I know I can do this to exclude one of the following directories by name:

find / -mount -local \( -type d -a \( -name dir1 -o -name dir2 -o dir3 \) \) -prune -o \( -nouser -o -nogroup \) -print

However, this will match dir1 and dir2 in any directory structure of any directories, which is not what I want at all.

I want to prevent searches from searching the following directories (as an example):

 /opt/dir1 /opt/dir2 /var/dir3/dir4 

And I still want it to detect files and directories not registered in the following directories:

 /opt/somedir/dir1 /var/dir2 /home/user1/dir1 

I tried using regex in the -name arguments, but since I only find the match "name" with the base name of what it finds, I cannot specify the path. Unfortunately, Solaris find does not support GNU search options like -wholename or -path, so I'm a little screwed up.

My goal would be to have a script with the following syntax:

script.sh "/path/to/dir1,/path/to/dir2,/path/to/dir3"

How can I do this using search and the standard sh script (/ bin / sh) in Solaris (5.8 and above)?

+4
source share
1 answer

You cannot map files in the full path using Solaris find , but you can map files in inode. So use ls -i to create a list of inodes to trim, and then call find . This assumes that there are not many directories that you want to trim to go through the command line limit.

 inode_matches=$(ls -bdi /opt/dir1 /opt/dir2 /var/dir3/dir4 | sed -e 's/ *\([0-9][0-9]*\) .*/-inum \1 -o/') find / -xdev \( $inode_matches -nouser -o -nogroup \) -prune -o -print 

An alternative approach would be to use a Perl or Python script and minimize your own directory traversal. Perl comes with a find2perl script that can get started with the File::Find module. In Python, see the walk function in the os.path module.

+2
source

All Articles