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

use*_*368 6 find busybox

如何查找和删除所有 .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
Run Code Online (Sandbox Code Playgroud)

mur*_*uru 8

您似乎在使用 Busybox find,而不是GNUfind。虽然Busyboxfind应该支持-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
Run Code Online (Sandbox Code Playgroud)

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

  • ... Ubuntu busybox 版本*确实* 似乎支持`-print0` 所以也许使用`-print0 | xargs -0 rm` 比普通的 `xargs` 更可取 (3认同)

Rin*_*ind 5

find . -type f -name "*.jpg" -exec rm -vf {} +
Run Code Online (Sandbox Code Playgroud)

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

请注意:您不应该以 root 身份执行此操作。命令中的错误可能会杀死您的系统,并且在不使用 root 时会出现权限错误。

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

如果你做一个

find . -type f -name "*.jpg" | more 
Run Code Online (Sandbox Code Playgroud)

您将获得将要删除的文件列表(| more它将以页面形式显示结果)。


find . type f -name "*.jpg" -print0 | xargs -0 rm
Run Code Online (Sandbox Code Playgroud)

也有效。相同的原则:结果通过管道传送到 xargs 命令,该命令为找到的每个文件执行 rm。