首页 > 解决方案 > 如何使用 bash 脚本删除具有特定编号的图像?

问题描述

我的目标是编写一个脚本,它将删除所有以“_0”命名的 .jpg。

示例:foo_0.jpg 和 bar_0.jpg。

脚本的预期行为:删除所有带有“_0.jpg”的文件我尝试:

#!/bin/bash

find . -name _0.jpg -type f -delete

导致意外错误消息: FIND: Parameter format not correct

如果find "_0.jpg" . -name -type f -delete 直接从命令行在文件夹中使用,我会得到意想不到的结果:

Access denied - .
File not found - -NAME
File not found - -TYPE
File not found - F
File not found - -DELETE

更新:运行find ""*_0.jpg""确实找到了文件。

预期行为会删除 foo_0.jpg 和 bar_0.jpg。运行 bash -x (调试)运行find ""*_0.jpg"" -name f -delete或运行find ""*_0.jpg"" -exec rm "{}" 意外结果:

 + find foo_0.jpg bar_0.jpg . -name f -delete
FIND: Parameter format not correct
+ find '*_0' -exec rm '{find *_0}'
File not found - *_0
+ exit 0

更新 2:由于 Windows 问题(使用错误find),我安装了 findutilschoco install -y findutils添加了一个新 PATH 并向下移动system32。结果运行:find --help

default path is the current directory; default expression is -print
expression may consist of: operators, options, tests, and actions:

operators (decreasing precedence; -and is implicit where no others are given):
      ( EXPR )   ! EXPR   -not EXPR   EXPR1 -a EXPR2   EXPR1 -and EXPR2
      EXPR1 -o EXPR2   EXPR1 -or EXPR2   EXPR1 , EXPR2

positional options (always true): -daystart -follow -regextype

normal options (always true, specified before other expressions):
      -depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf
      --version -xdev -ignore_readdir_race -noignore_readdir_race

tests (N can be +N or -N or N): -amin N -anewer FILE -atime N -cmin N
      -cnewer FILE -ctime N -empty -false -fstype TYPE -gid N -group NAME
      -ilname PATTERN -iname PATTERN -inum N -iwholename PATTERN -iregex PATTERN
      -links N -lname PATTERN -mmin N -mtime N -name PATTERN -newer FILE
      -nouser -nogroup -path PATTERN -perm [+-]MODE -regex PATTERN
      -wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N
      -used N -user NAME -xtype [bcdpfls]

actions: -delete -print0 -printf FORMAT -fprintf FILE FORMAT -print
      -fprint0 FILE -fprint FILE -ls -fls FILE -prune -quit
      -exec COMMAND ; -exec COMMAND {} + -ok COMMAND ;
      -execdir COMMAND ; -execdir COMMAND {} + -okdir COMMAND ;

Report (and track progress on fixing) bugs via the findutils bug-reporting
page at http://savannah.gnu.org/ or, if you have no web access, by sending
email to <bug-findutils@gnu.org>.

使用:

标签: windowsbashfind

解决方案


看来您使用的是 MicrosoftFIND.EXE而不是可爱的 GNU/Linuxfind实用程序。赠品是错误消息的大写及其无用的性质 - “参数不正确”

您可以通过运行以下命令来确认这一点:

FIND /?

如果输出看起来像这样,那就是 Windows FIND.EXE

解决方案是安装find您自己findutilsChocolatey Software找到的 GNU 版本。


推荐阅读