Linux Commands: Copy, Move, SCP, and Rsync

1. cp - Copy Files and Directories

The cp command is used to copy files and directories in Linux.

Example:

cp source_file.txt destination_file.txt

This copies source_file.txt to destination_file.txt.

Options:

Option Description
-r Recursively copy directories and their contents.
-i Prompt before overwriting files.
-u Copy only when the source file is newer than the destination file.
-v Verbose mode, showing files being copied.
-a Archive mode, preserving file attributes and copying recursively.
-p Preserve file attributes such as timestamps and permissions.
--help Display help information about the cp command.
--version Display the version of the cp command.

2. mv - Move or Rename Files

The mv command is used to move or rename files and directories.

Example:

mv old_name.txt new_name.txt

This renames old_name.txt to new_name.txt.

Option Description
-i Prompt before overwriting files.
-u Move only when the source file is newer than the destination file.
-v Verbose mode, showing files being moved.
--help Display help information about the mv command.
--version Display the version of the mv command.

Note: The mv command can also be used to move files between directories.

3. scp - Secure Copy

The scp command is used to securely copy files between systems over SSH.

Example:

scp file.txt user@remote_host:/path/to/destination

This copies file.txt to the remote host at the specified path.

Option Description
-r Recursively copy directories and their contents.
-i Prompt before overwriting files.
-v Verbose mode, showing files being copied.
-P Specify the port number for SSH connection.
--help Display help information about the scp command.
--version Display the version of the scp command.

Note: The scp command uses SSH for data transfer, ensuring security.

4. rsync - Remote Sync

The rsync command is used for fast and versatile file copying and synchronization.

Example:

rsync -avz source_dir/ user@remote_host:/path/to/destination

This synchronizes the contents of source_dir to the remote host.

Option Description
-a Archive mode, preserving file attributes and copying recursively.
-v Verbose mode, showing files being synchronized.
-z Compress files during transfer for faster synchronization.
-e Specify the remote shell to use (e.g., SSH).
--delete Delete files in the destination that are not present in the source.
--help Display help information about the rsync command.
--version Display the version of the rsync command.

Note: The rsync command is efficient for synchronizing large amounts of data.