How to Copy Files or Directories in Linux

One of the most prominent features of Linux based systems is the CLI interface. No doubt, other operating systems have them too, but no other operating system has it at the level of importance as Linux based systems. And the fact is, that it is great! CLI is much faster than GUI methods (like graphical file managers), and is much lighter on the system resources. Furthermore, it’s not only better, but it’s sometimes absolutely required in certain situations, like managing web servers. Most web servers are controlled remotely over the Internet, and requires the administrator to have CLI skills.





Today we are going to present a guide on one of the most basic activities on a computer system: Copying files. The command used by the Linux CLI to copy files is ‘cp’. It’s a very simple command with an ample amount of options, to provide flexibility to the users. The cp command and these instructions work on Ubuntu, CentOS, Linux Mint, and other Linux distros. Let us discuss the different uses of cp.

Plain copying

Simple copying of files requires two things: location of the source file, and the destination’s location. Thus, the most basic command has three parts, the command cp, the source file location, and the destination.

Basic copy command example:

cp testfile Documents/

What I just did was cp (copy) a file named testfile to a directory named Documents.

Simple copying.

If the CLI returns no output, it means that copying was successful.

If the testfile was located in a different directory than the one I’m currently in, I could also use a command like:

cp /var/www/testfile Documents/

Copying directories: -r

Copying directories is similar to normal copying of files, but requires just one additional option. That option is ‘-r’. What this option stands for is recursive. If you copy a file recursively, it means that you’re copying the directory and all its contents with it. If you do not add the -r option, the command line will simply omit the directories. Therefore, the basic command to copy directories is:

cp -r DirectoryName DestinationDirectory/

Example:

cp -r testDir/ Documents/
Copying directory.

Copying using a keyword – *

This method of copying often comes up in day-to-day life. Say you have a bunch of songs in a directory and you want to copy the songs of a certain artist to a folder. But all the songs are jumbled up, and there is no arrangement in which you are getting all the songs of an artist all together.

How to do this? Use the following syntax:

cp -r *keyword* DestinationDirectory/

(use -r only if you also want to copy directories).

The ‘*’ mark means everything. Literally. If you had a bunch of songs from Lorde, you’d just use “Lorde” as the keyword and the command would copy all files with “Lorde” in their name. So this command basically covers all files with everything before that keyword, and everything after it. This makes classification and management of information very easy.

Example:

Copying using keyword.

Updating using cp: -u

This feature is mostly for sharing of files, and creating manual backups. If you use the -u option, only the files which are present in the source directory but not present in the destination directory will be copied, and if a file is present in the destination, but is an older version of the same named file in the source, then that will be copied. This becomes useful when files have to be shared between employees of a company, or different storage drives of the same user, which is used to create backups.
Syntax:

cp -ur sourceDirectory/ toBeUpdatedDirectory/

Example:

Copying to update.

Symbolic link copying: -s

If you have limited space on your storage devices, and still want to copy a certain file to more locations, for the sake of arrangement, what do you do? You can use the ‘shortcut’ feature. You won’t actually copy the files, but create shortcuts to the same file, in the location that you want to have it in. This makes the arrangement easy, without filling up your storage devices much.

NOTE: To use symbolic link copying, you have to specify the full path of the source directory/file. To get the full link, go to the directory where the file is, and enter the command:

pwd

This command stands for ‘print working directory’ and will show you the full path to that directory. If you already know the exact location of the source file, you don’t have to do this.

Basic syntax:

cp -sr fullLocationSourceDirectory/  DestinationDirectory/

Example:

Copying links to files.

The last options that we are now going to discuss are not task-based options like the ones we discussed above, but for better user interaction with the process. The options are the following:

Interactive copying: -i

This option asks you if you really want to copy the file or not, if the file is already present in the destination directory. You’ll get a prompt, and just have to type ‘y’ for yes, or ‘n’ for no. This may not be very useful, but if a file exists in the destination with a common name, and you want to preserve it, you can use it.

Example:

cp -i *test* Documents/
Interactive copying.

Verbose options : -v

This option shows the name of the files as they are being copied. The ‘v’ stands for Verbose. This is very useful when your copying gets stuck at a point and you can not figure out at which file. If you use the verbose option, you will be able to see the names of the files as they are being copied, so you can stop which file is the one causing the trouble.

Example:

cp -v *test* testDir/
Verbose copying.

Conclusion on copying files and directories in Linux

That’s all for the important options of the cp command. These options would probably cover all you need, and if they don’t, you can search for more options in the manual pages, or the Internet. Because trust me, everything you need exists. Let us know about any questions in the comments.

If you’re a beginner and want to learn more basic commands, check out How to Rename a File in Linux and How to Delete a Directory in Linux and test them out yourself on a Vultr server.

Leave a comment

Your email address will not be published. Required fields are marked *