Find command not deleting anything when -delete is used

2

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.

TheSpawnMan

Posted 2016-08-30T20:33:38.687

Reputation: 23

1I think you should use find "$convfold" … in case there is a space in the folder path. It has nothing to do with the problem, still it's a good general practice. – Kamil Maciorowski – 2016-08-30T22:12:41.243

Hint: debug find commands, not by removing things like -delete, but by replacing them with -print. – G-Man Says 'Reinstate Monica' – 2016-08-31T01:36:12.977

Answers

2

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 that find . -name afile -o -name bfile -print will never print afile.

John1024

Posted 2016-08-30T20:33:38.687

Reputation: 13 893