The option you’re looking for is
cp -R path_to_source path_to_destination/
destinationdoesn’t exist, it will be created.
copy directories recursively. You can also use
-rsince it’s case-insensitive.
- To copy everything inside the source folder (symlinks, hidden files) without copying the source folder itself use
-aflag along with trailing
/.in the source :
cp -a path_to_source/. path_to_destination/
How to copy a folder with files to another folder in Linux?
You are looking for the
cp command. You need to change directories so that you are outside of the directory you are trying to copy.
If the directory you’re copying is called
dir1 and you want to copy it to your
cp -r dir1/ ~/Pictures/
Linux is case-sensitive and also needs the
/ after each directory to know that it isn’t a file.
~ is a special character in the terminal that automatically evaluates to the current user’s home directory. If you need to know what directory you are in, use the command
When you don’t know how to use a Linux command, there is a manual page that you can refer to by typing:
man [insert command here]
at a terminal prompt.
Also, to auto complete long file paths when typing in the terminal, you can hit Tab after you’ve started typing the path and you will either be presented with choices, or it will insert the remaining part of the path.
There is an important distinction between Linux and Unix in the answer because for Linux (GNU and BusyBox)
--recursive are all equivalent. For portability, i.e. POSIX compliance, you would want to use
-R because of some implementation-dependent differences with
-r. It’s important to read the man pages to know any idiosyncrasies that may arise (this is a good use case to show why POSIX standards are useful).
$ cp -R SRCFOLDER DESTFOLDER/
You can copy the content of a folder
/source to another existing folder
/dest with the command
cp -a /source/. /dest/
-a option is an improved recursive option, that preserve all file attributes, and also preserve symlinks.
. at end of the source path is a specific
cp syntax that allow to copy all files and folders, included hidden ones.
How to copy the contents of a folder to another folder in a different directory using terminal?
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
~/new_folder1 you would use
cp -r ~/folder1/. ~/new_folder1
new_folder1 would then contain all the files and folders from
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.
An alternate is
rsync -a source/ destination
The advantages of
- 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.
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.
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
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
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).
Hope you learned something from this post.
Follow Programming Articles for more!