Site icon LinuxAndUbuntu

Copy files & directories in Linux

Copy files or directories in Linux

Copy files or directories in Linux

In Linux, we copy files as we do in any other operating system available today. We open up a file manager, select a file to copy, and paste it anywhere we want. But what if you want or need to copy files or directories in Linux through the command line or apply some additional filters before copying? Well, that’s easy.

Copying files or directories locally

If you need to copy files or directories locally from one directory to another, there is a simple command for that – cp.

cp – Copy files and directories

Basic Syntax

cp source destination_path

The basic syntax of the command accepts two arguments, the first is the file path or directory, and the second is the destination path where you want the file or directory copied to. In any Linux system, this command will work flawlessly, but there may be cases where you should be aware of some important options that the cp command provides.

–backup[option]

We often need to copy so many files at once to a location where some of these files may already exist. In that scenario, the default behavior of the cp command will overwrite all the existing files. This could be problematic if the files contain different data inside them.

In that case, we can use the –backup option and tell the command how to make a backup of each existing file in the destination directory.

cp -v --backup myphotos Downloads/allphotos

The above command will copy all the files from myphotos to allphotos directory inside the Downloads directory. If allphotos directory already contains any of the files we transfer from myphotos, it’ll create a backup of that file. Another important feature of the cp command is that it can keep track of backup versions.

The above command will rename the backup file to the original filename + ‘`’ at the end.

cp command backup files
Backup files

Keep track of backups versions

The empty –backup options create a backup of each file at the destination directory, and if you try to copy the file again, the command will replace the existing backup files with the new backup files. If you want to keep the existing backup files and create a new one each time you try to copy files to the same directory, you can pass the ‘numbered’ or ‘t’ option to –backup.

The numbered option will create backup files and add a backup number at the end of each backup file. Here is an example command –

cp -rv --backup=numbered myphotos Downloads/allphotos

The above command will copy all the new files to the destination directory, and the duplicated files will be backed up and replaced with the new files. Each backup file will have a version number added to the filename.

Backup file with version number
Backup files versioning

Update files

One of the most useful options of the cp command is ‘-u’. This only copies newer files to the destination and leaves the rest. For example, if a file already exists in the destination directory, the ‘-u’ option will compare both files and will only copy if the copying file is newer than the one that already exists.

cp -rv -u myphotos Downloads/allphotos

Copy files attributes only (–attributes-only)

If you want to create files with some of the existing files’ attributes, you can pass argument –attributes-only. It will create files at the given destination with the same attributes and not data. Here is an example –

cp -rv --attributes-only myphotos Downloads/allphotos
Linux copy files attributes only
Create files with the same attributes

Ask before overwriting any files with ‘-i’ and ‘-n’

When you make a backup of a file, the existing file is backed up, and then the file is replaced with the new one. What if you want the command to ask you before overwriting any file? It is possible with the ‘-i’ and ‘-n’ options.

cp -rv -i myphotos Downloads/allphotos
Ask before overwriting any file
cp -rv -n myphotos Downloads/allphotos
Do not overwrite any file at all
Linux cp overwrites files.

Now the command will only overwrite a duplicate file if you type ‘y’ when asked by the command. And the ‘-n’ option will not overwrite any file at all.

Preserve the files attributes upon copying

All the above cp commands will copy files & their data to a destination but without preserving the file’s attributes such as permission, ownership, and timestamp. To also copy or preserve files attributes, use ‘-p’ option in the cp command.

cp -rf -p myphotos Downloads/allphotos
The above command will not only copy files data but also preserve all the attributes.

Note

The opposite of the -p argument is ‘–no-preserve’.

So these were some important options available for the cp command. For the full list of options, please see the man page of the cp command.

man cp

FreeFileSync – compare and synchronize files

FreeFileSync is an easy-to-use tool for syncing files between two directories. It has all the above-mentioned features plus many more advanced options to help you copy a large number of files.

FreeFileSync interface

You can select two directories to sync files between, apply the filters, and click compare. Based on the filter you apply, it’ll scan both the directories and provide the results. If you are satisfied with the results, you can click the ‘Synchronize’ button to start the copying process.

Compare files based on time, size, and content

Clicking on the ‘gear’ icon next to the ‘Compare’ button will allow you to select the properties to compare when comparing two directories. By default, the tool compares file time and size, but you can also select to compare the size of the files or content.

FreeFileSync compare settings

Apply filters

FreeFileSync can also apply filters when scanning the source directory and will only consider files or directories if at least one of the conditions in the include field is met or none of the conditions in exclude fields is met.

FreeFileSync filters

For example, to escape all the files from a specific directory in the source directory, add the directory name in the exclude field, set the minimum or maximum files size, or set the time span to only select files created or modified in the given time.

You can also exclude files with a specific extension, name, or word somewhere in the path.

\*.gif
This will exclude all the files in the base directory with '.gif' extension.
\tower.png
This will exclude the file in the base directory with the name tower.png.
*mountains*
This will exclude all the files in the base directory with the word 'mountains' anywhere in the path.

For more filter examples, head over to the official page of FreeFileSync.

Download FreeFileSync

Conclusion

So that’s about copying files or directories in Linux. You can go with any of the above methods to copy directories in Linux. The next article will show you how to conveniently copy or sync files between Linux computers either on the same network or different networks.

Exit mobile version