我有一个包含许多对文件的目录。每个非文本文件都有一个文本描述伙伴。例如,目录如下所示:
a.jpg
a.jpg.text
b.ogv
b.ogv.text
cd ef.JpG
cd ef.JpG.text
我想确认没有松散的文本文件(某些内容的描述文件已被删除)。因此,我尝试执行以下操作:
find . -name '*.text' -exec if [ ! -f `basename -s .text {}` ]; then echo {}; fi \;
但是,现在我得到一个错误:
bash: syntax error near unexpected token `then'
你就不能使用if
内部的的-exec
行动find
命令,你不妨方式。这是由于以下原因:
-exec
动作需要一个命令。这是来自man find
(我终端中第697行的某个位置): -exec command ;
Execute command; true if 0 status is returned. All following
arguments to find are taken to be arguments to the command until
an argument consisting of `;' is encountered. The string `{}'
is replaced by the current file name being processed everywhere
it occurs in the arguments to the command, not just in arguments
where it is alone, as in some versions of find. Both of these
constructions might need to be escaped (with a `\') or quoted to
protect them from expansion by the shell. See the EXAMPLES sec‐
tion for examples of the use of the -exec option. The specified
command is run once for each matched file. The command is exe‐
cuted in the starting directory. There are unavoidable secu‐
rity problems surrounding use of the -exec action; you should
use the -execdir option instead.
-exec command {} +
This variant of the -exec action runs the specified command on
the selected files, but the command line is built by appending
each selected file name at the end; the total number of invoca‐
tions of the command will be much less than the number of
matched files. The command line is built in much the same way
that xargs builds its command lines. Only one instance of `{}'
is allowed within the command. The command is executed in the
starting directory.
if
是不是命令你垫认为,但外壳的关键字。请参阅type if
命令的输出。现在,来完成你想要什么,你不是真的需要使用if
里面的-exec
。只需在中进行测试-exec
,然后使用-print
(man find
有关更多信息,请参见):
find . -name '*.text' -exec $SHELL -c '[ ! -f ${1%.*} ]' $SHELL '{}' ';' -print
另一种方法是使用bash脚本,如下所示:
find . -name '*.text' -exec my_if {} \;
cat ~/bin/my_if
在我的情况下,给出以下输出:
#!/bin/bash
if [ ! -f $(basename -s .text "$1") ]; then echo "$1"; fi
最后,我认为所有使用bash的普通人都会使用:
for f in *.text; do if [ ! -f $(basename -s .text "$f") ]; then echo "$f"; fi; done
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句