使用 busybox 的 find 递归删除一些文件

使用 busybox 的 find 递归删除一些文件

我如何才能找到并删除所有 .jpg?-删除似乎不起作用......

[/share/Multimedia/Music] # find . -name '*.jpg' -delete
BusyBox v1.01 (2015.01.25-18:47+0000) multi-call binary

Usage: find [PATH...] [EXPRESSION]

Search for files in a directory hierarchy.  The default PATH is
the current directory; default EXPRESSION is '-print'

EXPRESSION may consist of:
    -follow     Dereference symbolic links.
    -name PATTERN   File name (leading directories removed) matches PATTERN.
    -print      Print (default and assumed).
    -type X     Filetype matches X (where X is one of: f,d,l,b,c,...)
    -perm PERMS Permissions match any of (+NNN); all of (-NNN); or exactly (NNN)
    -mtime TIME Modified time is greater than (+N); less than (-N); or exactly (N) days

答案1

你似乎正在使用 Busybox find,而不是GNUfind。 尽管忙碌箱find应该支持-delete,Ubuntu的版本好像不支持。

$ busybox find --help
BusyBox v1.21.1 (Ubuntu 1:1.21.0-1ubuntu1) multi-call binary.

Usage: find [PATH]... [OPTIONS] [ACTIONS]

Search for files and perform actions on them.
First failed action stops processing of current file.
Defaults: PATH is current directory, action is '-print'

    -follow     Follow symlinks
    -xdev       Don't descend directories on other filesystems
    -maxdepth N Descend at most N levels. -maxdepth 0 applies
            actions to command line arguments only
    -mindepth N Don't act on first N levels
    -depth      Act on directory *after* traversing it

Actions:
    ( ACTIONS ) Group actions for -o / -a
    ! ACT       Invert ACT's success/failure
    ACT1 [-a] ACT2  If ACT1 fails, stop, else do ACT2
    ACT1 -o ACT2    If ACT1 succeeds, stop, else do ACT2
            Note: -a has higher priority than -o
    -name PATTERN   Match file name (w/o directory name) to PATTERN
    -iname PATTERN  Case insensitive -name
    -path PATTERN   Match path to PATTERN
    -ipath PATTERN  Case insensitive -path
    -regex PATTERN  Match path to regex PATTERN
    -type X     File type is X (one of: f,d,l,b,c,...)
    -perm MASK  At least one mask bit (+MASK), all bits (-MASK),
            or exactly MASK bits are set in file's mode
    -mtime DAYS mtime is greater than (+N), less than (-N),
            or exactly N days in the past
    -mmin MINS  mtime is greater than (+N), less than (-N),
            or exactly N minutes in the past
    -newer FILE mtime is more recent than FILE's
    -inum N     File has inode number N
    -user NAME/ID   File is owned by given user
    -group NAME/ID  File is owned by given group
    -size N[bck]    File size is N (c:bytes,k:kbytes,b:512 bytes(def.))
            +/-N: file size is bigger/smaller than N
    -links N    Number of links is greater than (+N), less than (-N),
            or exactly N
    -prune      If current file is directory, don't descend into it
If none of the following actions is specified, -print is assumed
    -print      Print file name
    -print0     Print file name, NUL terminated
    -exec CMD ARG ; Run CMD with all instances of {} replaced by
            file name. Fails if CMD exits with nonzero

我不知道您为什么要使用 Busybox find,但请尝试使用/usr/bin/find。或者-exec rm {} +(不支持-execdir任何一种)或xargsRinzwind 建议的解决方案。

答案2

find . -type f -name "*.jpg" -exec rm -vf {} +

这将删除以“.jpg”结尾的“文件”(而不是“目录”)。

但请注意:您不应以 root 身份执行此操作。命令中的错误可能会导致您的系统崩溃,如果不使用 root 身份,则会因权限错误而出错。

除非您创建了备份,否则无法恢复。

如果你做了

find . -type f -name "*.jpg" | more 

您将获得即将被删除的文件的列表(将| more在页面中显示结果)。


find . type f -name "*.jpg" -print0 | xargs -0 rm

也可以。原理相同:结果通过管道传送到 xargs 命令,该命令对找到的每个文件执行 rm。

答案3

-print0对于非常旧版本的 BusyBox(遗憾的是我的 QNAP NAS 使用的是该版本),使用、-exec和 的建议-delete不起作用。

我不得不建立一个列表并使用循环:

IFS=$'\n' aFiles=( $(find . -name '*.jpg') );
for sFile in "${aFiles[@]}"; do rm "${sFile}"; done

该命令分为两个步骤,允许人们在删除文件之前检查找到的文件是否正确。

相关内容