如何在 Solaris 上的 find 命令中排除完整目录路径列表 [英] How to exclude a list of full directory paths in find command on Solaris

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

问题描述

我非常需要使用脚本在 Solaris 中查找无主文件和目录,并且需要能够从查找中排除完整目录路径,因为它们可能包含数千个无主文件(这很正常,因为它们是文件托管在其他服务器上).我什至不想 find 在这些目录中搜索,因为它会挂起服务器(cpu 长时间飙升至 99%),因此在 egrep 中通过管道将 find 结果过滤掉这些目录不是一种选择.

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:

查找/-mount -local ( -type d -a ( -name dir1 -o -name dir2 -o dir3 ) ) -prune -o ( -nouser -o -nogroup )-打印

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

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

我希望能够阻止 find 甚至在以下目录中搜索(例如):

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

我尝试在 -name 参数中使用正则表达式,但由于 find 仅将名称"与所找到的基本名称匹配,因此我无法指定路径.不幸的是,Solaris 的 find 不支持 GNU find 选项,例如 -wholename 或 -path,所以我有点搞砸了.

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"

如何在 Solaris(5.8 及更高版本)上使用 find 和标准 sh 脚本 (/bin/sh) 来做到这一点?

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

推荐答案

你不能通过完整路径匹配文件 Solaris find,但是可以通过inode来匹配文件.因此,使用 ls -i 生成要修剪的 inode 列表,然后调用 find.这假设您要修剪的目录数量不会超过命令行长度限制.

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

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

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 命令中排除完整目录路径列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆