Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

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
add comment

6 Answers

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

share|improve this answer
add comment

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
share|improve this answer
    
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
    
not me for sure and i thank you for trying –  TDSii Jun 2 '11 at 12:20
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
add comment
find /tmp/backup/ -name "*.php" | xargs -I % cp % /home/mysite/public_html
share|improve this answer
add comment
cd /tmp/backup
find . -name '*.php' | cpio -pmud /home/mysite/public_html
share|improve this answer
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.