LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   cp command. (https://www.linuxquestions.org/questions/linux-newbie-8/cp-command-913429/)

gardenair 11-14-2011 05:11 AM

cp command.
 
hi,
I have two questions regarding to cp i.e copy command.

1- I have a folder which have thousands of files and few directories.I want to copy same folder in another location like "my_dir" where only files should only copy in it not the directories.

2- If I want to copy a file from another linux machine over LAN suppose PC-1 want to opy the data from PC-2 then what should be the command ?


thanks,
gardenair

GlennsPref 11-14-2011 06:24 AM

Hi

I recommend you look into backup software, like rsync.
man page, http://linux.die.net/man/1/rsync

tutorial, 15 examples including networks
http://www.thegeekstuff.com/2010/09/...mand-examples/

wiki
http://en.wikipedia.org/wiki/Rsync

regards Glenn

tronayne 11-14-2011 06:42 AM

You can do a recursive copy which will get "everything" from one direcotry to another; e.g.,
Code:

cp -pr my_dir .
that will copy the entire tree to the current directory and it will be named my_dir. Then, if you only have a few directories in my_dir, do something like this:
Code:

!#/bin/bash
#      find all the directories
for dir in $(find . -type d)
do
        #      we don't want to copy the current directory to itself
        if [ ${dir} != "." ]
        then
                #      not current directory, so copy
                cp ${dir}/* .
                #      and remove that directory
                rm -r ${dir}
        fi
done

Test the above two steps first somewhere with a small sample.

To copy between two machines, the easiest way is with scp -- so you need to have SSH access between the two machines. Set that up first if it's not already in place, then on your local machine get into a directory where you want to have the copied file tree and
Code:

cd the_directory
scp -pr PC_2:path_to_directory .

That will do a recursive copy from PC_2 to the current directory on PC_1.

It would be a good idea to review the manual pages for cp and scp.

Hope this helps some.

trappa01 11-14-2011 06:43 AM

For the first question, you could use the find command. ie : find start_dir -type f | xargs -I'{} cp {} my_dir
This will find all files from the start_dir downwards and copy them to my_dir. You will lose the original directory hierarchy. Is this what you meant? The only problem would be if you have 2 files with the same name but not in the same sub-directory.

For the second question, you can use scp.

gardenair 11-15-2011 03:18 AM

thanks for the replies. Well scp successfully work & it copy files over lan. The question is if we want to copy files & folder as well then which command should we use ?

tronayne 11-15-2011 05:54 AM

Quote:

Originally Posted by gardenair (Post 4524398)
thanks for the replies. Well scp successfully work & it copy files over lan. The question is if we want to copy files & folder as well then which command should we use ?

Yes, that's the purpose of it; copy file(s) and directory(ies) between servers via Ethernet (or whatever network service you may have; e.g., wi-fi).

The copy utilities (cp and scp to name two) support recursive copy; that is, walk a tree and copy it in its entirety. You could see what a tree looks like with the tree utility -- it walks a directory tree and shows all content -- that's what recursive means.

Hope this helps some.

gardenair 11-15-2011 10:12 AM

well i am again repeating my questions. Over the LAN we can copy files using scp command.If we also want to copy a directory from a remote pc on LAN what command should i use.
I try scp command but it only copy files.

thanks,
garenair

TB0ne 11-15-2011 10:25 AM

Quote:

Originally Posted by gardenair (Post 4524699)
well i am again repeating my questions. Over the LAN we can copy files using scp command.If we also want to copy a directory from a remote pc on LAN what command should i use. I try scp command but it only copy files.

Again, we are repeating the answers: use cp or scp...try reading the man pages on them. Pay particular attention to the "-R" (cp) and the "-r" (scp) options, which will copy EVERYTHING, directories or not.

You first asked for a command to copy all the files to ONE directory, and tronayne gave it to you. Want to copy everything, as is?? Use the commands given as they are.
Code:

cp -R /some/directory/* /some/destination/

--OR--

scp -r /some/directory/* <user>@<remote machine>:/some/path/


chrism01 11-15-2011 06:32 PM

In short, if the 'copying' is local disk to local disk (includes nfs, cifs mts), then use 'cp', else (for remote ie across LAN/WAN/internet) use scp.
You should read these pages
http://linux.die.net/man/1/cp
http://linux.die.net/man/1/scp

gardenair 11-16-2011 05:29 AM

Thanks for the reply.chrism01 very shory & have meaningfull reply regarding to cp and scp command.

For backup things I normally use tar command which compress the files & keep it in a single file.rsync is a new command in Linux.What is a main difference between rsync and tar command and in which situation we can use rsync or tar command ?
The mainthing which i read from web sites given below is rsync is used for remotely backup.

gardenair

TB0ne 11-16-2011 09:21 AM

Quote:

Originally Posted by gardenair (Post 4525421)
Thanks for the reply.chrism01 very shory & have meaningfull reply regarding to cp and scp command.

Reading the man pages on commands should be the first thing you do...you can often answer your own questions that way, quicker.
Quote:

For backup things I normally use tar command which compress the files & keep it in a single file.rsync is a new command in Linux.
Uhhh...'new' as opposed to what? Rsync has been around and in Linux for YEARS.
Quote:

What is a main difference between rsync and tar command and in which situation we can use rsync or tar command ? The mainthing which i read from web sites given below is rsync is used for remotely backup.
Again, read the man pages on rsync and tar. Tar is mainly used as an archival program, which CAN BE used for backups, but it's original purpose was to write data to tape devices (Tape ARchive, or TAR). Rsync was designed to keep two directories/sources in sync...they CAN be remote over a network, and they can also be on the same system.


All times are GMT -5. The time now is 10:30 PM.