Copying and Moving Files | cp & mv Commands | Mac/Linux Terminal
Video: Copying and Moving Files | cp & mv Commands | Mac/Linux Terminal by Taught by Celeste AI - AI Coding Coach
Watch full page →Copying and Moving Files | cp & mv Commands | Mac/Linux Terminal
Learn how to efficiently copy, move, and rename files and directories using the powerful cp and mv commands in Mac and Linux terminals. This guide covers basic copying, recursive directory copying, and interactive modes to prevent accidental overwrites, helping you manage your files confidently from the command line.
Code
# Copy a single file to another file or directory
cp source.txt destination.txt # Copy and rename file
cp source.txt /path/to/directory/ # Copy file into directory
# Copy multiple files into a directory
cp file1.txt file2.txt /backup/
# Copy directory recursively (including all subdirectories and files)
cp -r /project /backup/project_backup
# Copy recursively with verbose output to see each copied file
cp -rv /project /backup/project_backup
# Use interactive mode to prompt before overwriting files
cp -i source.txt destination.txt
# Move or rename a file
mv oldname.txt newname.txt # Rename file in same directory
mv file.txt /new/location/ # Move file to new directory
# Move multiple files to a directory
mv file1.txt file2.txt /archive/
# Move directories (no special flags needed)
mv /old_folder /new_folder_location
# Use interactive mode with mv to confirm before overwriting
mv -i file.txt /destination/
Key Points
cpcopies files and directories; use-rto copy directories recursively.cp -iandmv -ienable interactive mode to prevent accidental overwrites.mvmoves or renames files and directories without creating duplicates, making it faster for large files.- Use
cp -vorcp -rvfor verbose output to track copying progress. - Recursive copying with
cp -ris essential for backing up entire folder structures.