我想从查找结果中排除一组用户,他们不属于同一个 unix 组,这不是最好的方法,对吗?
find . -maxdepth 1 -type d -name '*_pattern_*' ! -user user1 ! -user user2 ...
我可以将用户作为字符串或数组传递吗?也许用 awk ?
答案1
在 cshell 上,您可以拼凑find
命令来执行该作业,如下所示:
#!/bin/tcsh -f
# persona non grata
set png = ( \
user1 \
user2 \
user3 \
user4 \
)
# build dynamically a portion of the `find` command
set cmd = ( `printf '! -user %s\n' $png:q` )
# now fire the generated find command
find . -maxdepth 1 -type d -name '*_pattern_*' $cmd:q
答案2
除了不喜欢未转义的感叹号这一事实之外csh
,在命令行中,您的命令看起来不错并且无法做得更好
答案3
如果您乐意从脚本运行它/bin/sh
:
#!/bin/sh
# the users to avoid
set -- user1 user2 user3 user4
# create a list of
# -o -user "username" -o -user "username" etc.
for user do
set -- "$@" -o -user "$user"
shift
done
shift # remove that first "-o"
# use the created array in the call to find
find . -maxdepth 1 -type d -name '*_pattern_*' ! '(' "$@" ')'
或者,构建与! -user "username"
您使用的相同类型的列表:
#!/bin/sh
# the users to avoid
set -- user1 user2 user3 user4
# create a list of
# ! -user "username" ! -user "username" etc.
for user do
set -- "$@" ! -user "$user"
shift
done
# use the created array in the call to find
find . -maxdepth 1 -type d -name '*_pattern_*' "$@"