cp

The cp command is used to copy files and directories.

The cp command is like copy and paste in a visual desktop operating system.

copy a file:

cp source_file destination_file

copy a directory recursively:

cp -r source_directory destination_directory

copy a file and preserve attributes (such as dates and permissions)

Copy a file to a directory:

cp file.txt /directory/destination

Copy a file with overwriting:

cp -f file.txt /directory/destination

In this example, the file file.txt is copied to the /directory/destination directory and overwrites the existing file with the same name.

Copy a file with confirmation

cp -i file.txt /directory/destination

In this example, the file file.txt is copied to the /directory/destination directory and a confirmation is requested before overwriting an existing file with the same name.

Copy a file with verbose

cp -v file.txt /directory/destination

In this example, the file file.txt is copied to the directory /directory/destination and the progress of the copy is shown.

Copy multiple files to a directory

cp file1.txt file2.txt /directory/destination

In this example, the files file1.txt and file2.txt are copied to the directory /directory/destination

MV Command: Move or rename files and directories

The mv command is like cut and paste in a visual desktop operating system.

Move a file to a directory

mv file.txt /directory/destination

Rename a file

mv file.txt new_name.txt

Move a directory to another directory

mv /source/directory /destination/directory

Rename a directory

mv /directory/source /directory/new_name

Move multiple files to a directory

mv file1.txt file2.txt /directory/destination

Move a file with overwriting

mv -f file.txt /directory/destination

Move a file with confirmation

mv -i file.txt /directory/destination

In this example, the file file.txt is moved to the directory /directory/destination and confirmation is asked before overwriting an existing file with the same name.

dd command

The dd command is a command-line tool used to copy and convert files and devices on Unix and Linux operating systems.

The dd command is a low-level copy tool on Unix and Linux operating systems.

It is used to copy and convert files, but can also be used to create images of disks, partitions, and other block devices.

dd can copy files and devices exactly, including metadata and permissions.

dd can convert files from one format to another, such as from text to binary or vice versa.

dd can erase devices by replacing their contents with zeros or specific byte patterns.

dd can be used to clone hard drives, which can be useful for creating identical copies of an operating system or for migrating data to a new drive.

dd can be used to create virtual devices, such as virtual hard disks or virtual partitions.

dd can be slow for copying large amounts of data.

dd may require superuser permissions to access certain devices or files.

The basic usage of the dd command is as follows:

dd if=input of=output

Copy a file

dd if=source_file of=copy_file
  • if specifies the input file or device.
  • of specifies the output file or device.

Some common options used with dd are:

  • bs: Specifies the size of the data block (default is 512 bytes).
  • count: Specifies the number of blocks to copy.
  • status: Shows the progress of the copy.

It is recommended to use the status option to monitor the progress of the copy and avoid errors.

Copy a device

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

the contents of the device /dev/sda are copied to the device /dev/sdb.

Create an image of a device

dd if=/dev/sda of=sda_image.img

Restore an image from a device

dd if=sda_image.img of=/dev/sda

In this example, the image of the device /dev/sda is restored from the file sda_image.img

Copy a file with a specific size

dd if=source_file of=copy_file bs=1024 count=10

In this example, the first 10 blocks of 1024 bytes are copied from the file source_file to the file copy_file.

Convert a file format

dd if=source_file of=copy_file conv=ucase

the file source_file is converted to uppercase and saved to the file copy_file.

Erase a device

dd if=/dev/zero of=/dev/sda

Common options:

  • if: Specifies the input file or device.
  • of: Specifies the output file or device.
  • bs: Specifies the block size (default is 512 bytes).
  • count: Specifies the number of blocks to copy.
  • conv: Specifies the conversion to perform (for example, ucase for uppercase).