5

I have a directory called 'existing_folder' and another directory called 'temp'

I want to replace the contents of 'existing_folder' with those of 'temp' along with any sub directories.

Because the directory contains web pages, this has to be done in a way that ensures minimal downtime.

Is there a way to do this? What command should I use to achieve this?

  • Do files/folders have (possibly) similar names? – Jacob Vlijm Mar 22 '15 at 8:01
  • Some of them have similar name and some don't – user2028856 Mar 22 '15 at 8:01
8

If both orig_folder and temp are on the same physical hard drive, renaming (moving) them is essentially instantaneous. That means you could simply do

mv orig_folder foo && mv temp orig_folder && rm -rf foo

That will rename orig_folder to foo, then rename temp to orig_folder and finally delete foo. On the same filesystem, the two mv operations will take next to no time (0.004 seconds on my system).

If the source and target directories are not on the same file system, in order to minimize the time that the files are not available, you would first need to move the source directory to the same filesystem and then rename:

mv /path/to/temp . && mv orig_folder foo && mv temp orig_folder && rm -rf foo
10

Have you tried rsync ?

From your requirements I think it is the best tool. It will replace only the files that changed, copy new ones and it can remove those that are gone in origin.

$ rsync -av --delete temp/ existing_folder/

Notice the slash after temp, it is required because you want to sync the contents. Without it it would create a directori temp inside existing folder.

The delete argument makes the files that are no longer in temp be removed in existing_folder.

You can also do a dry-run if you add -n argument. It will tell you what changes will be done without doing anything.

  • I found this to be a better solution. Using the -p -o and -g flags I was able to keep the permissions, owner and group – GrokSrc Jun 11 '15 at 0:55
2

Combining the accepted answer here with the command to copy one directory into another, the below command should do the job:

rm -rfv <existing_folder>/* && cp -r <temp>/* <existing_folder>

But, as the answer in the link says, note that:

  • the /* part is very important. If you put a space before the *, it will delete all your files in your current directory.
  • this won't delete hidden files
  • "be very (very) careful playing with rm, -r and * all in the same command. They can be a disastrous combination."
  • Thanks for the answer, will running this command cause the target folder to lose everything whilst the process is running? It's a folder that contains many static web pages and I just want to replace them without any downtime. – user2028856 Mar 22 '15 at 8:37
  • @user2028856 Ah, I see, indeed the target folder will be cleared before the copying takes place. It can be done differently, but then first the copying would have to take place, then the removing. That would need a "book-keeping" script. The existing content of existing_folder, that is not in temp should be removed, right? – Jacob Vlijm Mar 22 '15 at 8:45
  • @user2028856 Did you notice the last edit of my comment (if the files that are not in temp should be removed from existing_folder afterwards). :) – Jacob Vlijm Mar 22 '15 at 9:10
  • Yea I want basically to replace everything in 'existing_folder' with with those from 'temp' but without any down time. – user2028856 Mar 22 '15 at 9:46
  • 1
    @user2028856 please edit your question and include these requirements. Also tell us if there is a one-to-one correspondence between files in original_dir and temp. Finally, you should also explain whether the two directories are on the same physical disk. That makes a huge difference to the time needed to move things around. mv is essentially instantaneous on the same disk but can take a while if not. – terdon Mar 22 '15 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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