Find command not deleting anything when -delete is used
I'm using this script:
find $convfold -name \*.mkv -o -name \*.avi -o -name \*.mov -o -name \*.wmv -o -name \*.m4p -o -name \*.m4v -o -name \*.mpg -o -name \*.mp2 -o -name \*.mpeg -o -name \*.mpe -o -name \*.mpv -o -name \*.m2v -o -name *.m4v -delete
where $convfold
is a variable for the folder in which I'm deleting the video.
If I remove the -delete
from the code it displays the files it should be deleting but when I have it in the code I get no return errors nor exceptions.
Example:
flynn@Tron:~/FilebotHandbrake/testconverting$ find -name \*.mkv -o -name \*.mp4 -o -name \*.avi
./TV Shows/Mr. Robot/Season 02/Mr. Robot - S02E02 - eps2.0_unm4sk-pt2.tc.mkv
./TV Shows/Mr. Robot/Season 02/Mr. Robot - S02E01 - eps2.0_unm4sk-pt1.tc.mkv
Any help or suggestions would be appreciated.
Solution 1:
The issue is that, with find
, logical-and binds tighter than logical-or. Observe:
$ ls
file1 file2 file3 file4
$ find . -name \*1 -o -name \*2 -exec echo {} \;
./file2
In the above, -name \*2
and -exec echo {} \;
are bound together with an implied logical-and. This logical-and binds stronger than the logical-or, -o
, that connects the first -name
to the second.
To avoid that, you need to apply parens for grouping:
$ find . \( -name \*1 -o -name \*2 \) -exec echo {} \;
./file1
./file2
Or, for your complete command:
find $convfold \( -name \*.mkv -o -name \*.avi -o -name \*.mov -o -name \*.wmv -o -name \*.m4p -o -name \*.m4v -o -name \*.mpg -o -name \*.mp2 -o -name \*.mpeg -o -name \*.mpe -o -name \*.mpv -o -name \*.m2v -o -name *.m4v \) -delete
Documentation
From man find
:
Please note that
-a
when specified implicitly (for example by two tests appearing without an explicit operator between them) or explicitly has higher precedence than-o
. This means thatfind . -name afile -o -name bfile -print
will never print afile.