LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-14-2011, 05:11 AM   #1
gardenair
Member
 
Registered: Oct 2004
Location: LH
Posts: 648

Rep: Reputation: 45
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
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 11-14-2011, 06:24 AM   #2
GlennsPref
Senior Member
 
Registered: Apr 2004
Location: Brisbane, Australia
Distribution: Devuan
Posts: 3,656
Blog Entries: 33

Rep: Reputation: 283Reputation: 283Reputation: 283
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
 
Old 11-14-2011, 06:42 AM   #3
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
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.

Last edited by tronayne; 11-14-2011 at 06:44 AM.
 
2 members found this post helpful.
Old 11-14-2011, 06:43 AM   #4
trappa01
LQ Newbie
 
Registered: Dec 2009
Posts: 20

Rep: Reputation: 8
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.
 
3 members found this post helpful.
Old 11-15-2011, 03:18 AM   #5
gardenair
Member
 
Registered: Oct 2004
Location: LH
Posts: 648

Original Poster
Rep: Reputation: 45
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 ?
 
0 members found this post helpful.
Old 11-15-2011, 05:54 AM   #6
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Quote:
Originally Posted by gardenair View Post
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.
 
Old 11-15-2011, 10:12 AM   #7
gardenair
Member
 
Registered: Oct 2004
Location: LH
Posts: 648

Original Poster
Rep: Reputation: 45
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
 
0 members found this post helpful.
Old 11-15-2011, 10:25 AM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by gardenair View Post
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/

Last edited by TB0ne; 11-15-2011 at 01:13 PM.
 
1 members found this post helpful.
Old 11-15-2011, 06:32 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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
 
1 members found this post helpful.
Old 11-16-2011, 05:29 AM   #10
gardenair
Member
 
Registered: Oct 2004
Location: LH
Posts: 648

Original Poster
Rep: Reputation: 45
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
 
Old 11-16-2011, 09:21 AM   #11
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by gardenair View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Command Line Editor, while typing run another command before executing current? gumaheru Linux - General 5 04-13-2010 11:21 AM
how to copy drive using dd and tee command parallely? source code of dd command mdfakkeer Linux - Software 1 02-10-2010 01:31 PM
URGENT! Is there any command to get a history command lines and time in SUSE Linux.? igsoper Linux - Software 5 06-25-2009 02:14 AM
LXer: The Linux Command Shell For Beginners: Fear Not The Command Line! LXer Syndicated Linux News 0 12-22-2008 06:30 PM
Key stroke/command to shut down x and go into the command prompt screen? Fear58 Linux - General 1 07-14-2004 07:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration