Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Auto move folder
#1
I have a need to watch a folder and when a new folder has been added and is 2 days old move it to a different folder. I would like this to be ALL automatic and to always be running.

Sorry if this is simple but, I am just getting started with Linux.
Reply
#2
I think this can be useful for you:
NB: set DIRNAME variable to the directory you're watching, TARGET is the target directory to move to.

Code:
    # We go through the directories that have been modified more than 1 days ago and move them
    while IFS=  read -r -d $'\0'; do
        mv "$REPLY" "$TARGET"
    done < <(find "$DIRNAME" -maxdepth 1 -mindepth 1 -type d -ctime +1 -printf "%p\0")
    # https://stackoverflow.com/questions/16085958/find-the-files-that-have-been-changed-in-last-24-hours
    # https://stackoverflow.com/questions/23356779/how-can-i-store-the-find-command-results-as-an-array-in-bash#23357277
    
    # read
    #  -r: backslash characters treated as characters
    #  -d $'\0': the input is null-separated
    #  $REPLY: the default variable the read command reads into
    # find
    #  -maxdepth 1 -mindepth 1: only the directories right inside backup
    #  -type d: only directories
    #  -ctime +1: only files modified in the more than 1 days, at least 2 (positive number)
    #  -printf "%p\0": lists the filenames with zero termination

The problem with this is that ctime returns the modification of the newest file in the directory, so if the files inside the directory are changing that can have an effect.

Maybe someone else have better solutions.
My top 10 reasons to still use Arch after 2 months on my main PC at home.
Reply
#3
(11-19-2019, 02:01 AM)TarsolyGer Wrote: I think this can be useful for you:
NB: set DIRNAME variable to the directory you're watching, TARGET is the target directory to move to.

Code:
    # We go through the directories that have been modified more than 1 days ago and move them
    while IFS=  read -r -d $'\0'; do
        mv "$REPLY" "$TARGET"
    done < <(find "$DIRNAME" -maxdepth 1 -mindepth 1 -type d -ctime +1 -printf "%p\0")
    # https://stackoverflow.com/questions/16085958/find-the-files-that-have-been-changed-in-last-24-hours
    # https://stackoverflow.com/questions/23356779/how-can-i-store-the-find-command-results-as-an-array-in-bash#23357277
    
    # read
    #  -r: backslash characters treated as characters
    #  -d $'\0': the input is null-separated
    #  $REPLY: the default variable the read command reads into
    # find
    #  -maxdepth 1 -mindepth 1: only the directories right inside backup
    #  -type d: only directories
    #  -ctime +1: only files modified in the more than 1 days, at least 2 (positive number)
    #  -printf "%p\0": lists the filenames with zero termination

The problem with this is that ctime returns the modification of the newest file in the directory, so if the files inside the directory are changing that can have an effect.

Maybe someone else have better solutions.

Thank you. The files will not be changing inside the folder to be moved. I will give this a try.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)