Linux CLI HowTo 8 🐧 How To move and rename files
Linux CLI 8 – How-To move and rename files
1. How to move a directory to another location
Steps:
- Open terminal
- Use
mvcommand followed by source directory and destination - Example:
mv d1 d2
Explanation: This moves the directory d1 into the directory d2, creating d2/d1
2. How to move a file to a different directory with new name
Steps:
- Open terminal
- Use
mvcommand followed by source file and destination path with new name - Example:
mv file1.txt d3/fil2.txt
Explanation: This moves file1.txt from current location to d3/ directory and renames it to fil2.txt
3. How to rename a file
Steps:
- Open terminal
- Use
mvcommand with current filename and new filename - Example:
mv file1.txt file2.txt
Explanation: This renames file1.txt to file2.txt in the same directory
4. How to move/rename files with overwrite confirmation
Steps:
- Open terminal
- Use
mv -icommand followed by source and destination - Example:
mv -i file2.txt file.txt
Explanation: The -i flag prompts before overwriting any existing file, preventing accidental data loss
5. How to delete a single file
Steps:
- Open terminal
- Use
rmcommand followed by filename - Example:
rm file.txt
Explanation: This permanently deletes the file file.txt
6. How to recursively delete directories and their contents
Steps:
- Open terminal
- Use
rm -rcommand followed by directory name - Example:
rm -r d3/
Explanation: The -r flag removes directories and all their contents recursively
7. How to force delete files without confirmation prompts
Steps:
- Open terminal
- Use
rm -fcommand followed by filename or directory - Example:
rm -f file.txt
Explanation: The -f flag forces deletion without asking for confirmation
8. How to see detailed information about file operations
Steps:
- Open terminal
- Use
rm -vcommand followed by filename - Example:
rm -v file.txt
Explanation: The -v flag provides verbose output, showing exactly what files are being deleted
9. How to safely move and rename files with backup confirmation
Steps:
- Open terminal
- Use
mv -ifor safe moving/rename operations - Example:
mv -i oldfile.txt newfile.txt
Explanation: Always use -i flag when moving or renaming to prevent accidental overwrites
10. How to delete files with visual confirmation
Steps:
- Open terminal
- Use
rm -vto see what’s being deleted - Example:
rm -v file.txt
Explanation: Verbose output helps you confirm exactly what will be removed before it happens
11. How to organize files into new directory structure
Steps:
- Create target directories if needed:
mkdir -p d3/ - Move files to new location with new names:
mv file1.txt d3/fil2.txt - Verify the move was successful
Explanation: This creates a clean file organization system by moving files to specific directories with desired naming conventions
12. How to batch rename multiple files
Steps:
- Open terminal in directory containing files
- Use
mvcommand for each file renaming operation - Example sequence:
mv file1.txt newfile1.txtmv file2.txt newfile2.txt
Explanation: Individual rename operations allow precise control over filename changes
13. How to delete entire directory tree with contents
Steps:
- Open terminal
- Use
rm -rfcommand with caution - Example:
rm -rf d3/
Explanation: This completely removes the directory d3/ and everything inside it, use with extreme care
14. How to recover accidentally overwritten files
Steps:
- Check if you have backups or version control systems
- Use
mv -ifor future operations to prevent overwrites - Consider using file recovery tools if immediate action needed
Explanation: Prevention is better than recovery, always use -i flag when in doubt about potential overwrites
15. How to handle permission errors when moving files
Steps:
- Check current permissions with
ls -l - Use
sudoif necessary:sudo mv file.txt /destination/ - Ensure you have proper read/write permissions
Explanation: File operations require appropriate permissions; use sudo for elevated privileges when needed