I would like to archive all of the files older than Xdays. the files are in /home/user1/ and i would like to move the archive to /home/user1/folder_backup/
i am running ubuntu server 12
Your friend is possibly logrotate
. Create a my_config_file
config file with directives like:
/home/user1/ {
olddir /home/user1/folder_backup
}
Then call logrotate my_config_file
in your crontab.
You should probably grab a coffee and do a man logrotate
. It can deal with compression, rotation and the kitchen sink. It really is the standard for that kind of task.
If you really need to have precise control over the number of days, say 10, you can go with a naive find /home/user1 -mtime +10 -exec mv {} /home/user1/folder_backup/ \;
but that won't deal with all associated issues that will popup (file clobbering, rotation and so on).
If you are to write your own script, common practise is to get each file's last modification time (in unix time), put that info in an array (modtime - filename) and start moving the ones you want to archive.
For example if time now is 1372174701 and you want to archive files older than a week, you will move all files that their modification time satisfies the following condition:
modification time < 1372174701 - 604800