2

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?

4
  • man cp and look for -r. Commented Jun 2, 2011 at 11:30
  • remember certain extension only in all files Commented Jun 2, 2011 at 11:41
  • why the hell i am getting negatives on logical questions!? Commented Jun 2, 2011 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 Commented Jun 2, 2011 at 17:36

6 Answers 6

4
cd /tmp/backup
find . -name '*.php' | cpio -pmud /home/mysite/public_html
1

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.

1

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.)

0

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

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

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
4
  • i guess u mean ./tmp/backup/ edit: after trying ur command it fully corrupted my site by saving all files in the same directroy Commented Jun 2, 2011 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. Commented Jun 2, 2011 at 11:58
  • why the hell a -1 for this answer? Commented Jun 2, 2011 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. Commented Jun 2, 2011 at 17:43

You must log in to answer this question.