gudii9
asked on
unix how to copy a folder
hi,
how to copy test folder files
from below
/crm/home/systemtest/secur ity/test
to below folder
/crm/home/XYZ3ABC/config/s ecurity/te st
trying cp command but not working
is -r different from -R
please advise
do i need to go to /crm/home/XYZ3ABC/config/s ecurity/te st to execute cp command or to /crm/home/systemtest/secur ity/test
how to copy test folder files
from below
/crm/home/systemtest/secur
to below folder
/crm/home/XYZ3ABC/config/s
trying cp command but not working
is -r different from -R
please advise
do i need to go to /crm/home/XYZ3ABC/config/s
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/secur ity/test /crm/home/XYZ3ABC/config/s ecurity/te st
"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/secur
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
what is difference between -r and -R
what other option we use for cp command and why?
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.
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, chown & chmod at least. But, for instance, ls -r lists in reverse order.
I suspect cp -r was an accident.
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.
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.
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.
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.
ASKER
any gui tool to do this easily like windows gui tools?
ASKER
cp -r /crm/home/systemtest/security/test /crm/home/XYZ3ABC/config/s ecurity/
this should work
to do this do i need to stand/present at any specific folder?( like /crm/home/systemtest/secur
)
or i can be in any folder like /crm/home/ and issue below command?
cp -r /crm/home/systemtest/secur
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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
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:
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).
There are 2 scenarios, depending on whether directory /home/cg/root/f2 already exists:
- It doesn't exist: f1/* is copied to f2/*
- It does exist: f1/* is copied to f2/f1/*
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
bandicam-2017-11-08-10-30-42-721.avi
ASKER
let me check. thank you
ASKER
my requirement simple
how to copy test folder files
from below
/crm/home/systemtest/secur ity/test
to below folder
/crm/home/XYZ3ABC/config/s ecurity/te st
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"
how to copy test folder files
from below
/crm/home/systemtest/secur
to below folder
/crm/home/XYZ3ABC/config/s
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"
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_direc tory/
cp: cannot create regular file '/home/cg/root/target_dire ctory/': Not a directory
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/*
cp: cannot create regular file '/home/cg/root/target_dire
ASKER
my goal is when i go to
cd /home/cg/root/target_direc tory
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
cd /home/cg/root/target_direc
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.
mkdir DIR1
mkdir DIR2
cd DIR1
touch xyz.txt
cd ..
cp -r ./DIR1/*.* ./DIR2/
That is all it takes.
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_fold er
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_fold er
bash-4.4$ pwd
/home/cg/root
bash-4.4$ cd targett_folder/
bash-4.4$ pwd
/home/cg/root/targett_fold er
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
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_fold
bash-4.4$ cd ..
bash-4.4$ pwd
/home/cg/root
bash-4.4$ cp -r /home/cg/root/src_folder/*
bash-4.4$ pwd
/home/cg/root
bash-4.4$ cd targett_folder/
bash-4.4$ pwd
/home/cg/root/targett_fold
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
ASKER
bash-4.4$ cd src_folderdo i eed above step as i am using absolute paths?
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_fold er
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
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.
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_fold er
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_fold er
bash-4.4$ pwd
/home/cg/root
bash-4.4$ cd targett_folder/
bash-4.4$ pwd
/home/cg/root/targett_fold er
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_fold er
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_fold er
bash-4.4$ pwd
/home/cg/root
it worked even i gave cp command from targettt_folder as well as giving absolute paths
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_fold
bash-4.4$ cd ..
bash-4.4$ pwd
/home/cg/root
bash-4.4$ cp -r /home/cg/root/src_folder/*
bash-4.4$ pwd
/home/cg/root
bash-4.4$ cd targett_folder/
bash-4.4$ pwd
/home/cg/root/targett_fold
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_fold
bash-4.4$ cd ..
bash-4.4$ pwd
/home/cg/root
bash-4.4$ cp -r /home/cg/root/src_folder/*
bash-4.4$ pwd
/home/cg/root
it worked even i gave cp command from targettt_folder as well as giving absolute paths
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.
ASKER
one last question before i close
*.*
what is meaning of above?
*.*
what is meaning of above?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Can you go ahead and close this question please.
ASKER
bash-4.4$ cp -r /home/cg/root/src_folder/* .* /home/cg/root/targett_fold er
first * is for any file name like src1 etc
second * after . is for any kind of extension like txt
here . is not current directory?
first * is for any file name like src1 etc
second * after . is for any kind of extension like txt
here . is not current directory?
ASKER
Move all files of type either jpg or png (image files) into another directory.
mv public_html/*.??g public_html/images/
mv public_html/*.??g public_html/images/
this should work