π§ File & Directory Management Guide
π Table of Contentsβ
- [[#π¨ Core Operations]]
- [[#π Advanced File Tools]]
- [[#ποΈ Compression & Extraction]]
- [[#π Permissions & Security]]
- [[#π€ Scripting & Automation]]
- [[#π§Ή Cleanup & Recovery]]
- [[#π§© Aliases & Tips]]
- [[#βοΈ Copy & Paste, Cut & Paste Workflows]]
π¨ Core Operationsβ
π Createβ
Command | Use Case | Arguments |
---|---|---|
touch file.txt | Creates empty file | file.txt |
echo "text" > file.txt | Writes content | - (overwrites) |
mkdir dir | Creates directory | -p (nested) |
mkdir -m 700 secure_dir | Sets permissions | -m (mode) |
Example:
touch config.yaml
mkdir -p project/{src,tests,data}
π Renameβ
Command | Use Case | Notes | |
---|---|---|---|
mv old new | File/dir rename | - | |
rename -n 's/.bak/.log/' *.bak | Batch rename | -n = dry-run |
Example:
mv report.doc docs/2024_report.doc
rename 's/\ /_/' *.txt # Replace spaces in file names
π Copyβ
Command | Use Case | Flags |
---|---|---|
cp file dest/ | Copy files | -r (dir) |
cp -a source/ backup/ | Archive copy | -a = --archive |
cp -i file1 file2 | Interactive | -i = prompt |
Example:
cp -r website/ backup_website/
rsync -avz local/ remote_user@host:remote_dir/ # Secure sync
ποΈ Deleteβ
Command | Use Case | Flags |
---|---|---|
rm file.txt | File deletion | -i (confirm) |
rmdir dir | Remove empty dir | - |
rm -rf path/ | Recursive delete | **DANGEROUS** |
Example:
rm -i *.bak
rm -rf temp/logs/
π Advanced File Toolsβ
π findβ
find /var -type f -mtime +7 -exec rm -f {} \; # Delete 7-day-old files
find . -name "*.tmp" -delete # Fast cleanup
π renameβ
rename -n 's/-/./' *.jpeg # Replace hyphens with dots
rename 'BEGIN{$c=1} s/\.csv$/.backup_$c.csv/' *.csv # Add counter
π οΈ diff & patchβ
diff -r dir1 dir2 # Compare directories
patch < changes.patch # Apply changes to source
ποΈ Compression & Extractionβ
π¦ zipβ
zip -r archive.zip project/ # Compress directory
unzip -l archive.zip # List contents
unzip -o legacy.zip -d modern/ # Convert file names (WindowsβLinux)
π¦ tarβ
tar cvzf backup.tar.gz dir/ # Compress with gzip
tar xvzf backup.tar.gz -C restore/ # Extract to specific directory
π Permissions & Securityβ
πΎ chmodβ
chmod u+x script.sh # User executable
chmod 600 private_key # Owner read/write only
chmod 755 /var/www/html/ # Web server permissions
π§βπ§ chownβ
chown user:group file.txt
chown -R www-data:www-data web/ # Recursively change ownership
π€ Scripting & Automationβ
π Batch Processingβ
for f in *.md; do pandoc "$f" -o "${f%.md}.html"; done # Convert MD to HTML
π§ inotify (File Monitoring)β
inotifywait -m -r -e create,modify /watched_dir # Watch for changes
πΉοΈ rsync (Advanced Sync)β
rsync -a --delete source/ user@host:/dest/ # Mirror source in destination
rsync -e "ssh -i key.pem" local/ remote/ # Custom SSH key
π§Ή Cleanup & Recoveryβ
π§Ή Daily Maintenanceβ
find /tmp -type f -name "*.tmp" -delete # Clear temp files
tar cvzf logs_$(date +%F).tar.gz /var/log/ # Backup and compress logs
π§ Log Rotationβ
logrotate -f /etc/logrotate.conf # Forcibly rotate logs
π§© Aliases & Tipsβ
π§° Add to .bashrc
β
alias ll='ls -la'
alias cp='cp -i'
alias mv='mv -i'
alias rm='rm -i'
π¨ Safety Firstβ
alias rm='rm -i' # Always prompt for safety
alias diff='diff -u' # Unified diffs by default
βοΈ Copy & Paste, Cut & Paste Workflowsβ
π Copy & Paste (Content Transfer)β
Task | Command & Flags | Use Case |
---|---|---|
Copy file content to new file | cat source > destination | cat file1.txt > file2.txt (overwrite) |
Append content to file | cat >> file.txt | cat <<EOF >> file.txt (multi-line) |
Paste from clipboard (GUI tools) | xclip -o | Only in GUI environments |
Paste via output redirection | echo "text" > file.txt | Single-line overwrites |
Copy lines between files | sed -n '3,5 p' file.txt >> new.txt | Extract and append lines 3β5 |
Examples:
cat config.txt > new_config.txt # Overwrite new file with content
echo "New line" >> log.txt # Append to log
cat <<EOF >> script.sh
echo "Hello from EOF"
EOF
βοΈ Cut & Paste (Move + Append)β
Task | Command & Flags | Use Case |
---|---|---|
Move file and append to destination | mv file.txt && cat file.txt >> destination.txt | Combine move and paste |
Move directory and restructure | rsync -a --remove-source-files src/ dest/ && rmdir src/ | Cut + paste with cleanup |
Cut text from line in file | sed -i '1d' file.txt && cat file.txt >> newfile.txt | Delete + append |
Examples:
mv log2024.txt logs_dir/ # Cut to directory
cat logs_dir/log2024.txt >> combined_logs.txt # Paste content
sed -i '2d' data.csv && cat data.csv >> cleaned_data.csv # Remove line 2 + append