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.
- Install the
inotify-toolspackage on your distribution.
- Use the command
inotifywaitto 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.
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
inotifywait- Command that uses the inotify API. rovides a mechanism for monitoring filesystem events.
man inotifyfor further details.
inotifywaitrunning 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_dirto a destination called
/dst_dir. The loop will ensure that this move will happens every time an event is triggered by the
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.