filmov
tv
Copy directory contents using 'cp' command (7 Solutions!!)
Показать описание
Copy directory contents using 'cp' command
The Question: How do you copy all the contents of one directory into another?
For example:
$ cd /home/newuser
$ cp -a /backup/olduser/* .
The problem with the above is that the globing pattern '*' matches the hidden
directories '.' and '..' and you end up with a directory 'olduser' inside
'newuser', as well as the contents.
You could also do something like this:
$ rmdir /home/newuser
$ cp -a /backup/olduser /home/newuser
But what if newuser already contains some default files and directories?
What is the simplest, most correct, easiest to remember and not mess-up way to
move the contents of one directory to another using just the basic 'cp' command
and the shell?
Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful
== This solution helped 24 people ==
Two directories a and b.
Both have files in.
You are in a directory that contains a and b.
cp -r ./a b
-r = recursively.
== This solution helped 32 people ==
Try:
cp -ra /backup/olduser/. /home/newuser
== This solution helped 6 people ==
Unless you have seriously reconfigured your shell, the globbing pattern '*'
does not match '.' or '..', as you can verify using just echo *. What it does
instead do is omit files whose name begins with a '.', so your approach will
miss all hidden files. You can tweak some of this behavior with shell options,
for example the dotglob option in bash, but it then won't be the portable and
robust option that you are looking for.
If you need to do this more than once or twice, I recommend that you look into
rsync or unison (depending on specific needs) with carefully crafted source and
target specifications.
Another alternative is to put the source directory in a tarball and untar it
over the existing target directory.
To copy files that begins with a dot just do cp .* target/
So easiest is just to do the cp command two times.
As Peter Eisentraut sais normal globbing rules do not include .. and . (hm, how
to end this sentence? ;)
Just use -r to make it recursive and -i to make cp ask whether you really want
to overwrite a file.
cp -ri /backup/olduser/* /newuser/
cp -ri /backup/olduser/.* /newuser/
== This solution helped 19 people ==
Remember that, by default, cp copies the first directory into the second
directory if the second exists.
For example cp -r a b will copy the directory a into b. If b does not exist, it
will be created with the contents of a.
If you want to copy the content of a into b (for example when copying a whole
filesystem into a mount point) use:
cp -r a/. b
as in the previous answer.
Please also note that -a, used in some of the other answers, is the same as -dr
--preserve=all and will preserve timestamps, context and extended attributes.
The Question: How do you copy all the contents of one directory into another?
For example:
$ cd /home/newuser
$ cp -a /backup/olduser/* .
The problem with the above is that the globing pattern '*' matches the hidden
directories '.' and '..' and you end up with a directory 'olduser' inside
'newuser', as well as the contents.
You could also do something like this:
$ rmdir /home/newuser
$ cp -a /backup/olduser /home/newuser
But what if newuser already contains some default files and directories?
What is the simplest, most correct, easiest to remember and not mess-up way to
move the contents of one directory to another using just the basic 'cp' command
and the shell?
Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful
== This solution helped 24 people ==
Two directories a and b.
Both have files in.
You are in a directory that contains a and b.
cp -r ./a b
-r = recursively.
== This solution helped 32 people ==
Try:
cp -ra /backup/olduser/. /home/newuser
== This solution helped 6 people ==
Unless you have seriously reconfigured your shell, the globbing pattern '*'
does not match '.' or '..', as you can verify using just echo *. What it does
instead do is omit files whose name begins with a '.', so your approach will
miss all hidden files. You can tweak some of this behavior with shell options,
for example the dotglob option in bash, but it then won't be the portable and
robust option that you are looking for.
If you need to do this more than once or twice, I recommend that you look into
rsync or unison (depending on specific needs) with carefully crafted source and
target specifications.
Another alternative is to put the source directory in a tarball and untar it
over the existing target directory.
To copy files that begins with a dot just do cp .* target/
So easiest is just to do the cp command two times.
As Peter Eisentraut sais normal globbing rules do not include .. and . (hm, how
to end this sentence? ;)
Just use -r to make it recursive and -i to make cp ask whether you really want
to overwrite a file.
cp -ri /backup/olduser/* /newuser/
cp -ri /backup/olduser/.* /newuser/
== This solution helped 19 people ==
Remember that, by default, cp copies the first directory into the second
directory if the second exists.
For example cp -r a b will copy the directory a into b. If b does not exist, it
will be created with the contents of a.
If you want to copy the content of a into b (for example when copying a whole
filesystem into a mount point) use:
cp -r a/. b
as in the previous answer.
Please also note that -a, used in some of the other answers, is the same as -dr
--preserve=all and will preserve timestamps, context and extended attributes.