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
#4
(11-19-2019, 02:43 PM)bracketk Wrote:
(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. 
Perfect.....just what I wanted.
Reply
#5
Is this applicable for moving a large number of folders automatically?
Reply
#6
Forgive me if this seems ignorant (and probably is) but why would you want to have a new folder automatically moved somewhere else after a set period of time?
Jeannie

One has to be proactive, not reactive, to ensure the safety of one's data so backup your data! And RAID is NOT a backup!
Reply
#7
(12-14-2019, 12:46 PM)Lady Fitzgerald Wrote: Forgive me if this seems ignorant (and probably is) but why would you want to have a new folder automatically moved somewhere else after a set period of time?

I also can't understand the logic behind that either. [Image: smileys-confused-389931.gif]
Linux Forever...Windoze Never       [Image: t12701.gif]
Reply
#8
(12-16-2019, 04:47 AM)bob777 Wrote:
(12-14-2019, 12:46 PM)Lady Fitzgerald Wrote: Forgive me if this seems ignorant (and probably is) but why would you want to have a new folder automatically moved somewhere else after a set period of time?

I also can't understand the logic behind that either. [Image: smileys-confused-389931.gif]

I'm sure there is a perfectly good reason for it (otherwise, why bother), but I fear I'm too dense to know what it is.
Jeannie

One has to be proactive, not reactive, to ensure the safety of one's data so backup your data! And RAID is NOT a backup!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)