Listing files older than 2 months

A script or command to list files older than 2 months in a specified directory and remove it.

Using the find command is one way - check the man page as there are examples.

Just be careful about removing files older than 2 months as a system files are normally older than two months and could also be removed.

find ./ -name "*" -ctime +60 -exec rm {} \;
You can use ctime, atime or mtime

(Important - don't get the last part wrong as very bad things can happen!!! Try it some time on a test system.)

You should put some sort of qualifier in the -name option, like -name ".logfile", depending on the naming scheme you're going for. If there isn't one, you can leave the -name "" off altogether, as it will ignore any files beginning with "."... Unless that's what you're going for. It's also a good idea to simply move all the files to their own directory first, then examine them to make sure you aren't getting rid of anything important before removing them.

I will create one file with timestamp two months back and
search for newer files.

eg.

#touch -t 0112310000 /xyz
#find / -newer /xyz -print -exec ls -ls {} \;

Refer manual pages of touch and find for mor info

Hope it helps

Regards

Sharad