Top 13 CP Command Useful Examples For Beginner On 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
or: cp [OPTION]... SOURCE... DIRECTORY
or: cp [OPTION]... -t DIRECTORY SOURCE...
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.

[root@worker1 data]# cp file1 /root/
[root@worker1 data]# ls /root/file1 
/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.

[root@worker1 data]# cp -r file1 file2 /root/Desktop/
[root@worker1 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.

[root@worker1 data]# cp -v file1 /root/Desktop/
‘file1’ -> ‘/root/Desktop/file1’
[root@worker1 data]# ls /root/Desktop/
file1

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

[root@worker1 data]# chmod 777 file*
[root@worker1 data]# ll
total 0
-rwxrwxrwx 1 root root 0 Mar 28 17:08 file1
-rwxrwxrwx 1 root root 0 Mar 28 17:08 file2
[root@worker1 data]# cp -p file* /root/Desktop/
[root@worker1 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.

[root@worker1 data]# touch abcd.pdf
[root@worker1 data]# cp *.pdf /root/Desktop/
[root@worker1 data]# ls /root/Desktop/
abcd.pdf

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

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

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.

[root@worker1 ~]# cp /etc/passwd /etc/locale.conf /root/data/
[root@worker1 ~]# 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.

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

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

[root@worker1 Desktop]# touch file1
[root@worker1 Desktop]# echo "testing" > file1 
[root@worker1 Desktop]# cd
[root@worker1 ~]# touch file1
[root@worker1 ~]# 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. 

[root@worker1 data]# touch file1 file2
[root@worker1 data]# cd /root/
[root@worker1 ~]# echo 'testing it' > file3
[root@worker1 ~]# echo 'testing it' > file4
[root@worker1 ~]# cp -u -v file* /root/Desktop/data/
‘file3’ -> ‘/root/Desktop/data/file3’
‘file4’ -> ‘/root/Desktop/data/file4’
cp: omitting directory ‘files’
[root@worker1 ~]# 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’.

[root@worker1 ~]# cp -b file1 /root/Desktop/data/
 [root@worker1 ~]# 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

root@worker1 ~]# ln -s data/file /root/Desktop/data/
[root@worker1 ~]# ls -ld /root/Desktop/data/
drwxr-xr-x 2 root root 78 Mar 28 18:32 /root/Desktop/data/
[root@worker1 ~]# 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

[root@worker1 ~]# cp -P data/file /root/Videos/
[root@worker1 ~]# 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

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

You’re done copy command in linux cp syntax

Share on:

I'm the founder of Curious Viral. I hope this blog will provide you complete information about Linux Technology & I would like to share my technical knowledge with you which I have learned during this period.

Other Posts You May Like...

Leave a comment