如何在Solaris上的find命令中排除完整目录路径的列表

如何在Solaris上的find命令中排除完整目录路径的列表

本文介绍了如何在Solaris上的find命令中排除完整目录路径的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常特殊的需求,使用脚本在Solaris中找到无主文件和目录,并且需要能够从查找中排除完整的目录路径,因为它们可能包含数千个无主文件(这是正常的,因为它们是文件托管在其他服务器上)。我甚至不想找到在这些目录中搜索,因为它会挂起服务器(CPU长达99%,很长一段时间),因此管道查找结果egrep过滤掉这些目录不是一个选项。 >

我知道我可以这样做,通过名称排除多个目录之一:
$ b

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



但是,这将匹配任何目录的目录结构中的dir1和dir2,这是不是我想要的。

我希望能够防止查找甚至搜索下面的目录(例如):

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

我仍然希望它能在以下目录中找到无主文件和目录:

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

我试过使用正则表达式在-name参数中,但是因为只找到匹配的名称与它所找到的基名称相匹配,所以我不能指定一个路径。不幸的是,Solaris的查找不支持GNU查找选项,如-wholename或-path,所以我有点拧。



我的目标是有一个脚本以下语法:
$ b $ p $ script.sh/ path / to / dir1,/ path / to / dir2,/ path / to / dir3



如何在Solaris(5.8或更高版本)上使用find和standard sh脚本(/ bin / sh)?

解决方案

无法通过完整路径将文件与,但可以通过inode匹配文件。因此,使用 ls -i 来生成一个要修剪的inode列表,然后调用 find 。这假定没有太多的目录你想修剪,你会超过命令行的长度限制。

  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

另一种方法是使用Perl或Python脚本,并滚动您自己的目录遍历。 Perl带有一个 find2perl 脚本,可以让你开始使用 File :: Find 模块。在Python中,请参阅 os.path 模块中的 walk 函数。


I have a very specific need to find unowned files and directories in Solaris using a script, and need to be able to exclude full directory paths from the find because they contain potentially thousands of unowned files (and it's normal because they are files hosted on other servers). I don't even want find to search in those directories as it will hang the server (cpu spiking to 99% for a long time), therefore piping the find results in egrep to filter out those directories is not an option.

I know I can do this to exclude one of more 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 anywhere in the directory structure of any directories, which is not what I want at all.

I want to be able to prevent find from even searching in the following directories (as an example):

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

And I still want it to find unowned files and directories in the following directories:

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

I have tried using regex in the -name arguments, but since find only matches 'name' against the basename of what it finds, I can't specify a path. Unfortunately, Solaris's find does not support GNU find options such as -wholename or -path, so I'm kind of screwed.

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

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

How could I do that using find and standard sh scripting (/bin/sh) on Solaris (5.8 and up)?

解决方案

You can't match files by full path with Solaris find, but you can match files by inode. So use ls -i to generate a list of inodes to prune, then call find. This assumes that there aren't so many directories you want to prune that you'd go over the command line length 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 roll your own directory traversal. Perl ships with a find2perl script that can get you started with the File::Find module. In Python, see the walk function in the os.path module.

这篇关于如何在Solaris上的find命令中排除完整目录路径的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 20:22