0

This question already has an answer here:

I want to move all the *.xxx file created in a directory to another directory. But, I want that as soon as the files are created, they should move to another directory. Please Help. Thanks in advance.

marked as duplicate by Gilles linux Oct 22 '14 at 16:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3
  1. Install the inotify-tools package on your distribution.
  2. Use the command inotifywait to create a continuous lookup on the desired directory. Ex: inotifywait -m -r -e create /src_dir. This tool can watch other aspects of the filesystem(attributes change, close write, move, delete), so, lets stick with the file creation thing.
  3. Preapare the notification and continuous running with this command and execute this with a user that have enough privileges:

    inotifywait -m -r -e create /src_dir |
    while read file; do
          mv /src_dir/*.xxx /dst_dir
    done
    

Detailed explanation:

  • inotifywait - Command that uses the inotify API. rovides a mechanism for monitoring filesystem events. man inotify for further details.
  • -m - Keep inotifywait running after the first event occurs.
  • -r - Run recursively. Remove this if you dont want this behavior.
  • -e create - Notify on a determined event. The one we are using is create. Omit this parameter to monitor all known events
  • /src_dir - Argument of place being monitored
  • | - Pipe operator. Redirect one command output to another.
  • /while (...) done - Move everything called *.xxx from /src_dir to a destination called /dst_dir. The loop will ensure that this move will happens every time an event is triggered by the inotifywait command.

Extracted from the man mapges:

-m, --monitor
Instead  of  exiting  after  receiving  a single event, execute indefinitely.  
The default behaviour is to exit after the first event occurs.

-e <event>, --event <event>
Listen for specific event(s) only.  The events which can be listened for are 
listed in the EVENTS section.  This option can be speci‐fied more than once.  
If omitted, all events are listened for.

-r, --recursive
Watch all subdirectories of any directories passed as arguments.  Watches will be
set up recursively to an unlimited depth.  Symbolic links are not traversed.
 Newly created subdirectories will also be watched.
  • 1
    I don't think -m with && is going to what you describe... try |while read dir event file; do ...; done. inotify-wait calls fflush() so there shouldn't be any buffering problems. – mr.spuratic Oct 22 '14 at 12:51
  • Thats true. I'll change my answer. – user34720 Oct 22 '14 at 13:03

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