Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I recursively copy a directory into another and replace only the files that have not changed?

Tags:

I am looking to do a specific copy in Fedora.

I have two folders:

  • 'webroot': holding ALL web files/images etc

  • 'export': folder containing thousands of PHP, CSS, JS documents that are exported from my SVN repo.

The export directory contains many of the same files/folders that the root does, however the root contains additional ones not found in export.

I'd like to merge all of the contents of export with my webroot with the following options:

  1. Overwriting the file in webroot if export's version contains different code than what is inside of webroot's version (live)
  2. Preserve the permissions/users/groups of the file if it is overwritten (the export version replacing the live version) *NOTE I would like the webroots permissions/ownership maintained, but with export's contents
  3. No prompting/stopping of the copy of any kind (ie not verbose)
  4. Recursive copy - obviously I would like to copy all* files folders and subfolders found in export

I've done a bit of research into cp - would this do the job?:

cp -pruf ./export /path/to/webroot
like image 683
barfoon Avatar asked Mar 02 '09 04:03

barfoon


People also ask

How do I copy a directory recursively?

In order to copy the content of a directory recursively, you have to use the “cp” command with the “-R” option and specify the source directory followed by a wildcard character.

How do I overwrite an existing directory in Linux?

Usually, when you run a cp command, it overwrites the destination file(s) or directory as shown. To run cp in interactive mode so that it prompts you before overwriting an existing file or directory, use the -i flag as shown.

What command do you use to copy files from all subdirectories to another directory?

To copy a directory with all subdirectories and files, use the cp command.

How can you copy a directory recursively with all of its contents including sub directories )?

Copying Directories with cp Command To copy a directory, including all its files and subdirectories, use the -R or -r option. The command above creates the destination directory and recursively copy all files and subdirectories from the source to the destination directory.


1 Answers

It might, but any time the corresponding files in export and webroot have the same content but different modification times, you'd wind up performing an unnecessary copy operation. You'd probably get slightly smarter behavior from rsync:

rsync -pr ./export /path/to/webroot

Besides, rsync can copy files from one host to another over an SSH connection, if you ever have a need to do that. Plus, it has a zillion options you can specify to tweak its behavior - look in the man page for details.

EDIT: with respect to your clarification about what you mean by preserving permissions: you'd probably want to leave off the -p option.

like image 52
David Z Avatar answered Oct 16 '22 05:10

David Z