Bash: Moving and Copying Files
Moving and copying files is essential for organizing data, creating backups, and managing reconnaissance findings. The mv and cp commands are powerful tools that every penetration tester must master. This lesson covers everything from basic operations to advanced techniques.
What You'll Learn
- Copying files and directories (cp)
- Moving and renaming files (mv)
- Creating backups
- Preserving permissions and attributes
- Interactive and force modes
- Wildcards and bulk operations
- Practical pentesting workflows
Copying Files (cp)
Basic File Copy
Copy single file:
cp source.txt destination.txt
Copy to directory:
cp file.txt /path/to/directory/
Copy multiple files:
cp file1.txt file2.txt file3.txt /destination/
Copy with new name:
cp original.txt backup.txt
Copying Directories
Recursive copy (directory and contents):
cp -r source_directory/ destination_directory/
Example:
# Copy entire directory
cp -r /home/user/Documents /backup/
# Copy directory contents only
cp -r /home/user/Documents/* /backup/
Common cp Options
Interactive mode (prompt before overwrite):
cp -i source.txt destination.txt
Verbose mode (show what's being copied):
cp -v source.txt destination.txt
Preserve attributes (permissions, ownership, timestamps):
cp -p source.txt destination.txt
Archive mode (preserve everything, recursive):
cp -a source_directory/ destination_directory/
# Equivalent to: cp -dpR
Force overwrite (no prompts):
cp -f source.txt destination.txt
Update only (copy only if source is newer):
cp -u source.txt destination.txt
Create hard link instead of copy:
cp -l source.txt link.txt
Create symbolic link:
cp -s source.txt symlink.txt
Moving Files (mv)
Basic Move
Move file:
mv source.txt destination.txt
Move to directory:
mv file.txt /path/to/directory/
Move multiple files:
mv file1.txt file2.txt file3.txt /destination/
Renaming Files
Rename is the same as move:
mv oldname.txt newname.txt
Examples:
# Rename file
mv report.txt final_report.txt
# Rename directory
mv old_project new_project
# Move and rename
mv /tmp/file.txt /home/user/documents/renamed_file.txt
Common mv Options
Interactive mode (prompt before overwrite):
mv -i source.txt destination.txt
Verbose mode:
mv -v source.txt destination.txt
No clobber (never overwrite):
mv -n source.txt destination.txt
Force overwrite:
mv -f source.txt destination.txt
Update only (move only if source is newer):
mv -u source.txt destination.txt
Wildcards and Bulk Operations
Copy Multiple Files by Pattern
# Copy all .txt files
cp *.txt /destination/
# Copy all files starting with "test"
cp test* /destination/
# Copy all .log and .txt files
cp *.{log,txt} /destination/
# Copy numbered files
cp file[1-5].txt /destination/
Move Multiple Files
# Move all .log files
mv *.log /var/log/
# Move all files except .txt
shopt -s extglob
mv !(*.txt) /destination/
# Move all files modified today
find . -type f -mtime 0 -exec mv {} /destination/ \;
Creating Backups
Simple Backup
# Copy with backup suffix
cp original.txt original.txt.bak
# Or
cp original.txt original.txt.$(date +%Y%m%d)
Backup Before Overwrite
# Create backup of destination if it exists
cp --backup=numbered source.txt destination.txt
Archive Backup
# Create dated backup directory
cp -a important_directory backup_directory_$(date +%Y%m%d)
# Create backup with tar
tar -czf backup_$(date +%Y%m%d).tar.gz directory/
Preserving Attributes
Why Preserve?
Default cp:
- New file has your ownership
- New timestamps
- Permissions may change
Preserve (-p or -a):
✓ Original permissions
✓ Original ownership (if root)
✓ Original timestamps
✓ Extended attributes
Examples
# Preserve permissions and timestamps
cp -p /etc/passwd /tmp/passwd.backup
# Full archive (preserve everything)
cp -a /var/www/html /backup/website
# Preserve specific attributes
cp --preserve=mode,ownership,timestamps source dest
Practical Pentesting Scenarios
Scenario 1: Organize Engagement Files
# Create directory structure
mkdir -p engagement/{recon,scans,exploits,loot,reports}
# Copy reconnaissance data
cp -r ~/tools/wordlists/* engagement/recon/
cp targets.txt engagement/recon/
# Move scan results
mv *.xml engagement/scans/
mv nmap_*.txt engagement/scans/
# Archive engagement
cp -a engagement engagement_backup_$(date +%Y%m%d)
Scenario 2: Backup Before Modification
# Always backup before editing configs
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
# Edit safely
nano /etc/ssh/sshd_config
# If something breaks, restore:
cp /etc/ssh/sshd_config.bak /etc/ssh/sshd_config
Scenario 3: Collect Loot
# Create loot directory
mkdir -p ~/loot/$(date +%Y%m%d)
# Copy sensitive files found
cp /etc/passwd ~/loot/$(date +%Y%m%d)/
cp /etc/shadow ~/loot/$(date +%Y%m%d)/ # if accessible
cp ~/.ssh/id_rsa ~/loot/$(date +%Y%m%d)/
# Copy web configs
cp /var/www/html/wp-config.php ~/loot/$(date +%Y%m%d)/
Scenario 4: Deploy Tools
# Copy tools to target
scp exploit.py user@target:/tmp/
# Or with existing shell:
cp ~/tools/linpeas.sh /tmp/
chmod +x /tmp/linpeas.sh
# Deploy wordlists
cp -r /usr/share/wordlists/rockyou.txt /tmp/
Advanced Techniques
Copy Only Newer Files
# Update backup only if source is newer
cp -u source/* destination/
# Sync directories (like rsync lite)
cp -ru source/ destination/
Copy with Progress
# Using rsync for progress
rsync -ah --progress source/ destination/
# Or with pv (pipe viewer)
tar -cf - source/ | pv | tar -xf - -C destination/
Copy Across Network
# Using scp
scp file.txt user@remote:/path/
# Copy directory
scp -r directory/ user@remote:/path/
# From remote to local
scp user@remote:/path/file.txt ./
# Multiple files
scp file1.txt file2.txt user@remote:/path/
Copy with Exclusions
# Use rsync to exclude patterns
rsync -av --exclude='*.log' --exclude='*.tmp' source/ destination/
# Exclude multiple patterns
rsync -av --exclude={'*.log','*.tmp','cache/*'} source/ destination/
Safety Practices
1. Always Use Interactive Mode
Add to ~/.bashrc:
alias cp='cp -i'
alias mv='mv -i'
alias rm='rm -i'
2. Preview Before Executing
# See what will be moved
ls *.txt
# Then move
mv *.txt /destination/
# Or use echo to preview
echo mv *.txt /destination/
3. Backup Important Files
# Before modifying system files
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak.$(date +%Y%m%d)
# Before modifying scripts
cp script.sh script.sh.bak
4. Check Destination First
# Does destination exist?
ls -la /destination/
# Will I overwrite something important?
ls /destination/file.txt
Common Mistakes
1. Forgetting Trailing Slash
# Different behaviors:
cp -r source destination # Creates destination/source/
cp -r source/ destination/ # Copies contents into destination/
# Be explicit!
2. Overwriting Without Realizing
# This overwrites destination.txt without warning!
cp source.txt destination.txt
# Use -i to be safe
cp -i source.txt destination.txt
3. Moving Instead of Copying
# Oops! Original is gone
mv important_file.txt /tmp/
# Should have copied
cp important_file.txt /tmp/
4. Wrong Directory
# Always check your location
pwd
# Then copy/move
cp file.txt /correct/path/
Practical Exercises
Exercise 1: Basic Copying
# Create test files
touch file{1..5}.txt
# Copy single file
cp file1.txt file1_copy.txt
# Copy multiple files to directory
mkdir copies
cp file2.txt file3.txt file4.txt copies/
# Copy all .txt files
cp *.txt copies/
# Verify
ls copies/
Exercise 2: Directory Operations
# Create directory structure
mkdir -p project/{src,tests,docs}
touch project/src/main.py
touch project/tests/test.py
# Copy entire directory
cp -r project project_backup
# Verify structure
tree project_backup
Exercise 3: Moving and Renaming
# Create test files
touch old_name.txt
# Rename
mv old_name.txt new_name.txt
# Create and move
touch temp1.txt temp2.txt
mkdir temp_files
mv temp*.txt temp_files/
# Verify
ls temp_files/
Exercise 4: Backup Workflow
# Create important file
echo "Important data" > important.txt
# Backup before editing
cp important.txt important.txt.bak
# Edit original
echo "Modified data" > important.txt
# Compare
diff important.txt important.txt.bak
# Restore if needed
cp important.txt.bak important.txt
Quick Reference
Copying:
cp file.txt dest.txt # Copy file
cp file.txt /path/ # Copy to directory
cp file1 file2 /path/ # Copy multiple
cp -r dir/ dest/ # Copy directory (recursive)
cp -p file dest # Preserve attributes
cp -a dir/ dest/ # Archive mode (preserve all)
cp -i file dest # Interactive (prompt)
cp -v file dest # Verbose (show progress)
cp -u file dest # Update (only if newer)
Moving/Renaming:
mv file.txt dest.txt # Move/rename file
mv file.txt /path/ # Move to directory
mv file1 file2 /path/ # Move multiple
mv -i file dest # Interactive
mv -v file dest # Verbose
mv -n file dest # No clobber (never overwrite)
Wildcards:
cp *.txt /path/ # All .txt files
cp file[1-5].txt /path/ # Numbered files
cp *.{txt,log} /path/ # Multiple extensions
Backup:
cp file.txt file.txt.bak # Simple backup
cp -a dir dir.bak # Directory backup
cp --backup=numbered f d # Numbered backups
Resources
Man Pages:
man cp
man mv
man rsync
Practice:
- Create test directory structures
- Practice on non-critical files
- Use
-iuntil you're confident
Next Steps
- Set up safety aliases (cp -i, mv -i)
- Practice with wildcards for bulk operations
- Learn rsync for advanced copying
- Create backup scripts for important files
- Always backup before modifying critical files!
Copying and moving files seems simple, but doing it safely and efficiently requires practice. Master these commands to avoid costly mistakes!
"cp -a: Because losing data is not an option."