How can I copy the contents of a folder to another folder in a different directory using terminal?
Solution 1
You can copy the content of a folder /source
to another existing folder /dest
with the command
cp -a /source/. /dest/
The -a
option is an improved recursive option, that preserve all file attributes, and also preserve symlinks.
The .
at end of the source path is a specific cp
syntax that allow to copy all files and folders, included hidden ones.
Solution 2
An alternate is rsync
:
rsync -a source/ destination
The advantages of rsync
are:
- After the initial sync, it will then copy only the files that have changed.
- You can use it over a network, convenient for files in $HOME, especially config files.
Solution 3
Lets say you have a folder called folder1 in your ~
, inside folder1 is 1 file called file1 and 2 folders called sub1 and sub2 each with other files and folders inside them.
To copy all the contents of ~/folder1
to ~/new_folder1
you would use
cp -r ~/folder1/. ~/new_folder1
new_folder1
would then contain all the files and folders from folder1
.
cp
is the command to copy using a terminal, -r
makes it recursively (so, current directory + further directories inside current) ~/folder1
is the origin folder, ~/new_folder1
is the destination folder for the files/folders inside the origin.
Solution 4
Simple example.
Copy the directory dir_1 and its contents (files) into directory dir_2:
cp -r ./dir_1 ./dir_2
# or
cp -r ./dir_1/ ./dir_2/
# Results in: ./dir_2/dir_1/_files_
Copy only the contents (files) of dir_1 into directory dir_2:
cp -r ./dir_1/. ./dir_2
# or
cp -r ./dir_1/. ./dir_2/
# Results in: ./dir_2/_files_
_files_
is a placeholder for the actual files located in the directory.
Solution 5
Check this http://www.cyberciti.biz/faq/copy-folder-linux-command-line/ for more information on copying folder. Hope this helps.
cp Command
cp
is a Linux command for copying files and directories. The syntax is as follows:
cp source destination
cp dir1 dir2
cp -option source destination
cp -option1 -option2 source destination
In this example copy /home/vivek/letters
folder and all its files to /usb/backup
directory:
cp -avr /home/vivek/letters /usb/backup
Where,
-a
: Preserve the specified attributes such as directory an file mode, ownership, timestamps, if possible additional attributes: context, links, xattr, all.
-v
: Explain what is being done.
-r
: Copy directories recursively.
Example
Copy a folder called /tmp/conf to /tmp/backup:
$ cp -avr /tmp/conf/ /tmp/backup
Related videos on Youtube
pandisvezia
Updated on September 18, 2022Comments
-
pandisvezia almost 2 years
I am trying to copy the contents of a folder to another folder in a different directory using terminal.
Would somebody be able to provide me an example of the command line syntax required to achieve this?
-
irumata over 12 yearsit does not catch hidden files
-
pandisvezia over 12 yearsThank you Bruno! It helped me to understand the syntax, though I had to change it a bit(removing ~ sign). Maybe because the destination folder was in /opt, which resides in another file system. And thank you Portablejim to remember the hidden file thing!
-
enzotib over 10 years@Funzies: probably your command should be:
cp -a ~/Downloads/textext/. ~/.config/inkscape/extensions/
-
Dylan Valade over 9 yearsAdd
-p
flag as to preserve the file permissions and timestamps.cp -ap /var/www/original.com/images/. /var/www/new.com/images/
-
enzotib over 9 years@DylanValade:
-a
already implies--preserve=all
, that is wider than-p = --preserve=mode,ownership,timestamps
. -
wisbucky over 9 yearsThe trailing period is important. Without it, sometimes it may create a new subdirectory
~/new_folder1/folder1
instead of copying the contents over. -
AStopher over 9 yearsThis isn't what the question asks.
-
Benny Neugebauer almost 9 yearsNote: If you directory name contains spaces, then you need to quote the arguments like
cp -a /media/ubuntu/Volume/. '/media/ubuntu/My Passport/'
. And if you want to encrypt your files, then you can use thescp
(Secure Copy) command. -
enzotib almost 9 years@BennyNeugebauer:
scp
is used to copy over a network (throughssh
) and only encrypts the communication channel, not the files on the destination filesystem. -
Joschua over 8 yearsI think you don't need the asterisk.
rsync -r source/ destination
should be enough, no? -
enzotib about 8 years@PawelCioch:
cp -r
is recursive copy,-a
add some other option, like--preserve=all
-
AntK about 8 yearsI'd suggest prepending the folder paths in the answer with
.
so the command will work in the local directory, just not to confuse the begginers -
Shridutt Kothari almost 8 yearsThis one is more appropriate: 'rsync -rtvp source/* destination'
-
Nam G VU almost 8 years-r not works for hidden files/folders
-
sauerburger over 7 yearsThis will not copy hidden files, since bash expands * only to non-hidden file. The solution by @Joschua is safer.
-
Panther over 7 yearsrsync will copy the entire directory, including hidden files, if you use the trailing / without a * so
rync source/ dest
Obviously people will need to customize the rsync command to function as they wish. -
Muhammad Ali over 7 yearsSuch a great answer! The
-a
option even set up git in my destination directory. -
user1271772 about 7 yearsWhat I like about -a is that it preserves symlinks. What I don't like is that it copies recursively (including the sub-folders, which is not what cp usually does). How can I preserve symlinks without copying sub-folders recursively?
-
enzotib about 7 years@user1271772: to preserve symlinks without recursing directories you need
cp -d
, you may also add--preserve=all
-
Katu almost 7 yearsI rather the verbose and all option:
rsync -av source/ destination
-
Alex Varga almost 7 yearsYou can use the
--delete
flag to delete files fromdestination
that aren't insource
-
cristoper over 6 years(Using -r with -a is redundant; on gnu cp -a is the same as "-dr --preserve=all")
-
emagar about 6 yearsThe
mv
command does not list the -a flag. Could thecp
move files instead of copying them with another flag? -
Alex78191 almost 6 yearsWhy not
cp -r ~/folder1/* ~/new_folder1
-
Bruno Pereira almost 6 years@Alex78191
[root@ home]# mkdir food [root@ home]# cd food/ [root@ food]# mkdir .fruit [root@ food]# mkdir veggies [root@ food]# touch veggies/carrots [root@ food]# touch .fruit/apple [root@ food]# ls * carrots [root@ food]#
-
Mark Deven almost 6 yearsIs the rsync command faster than CP? IT is doing like one 10 KB text file per 10 seconds.
-
Panther almost 6 yearsI dont know , but it will only transfer new or changed files so will be simple to script
-
Matthew Lock over 5 yearsIf it's a big folder you may wish to use one of these options to view progress while it's copying askubuntu.com/questions/609303/…
-
IgorGanapolsky over 5 yearscp: the -R and -r options may not be specified together.
-
HeggyHere over 5 years@Alex78191 Why do we use * vs . ? > cp -a /source/. /dest/ vs. > cp -r ~/source/* ~/dest/
-
Alex78191 over 5 years@HeggyHere why?
-
Felipe Alvarez about 5 yearsOne cannot always presume
rsync
is installed, so it is important to know how to achieve similar with GNUcp
. Thanks. -
Bruno Pereira almost 5 years"The . at end of the source path is a specific cp syntax that allow to copy all files and folders, included hidden ones.", this is not related to cp, but related to bash. The dot means "this location" and avoids the use of bash globbing, where by default files/directories starting with a . are not expanded.
-
enzotib almost 5 years@BrunoPereira: my sentence is wrong, but I think
bash
has nothing to do with this. It is UNIX that indicates the current directory with a dot, andcp
, when given two directoriespath/foo
andbar
as arguments and a recursive option, like-a
, creates a new directoryfoo
underbar
and copies all files contained inpath/foo
underbar/foo
. Ifbar/foo
already exists, the creation step is skipped. This is what happens here, given thatdest/.
already exists. -
unbreak over 4 yearsWhat if in source directory i will remove something after init sync, and run again sync? It will be removed also in destination, or only new things will be copied?
-
Ali over 3 yearsI would suggest
yes | cp -a /source/. /dest/
to accept the replacing -
c z over 3 yearsThe OP asks "copy the contents of a folder to another folder". Removing the asterisk means we now copy the folder itself, which isn't the right answer when we wish to retain the target (e.g. because it's a drive or has existing content).
-
flow2k over 3 years@enzotib Following up on Bruno Pereira's thread, could you edit your answer to remove the part about "specific cp syntax"? I think it's a bit misleading. Otherwise, it's a great answer.
-
CpILL over 3 yearsthis gives me
/dest/source/
i just want the contents of/source/
not the folder itself :( -
CpILL over 3 yearsI'm still getting
~/folder1/new_folder1/
, period doesn't seem to have an effect? Which version of bash does this work for? -
intagli over 3 yearsI think it should be noted that if the destination folder already exists, it and its contents might be overwritten by cp. (unix.stackexchange.com/questions/275969/…)
-
Artur Meinild almost 3 yearsThis isn't a good answer, since the asterisk wildcard only matches alphanumeric characters. So this answer would only match files starting with an alphanumeric, so hidden files (starting with
.
would be excluded). -
bac0n almost 3 yearsbetter using
cp -T folder1 folder2
it may also be wise to append-n
or-b
-
Indacochea Wachín over 2 yearsI want to copy a fonts files from a source software to .fonts in home, and for my working: cp -a fonts/. ~/.fonts/Shutter-Encoder
-
Elysiumplain about 2 yearsCan someone explain the need behind use of
mypath/.
instead of the existing bash wildcard expansion character,mypath/*
? -
enzotib about 2 years@Elysiumplain: you missed the "included hidden files" part