Delete files older than a certain number of days in Linux

Sometimes, it is a good practice to clean up files older than certian number of days. This is expecially true when it comes to deleting old backups or temporary files. This task can be done easily in Linux shell. For example, if we need to delete all files older than 14 days in a folder named /srv/my-site/backups, we can do this:

# find /srv/my-site/backups -maxdepth 1 -type f -mtime +14 -delete

Pay attention that this command removes only the files on root directory, not the files found in sub-directories as specified by -maxdepth 1.