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?
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.
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.
If both
orig_folder
andtemp
are on the same physical hard drive, renaming (moving) them is essentially instantaneous. That means you could simply doThat will rename
orig_folder
tofoo
, then renametemp
toorig_folder
and finally deletefoo
. On the same filesystem, the twomv
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:
Combining the accepted answer here with the command to copy one directory into another, the below command should do the job:
But, as the answer in the link says, note that:
/*
part is very important. If you put a space before the*
, it will delete all your files in your current directory.rm
,-r
and*
all in the same command. They can be a disastrous combination."