Creating copies of files can be useful for numerous reasons:
- If a copy of a file is created before changes are made, then it is possible to revert back to the original.
- A copy of a file can be used to transfer a file to removable media devices.
- A copy of an existing document can be used as a template for a new document.
cp [OPTIONS] SOURCE DESTINATION
cp command is used to copy files. Similar to the
mv command, it requires at least two arguments: a source and a destination. For example, to copy the /etc/passwd file to the current directory, use the following command:
Note: The second argument is the . character. Recall from the Changing Directories section that is a shortcut which represents the current directory.
The result of executing the previous command would create a copy of the contents of the /etc/passwd file in the Documents directory, since that is our current directory. This can be confirmed using the
Permissions can have an impact on file management commands, such as the cp command. In order to copy a file, it is necessary to have execute permission to access the directory where the file is located and the read permission for the file being copied. It is also necessary to have write and execute permission on the directory the file is being copied to. Typically, there are two places where you should always have write and execute permission on the directory: your home directory and the /tmp directory.
dd command is a utility for copying files or entire partitions at the bit level.
dd [OPTIONS] OPERAND
This command has several useful features, including:
- It can be used to clone or delete (wipe) entire disks or partitions.
- It can be used to copy raw data to removable devices, such as USB drives and CDROMs.
- It can backup and restore the MBR (Master Boot Record).
- It can be used to create a file of a specific size that is filled with binary zeros, which can then be used as a swap file (virtual memory).
Let’s examine the following example. The
dd command creates a file named /tmp/swapex with 50 blocks of zeros that are one megabyte in size:
dd command uses special arguments to specify how it will work. The following illustrates some of the more commonly used arguments:
|if||Input File: The input file to be read from.|
The example reads from the /dev/zerofile, a special file containing an unlimited number of zeros.
|of||Output File: The output file to be written.|
|bs||Block Size: The block size to be used. By default, the value is considered to be in bytes. Use the following suffixes to specify other units: K, M, G, and T for kilobytes, megabytes, gigabytes and terabytes respectively.|
|count||Count: The number of blocks to be read from the input file.|
The example command reads 50 blocks.