I have a backup copy of my site located in /tmp/backup and want to copy it to /home/mysite/public_html But my aim is to copy only the .php files (with nested folders) and replace.

If I don't have nested folders i simply do cp /tmp/backup/*.php /home/mysite/public_html

But how i can do it for the whole site with many folders?

migrated from stackoverflow.com Jun 2 '11 at 15:51

This question came from our site for professional and enthusiast programmers.

  • man cp and look for -r. – Mel Jun 2 '11 at 11:30
  • remember certain extension only in all files – TDSii Jun 2 '11 at 11:41
  • why the hell i am getting negatives on logical questions!? – TDSii Jun 2 '11 at 12:01
  • @TDSii You get automatic downvotes when people vote to close as off-topic; they're not migrated with the post, so you're at +2 here – Michael Mrozek Jun 2 '11 at 17:36
cd /tmp/backup
find . -name '*.php' | cpio -pmud /home/mysite/public_html

On recent versions of bash, you can enable globstar (shopt -s globstar), and then do:

cd /tmp/backup
tar cvf - **/*.php | tar xvf - -C /home/mysite/public_html

** matches all files, dirs and subdirs

I'm using tar here because it preserves the directory structure.


With POSIX tools, use pax (the POSIX replacement to tar and cpio):

cd /tmp/backup
find . -name '*.php' | pax -rw -pp /home/mysite/public_html

With zsh, first create the target directories, then copy the files. Put the first two lines in your ~/.zshrc.

autoload zmv
alias zcp='zmv -C' zln='zmv -L'
cd /tmp/backup
mkdir -p **/*.php(e\''REPLY=/home/mysite/public_html/${REPLY:h}'\')
zcp '(**/)(*.php)' '/home/mysite/public_html/$1$2'

(Here, zsh has no advantage over the standard method, because zcp doesn't have a way to create target directories as needed.)


Try this:
cp /tmp/backup/*/*.php /home/mysite/public_html/

  • try or sure? can anyone verify this please – TDSii Jun 2 '11 at 11:44
  • 1
    -1 This won't work because it does not preserve directories. It will copy all files into the same directory. – dogbane Jun 2 '11 at 11:49
find /tmp/backup/ -name "*.php" | xargs -I % cp % /home/mysite/public_html

updated the answer run the below command one after other. this will preserve the directory structure also:

cp /tmp/backup /home/mysite/public_html
find /home/mysite/public_html -not -name "*.php"|xargs rm -rf
  • i guess u mean ./tmp/backup/ edit: after trying ur command it fully corrupted my site by saving all files in the same directroy – TDSii Jun 2 '11 at 11:36
  • if thats the case then you need to mention in the question clearly that you also need the same directory structure to be copied.the question looks vague to answer. – Rahul Dravid Jun 2 '11 at 11:58
  • why the hell a -1 for this answer? – Rahul Dravid Jun 2 '11 at 12:06
  • 1
    This answer is WRONG AND DANGEROUS. The first thing the find command will print out is /home/mysite/public_html, which rm will dutifully remove. Even if the find command was amended to exclude directories, any file that wasn't the result of the copy would be deleted, which isn't in the requirement. Furthermore, xargs expects an input format that find doesn't produce, so if there are any files with special characters (whitespace or \'") in their names, xargs will pass unintended names to rm, and files anywhere on the system may be deleted. – Gilles Jun 2 '11 at 17:43

Your Answer

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