Is there any way to copy the file in Unix as soon as it is created? I have one location where files will be created in different timings.How can I copy the files automatically using shell script to some other location in Unix?
2 Answers
inotifywait -e close_write --format "%f" --monitor . | while IFS= read -r line do #cp "$line" destination/ #this would not replicate directories tar cf - "$line" | tar -C destination/ xf - #this should replicate directories done should do the trick, unless the number of files in your directory (I'm using . for the current directory) exceeds your system's limit for inotify watches (fs.inotify.max_user_watches, as coteyr mentions).
- 2Also important to be aware of the file watch limit (fs.inotify.max_user_watches) I hit that limit all the time using inotify.coteyr– coteyr2015-06-08 10:44:47 +00:00Commented Jun 8, 2015 at 10:44
- Thank you guys very much for the comments. I've incorporated both into the answer. I believe
--monitorswitch along withclose_writeshould make it reliable.Petr Skocik– Petr Skocik2015-06-08 11:38:13 +00:00Commented Jun 8, 2015 at 11:38 - Thanks a lot for the answer.But, I do not have rsync installed on my linux machine.Is there any other way to do this?M.Nehru– M.Nehru2015-06-08 11:52:45 +00:00Commented Jun 8, 2015 at 11:52
- Install it if you can.
rsyncis useful. But I believe there is. Give me a few seconds.Petr Skocik– Petr Skocik2015-06-08 11:53:54 +00:00Commented Jun 8, 2015 at 11:53 - Do you have tar or cpio?Petr Skocik– Petr Skocik2015-06-08 11:54:35 +00:00Commented Jun 8, 2015 at 11:54
You can't really copy files the instant they are created with a shell script alone. Three solutions that come to mind.
rsync with cron - It's simple, standard, and easy. Once every so often ( one time an hour) you run rsync against the directory. Huge structures can be synced in seconds, depending on the amount of changes.
Gluster or similar. Depending on needs.
Just use a symlink.
- What is symlink? how can I use it to copy the files automatically?M.Nehru– M.Nehru2015-06-08 11:54:38 +00:00Commented Jun 8, 2015 at 11:54
- man7.org/linux/man-pages/man2/symlink.2.htmlcoteyr– coteyr2015-06-08 12:05:37 +00:00Commented Jun 8, 2015 at 12:05
- You should google it. It's complex to explain but easy to grasp.coteyr– coteyr2015-06-08 12:06:11 +00:00Commented Jun 8, 2015 at 12:06