6

How can I find and delete all .jpg? -delete doesn't seem to work...

[/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
2

3 Answers 3

8

You seem to be using Busybox find, not GNU find. While Busybox find is supposed to support -delete, the version with Ubuntu doesn't seem to.

$ 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

I don't know why you are using Busybox find, but try using /usr/bin/find instead. Or -exec rm {} + (no support for -execdir either) or the xargs solution that Rinzwind suggests.

5
  • Hmm I did not even think about checking busybox. I just thought find does not have a "delete" option :P
    – Rinzwind
    Feb 8, 2015 at 12:56
  • 3
    ... the Ubuntu busybox version does seem to support -print0 so perhaps using -print0 | xargs -0 rm would be preferable to plain xargs Feb 8, 2015 at 13:01
  • @steeldriver Indeed Busybox xargs does seem to support -0. I'll edit Rinzwind's answer.
    – muru
    Feb 8, 2015 at 13:02
  • 1
    I didn't think about that either... duh. I installed Ubuntu so I could view the hidden files on my NAS that windows won't view (i.e. directories created by QNAP (NAS manufacturer) that I want to delete) such as "@__thumb." I don't know why I assumed that the the same command would work when I SSH'd into a different version of Linux (QNAP NAS is running Busybox). Feb 8, 2015 at 16:05
  • @user1543368 Your assumption is reasonable, as the Busybox site shows -delete is supported. The question then is why Ubuntu's build of Busybox doesn't. Perhaps a bug, a skipped feature or an outdated version.
    – muru
    Feb 8, 2015 at 16:12
5
find . -type f -name "*.jpg" -exec rm -vf {} +

This will remove "files" (and not "directories") ending on ".jpg".

Mind though: you should NOT do this as root. A mistake in the command can kill your system and when not using root would error out on a permissions error.

There is no way back from this unless you created a backup.

If you do a

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

you will get a list of the files about to be deleted (the | more will show you the result in pages).


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

works too. Same principle: the results are piped to the xargs command that does an rm for every file found.

6
  • What's wrong with rm *.jpg ? Feb 8, 2015 at 12:45
  • 1
    @AvinashRaj recursion (or lack thereof).
    – muru
    Feb 8, 2015 at 12:49
  • 1
    you can get a "argument list too long" ;)
    – Rinzwind
    Feb 8, 2015 at 12:49
  • 1
    @AvinashRaj bash actually expands the asterisk to every matching file, producing a very long command line. Happens with rm but also with cp and ls.
    – Rinzwind
    Feb 8, 2015 at 12:51
  • 1
    @AvinashRaj the -r switch makes no difference. What we need here is to find files ending in .jpg recursively. The -r flag won't help.
    – terdon
    Feb 8, 2015 at 15:17
0

For really old versions of BusyBox (as sadly used by my QNAP NAS), the suggestions using -print0, -exec and -delete won't work.

I had to resort to building a list and using a loop:

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

The command being split into two steps does allow for one to check if the found files are correct before deleting them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.