I want to delete regular files whose names end in .mp4. I tried using .mp4$ with find -name, but it did not match anything. What pattern should I use, and why?
Why doesn’t my find pattern match filenames ending in .mp4?
Answers
-name uses shell-style glob patterns, not regular expressions. Use find . -type f -name '*.mp4' -delete. The * matches any sequence of characters before the suffix, and the quoted pattern is passed to find instead of being expanded by your shell.
Reply to this answer
The dollar sign is the clue that you are thinking in regular expressions. It marks the end of a match in many regex flavors, but -name does not interpret it that way. For just this task, '*.mp4' is simpler than switching find to a regex option.
Reply to this answer
Before using -delete, I’d preview the matches: find . -type f -name '*.mp4' -print. Check that the list is exactly what you intend to remove, then run the same command with -delete. Also, the original '*.mp4*' pattern is broader than “ends with .mp4.”
Reply to this answer
Add your answer
Answers are reviewed before publication.
paperweight-iv:
So the * already means the filename has to end in .mp4?
zesty_unit:
Right. In a glob pattern, *.mp4 matches names with any prefix followed by .mp4 at the end. By contrast, *.mp4* would also match names with extra characters after that suffix.