2

I want to run a command similar to this where AVI files older than 7 days will be deleted:

find "/home/user/videos folder/*.avi" -mtime +7 -exec rm -f

But as you can see, the videos folder has a space so I had to put that path in quotes. But, will the asterisk still do its job to find all AVI files, or will Linux now think the asterisk is the actual filename now?

Let's just assume I didn't want to remove the space in the path by renaming the folder.

1 Answer 1

3

You need to reformat your command a bit for it to work.

Try this:

find "/home/user/videos folder" -name "*.avi" -mtime +7 -exec rm -f {} \;

Or, in GNU find, to achieve the same thing without -exec:

find "/home/user/videos folder" -name "*.avi" -mtime +7 -delete

The first argument to find is the folder to look in - you shouldn't try and put your entire filter in here, just the starting point. Then use -name to tell it what pattern to look for. You can try this without the -exec or -delete first until you are satisified, then you won't do any damage by trying it.

Enclosing an argument in quotes basically stops the shell from messing with it. If you use -name *.avi, then the argument that find will see could be anything, because the shell will parse it (and replace it with the contents of the current directory that end in .avi) before calling find. This is usually bad and will lead to unexpected results. find knows how to parse * itself.

2
  • 1
    You missed the final {} \;. Also, you could use -delete with GNU find, instead of -exec rm {} \;.
    – enzotib
    Dec 10, 2011 at 15:49
  • Thank you very much! That has answered my question exactly. This works for me on Ubuntu Server: find "/home/user/videos folder" -name "*.avi" -mtime +7 -exec rm -f {} \;
    – nLinked
    Dec 10, 2011 at 16:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .