Copying Files Using cp

To copy files, we could use the cp command. The general format of the command when used for simple copying is

$ cp -a source_directory target_directory

The -a argument is the same as giving -dpR, which would be

-d Dereferences symbolic links (never follows symbolic links) and copies the files that they point to instead of copying the links.

-p Preserves all file attributes if possible. (File ownership might interfere.)

-R Copies directories recursively.

The cp command can also be used to quickly replicate directories and retain permissions by using the -avR command-line options. Using these options preserves file and directory permissions, gives verbose output, and recursively copies and re-creates subdirectories. A log of the backup can also be created during the backup by redirecting the standard output like this:

sudo cp -avR directory_to_backup destination_vol_or_dir 1>/root/backup_log.txt or sudo cp -avR ubuntu /test2 1>/root/backup_log.txt

This example makes an exact copy of the directory named /ubuntu on the volume named /test2, and saves a backup report named backup_log.txt under /root.

Was this article helpful?

0 0

Post a comment