Linux CLI HowTo 7 🐧 copy files and directories
Linux CLI 7 – HowTo copy files/directories
1. How to Copy a File with Different Name
Steps:
- Open terminal/command prompt
- Type
cp filename.txt newfilename.txt - Press Enter
- Verify the copy was successful by listing files
Example: cp document.pdf important_document.pdf
2. How to Copy Directory Recursively
Steps:
- Navigate to parent directory containing the folder
- Use command:
cp -r directory_name new_directory_name - Press Enter
- Check that all files and subdirectories were copied
Example: cp -r project_backup my_project_backup
3. How to Preserve File Attributes During Copy
Steps:
- Prepare source file with specific permissions/ownership
- Execute:
cp -p source_file target_file - Verify attributes are preserved using
ls -l - Compare ownership, permissions, and timestamps
Example: cp -p config.ini backup_config.ini
4. How to Copy Files to Specific Directory
Steps:
- Identify the target directory path
- Use command:
cp -t target_directory source_file(s) - Press Enter
- Confirm files appear in target directory
Example: cp -t /home/user/documents/ file1.txt file2.txt
5. How to Copy Files with All Attributes (Archive Mode)
Steps:
- Execute:
cp -a source_directory target_directory - Wait for completion
- Verify all attributes including permissions, timestamps, links
Example: cp -a /home/user/important_data /backup/important_data
6. How to Copy Only Newer Files
Steps:
- Prepare source and destination directories
- Run command:
cp -u source_directory target_directory - Press Enter
- Check that only newer files were copied
Example: cp -u /home/user/downloads/ /backup/downloads/
7. How to Copy with Verbose Output
Steps:
- Prepare source and destination paths
- Execute:
cp -v source_file target_file - Observe detailed progress messages
- Note any warnings or errors during copy
Example: cp -v large_file.zip /backup/
8. How to Copy Multiple Files at Once
Steps:
- List all files to be copied in one command
- Use pattern matching if applicable:
cp file1.txt file2.txt file3.txt target_directory/ - Press Enter
- Verify each file was copied successfully
Example: cp *.txt /home/user/documents/
9. How to Copy Files with Interactive Prompt
Steps:
- Use command:
cp -i source_file target_file - When overwriting, system will prompt for confirmation
- Respond with ‘y’ to proceed or ‘n’ to skip
- Continue with other files as needed
Example: cp -i important.txt backup.txt
10. How to Copy Directory with Hidden Files
Steps:
- Navigate to parent directory
- Execute:
cp -r .[!.]* ../* target_directory/ - Or use:
cp -r directory_name target_directory/ - Verify hidden files were copied using
ls -la
Example: cp -r .git my_backup/.git
11. How to Copy with Progress Information
Steps:
- Use verbose mode:
cp -v source_file target_file - Watch real-time progress messages
- Monitor file size and transfer status
- Wait for completion message
Example: cp -v large_dataset.zip /home/user/backup/
12. How to Copy with Backup Option
Steps:
- Use backup mode:
cp -b source_file target_file - This creates a backup of existing target file
- Specify backup suffix if desired:
cp -b --suffix=.old source_file target_file - Check that both original and backup exist
Example: cp -b document.txt document.txt.bak
13. How to Copy Files Without Following Symbolic Links
Steps:
- Use command:
cp -P source_file target_file - This preserves symbolic links as links instead of copying contents
- Verify with
ls -lthat links are preserved - Test that the link structure remains intact
Example: cp -P symlinked_file original_file
14. How to Copy with Specific Timestamp Preservation
Steps:
- Prepare source file with specific timestamps
- Execute:
cp -p source_file target_file - Verify timestamps match using
ls -l - Check that modification, access, and change times are preserved
Example: cp -p log_file.txt backup_log_file.txt
15. How to Copy Large Files with Error Handling
Steps:
- Use verbose mode for monitoring:
cp -v large_file.zip /destination/ - Watch for any error messages during transfer
- Check disk space availability before copying
- Verify integrity of copied file using checksums if needed
Example: cp -v /home/user/large_video.mp4 /backup/
16. How to Copy Files with Specific Permissions
Steps:
- Set desired permissions on target directory:
chmod 755 target_directory - Execute copy command with preservation:
cp -p source_file target_file - Verify permissions are maintained using
ls -l - Test that copied files behave as expected
Example: cp -p script.sh /home/user/bin/script.sh
17. How to Copy from Remote Location (if supported)
Steps:
- Ensure remote access is available
- Use command format:
cp user@remote:/path/file.txt /local/destination/ - Enter password when prompted
- Confirm file was transferred successfully
Example: cp user@server:/home/user/data.txt /backup/data.txt
18. How to Copy with Batch Processing
Steps:
- Create a shell script with multiple cp commands
- Include error handling:
cp -v file1.txt /dest/ && cp -v file2.txt /dest/ - Make script executable:
chmod +x copy_script.sh - Run the batch process:
./copy_script.sh
Example: Script content:
#!/bin/bash
cp -v *.txt /home/user/documents/
cp -v *.pdf /home/user/pdfs/
cp -v *.jpg /home/user/images/