2

I have a folder called Research (which also includes sub-folders).

I wish to regularly move files from Research that are greater than 6 months old, to a different folder called Research_Archive, which is on a different drive. Files >6 months will be deleted from Research.
The folder Research_Archive will reproduce the sub-folder structure of Research.

The script would run periodically, thus Research_Archive will be updated.

I saw a script like this:

find . -type f -mtime +5 -print0 | while IFS= read -r -d '' file; do
    dir="${file%/*}"
    mkdir -p ../rootarchive/"$dir"
    mv "$file" ../rootarchive/"$file"
done

But as a newbie to scripting, I'm totally confused by its relative paths. For example, my source path is: /home/me/Documents/Research/ and destination would be: /media/me/drive21/Documents/Research_Archive/.

And I'm not sure if running the above script more than once, would recreate the Research_Archive folder again. And how would permissions be set on the Research_Archive folder (when I tried testing, I was locked out from viewing view file manger of the created folder).

Any help/specific examples will be appreciated.

  • If run in your home directory it shouldn't lock you out, and it will produce an error after the first use since the folder will already be created – George Udosen Sep 12 '17 at 14:55
2

This script should help in this regards:

#!/bin/bash

from=$1
destination=$2
if [[ -d "$from" && -s "$from" ]]; then

        # extract the name of the sub directory
        # to be create
        src=$(sed -r 's/.*\/(Research).*/\1/' <<< "$from")

        if [[ -d /media/me/drive21/Documents/Research_Archive/ ]]; then

                destination=/media/me/drive21/Documents/Research_Archive/

                if [[ ! -d "$destination/$src" ]]; then                       

                        mkdir "$destination/$src"
                        newdes="$destination/$src"
                        find "$from" -type f -mtime +5 -print0 -exec mv {} "$newdes" \;
                else
                        cd "$destination"
                        find "$from" -type f -mtime +5 -print0 -exec mv {} "$newdes" \;

                fi

         else
                exit 1
         fi
else
        exit 1
fi

Usage:

chmod +x mv.sh
./mv.sh /home/me/Documents/Research/  /media/me/drive21/Documents/Research_Archive

Using a udev rule to automate the moving of the files to external storage:

  1. Alter the code to work with this setup:
    #!/bin/bash

    from=$1
    destination=$2
    if [[ -d "$from" && -s "$from" ]]; then

            # extract the name of the sub directory
            # to be create
            src=$(sed -r 's/.*\/(arch).*/\1/' <<< "$from")

            if [[ -d "/media/me/drive21/Documents/Research_Archive/" ]];then


                 destination="/media/me/drive21/Documents/Research_Archive/"

                    if [[ ! -d "$destination/$src" ]]; then

                            cd "$destination"
                            mkdir "$src"

                            find "$from" -type f -mtime -5 -exec mv {} "$src" \;
                    else
                            cd "$destination"
                            find "$from" -type f -mtime -5 -exec mv {} "$src" \;

                    fi

            else
                    exit 1
            fi
    else
            exit 1
    fi
  1. Set up mount device event:

    • Create a .rule file:

      KERNEL=="sd*", SUBSYSTEM=="block", ATTR{removable}=="1", ATTRS{serial}=="07083395757D2761", MODE="0777", RUN+="/home/me/mv.sh /home/me/Documents/Research/ /media/me/drive21/Documents/Research_Archive"
      
    • call this 90-move.rules and place in /etc/udev/rules.d directory

    • Use udevadm info -a -n /dev/sd* to get the needed information about your external storage device.

      enter image description here

  2. Now all need do is plugin your device and once it's identified by the rule set, the files should be moved. Please tweak the code to the way it will work best for you.

Source:

http://www.reactivated.net/writing_udev_rules.html

https://docs.oracle.com/cd/E37670_01/E41138/html/ch07s03.html

https://wiki.debian.org/udev

https://www.linux.com/news/udev-introduction-device-management-modern-linux-system

  • Thanks. Moved the files fine. But did not recreate the sub-folders of Research in Research_Archive. It just moved files into the main folder. – snowcrash101 Sep 12 '17 at 16:26
  • Also, how would I call this script, if wanted to automate at boot or run as a cron? Thanks – snowcrash101 Sep 12 '17 at 16:36
  • Thanks for working on this. But when testing your updated script using './mv.sh /home/me/Documents/Research/ /media/me/drive21/Documents/Research_Archive', it still does not create the sub-folder of Research in Research_Archive. It still moves all files in the main Research_Archivefolder, which is now located in a series of other folders: home/me/Documents/.../Research_Archive – snowcrash101 Sep 13 '17 at 15:24
  • Is this with the run at boot setup? – George Udosen Sep 13 '17 at 15:28
  • No, just ran the command /mv.sh... in terminal – snowcrash101 Sep 13 '17 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.