我有一个包含多对文件的目录。每个非文本文件都有一个文本描述伙伴。例如,目录可能如下所示:
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'
答案1
您无法按照自己希望的方式在命令操作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
是不是您可能认为是命令,但shell 关键字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
答案2
if
以下是在子句中使用 in 的未优化方法exec
:
find . -name '*.text' -exec sh -c \
'if [ ! -f "$(dirname "$1")/$(basename "$1" .text)" ]; then echo == $1; fi' sh {} \;
这里有一个更好的方法,它不需要为每个文件启动一个 shell:
find . -name '*.text' -exec sh -c \
'for i do if [ ! -f "${i%.text}" ]; then echo == $i; fi;done' sh {} +