Cyberithub

8 Popular Linux cp command examples for Professionals

Advertisements

In this tutorial, I will take you through 8 Popular Linux cp command examples for professionals. Linux cp command is an open source command which usually comes with Linux System used for copying files and directories within a local System. This command is used by almost all Kind of Linux Professionals on day to day basis for performing multiple complex Operations.

I have already covered basics cp command examples in my previous articles and now we will go and check few advanced usage of cp command in Linux.

8 Popular Linux cp command examples for Professionals 1

Linux cp command 

Also Read: 10 Best Linux Copy File Command Examples

1. Copy Files Interactively Using linux cp command

You can use -i option to copy files interactively from source to destination. In this example, I am trying to copy file2.txt interactively to /opt destination. If you want you can also use -v option to visualize the progress of linux cp command operations.

[root@localhost ~]# cp -i file2.txt /opt/

2. Copy Entire Contents using wildcard(*) character

If you want to copy entire contents of current directory or from current path, then you can easily do that by using wildcard character as shown below. Here * means everything from current directory. Hence in this example we are trying to copy all the files from current directory to /root destination path as shown below.

[root@localhost example]# cp -v * /root/
'file2.txt' -> '/root/file2.txt'
'file.list' -> '/root/file.list'
'file.txt' -> '/root/file.txt'

NOTE:

Please note that if in the current path you have directories also along with the files, then you might you want use -r or -R option to recursively copy all the directories and its contents along with the files.

You can also do the same thing by specifying current path using dot(.) operator as shown below.

[root@localhost example]# cp -v ./* /root/
'./file2.txt' -> '/root/file2.txt'
'./file.list' -> '/root/file.list'
'./file.txt' -> '/root/file.txt'

3. Copy File Using Complete Source Path

You can always copy files by specifying complete Source Path as shown below. In this example, we are trying to copy file2.txt to destination path /root.

[root@localhost example]# cp /root/example/file2.txt /root/

4. Copy multiple Files at Once using linux cp command

You can copy multiple files at once by specifying the file name in source path. Here we are trying to copy file.txt and file2.txt from current path to /root path. You can also use -v option to visualize the progress.

[root@localhost example]# cp file.txt file2.txt /root/

5. Running Multiple Linux Copy File Commands

If you want to launch multiple copy commands in one line then you can launch and end with semicolon(;) to separate out the commands from each other.

[root@localhost example]# cp -v file.txt /root/;cp -v file2.txt /root/test/
'file.txt' -> '/root/file.txt'
'file2.txt' -> '/root/test/file2.txt'

6. Copy Same file in multiple destinations

If you want to copy same file in multiple directories or multiple destinations in one line of command then probably you can use multiple linux cp command in one line separated by semicolon(;) as shown below.

[root@localhost example]# cp -v file.txt /root/;cp -v file.txt /root/test/
'file.txt' -> '/root/file.txt'
'file.txt' -> '/root/test/file.txt'

7. Copying multiple files Using for loop

First of all you need to create a file and put all the file names along with its complete path in that file list as shown below.

[root@localhost example]# cat file.list
/root/example/file.txt
/root/example/file2.txt

Then, you can copy using for loop as shown below. Here we are looping through the contents of file.list and copying it through linux cp command to /root destination path.

[root@localhost example]# for i in $(cat file.list);do cp -v $i /root/;done
'/root/example/file.txt' -> '/root/file.txt'
'/root/example/file2.txt' -> '/root/file2.txt'

8. Using backup Option to take Linux Backup

If you want to take Linux backup using cp command then you can use -b that comes in handy with cp command as shown below.

[root@localhost example]# cp -bv file2.txt /root/
'file2.txt' -> '/root/file2.txt'

 

Also Read: Create Ansible Roles From Scratch

Leave a Comment