How to Copy Files in Linux With the cp Command

Copying files and directories can be quick and painless in Linux if you utilize the cp command. We’ll learn how to use cp and take advantage of its handy options that will make your copy jobs safer.

What Is the cp Command in Linux?

A command-line utility for Unix and Linux systems capable of copying both files and folders, cp is available on essentially every Linux distro. You’ll see it referenced often in guides for accomplishing file management tasks in Linux.

You don’t need to be terminal whiz to use it. Its syntax is simple, and it’s easiest to use if you open the terminal in (or cd to) the directory containing the files you want to copy.

cp Syntax

The syntax for cp is very similar to the mv command, requiring you simply to specify a source or sources (the files or folders you want copied) and a destination (the directory or file name for the copies).

cp [options] >source>... >destination>

Your command can contain multiple sources, but there can only be one destination. The destination can be another directory, a new file name, or both.

Copying Files With cp

If you want to produce a copy of a single file in the same directory, but with a different name, use a command like this:

cp file.txt newfile.txt

To do the same thing, but in a sub-directory, use a command like this:

cp file.txt Backup/newfile.txt

The above command assumes you have a folder named Backup within your current directory.

If you don’t want a new name for the file, just specify the directory and not the file name in the destination:

cp file.txt Backup

To copy multiple files with cp, simply write out all the files you want copied, separated by a space, before giving the destination.

cp file1.txt file2.txt file3.txt Backup

To save time in copying multiple files, you can use the wildcard tag, an asterisk (*), to automatically copy all files in the directory with the same extension, using something like the example below:

cp *.txt Backup

The above command will find all files that end with .txt in the current directory and copy them to the Backup directory.

Of course, this is only useful if all or most of the files you want to copy have the same extension, or something else in common in the file name.

Using cp To Copy a Folder/Directory

If you want a directory and all its contents copied to a new location, you’ll need to specify the -R option. Here’s an example:

cp -R Files Backup

The above command will copy the folder Files and place the copy inside a folder named Backup.

If you want the contents of a folder copied, but not the folder itself, you’ll need to use the -T option:

cp -RT Files Backup

Useful cp Options

These commonly used options for the cp command are mainly helpful if you’re concerned about possible overwrites or file attribute conflicts.

You’ll notice in the previous commands that you get no message from the terminal confirming that anything happened. To see what’s going on, use the verbose option, -v:

cp -v file.txt newfile.txt

If you’re concerned about an accidental overwrite, you can set the -i option to always ask for confirmation when there’s a file name conflict. For example:

cp -i file.txt newfile.txt

With the above command, if there’s a file named newfile.txt already present, cp will ask if you’re sure you want to overwrite it.

You can also preserve file attributes, such as user ownership, file mode, and modification dates, with the -p option:

cp -v file.txt newfile.txt

The preserve option is helpful if you have multiple users on your system, or if you have another syncing operation that’s sensitive to file modification dates.

Command Line Copying Confidence

You now know how to use cp to copy files with ease and safety, and you’ve taken an important step in becoming comfortable and versatile using the Linux command line.

If you need to create large backups of your files, there are several tools available for Linux users that you might want to consider.

Source: makeuseof.com

Related posts

Is This Free Adobe Illustrator Alternative Better Than the Real Deal?

Why You Shouldn’t Trust ChatGPT to Summarize Your Text

How to Turn Off AMBER Alerts on Your Smartphone