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?
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?
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.
cp -a ~/Downloads/textext/. ~/.config/inkscape/extensions/
– enzotib
Feb 06 '14 at 07:58
-p
flag as to preserve the file permissions and timestamps. cp -ap /var/www/original.com/images/. /var/www/new.com/images/
– Dylan Valade
Dec 17 '14 at 02:31
-a
already implies --preserve=all
, that is wider than -p = --preserve=mode,ownership,timestamps
.
– enzotib
Dec 22 '14 at 16:22
cp -a /media/ubuntu/Volume/. '/media/ubuntu/My Passport/'
. And if you want to encrypt your files, then you can use the scp
(Secure Copy) command.
– Benny Code
Jul 09 '15 at 11:13
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 09 '15 at 13:46
cp -r
is recursive copy, -a
add some other option, like --preserve=all
– enzotib
May 04 '16 at 20:21
.
so the command will work in the local directory, just not to confuse the begginers
– Anton Kastritskiy
Jun 07 '16 at 16:00
-a
option even set up git in my destination directory.
– Muhammad Ali
Mar 23 '17 at 15:10
cp -d
, you may also add --preserve=all
– enzotib
Apr 10 '17 at 06:43
mv
command does not list the -a flag. Could the cp
move files instead of copying them with another flag?
– emagar
Apr 21 '18 at 08:58
rsync
is installed, so it is important to know how to achieve similar with GNU cp
. Thanks.
– Felipe Alvarez
Apr 13 '19 at 07:47
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 07 '19 at 16:15
/dest/source/
i just want the contents of /source/
not the folder itself :(
– CpILL
Nov 29 '20 at 23:21
mypath/.
instead of the existing bash wildcard expansion character, mypath/*
?
– Elysiumplain
Apr 07 '22 at 01:39
An alternate is rsync
:
rsync -a source/ destination
The advantages of rsync
are:
rsync -r source/ destination
should be enough, no?
– Joschua
Dec 17 '15 at 15:10
rync source/ dest
Obviously people will need to customize the rsync command to function as they wish.
– Panther
Mar 10 '17 at 16:40
--delete
flag to delete files from destination
that aren't in source
– Alex Varga
Aug 29 '17 at 17:49
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.
~/new_folder1/folder1
instead of copying the contents over.
– wisbucky
Jan 19 '15 at 23:00
[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]#
– Bruno Pereira
Jul 24 '18 at 11:19
cp -a /source/. /dest/
vs.
– HeggyHere Dec 22 '18 at 07:15cp -r ~/source/* ~/dest/
~/folder1/new_folder1/
, period doesn't seem to have an effect? Which version of bash does this work for?
– CpILL
Nov 29 '20 at 23:24
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.
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
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".
-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.
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:
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).