Skip to main content

🐧 File & Directory Management Guide


πŸ“š Table of Contents​

  1. [[#πŸ”¨ Core Operations]]
  2. [[#πŸ” Advanced File Tools]]
  3. [[#πŸ—œοΈ Compression & Extraction]]
  4. [[#πŸ” Permissions & Security]]
  5. [[#πŸ€– Scripting & Automation]]
  6. [[#🧹 Cleanup & Recovery]]
  7. [[#🧩 Aliases & Tips]]
  8. [[#βœ‚οΈ Copy & Paste, Cut & Paste Workflows]]

πŸ”¨ Core Operations​

πŸ†• Create​

CommandUse CaseArguments
touch file.txtCreates empty filefile.txt
echo "text" > file.txtWrites content- (overwrites)
mkdir dirCreates directory-p (nested)
mkdir -m 700 secure_dirSets permissions-m (mode)

Example:

touch config.yaml
mkdir -p project/{src,tests,data}

πŸ”„ Rename​

CommandUse CaseNotes
mv old newFile/dir rename-
rename -n 's/.bak/.log/' *.bakBatch rename-n = dry-run

Example:

mv report.doc docs/2024_report.doc
rename 's/\ /_/' *.txt # Replace spaces in file names

πŸ”‚ Copy​

CommandUse CaseFlags
cp file dest/Copy files-r (dir)
cp -a source/ backup/Archive copy-a = --archive
cp -i file1 file2Interactive-i = prompt

Example:

cp -r website/ backup_website/
rsync -avz local/ remote_user@host:remote_dir/ # Secure sync

πŸ—‘οΈ Delete​

CommandUse CaseFlags
rm file.txtFile deletion-i (confirm)
rmdir dirRemove 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)​

TaskCommand & FlagsUse Case
Copy file content to new filecat source > destinationcat file1.txt > file2.txt (overwrite)
Append content to filecat >> file.txtcat <<EOF >> file.txt (multi-line)
Paste from clipboard (GUI tools)xclip -oOnly in GUI environments
Paste via output redirectionecho "text" > file.txtSingle-line overwrites
Copy lines between filessed -n '3,5 p' file.txt >> new.txtExtract 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)​

TaskCommand & FlagsUse Case
Move file and append to destinationmv file.txt && cat file.txt >> destination.txtCombine move and paste
Move directory and restructurersync -a --remove-source-files src/ dest/ && rmdir src/Cut + paste with cleanup
Cut text from line in filesed -i '1d' file.txt && cat file.txt >> newfile.txtDelete + 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