Copying Files

0

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

Follow Along

Use the following command to switch to the Documents directory:

sysadmin@localhost:~$ cd ~/Documents

The 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:

sysadmin@localhost:~/Documents$ cp /etc/passwd .

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 ls command:

sysadmin@localhost:~/Documents$ ls
School            alpha-third.txt  hidden.txt    numbers.txt  red.txt           
Work              alpha.txt        letters.txt   os.csv                         
adjectives.txt    animals.txt      linux.txt     passwd                         
alpha-first.txt   food.txt         longfile.txt  people.csv                     
alpha-second.txt  hello.sh         newhome.txt   profile.txt

Consider This

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


The 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:

Follow Along

Use the following cd command to return to the home directory:

sysadmin@localhost:~/Documents$ cd ~
sysadmin@localhost:~$ dd if=/dev/zero of=/tmp/swapex bs=1M count=50 
50+0 records in
50+0 records out
52428800 bytes (52 MB) copied, 0.825745 s, 635 MB/s

The dd command uses special arguments to specify how it will work. The following illustrates some of the more commonly used arguments:

ArgumentDescription
if

Input File: The input file to be read from.

dd if=/dev/zero of=/tmp/swapex bs=1M count=50 

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.

dd if=/dev/zero of=/tmp/swapex bs=1M count=50
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: KMG, and T for kilobytes, megabytes, gigabytes and terabytes respectively.

dd if=/dev/zero of=/tmp/swapex bs=1M count=50

The example uses a block size of one megabyte.

count

Count: The number of blocks to be read from the input file.

dd if=/dev/zero of=/tmp/swapex bs=1M count=50

The example command reads 50 blocks.

Consider This

No block size or count needs to be specified when copying over entire devices. For example, to clone from one hard drive (/dev/sda) to another (/dev/sdb) execute the following command:

dd if=/dev/sda of=/dev/sdb

Post a Comment

0Comments
Post a Comment (0)