AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 573385
Accepted
v010dya
v010dya
Asked: 2015-01-14 10:05:36 +0800 CST2015-01-14 10:05:36 +0800 CST 2015-01-14 10:05:36 +0800 CST

如何在“find”的“-exec”属性中使用“if”?

  • 772

我有一个包含多对文件的目录。每个非文本文件都有一个文本描述伙伴。例如一个目录可以是这样的:

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'
command-line
  • 2 2 个回答
  • 4917 Views

2 个回答

  • Voted
  1. Best Answer
    Radu Rădeanu
    2015-01-15T12:52:30+08:002015-01-15T12:52:30+08:00

    您只是不能以您希望的方式在命令操作if内部使用。这是因为以下原因:-execfind

    • 该-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命令的输出。

    现在,要完成你想要的,你真的不需要使用ifinside of -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
    
    • 7
  2. jlliagre
    2015-01-14T10:11:52+08:002015-01-14T10:11:52+08:00

    if这是在exec子句中使用 an 的非优化方式:

    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 {} +
    
    • 3

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve