5 Powerful Linux Commands to Delete All Files in a Directory
This guide presents five effective Linux command-line techniques for removing every file in the current directory and another five methods for clearing all files within a specified directory, covering simple rm usage, find with -delete or -exec, xargs, and loop constructs.
When you have idle time and need to clean up a directory, here are several Linux command-line methods to delete all files in the current directory or a specified directory.
Delete files in the current directory
rm -f *– the classic way to delete all files of any type in the current directory. find . -type f -delete or find . -type f -exec rm -f {} \; – use find to locate regular files and delete them. find . -type f | xargs rm -f – useful when the argument list is too long. rm -f `find . -type f` – delete all regular files. for delete in `ls -l`; do rm -f *; done – use a for loop to delete all file types.
Delete files in a specified directory
rm -f /path/to/dir/*– classic method for a specific directory. find /path/to/dir -type f -delete or find /path/to/dir -type f -exec rm -f {} \; – find and delete regular files in the given directory. find /path/to/dir -type f | xargs rm -f – handles many files. rm -f `find /path/to/dir -type f` – delete all regular files in the specified directory. for delete in `ls -l /path/to/dir`; do rm -f *; done – loop to delete all file types in the target directory.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
