rm
Practical examples of the rm command, covering safe deletion, recursive removal, exclusions, edge cases, and defensive usage patterns
Delete a file or folder with verbose on
Delete all files in a folder that don't match a certain file extension
Remove all but one specific file
Remove all files previously extracted from a tar(.gz) file
tar -tf <file.tar.gz> | xargs rm -r
Delete all folders regardless of content, use rm -rf with find.
find /path/to/parent -type d -exec rm -rf {} +
Remove directories bottom-up (prevents “not empty” errors)
find /path/to/parent -type d -depth -exec rmdir {} +
Remove only empty directories
find /path/to/parent -type d -empty -delete
Remove everything except the top-level directory
find /path/to/parent -mindepth 1 -exec rm -rf {} +
Remove everything except the top-level directory (more ffciently then above)
rm -rf /path/to/parent/*
rm -rf /path/to/parent/.[!.]*
Delete a path or filename under ---
Use -- (the canonical solution)