Search code examples
basharchive

Archiving file in another disk linux


How do I archive a certain file in another hard disk on linux?

for example I want to archive the files that exist my pwd and are older than two years, I wrote in my bash script:

if [ $file -mtime +730 ]; then

And now I don't know which command to add in order to archive the file in /dev/sdb1 for example.

Thanks for any help in advance!


Solution

  • I want to archive the files that exist my pwd and are older than two years

    Consist of two steps:

    • finding all files older then two years
    • archiving files from step one

    To "find" files use find. The program find takes the argument -mtime. Command [ does not take -mtime argument.

    "Archiving" I believe is just cp. To archive to a disc, you first have to format that disc to store files, and then you can copy files into the filesystem created on that disc.

    Overall, it would something along:

     # First format and mount
     # Note - it will erase all the files
     sudo mkfs /dev/sdb1
     sudo mount /dev/sdb1 /mnt/somedir
     # then copy
     find . -type f -mtime +730 -print0 |
     xargs -0 cp -a -t /mnt/somedir --parents
    

    This uses GNU tools and the -t and --parents options specific to GNU cp - see manual. But you should most probably use rsync anyway and research rdiff-backup and other better tools to do "archiving".