how do you copy a directory and its contents to a new location under a new directory name?
Solution 1
You can use cp
with the -r
(copy recursively) flag and specify the new directory name:
cp -r /path/to/directory /path/to/location/new-name
In the above command replace the given paths with the real ones.
For example, to copy stuff
from my home directory to an existing directory named backup
and name the new directory stuff-backup
(if this directory already exists, note that stuff
will be copied into it, not overwrite it), you run:
cp -r ~/stuff ~/backup/stuff-backup
~
is a shortcut for your home directory /home/$USER
or /home/zanna
in my case. You can omit it if the current working directory is your home directory - you can see this from your prompt
zanna@monster:~$
^--the ~ here is where I am - home!
You can add the -v
(verbose) flag to make cp
report each copy being performed:
$ cp -vr stuff backup/stuff-backup
'stuff/thing1' -> 'backup/stuff-backup/thing1'
'stuff/thing2' -> 'backup/stuff-backup/thing2
...
Solution 2
The command you need is simply cp
which stands for "copy".
You can use it for example with one of these syntaxes:
cp SOURCEFILE TARGETFILE
cp SOURCEFILE TARGETDIRECTORY
The first variant allows you to specify a new file name for the target file, while the second variant creates a copy with the same name in the target directory. You must of course substitute the place holders in capital letters with valid paths first.
However, cp
by default operates on files only, not on directories. To get it to copy a complete directory with all its content recursively, you must add the -r
option:
cp -r SOURCEDIRECTORY TARGETDIRECTORY
You can learn more about the cp
command by typing man cp
in your terminal.
Solution 3
You can use the below command to copy directory from one place to another sub directory.
cp -Rvp /home/edmuntu/data /home/edmuntu/mydata/
Explanation of command:
-R
= copy directories recursivelyv
= explain what is being donep
= It will help your to preserve your permission and ownership with timestamps
Solution 4
As an alternative to the generally useful cp command you can use rsync which has the added benefit of replicating the permissions/timestamps of the original directory structure:
rsync -av dir/ newdir
-a archive -v verbose
note the slash after the source dir.
Solution 5
cp
is the command that does the function that you're looking for. It stands for copy and it does just that. The command that would work for you in this situation is
cp -R source/ destination/
This would copy the source/
folder and all of its sub-directories to destination/
.
If destination/
doesn't exist, it will be created.
The -R
flag stands for recursive, which tells cp
to copy all sub-directories.
Related videos on Youtube
Edmuntu
Updated on September 18, 2022Comments
-
Edmuntu almost 2 years
I'm new to the Linux command line and am trying to get to grips with the copy command at the moment.
Can anyone please tell me if it's possible to copy a directory with its subdirectories and associated files to a new directory with a new name, e.g.
directory_backup
?Thanks for any and all help in advance.
-
BlueCacti over 7 yearsWhile this does explain how to copy all files from your current dir to another dir, it doesn't fully answer OPs question on how to copy a dir (and its contents) to a different name dir. There are shorter commands (such as
cp -r
) which will take care of creating a new directory and copying all files into it -
Edmuntu over 7 yearsSorry Zanna. I'll put your advice into practice and see how I go. Like the idea of the verbose flag . assume this will also work as a report while other command are being worked.. Cheers.
-
Edmuntu over 7 yearsNot used to this comment section Byte Commander...keep hitting return to change line but post instead. Very clear outline in your answer . Look forward to sorting out those directories now that I've been given good instruction on the correct usage of the available tools. Excellent.
-
Zanna over 7 years@Edmuntu sorry for what? Yes, lots of commands have a
-v
or--verbose
flag :) -
Edmuntu over 7 yearsThank You@Owen Hines. Sound like this is definitely the command for my task.Cheers
-
TheOdd over 7 years@Edmuntu Glad I could help, if you found the solution to your issue, can you please mark the answer that worked for you as the correct one by clicking the check mark under it? This will help alert others who view the page of the question being solved.
-
Edmuntu over 7 yearsThank you @Rakesh Chauhan."preserve your permission and ownership with timestamps" ??? Wow....I need to get to it!!!! Yet another aspect of line commands to be explored. Thank you for your reply.
-
Edmuntu over 7 yearssorry for initial short" thank you"....hit return key in error.
-
WinEunuuchs2Unix over 7 yearsIt does explain how to copy and dir and it's contents--the difference is it changes to the source directory first as a precautionary habit.
cp -r
as you recommend is already on the third command line. The tutorial is based on the fact everyone has a/boot
directory and can try this on their own machine. Had I usedcp -r /home/GroundZero/Documents /home/GroundZero/Documents_backup
readers could not copy and paste into their terminal--they would have changeGroundZero
creating more work and chances for error. I hope you appreciate the KISS of this answer with an embedded tutorial. -
WinEunuuchs2Unix over 7 years@GroundZero doing a
cd ~/Documents/SomeObsucreFolder/2016/Nov/
to set current directory as source directory will change the Terminal prompt to ensure it is spelled correctly and then usingcp -r .
as the source eliminates retyping the source directory. It is simply what I consider a good habit. Additionally once changing to source directory it's easy to dols FIleName1
,ls FileName2
, etc. to esnure the files you want to backup are there without having to repeat the source directory name for thels
command. -
TheOdd over 7 years@Edmuntu In the future, use SHIFT+ENTER while commenting to go down a line.
-
Byte Commander over 7 years@OwenHines Just that comments strip any linebreak and additional whitespace and having them in the editor doesn't change anything in the result.
-
TheOdd over 7 years@ByteCommander Oh right, nevermind. Well, now he knows.
-
Hello Universe over 3 yearsThis is much better than copy as it also copied the permissions
-
Tim Rogers almost 3 yearsI think this is the best answer. From the manual:
-a
is the same as-dR --preserve=all