Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

unix how to copy a folder

hi,

how to copy test folder files
from below
/crm/home/systemtest/security/test
to below folder
/crm/home/XYZ3ABC/config/security/test

trying cp command but not working
is -r different from -R

please advise
do i need to go to /crm/home/XYZ3ABC/config/security/test to execute cp command or  to  /crm/home/systemtest/security/test
Avatar of Gregory Miller
Gregory Miller
Flag of United States of America image

cp -r /crm/home/systemtest/security/test /crm/home/XYZ3ABC/config/security/

this should work
Avatar of Sam Chen
Sam Chen

Please see below for cp usage,
"cp path_of_source path_of_destination"
-r and R are the same. They are use for recursiving.
I would recommend -p so that you copy your attributes over.

You mention that the command did not work , have you try sudo?


In your case please try,
sudo cp -p -r /crm/home/systemtest/security/test /crm/home/XYZ3ABC/config/security/test
SOLUTION
Avatar of David Favor
David Favor
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

what is difference between -r and -R
what other option we use for cp command and why?
-R, -r, --recursive          copy directories recursively

All three of these switches do the same thing in the cp command. This is not always the case in other *nix distributions and other commands.
-R is some kind of standard: it means --recursive for cp, ls, chownchmod at least. But, for instance, ls -r lists in reverse order.
I suspect cp -r was an accident.
I cannot speak to all distros of Linux but i suspect that most support the -r  and the --recursive parameter in the core utility cp.

http://man7.org/linux/man-pages/man1/cp.1.html
This is a very prominent man page collection which clearly shows support for the lower case and upper case option with cp

Also the command 'man cp' on my servers all give similar results.
User generated image
I did find that on FreeBSD, they do not list the -r parameter only the -R parameter in their man pages online but I do not have a FreeBSD box to test this with. For backwards compatibility, I suspect that FreeBSD also supports both the -r and --recursive parameters but again, I cannot prove this.
Avatar of gudii9

ASKER

any gui tool to do this easily like windows gui tools?
Avatar of gudii9

ASKER

cp -r /crm/home/systemtest/security/test /crm/home/XYZ3ABC/config/security/

this should work

to do this do i need to stand/present at any specific folder?( like /crm/home/systemtest/security/test or /crm/home/XYZ3ABC/config/security/
)

or i can be in any folder like /crm/home/ and issue below command?
cp -r /crm/home/systemtest/security/test /crm/home/XYZ3ABC/config/security/
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

https://www.tutorialspoint.com/unix_terminal_online.php

i created f1 directory and created a  file called abcd.txt using above online unix editor

i created f2 directory as well there

now how to copy all f1 contents, files into f2 using this cp command?


sh-4.4$ /bin/bash                                                                                                                                  
bash-4.4$ mkdir f1                                                                                                                                
bash-4.4$ vi ttt.txt                                                                                                                              
bash-4.4$ mkdir f2                                                                                                                                
bash-4.4$ pwd                                                                                                                                      
/home/cg/root                                                                                                                                      
bash-4.4$ ls -ltr                                                                                                                                  
total 16                                                                                                                                          
-rw-r--r-- 1 56867 56867  978 Nov  7 21:49 README.txt                                                                                              
drwxr-xr-x 2 56867 56867 4096 Nov  7 21:49 f1                                                                                                      
-rw-r--r-- 1 56867 56867    8 Nov  7 21:49 ttt.txt                                                                                                
drwxr-xr-x 2 56867 56867 4096 Nov  7 21:49 f2                                                                                                      
bash-4.4$ cd f1                                                                                                                                    
bash-4.4$ pwd                                                                                                                                      
/home/cg/root/f1                                                                                                                                  
bash-4.4$ vi abcd.txt                                                                                                                              
bash-4.4$ ls -ltr                                                                                                                                  
total 4                                                                                                                                            
-rw-r--r-- 1 56867 56867 7 Nov  7 21:51 abcd.txt                                                                                                  
bash-4.4$ pwd                                                                                                                                      
/home/cg/root/f1                                                                                                                                  


Please advise
Avatar of gudii9

ASKER

still need clarity
Possibly you could tell us what did not work or the errors you received when trying any of the suggestions we gave above. The cp (copy) command is pretty basic and all you need to do is adjust the folder names to match your new scenario. In case you are unsure how to do that, here is the new version suggestion:

cp -r /home/cg/root/f1 /home/cg/root/f2
which will copy the folder "f1" to the folder "f2"

or

cp -r /home/cg/root/f1/*.* /home/cg/root/f2/
which will copy the files from the folder "f1" into the folder "f2"
cp -r /home/cg/root/f1 /home/cg/root/f2
There are 2 scenarios, depending on whether directory /home/cg/root/f2 already exists:
  1. It doesn't exist: f1/* is copied to f2/*
  2. It does exist: f1/* is copied to f2/f1/*
I always use cp -v (verbose copy) to check which is happening. Usually I do not want option 2.
If you always rm -rf f2 first, you will never get option 2, but beware you might lose something you wanted to keep. A gentler safeguard would be mv -v f2 f2.old (verbose in case f2.old already existed, in which case f2 gets moved to f2.old/f2).
I agree with Duncan above. If the destination folder does not already exist the the command as he indicated works as he described. Since you indicated in your examples an existing destination folder all of the previous examples work perfectly. I also attached a quick video showing how to do it.
bandicam-2017-11-08-10-30-42-721.avi
Avatar of gudii9

ASKER

let me check. thank you
Avatar of gudii9

ASKER

my requirement simple
how to copy test folder files
from below
/crm/home/systemtest/security/test
to below folder
/crm/home/XYZ3ABC/config/security/test


assuming all files existing there


cp -r /home/cg/root/f1/*.* /home/cg/root/f2/
which will copy the files from the folder "f1" into the folder "f2"


i want to do above scenario not below scenario

cp -r /home/cg/root/f1 /home/cg/root/f2
which will copy the folder "f1" to the folder "f2"
Avatar of gudii9

ASKER

i  tried getting below error. how to resolve this?

bash-4.4$ mkdir source_dir                                                                                                                        
bash-4.4$ cd source_dir                                                                                                                            
bash-4.4$ vi source_dir_file.txt                                                                                                                  
bash-4.4$ pwd                                                                                                                                      
/home/cg/root/source_dir                                                                                                                          
bash-4.4$ ls -ltr                                                                                                                                  
total 4                                                                                                                                            
-rw-r--r-- 1 6589 6589 3 Nov  8 16:50 source_dir_file.txt                                                                                          
bash-4.4$ mkdir target_directory                                                                                                                  
bash-4.4$ pwd                                                                                                                                      
/home/cg/root/source_dir                                                                                                                          
bash-4.4$ ls -ltr                                                                                                                                  
total 8                                                                                                                                            
-rw-r--r-- 1 6589 6589    3 Nov  8 16:50 source_dir_file.txt                                                                                      
drwxr-xr-x 2 6589 6589 4096 Nov  8 16:51 target_directory                                                                                          
bash-4.4$ cp -r /home/cg/root/source_dir/*.* /home/cg/root/target_directory/                                                                      
cp: cannot create regular file '/home/cg/root/target_directory/': Not a directory
Avatar of gudii9

ASKER

my goal is when i go to
cd /home/cg/root/target_directory
i should see below file there as i copied it over from source_dir
-rw-r--r-- 1 6589 6589    3 Nov  8 16:59 source_dir_file.txt
you created the target_directory inside of the source_dir as far as I can tell by your copy & paste above
cd
mkdir DIR1
mkdir DIR2
cd DIR1
touch xyz.txt
cd ..
cp -r ./DIR1/*.* ./DIR2/


That is all it takes.
Avatar of gudii9

ASKER

bash-4.4$ cd src_folder                                                                                                                            
bash-4.4$ pwd                                                                                                                                      
/home/cg/root/src_folder                                                                                                                          
bash-4.4$ vi src1.txt                                                                                                                              
bash-4.4$ ls -ltr                                                                                                                                  
total 4                                                                                                                                            
-rw-r--r-- 1 6589 6589 4 Nov  8 16:59 src1.txt                                                                                                    
bash-4.4$ pwd                                                                                                                                      
/home/cg/root/src_folder                                                                                                                          
bash-4.4$ cd ..                                                                                                                                    
bash-4.4$ pwd                                                                                                                                      
/home/cg/root                                                                                                                                      
bash-4.4$ mkdir targett_folder                                                                                                                    
bash-4.4$ pwd                                                                                                                                      
/home/cg/root                                                                                                                                      
bash-4.4$ cd targett_folder/                                                                                                                      
bash-4.4$ pwd                                                                                                                                      
/home/cg/root/targett_folder                                                                                                                      
bash-4.4$ cd ..                                                                                                                                    
bash-4.4$ pwd                                                                                                                                      
/home/cg/root                                                                                                                                      
bash-4.4$ cp -r /home/cg/root/src_folder/*.* /home/cg/root/targett_folder                                                                          
bash-4.4$ pwd                                                                                                                                      
/home/cg/root                                                                                                                                      
bash-4.4$ cd targett_folder/                                                                                                                      
bash-4.4$ pwd                                                                                                                                      
/home/cg/root/targett_folder                                                                                                                      
bash-4.4$ ls -ltr                                                                                                                                  
total 4                                                                                                                                            
-rw-r--r-- 1 6589 6589 4 Nov  8 17:02 src1.txt                                                                                                    


i think it worked finallly
Avatar of gudii9

ASKER

bash-4.4$ cd src_folder                                                                                                                            
bash-4.4$ pwd                                                                                                                                      
/home/cg/root/src_folder                                                                                                                          
bash-4.4$ vi src1.txt                                                                                                                              
bash-4.4$ ls -ltr                                                                                                                                  
total 4                                                                                                                                            
-rw-r--r-- 1 6589 6589 4 Nov  8 16:59 src1.txt                                                                                                    
bash-4.4$ pwd                                                                                                                                      
/home/cg/root/src_folder                                                                                                                          
bash-4.4$ cd ..                                                                                                                                    
bash-4.4$ pwd                                                                                                                                      
/home/cg/root                                                                                                                                      
bash-4.4$ mkdir targett_folder                                                                                                                    
bash-4.4$ pwd                                                                                                                                      
/home/cg/root                                                                                                                                      
bash-4.4$ cd targett_folder/                                                                                                                      
bash-4.4$ pwd                                                                                                                                      
/home/cg/root/targett_folder                                                                                                                      
bash-4.4$ cd ..                                                                                                                                    
bash-4.4$ pwd                                                                                                                                      
/home/cg/root                                                                                                                                      
bash-4.4$ cp -r /home/cg/root/src_folder/*.* /home/cg/root/targett_folder                                                                          
bash-4.4$ pwd                                                                                                                                      
/home/cg/root                                                                                                                                      
bash-4.4$ cd targett_folder/                                                                                                                      
bash-4.4$ pwd                                                                                                                                      
/home/cg/root/targett_folder                                                                                                                      
bash-4.4$ ls -ltr                                                                                                                                  
total 4                                                                                                                                            
-rw-r--r-- 1 6589 6589 4 Nov  8 17:02 src1.txt                                                                                                    
do i eed above step as i am using absolute paths?
If you use an absolute path, it makes no difference what current folder you are in when you execute the command, so no.
Can you go ahead and close this question please.
Avatar of gudii9

ASKER

bash-4.4$ cd src_folder                                                                                                                            
bash-4.4$ pwd                                                                                                                                      
/home/cg/root/src_folder                                                                                                                          
bash-4.4$ vi src1.txt                                                                                                                              
bash-4.4$ ls -ltr                                                                                                                                  
total 4                                                                                                                                            
-rw-r--r-- 1 6589 6589 4 Nov  8 16:59 src1.txt                                                                                                    
bash-4.4$ pwd                                                                                                                                      
/home/cg/root/src_folder                                                                                                                          
bash-4.4$ cd ..                                                                                                                                    
bash-4.4$ pwd                                                                                                                                      
/home/cg/root                                                                                                                                      
bash-4.4$ mkdir targett_folder                                                                                                                    
bash-4.4$ pwd                                                                                                                                      
/home/cg/root                                                                                                                                      
bash-4.4$ cd targett_folder/                                                                                                                      
bash-4.4$ pwd                                                                                                                                      
/home/cg/root/targett_folder                                                                                                                      
bash-4.4$ cd ..                                                                                                                                    
bash-4.4$ pwd                                                                                                                                      
/home/cg/root                                                                                                                                      
bash-4.4$ cp -r /home/cg/root/src_folder/*.* /home/cg/root/targett_folder                                                                          
bash-4.4$ pwd                                                                                                                                      
/home/cg/root                                                                                                                                      
bash-4.4$ cd targett_folder/                                                                                                                      
bash-4.4$ pwd                                                                                                                                      
/home/cg/root/targett_folder                                                                                                                      
bash-4.4$ ls -ltr                                                                                                                                  
total 4                                                                                                                                            
-rw-r--r-- 1 6589 6589 4 Nov  8 17:02 src1.txt                                                                                                    
bash-4.4$ cd src_folder                                                                                                                            
bash-4.4$ pwd                                                                                                                                      
/home/cg/root/src_folder                                                                                                                          
bash-4.4$ vi src1.txt                                                                                                                              
bash-4.4$ ls -ltr                                                                                                                                  
total 4                                                                                                                                            
-rw-r--r-- 1 6589 6589 4 Nov  8 16:59 src1.txt                                                                                                    
bash-4.4$ pwd                                                                                                                                      
/home/cg/root/src_folder                                                                                                                          
bash-4.4$ cd ..                                                                                                                                    
bash-4.4$ pwd                                                                                                                                      
/home/cg/root                                                                                                                                      
bash-4.4$ mkdir targett_folder                                                                                                                    
bash-4.4$ pwd                                                                                                                                      
/home/cg/root                                                                                                                                      
bash-4.4$ cd targett_folder/                                                                                                                      
bash-4.4$ pwd                                                                                                                                      
/home/cg/root/targett_folder                                                                                                                      
bash-4.4$ cd ..                                                                                                                                    
bash-4.4$ pwd                                                                                                                                      
/home/cg/root                                                                                                                                      
bash-4.4$ cp -r /home/cg/root/src_folder/*.* /home/cg/root/targett_folder                                                                          
bash-4.4$ pwd                                                                                                                                      
/home/cg/root          

it worked even i gave cp command from targettt_folder as well as giving absolute paths
Avatar of gudii9

ASKER

what happens if i donot give -r in cp command
the -r is only required if you want sub-folders and files within the source sub-folder to also be copied. If there is no sub-folders in the source and only files, it makes no difference.
Can you go ahead and close this question please.
Avatar of gudii9

ASKER

one last question before i close

*.*

what is meaning of above?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Can you go ahead and close this question please.
Avatar of gudii9

ASKER

bash-4.4$ cp -r /home/cg/root/src_folder/*.* /home/cg/root/targett_folder  

first * is for any file name like src1 etc
second * after . is for any kind of extension like txt
here . is not current directory?
Avatar of gudii9

ASKER

Move all files of type either jpg or png (image files) into another directory.
mv public_html/*.??g public_html/images/