Ubuntu – Will this ‘find and delete’ command work with the asterisk even with quotes

command linedirectoryfiles

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.

Best Answer

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.