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 do copy files or directories in Linux through 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 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 cp command provides.
–backup[option]
Many a times we need to copy so many files at ones to a location where some of these files may already exist. In that scenario, the default behavior of cp command will overwrite all the existing files. This could be problematic if the files contain different data inside it.
In that case, we can use –backup option and tell the command how to make 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 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 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.
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, then you can pass ‘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.
Update files
One of the most useful options of cp command is ‘-u’. This only copies newer files to the destination and leave 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 already exist.
cp -rv -u myphotos Downloads/allphotos
Copy files attributes only (–attributes-only)
If you want to create files with the attributes of some of the existing files, then 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
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
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 cp command. For the full list of options, please see the man page of 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.
You can select two directories that you want 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. Now if you are satisfied with the results, you can click ‘Synchronize’ button to start the copying process.
Compare files based on time, size, and content
Clicking on the ‘gear’ icon next to ‘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.
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.
For example, to escape all the files from a specific directory in the source directory, add the directory name in the exclude field or 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, or a name or a word somewhere in the path.
\*.gif This will exclude all the files in the base directory with '.gif' extension. Exclude files with a specific name \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.
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. In the next article, I will show you how to conveniently copy or sync files between Linux computers either on the same network or different networks.
The post Copy files & directories in Linux appeared first on LinuxAndUbuntu.
from LinuxAndUbuntu https://ift.tt/391n89j
0 Comments