Friday, December 27, 2013

How to find and tar files older than x days on Linux and probably most other unix

So you need to search a single directory for all files older then x days, compress them and remove the originals?

Let's start by printing files in a directory that are older than let say 30 days:

find . -mtime +30 -print


Now here's how you find all files in a given directory that are older than 30 days, the tar them and then delete the originals in a single command:

tar -cf files_older_than_30_days.tar --remove-files `find . -mtime +30 -print`

Please notice that before find . -mtime +30 -print I used back tick ` and not an apostrophe ’.


Have you ever wondered how to enter a back tick on an iPhone or iPad?

No comments:

Post a Comment