How to delete unwanted files in Raspberry Pi command?

 Here’s a safe, no-nonsense cheat-sheet for deleting files from the Raspberry Pi command line (Raspberry Pi OS / Linux).



0) Quick safety checklist

  • Double-check the path: pwd then ls -al (shows hidden files too).

  • Tab-complete names; quote paths with spaces: "My file.txt".

  • Prefer interactive/confirming deletes at first: rm -i / rm -I.

  • Avoid sudo rm -rf unless you’re 100% sure.


1) Delete single files

# basic rm file.txt # confirm before each delete rm -i file.txt # verbose (show what’s removed) rm -v file.txt # file with spaces or leading dash rm -- "My file.txt" rm -- --weird-name

2) Delete many by pattern (glob)

# delete all .log in current folder rm *.log # confirm each rm -i *.log # include hidden dotfiles rm .*.tmp 2>/dev/null

3) Delete directories

# remove an empty dir rmdir mydir # remove a non-empty dir (careful!) rm -r mydir # safer: prompt once if >3 files rm -rI mydir # show each removal rm -rv mydir

4) Delete by size, age, or name (find)

These are powerful—and safer—because you can preview first.

# PREVIEW large files (>100 MB) under current dir find . -type f -size +100M -print # DELETE them after you’re sure find . -type f -size +100M -delete # Delete *.log older than 7 days under /var/log (needs sudo) sudo find /var/log -type f -name "*.log" -mtime +7 -delete # Delete empty directories find . -type d -empty -delete # Delete node_modules folders under current tree (preview, then delete) find . -type d -name "node_modules" -prune -print find . -type d -name "node_modules" -prune -exec rm -rf {} +

Tip: swap -delete/-exec … rm with -print first to see what would be removed.

5) Free up space (common places)

# Show what’s big in current dir du -sh * .[^.]* 2>/dev/null | sort -h # System package cache sudo apt clean # remove all cached .deb files sudo apt autoremove # remove unused dependencies # systemd journal (if used) journalctl --disk-usage sudo journalctl --vacuum-size=100M # keep only 100 MB of logs # or: sudo journalctl --vacuum-time=7d # Trash for your user (GUI deletions live here) du -sh ~/.local/share/Trash 2>/dev/null rm -r ~/.local/share/Trash/* 2>/dev/null

6) “Send to Trash” from terminal (reversible)

If you prefer a recycle-bin style workflow:

sudo apt update sudo apt install trash-cli # move to trash instead of permanent delete trash-put file.txt mydir/ # list / restore / empty trash-list trash-restore trash-empty # empty everything trash-empty 7 # empty items older than 7 days

7) Permissions & stubborn files

# if "Permission denied", you might need sudo sudo rm file.txt # if write-protected, force with -f (use sparingly) rm -f file.txt # if a process is using the file lsof | grep filename # stop the process, then delete

8) Nice extras for finding big stuff

# ncdu = interactive disk usage browser (highly recommended) sudo apt install ncdu ncdu /

Common gotchas to avoid

  • Don’t run destructive commands as root in /, /boot, /usr, /etc, /lib, /var unless you know exactly what you’re doing.

  • rm -rf .* in your home directory can nuke .. (parent) on some shells—don’t do that.

  • Always preview with find … -print or ls before deleting.

评论

此博客中的热门博文

Detailed Explanation of STM32 HAL Library Clock System

How to remove write protection of STM32 chip?

The automatic white balance algorithm of Raspberry Pi