Sure, that this will work (as *expected* I mean) with filenames like " * " ?

Not sure if it will work with all 'pattern' filenames, but it does work with "*".

I never knew about the -delete option ACOSWT mentioned though...so that's pretty slick.

I would add one more option for correctness though, to both of those...it should probably be restricted to 'file' types only. I noticed find command finds the target directory as well, if it's within the time range...so it would probably be smart to restrict the command to files only, if that's what you want:

find /directory/path -mtime +180 -type f -delete

EDIT: Not sure about '*'...hard to test what impact that has on accidentally removing all files.

In this particular case you can use -delete (but please, make a print run first to verify you got your find conditions right!), for many other cases you can use -exec cmd ; or -exec cmd + (see the manpage).

If you ever need to pass on find output to xargs, the preferred method is

Code:

find ... -print0 | xargs -0 ...

as that way you have special characters, spaces in filenames, etc. handled properly.

This will misbehave in the presence of files with certain whitespace in the name. To avoid such problems, use only null terminated parsing. Use -print0 as your print predicate in find, and use read -d '' F in your read. The -print0 statement is a GNU find extension.