Learn to use “Copy” Command In Linux
In this article we are learning about how to use a copy command and copy to files and directories it is one of the basic activity done while using the Linux operating system.Backup activity is basically is creating a copy of files and directories. On Linux system, we can use cp command to do it.
What is copy command :
As we mentioned above, cp command is a command to create copy of files and directories. Here’s some samples of cp command that might useful in day-to-day operation
1. Run cp without any options
This is a very basic cp usage. To copy a file name file.txt from one location to another location, we can type like this
$ cp file.txt /home/myuser/office
If we don’t type absolute path, it mean that we are copying a file on current directory. From example above, file.txt is located in /home/myuser/Documents. We don’t have to type /home/myuser/Documents/file.txt to copy file.txt if we are in that /home/myuser/Documents directory. While /home/myuser/office is a folder where the file will be copied.
2. Copy multiple files at the same time
To copy multiple file at the same time, we can just put the files behind the copy command which separated by space. Here’s an example :
$ cp file1.txt file2.txt /home/myuser/office
3. Copy a directory
Copying a directory is a little bit tricky. You need to add -r or -R option to do it. -r or -R option means recursive. This option is a must whether the directory is empty or not. Here’s an example :
$ cp -r directory1 /home/myuser/office
4. Create hard links to files instead of copying them
Copying file means you must have some space on the storage to store the copied files. Sometimes for any reasons, you may want to create “shortcut” or links to the files instead of copying them. To do this, we can use -l option.
$ cp -l file4.txt /home/myuser/office
5. Create symbolic links to files
There is another type of links called softlinks or symbolic links. We use -s option to do this. Here’s a sample command.
$ cp -s /home/myuser/Documents/file6.txt file6.txt
Creating symlinks only can be done in current directory. On screenshot above, we want to create symbolic links from source directory – /home/myuser/Documents/file6.txt to /home/myuser/office. But to create symbolic links, I must inside /home/pungki/office as a destination folder. Once I manage to be there, I can run cp -s command above.
Then when you list the file with detail, you will see that /home/myuser/office/file_.txt is pointing to the original file. Its marked with arrow sign after the file name.
6. Copy without following symbolic links in Source
To do this, we can use -P option. When cp command found a file with symbolic links, it will copy the as is. Take a look at the sample below.
$ cp -P file6.txt ./movie
7. Copy with following symbolic links in Source
Now we can do this with -L option. Basically, this is an opposite of -P option above. Here’s the sample.
$ cp -L file_6.txt ./movie
With this option, the copied file is the same file with the source file of file_6.txt. This is known from the file size. The copied file has 50 bytes file size while the file_6.txt as symbolic link has 33 bytes file size.
8. Archive the files
When we are going to copy a directory, we will use -r or -R option. But we can also use -a option to archive file. This will create an exact copy of files and directories including symbolic links if any. Here’s a sample :
$ cp -a directory1/ /home/myuser/office
9. Explain what is being done
By default, when copying activity is success, we will see a command prompt again. If you want to know what happen during the copying file, we can use -v option.
$ cp -v *.txt /home/myuser/office
When we copying all txt files in current directory to /home/myuser/office/ directory, -v option will show what is being done. This additional information will make us more sure about the copying activity.
10. Copy only when the source file is newer
To do this, we can use -u option. Take a look this example below.
$ cp -vu *.txt /home/myuser/office
11. Use interactive mode
Interactive mode will ask if the destination folder have already the file. To activate interactive mode, use -i option.
$ cp -ir directory1/ /home/myuser/office/
12. Create backup date of each copied file
When the destination folder already have the file, by default cp command will overwrite the same file in the destination directory. Using –backup option, cp command will make a backup of each existing destination file. ../office will refer to /home/myuser/office. Here’s a sample :
$ cp --backup=simple -v *.txt ../office
As we can see, –backup=simple option will create a backup file which marked by a tilde sign (~) at the end of the file. –backup option has some Control, which are :
none, off : never backups (even if –backup is given)
numbered, t. : make numbered backups
existing, nil : numbered if numbered backup exist, simple otherwise
simple, never : always make simple backups
From all these commands we are knowing and learning about the cp command . It is the basic utility command for daily and new user.