Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
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.
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.
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 ?
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
Quote:
Originally Posted by gardenair
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.
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.
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.
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.