Problem Statement
Explain the differences between cp, mv, and rsync commands. When would you use each?
Explanation
The cp command copies files and directories, creating a duplicate while leaving the original intact. Use cp for local copying when you need both source and destination files. Common flags include -r for recursive directory copying, -p to preserve attributes, -i for interactive mode, and -v for verbose output. Cp is simple and sufficient for most local file duplication needs.
The mv command moves or renames files and directories. Unlike cp, it doesn't create a duplicate - it either renames a file (same filesystem) or copies and deletes the original (across filesystems). Use mv when you want to relocate files or change names without duplication. Mv is atomic within the same filesystem, making it safer for moving important files.
Rsync is a powerful tool for copying and synchronizing files locally or remotely over SSH. It uses delta-transfer algorithm, only copying changed portions of files, making it efficient for large files or directories. Rsync can resume interrupted transfers, preserve permissions/timestamps, show progress, and exclude patterns. Use rsync for backups, syncing large datasets, remote copies, or when you need advanced features.
Example: rsync -avz --progress source/ user@remote:/destination/ copies with archive mode, compression, and progress display. Choose cp for simple local copies, mv for relocating files, and rsync for efficient syncing, backups, or remote transfers. Understanding each tool's strengths helps you select the right one for your task.
