KandZ – Tuts

We like to help…!

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:

  1. Open terminal/command prompt
  2. Type cp filename.txt newfilename.txt
  3. Press Enter
  4. Verify the copy was successful by listing files

Example: cp document.pdf important_document.pdf

2. How to Copy Directory Recursively

Steps:

  1. Navigate to parent directory containing the folder
  2. Use command: cp -r directory_name new_directory_name
  3. Press Enter
  4. 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:

  1. Prepare source file with specific permissions/ownership
  2. Execute: cp -p source_file target_file
  3. Verify attributes are preserved using ls -l
  4. Compare ownership, permissions, and timestamps

Example: cp -p config.ini backup_config.ini

4. How to Copy Files to Specific Directory

Steps:

  1. Identify the target directory path
  2. Use command: cp -t target_directory source_file(s)
  3. Press Enter
  4. 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:

  1. Execute: cp -a source_directory target_directory
  2. Wait for completion
  3. 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:

  1. Prepare source and destination directories
  2. Run command: cp -u source_directory target_directory
  3. Press Enter
  4. Check that only newer files were copied

Example: cp -u /home/user/downloads/ /backup/downloads/

7. How to Copy with Verbose Output

Steps:

  1. Prepare source and destination paths
  2. Execute: cp -v source_file target_file
  3. Observe detailed progress messages
  4. Note any warnings or errors during copy

Example: cp -v large_file.zip /backup/

8. How to Copy Multiple Files at Once

Steps:

  1. List all files to be copied in one command
  2. Use pattern matching if applicable: cp file1.txt file2.txt file3.txt target_directory/
  3. Press Enter
  4. Verify each file was copied successfully

Example: cp *.txt /home/user/documents/

9. How to Copy Files with Interactive Prompt

Steps:

  1. Use command: cp -i source_file target_file
  2. When overwriting, system will prompt for confirmation
  3. Respond with ‘y’ to proceed or ‘n’ to skip
  4. Continue with other files as needed

Example: cp -i important.txt backup.txt

10. How to Copy Directory with Hidden Files

Steps:

  1. Navigate to parent directory
  2. Execute: cp -r .[!.]* ../* target_directory/
  3. Or use: cp -r directory_name target_directory/
  4. Verify hidden files were copied using ls -la

Example: cp -r .git my_backup/.git

11. How to Copy with Progress Information

Steps:

  1. Use verbose mode: cp -v source_file target_file
  2. Watch real-time progress messages
  3. Monitor file size and transfer status
  4. Wait for completion message

Example: cp -v large_dataset.zip /home/user/backup/

12. How to Copy with Backup Option

Steps:

  1. Use backup mode: cp -b source_file target_file
  2. This creates a backup of existing target file
  3. Specify backup suffix if desired: cp -b --suffix=.old source_file target_file
  4. 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:

  1. Use command: cp -P source_file target_file
  2. This preserves symbolic links as links instead of copying contents
  3. Verify with ls -l that links are preserved
  4. Test that the link structure remains intact

Example: cp -P symlinked_file original_file

14. How to Copy with Specific Timestamp Preservation

Steps:

  1. Prepare source file with specific timestamps
  2. Execute: cp -p source_file target_file
  3. Verify timestamps match using ls -l
  4. 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:

  1. Use verbose mode for monitoring: cp -v large_file.zip /destination/
  2. Watch for any error messages during transfer
  3. Check disk space availability before copying
  4. 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:

  1. Set desired permissions on target directory: chmod 755 target_directory
  2. Execute copy command with preservation: cp -p source_file target_file
  3. Verify permissions are maintained using ls -l
  4. 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:

  1. Ensure remote access is available
  2. Use command format: cp user@remote:/path/file.txt /local/destination/
  3. Enter password when prompted
  4. Confirm file was transferred successfully

Example: cp user@server:/home/user/data.txt /backup/data.txt

18. How to Copy with Batch Processing

Steps:

  1. Create a shell script with multiple cp commands
  2. Include error handling: cp -v file1.txt /dest/ && cp -v file2.txt /dest/
  3. Make script executable: chmod +x copy_script.sh
  4. 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/

Leave a Reply