The mv and cp commands in Linux are used for file operations, but they serve different purposes:
mv (Move)
- Function: Moves files or directories from one location to another.
- Behavior: If the destination is on the same filesystem, it simply changes the file's location. If it's on a different filesystem, it copies the file and then deletes the original.
- Usage: It can also be used to rename files or directories.
- Example:
mv source_file.txt /path/to/destination/
cp (Copy)
- Function: Copies files or directories from one location to another.
- Behavior: Creates a duplicate of the source file or directory at the destination. The original remains unchanged.
- Usage: You can use options like
-rfor recursive copying of directories. - Example:
cp source_file.txt /path/to/destination/
Summary
- Use
mvto move or rename files/directories. - Use
cpto create copies of files/directories.
