copy command in linux

Top 13 CP Command Useful Examples For Beginner On Linux

We have explained the best top 13 cp command in Linux. You can transfer the files using the copy & cp command with below syntax in Linux. This command is used to copy files and directories in Unix and Linux machines.

We use want to copy some files from one location to another location. So, we have a cp command in Linux that can help us to copy files. We have shown you the cp command example in Linux. Let’s see, how to use cp command with examples in Linux. We are also have known this command as copy a folder in Linux.

Syntax: cp usage Linux

Usage: cp [OPTION]... [-T] SOURCE DEST
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

Example 1: you can simply use the ‘cpcommand to copy the files to the destination folders.

[[email protected] data]# cp file1 /root/
[[email protected] data]# ls /root/file1 

Check Out: Install Python 3 Version Using Source Code and Yum On Linux

Example 2: We can recursively copy the files and directory using ‘-r’  or ‘-R‘ options.

[[email protected] data]# cp -r file1 file2 /root/Desktop/
[[email protected] data]# ls /root/Desktop/
file1 file2

These will copy all files and subdirectories.

Example 3: you can also see the output while copying the files from one location to another with the ‘-v’ option.

[[email protected] data]# cp -v file1 /root/Desktop/
‘file1’ -> ‘/root/Desktop/file1’
[[email protected] data]# ls /root/Desktop/

Examples 4: if you require to copy the file with the same permission, ownership, and timestamp. We can use ‘-p’ options.

[[email protected] data]# chmod 777 file*
[[email protected] data]# ll
total 0
-rwxrwxrwx 1 root root 0 Mar 28 17:08 file1
-rwxrwxrwx 1 root root 0 Mar 28 17:08 file2
[[email protected] data]# cp -p file* /root/Desktop/
[[email protected] data]# ll /root/Desktop/
total 0
-rwxrwxrwx 1 root root 0 Mar 28 17:08 file1
-rwxrwxrwx 1 root root 0 Mar 28 17:08 file2

Check Out: How To Mount Same Volume To Multiple Container In Docker

Examples 5: we can only copy files with a particular extension like .pdf, .txt, etc.

[[email protected] data]# touch abcd.pdf
[[email protected] data]# cp *.pdf /root/Desktop/
[[email protected] data]# ls /root/Desktop/

Examples 6: Prompt before overwriting the files with the ‘-i’ option.

[[email protected] data]# cp -i abcd.pdf /root/Desktop/
cp: overwrite ‘/root/Desktop/abcd.pdf’? yes
[[email protected] data]# ls /root/Desktop/

We can check whether files are present already to the destination using the above options. it will prompt you if the file is present.

Check Out: How To Solve Sentrifugo PHP Mailer Autoloader Error On Linux

Examples 7: Copy multiple files at the same time in the same directory. We will copy the few files to the same directory /root/data.

[[email protected] ~]# cp /etc/passwd /etc/locale.conf /root/data/
[[email protected] ~]# ls /root/data/
locale.conf passwd

Examples 8: Copy files and directory with ‘-a’ that will achieve the files to the destination and we generally use ‘-r‘ or ‘-R‘ option to copy the files.

[[email protected] ~]# cp -a /root/data /root/Desktop/data/
[[email protected] ~]# ls /root/Desktop/data/
[[email protected] ~]# ls /root/Desktop/data/data/
abcd.pdf file1 file2 locale.conf passwd

Examples 9: Copy files without overwriting with the ‘-n’ option.

[[email protected] Desktop]# touch file1
[[email protected] Desktop]# echo "testing" > file1 
[[email protected] Desktop]# cd
[[email protected] ~]# touch file1
[[email protected] ~]# cp -n file1 /root/Desktop/file1

Check Out: How To Install Java Using Apt-Get Command In Ubuntu

Examples 10: Update the newer files on the destination with the ‘-u’ option. 

[[email protected] data]# touch file1 file2
[[email protected] data]# cd /root/
[[email protected] ~]# echo 'testing it' > file3
[[email protected] ~]# echo 'testing it' > file4
[[email protected] ~]# cp -u -v file* /root/Desktop/data/
‘file3’ -> ‘/root/Desktop/data/file3’
‘file4’ -> ‘/root/Desktop/data/file4’
cp: omitting directory ‘files’
[[email protected] ~]# ls /root/Desktop/data/
file1 file2 file3 file4

Examples 11: we can also have the backup of the files to the destination folder using ‘-b’ or ‘–backup’.

[[email protected] ~]# cp -b file1 /root/Desktop/data/
 [[email protected] ~]# ls /root/Desktop/data/
file1 file1~ file2 file3 file4

Check Out: How To Create bucket In S3 Storage On AWS Cloud

Examples 12: we can copy the files without symbolic links using ‘-P’ options. Suppose we have created a file and also created a symbolic link on /root/Desktop/data/ so

[email protected] ~]# ln -s data/file /root/Desktop/data/
[[email protected] ~]# ls -ld /root/Desktop/data/
drwxr-xr-x 2 root root 78 Mar 28 18:32 /root/Desktop/data/
[[email protected] ~]# ls -l /root/Desktop/data/
total 8
lrwxrwxrwx 1 root root 9 Mar 28 18:32 file -> data/file

I will copy the file to the destination folder. You will see there won’t be a symbolic link on it. so

[[email protected] ~]# cp -P data/file /root/Videos/
[[email protected] ~]# ls -l /root/Videos/
total 0
-rw-r--r-- 1 root root 0 Mar 28 18:38 file

Examples 13: Copy files and directory forcefully using ‘-f’ options. so

[[email protected] ~]# cp -f anaconda-ks.cfg /root/Desktop/data/
[[email protected] ~]# ls /root/Desktop/data/

You’re done copy command in linux cp syntax


Please enter your comment!
Please enter your name here