copy directory linux command examples

While working on project/development in Linux operation system , we often need to  backup the current working folder i.e copy directory .  We will be seeing here various ways to copy directory linux.

Linux Operating system provides cp command to copy the files/directory from one place to another  and mv command to move the file/directory.I would explaining each command in details here with many examples

cp  copy files/dirs

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


cp General 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 file being copied.
-u copy only if the source file is newer
–backup This option will make backup copy of the file in destination side.–backup=simple option will create a backup file which marked by a tilde sign (~)

–attributes-only  This option will copy file with zero bytes and 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 source

How it works for General option

Command Results
cp x y Copies 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 y Like 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 d1 Copy the contents of x (into a file named x) inside of directory d1.
cp -R d1 d2 Copy the contents of the directory d1. If directory d2 does not exist, it is created. Otherwise, it creates a directory named d1 within directory d2.

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 wih 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

mv – move files(s) /dirs

Options: –

-f   mv will move the file(s) without prompting even if it is writing over an existing target.

-i   mv will prompt for confirmation whenever the  move would  overwrite  an  existing  target


  1.   mv –i f1.txt f2.txt

[moves f1.txt to f2.txt, if f2.txt exists prompts for confirmation.]

2.   mv file1 file2 file3  dest_directory.

[ moves file1 to 3 to dst_directory.]

3.    mv –r oracle/app /u000

[ moves directory app and all its subdirectories files to /u000 directory]

4. mv f1.txt oracle/f2.txt

[ moves directory app and all its subdirectories files to /u000 directory]


You can find all the option of cp and mv using the command man cp,man mv

linux copy directory

I hope you like the content on linux copy directory command .

Related Articles

Unix Tutorial: how to use grep command in unix/linux

awk command in unix with examples

Awesome sed command in unix examples

How to use tar command in Unix