Scripting

How to Copy Multiple Files Using cp command in Linux

Introduction

The cp command in Linux is used to copy files and directories by using the command line. This command also allows you to move multiple files and folders at once while preserving their backup and attribute information. So the cp command is very useful for Linux users.

Now we’re gonna guide you about copying multiple files using the cp command in Linux. Hope you understand.

Syntax to copy a file of the cp command

$ cp [filename] [filename2]

For example, we’ll see what’s in our current directory by the ls command

Now, we will copy the hello.pl file:

$ cp hello.pl hello1.pl

Then use the ls command to check:

Syntax to copy multiple files of the cp command

You must provide a filename and destination directory to do this.

The syntax:

$ cp [filename] [filename1] [folder name]/

For example, we’ll see what’s in our current directory by the ls command.

We will copy hello.pl and hello1.pl files into the test directory.

$ cp hello.pl hello1.pl test/

Output:

Syntax to copy a directory of the cp command

To copy the directory we must use the -r option:

$ cp -r foldername/ foldername 1

Now we will try to copy the test directory:

$ cp -r test/ test2

Output:

Syntax to copy multiple directories of the cp command

The syntax:

$ cp -r foldername foldername 1 foldername 2

The destination folder will be at the end.

For example:

$ cp -r test test2 testfinal

Output:

In the above example, we copied test and test2 folders into testfinal folder.

Backup when copying files

Let’s use the -b option to backup when copying files:

$ cp -b [filename] [filename2]

For example, we will backup the hello.pl file:

$ cp -b hello.pl hellobk.pl

Output:

Conclusion

And we guided you on how to copy multiple files using the cp command in Linux.

Thank you for checking it out!

Similar Posts