How can we automaitcally sync/copy files from one directoy to another ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can we automaitcally sync/copy files from one directoy to another ?
# 1  
Old 03-25-2015
How can we automaitcally sync/copy files from one directoy to another ?

Hi,

I would like to achieve below requirement,

I have a directory "/mydir" and I want to automatically sync/copy all the content of /mydir directory to "/yourdir" directory all the time.
meaning, if some application creates a file in /mydir, it supposed to be copied/available in "/yourdir" directory

I need to have two copies.

Can we do this using rsync ? or hard link ? (*both /mydir and /yourdir available in same server)

please explain. Thanks.
# 2  
Old 03-25-2015
the link will not result in "two copies" as you say. The rsync is ideal tool for this task IMO.

the simplest form would be
Code:
rsync -a source target

If you delete a file from your source and want it to be deleted from your target rsync does that as well.
# 3  
Old 03-25-2015
Quote:
Originally Posted by aaron8667
Hi,

I would like to achieve below requirement,

I have a directory "/mydir" and I want to automatically sync/copy all the content of /mydir directory to "/yourdir" directory all the time.
meaning, if some application creates a file in /mydir, it supposed to be copied/available in "/yourdir" directory

I need to have two copies.

Can we do this using rsync ? or hard link ? (*both /mydir and /yourdir available in same server)

please explain. Thanks.
As migurus said, a hard link doesn't create a copy of a file; it creates another name for a single file.

And, even if /mydir and /yourdir are on the same server, you can't create a hard link between files in those directories unless both of those directories are in the same filesystem (i.e. under the same mount point) on that server.
# 4  
Old 03-25-2015
And to constantly check if the content of a directory has changed, you would need to write either:
  • Your own service, which then invokes rsync
  • A script that checks your path every single second, and if there was a change, start rsync - if it is not alredy running...
Which both is about the same in the effect, obviously...

You do NOT want start a new instance to rsync syncinc a multi GB large folder to another ever second.

hth
# 5  
Old 03-26-2015
Hi.

Linux has a feature called intotify:
Code:
DESCRIPTION
       The  inotify API provides a mechanism for monitoring filesystem events.
       Inotify can be used to monitor individual files, or to monitor directo‐
       ries.   When  a  directory is monitored, inotify will return events for
       the directory itself, and for files inside the directory.

which is accessible from the shell with:
Code:
 inotify-tools is a set of command-line programs for Linux providing a
 simple interface to inotify. These programs can be used to monitor and
 act upon filesystem events. inotify-tools consists of two utilities:
 .
 inotifywait simply blocks for inotify events, making it appropriate
 for use in shell scripts.
 .
 inotifywatch collects filesystem usage statistics and outputs counts
 of each inotify event.

So one could run inotifywait which would block execution of a script until there was a change, then the script could call rsync, then start all over again.

Best wishes ... cheers, drl

Last edited by drl; 03-26-2015 at 11:58 AM.. Reason: Correct minor spelling error.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

One directory, extracting only files to a new directoy

I need a hint for extracting and copying only the loads of .mp3 files from one directory and its subdirectories to get them all into only one directory. This way I am doing gives me a identical copy with hundreds of subdirectories. But I just don't get it to extract only the files. Though I tried... (9 Replies)
Discussion started by: 1in10
9 Replies

2. Homework & Coursework Questions

java project to sync 2 files

excuse me i have an question, school projcet about writing a java programme to syncronise 2 file in network using fuse with java plz i need help (1 Reply)
Discussion started by: yohana
1 Replies

3. Shell Programming and Scripting

Question on sync 2 folders with checking files' modify date

Hi Linux Community I would like to ask about how to compare files in deferent server with date. Those A and B servers has the same folder, I have write a sample script to "ls" both folders and "diff" them, and then "rsync" the missing files. It was running well, both A and B are sync, until... (2 Replies)
Discussion started by: lunaticdawn
2 Replies

4. Shell Programming and Scripting

Directoy Size - avoid cannot read directory

Hello, I need to write a script to check directory size on a linux server. I do not have access to some directories Inside the directory tree so I've got some warning in the output that say : du : cannot read directory .... Could you please help me. I did try Inside of my script to... (2 Replies)
Discussion started by: Aswex
2 Replies

5. Shell Programming and Scripting

crontab using shell script to sync files.

Hi, I developed one shell script where it will sync the files using perforce, #!/bin/bash TERM=linux export TERM clear echo "" $PATH echo "" cd /u/userk/p4/p4_client/TES_DATE echo "" echo "Sync p4 " p4 sync echo "Executing for second time " p4 -u userk -p p4net:161 -c... (8 Replies)
Discussion started by: asak
8 Replies

6. Shell Programming and Scripting

Korn shell script to sync/move files that are not in use

Hello all. This may seem like a dumb/easy question but right now I have a little script I made that uses rsync to sync a directory that has files in it that may or may not be complete files. I want to come up with a better solution for this. What it is is I have a directory lets say /incomplete... (4 Replies)
Discussion started by: linuxn00b
4 Replies

7. UNIX for Dummies Questions & Answers

Sync files between unix client and windows 2003 server

Hi everybody... I want to sync files between unix client machine and windows 2003 server machine. I thought of using Cygwin for windows server and then rsync between two to sync files, but have come to know that might be Cygwin will not be able to handle multiple clients request.... Can any... (2 Replies)
Discussion started by: lokeshsingla
2 Replies

8. Shell Programming and Scripting

sync files from two different servers.

I have a directory called UNIX 1 which contains 2 files and uploaded into two different servers. Now I want to check whether the directory contains both the files. If not, then need to sync the directory. Please let me know how to do that in shell scripting. (2 Replies)
Discussion started by: madan1
2 Replies

9. Solaris

Sync to Green vs. Separate Sync

Hi all....I have a Sun Ultra2 that I want to use with my PC monitor. I have purchased an adapter that does not work and I was told I need to change my video card setting (if I can) to Separate Sync.....my Monitor product number ends in 1343......I am running SunOS 5.7 ......anyone have any ideas? ... (0 Replies)
Discussion started by: psantinello
0 Replies

10. UNIX for Dummies Questions & Answers

Searching a directoy

Hi, How to search a directory and know it's path. Please Help. Thanks (3 Replies)
Discussion started by: gumsun
3 Replies
Login or Register to Ask a Question