1312

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?

1
  • Ciao @pandisvezia! Serviva anche a me questa cosa!
    – djjeck
    Feb 22 at 22:24

8 Answers 8

1953

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.

22
  • 7
    @DylanValade: -a already implies --preserve=all, that is wider than -p = --preserve=mode,ownership,timestamps.
    – enzotib
    Dec 22, 2014 at 16:22
  • 2
    @BennyNeugebauer: scp is used to copy over a network (through ssh) and only encrypts the communication channel, not the files on the destination filesystem.
    – enzotib
    Jul 9, 2015 at 13:46
  • 1
    Such a great answer! The -a option even set up git in my destination directory. Mar 23, 2017 at 15:10
  • 5
    "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. Sep 5, 2019 at 12:13
  • 2
    @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, and cp, when given two directories path/foo and bar as arguments and a recursive option, like -a, creates a new directory foo under bar and copies all files contained in path/foo under bar/foo. If bar/foo already exists, the creation step is skipped. This is what happens here, given that dest/. already exists.
    – enzotib
    Sep 7, 2019 at 16:15
199

An alternate is rsync:

rsync -a source/ destination

The advantages of rsync are:

  1. After the initial sync, it will then copy only the files that have changed.
  2. You can use it over a network, convenient for files in $HOME, especially config files.
12
  • 7
    I think you don't need the asterisk. rsync -r source/ destination should be enough, no?
    – Joschua
    Dec 17, 2015 at 15:10
  • 1
    This one is more appropriate: 'rsync -rtvp source/* destination' Sep 16, 2016 at 14:14
  • -r not works for hidden files/folders
    – Nam G VU
    Sep 21, 2016 at 4:42
  • 1
    This will not copy hidden files, since bash expands * only to non-hidden file. The solution by @Joschua is safer. Mar 9, 2017 at 23:00
  • 2
    If 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/… Nov 13, 2018 at 5:19
129

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.

8
  • 7
    it does not catch hidden files Dec 11, 2011 at 13:04
  • 1
    Thank 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! Dec 11, 2011 at 15:51
  • 7
    The trailing period is important. Without it, sometimes it may create a new subdirectory ~/new_folder1/folder1 instead of copying the contents over.
    – wisbucky
    Jan 19, 2015 at 23:00
  • 5
    Why not cp -r ~/folder1/* ~/new_folder1
    – Alex78191
    Jul 24, 2018 at 10:59
  • @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]# Jul 24, 2018 at 11:19
51

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.

25

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
3
  • 3
    This isn't what the question asks.
    – AStopher
    Mar 8, 2015 at 13:18
  • 1
    (Using -r with -a is redundant; on gnu cp -a is the same as "-dr --preserve=all")
    – cristoper
    Feb 14, 2018 at 16:35
  • 1
    cp: the -R and -r options may not be specified together. Dec 19, 2018 at 23:07
6

This code with Flag "-R" copies perfectly all the contents of "folder1" to existing "folder2":

cp -R folder1/. folder2

Flag "-R" copies symbolic links as well but Flag "-r" skips symbolic links so Flag "-R" is better than Flag "-r".

  • The latest GNU Grep 3.7:
-R, --dereference-recursive

For each directory operand, read and process all files in that directory, 
recursively, following all symbolic links.
-r, --recursive

For each directory operand, read and process all files in that directory, 
recursively. Follow symbolic links on the command line, but skip symlinks 
that are encountered recursively. Note that if no file operand is given, 
grep searches the working directory. This is the same as the 
‘--directories=recurse’ option.
0
5

I like this command

rsync -av --progress ~/code/project-source/. ~/code/project-destination --exclude .git --exclude node_modules

Some of the commonly used options in rsync command are listed below:

  • -v, –verbose: Verbose output
  • -q, –quiet: suppress message output
  • -a, –archive: archive files and directory while synchronizing ( -an equal to following options -rlptgoD)
  • -r, –recursive: sync files and directories recursively
  • -b, –backup: take the backup during synchronization
  • -u, –update: don’t copy the files from source to destination if destination files are newer
  • -l, –links: copy symlinks as symlinks during the sync
  • -n, –dry-run: perform a trial run without synchronization
  • -e, –rsh=COMMAND: mention the remote shell to use in rsync
  • -z, –compress: compress file data during the transfer
  • -h, –human-readable: display the output numbers in a human-readable format
  • –progress: show the sync progress during transfer
3

If there are two folders: (with write permission)

drwxr-xr-x 4 vimal vimal  4096 Sep  9 12:17 .
drwxr-xr-x 3 root  root   4096 Aug 18 14:35 ..
drwxrwxrwx 6 vimal vimal  4096 Sep  9 12:15 DATA
drwxrwxrwx 7 vimal vimal  4096 Sep  9 12:15 PORTAL

If you are inside the folder called PORTAL where you want to copy all content of another folder say DATA at the same level then you will do

vimal@vimal-D3H:/var/www/html/PORTAL$ cp -a ../DATA/. .

You have to notice 2 dots. Last dot says copy here in present folder

and

one following /DATA/. says that all the CONTENTS inside DATA folder to be copied, and not the DATA folder itself.

If you remove this trailing "." from /DATA/

then whole DATA folder will be copied inside PORTAL(from where you are coping).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .