Home » Unix command and scripts » how to copy directory in Linux with Examples

how to copy directory in Linux with Examples

While working on development projects or support activities in the Linux operation system, I often need to back up or rename the current working folder /directory before many any changes. Let’s see how we can do accomplish that.

How to copy Files/folder  in Linux Operating system

Linux Operating system provides “cp” command to copy the files/directory from one place to another  

cp  copy files/dirs [copy linux command]

This  command is used in Unix/Linux Operation system to copy the files and directory

cpGeneral Options:
-i Interactive. cp will prompt for confirmation whenever the copy would overwrite an existing target.
-r Recursive. cp will copy the directory and all its files, including any subdirectories and their files to target.
-p Preserve. cp duplicates not only the contents of source_file but also preserves the owner and group id, permissions modes, modification, and access time.
More Options:
-v verbose. cp will show all the files being copied.
-u copy only if the source file is newer
–backup This option will make a backup copy of the file on the destination side.–backup=simple option will create a backup file which marked by a tilde sign (~)–attributes-only  This option will copy the file with zero bytes and the same attribute as source file-f  Force option.  it will force the copying activity. If the destination files cannot be opened, then -f will try again.-a  archive option. It will copy the directory with exact files and directory including symbolic link-L copy without following symbolic link in source-P copy following symbolic link in the source

How it works for General option

If directory d2 does not exist, it is created. Otherwise, it creates a directory named d1 within directory d2.

See also  How to use sed replace string in file
CommandResults
cp x yCopies the contents of x into y. If y does not exist, it is created; otherwise, y is overwritten with the contents of x.
cp -i x yLike above, however, since the “-i” (interactive) option is specified, if y exists, the user is prompted before it is overwritten with the contents of x.
cp x d1Copy the contents of x (into a file named x) inside of directory d1.
cp -R d1 d2Copy the contents of the directory d1.

Examples of copy directory linux command

Copy the file myfile1 to myfile2
cp myfile1 myfile2

Copy multiple files to the directory
cp file1 file2 file3 dir1/

Copy the file preserving the timestamp
cp -p file1 file1_backup

Copy a directory dir1 to dir2
cp -r dir1 dir2

Copy a directory dir1 to home directory
cp -r dir1 /home/user/

Copy the file myfile1 to myfile2 with verbose option
cp -v myfile1 myfile2

Copy the file myfile1 to myfile2 only if source file is newer with verbose option
cp -vu myfile.txt /home/users/

Copy the files using interactive mode
cp -I *.txt dir/

Screenshot of  copy directory linux command

copy directory linux

You can find all the options of cp command using the command

man cp
linux copy directory

Frequently asked question for copy directory command

(1) how to copy multiple directories to a directory in Linux

Answer

You can use a copy command like this

suppose you need to copy directories dir1 dir2 to dir3, then copy like below

cp -r dir1 dir2 dir3/

(2) How to copy the directory tree structure without copying the files inside them

Answer

find SOURCE -type d -exec mkdir -p TARGET/{} \;

Conclusion

Copying or backing up the folder or files is not a scary task. If you know the commands with the option available, it is easy to copy or backup the folder/directory in Linux. We can use either of three commands cp, mv, or rsync according to the need and circumstances. I would suggest you to go over the Unix box and create a directory with sample files  and try the option given above to get hands-on experience with the commands

See also  Linux Operating System and Distributions

What tips are you having for copying the files?

Related Articles

grep command in Linux : Grep command Means – globally search regular expression. grep command in unix is used for searching text, regular expression across multiple files
awk command in unix with examples : Awk command in Unix or Linux is a powerful command for processing text. Learn about awk syntax, records, fields, line Separator with examples of awk in Unix
sed command in Unix examples: sed command is a Stream EDitor – works as a filter processing  input line by line And here are 32 Useful sed command examples in Linux/Unix
how to tar a directory in Linux: how to tar a directory in Linux, tar command example, tar compress directory, tar gzip command , untar/extract command, tar cvfz command, tar xvfz command
RSYNC Command: Check out this post on RSYNC command – sync the data between two directory

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top