Andrew Fletcher published: 2 August 2021 1 minute read
To remove all the contents from a directory, you need to ask yourself:
Do I want to remove everything including the directory itself?
To remove a directory with all its contents (including all interior folders):
rm -rf /path/to/directory
Do I want to keep the directory, but delete everything else?
Need to remove all the contents of the directory (including all interior directories) but not the directory itself:
rm -rf /path/to/directory/*
What about the hidden files and directories?
Wanting to make sure that hidden files/directories are also removed:
rm -rf /path/to/directory/{*,.*}
Want to remove the files only?
To remove all the "files" from inside a directory(not removing interior directories):
rm -f /path/to/directory/{*,.*}
Spaces in the directory names
Note: if you have spaces in your path, make sure to always use quotes.
rm -rf "/path/to the/directory/"*
is equivalent to 2 separate rm -rf calls:
rm -rf /path/to rm -rf the/directory/*
To avoid this issue, you can use 'single-quotes'(prevents all expansions, even of shell variables) or "double-quotes"(allows expansion of shell variables, but prevents other expansions):
rm -rf "/path/to the/directory/"*
Where:
rm :: stands for remove -f :: stands for force which is helpful when you don't want to be asked/prompted if you want to remove an archive -r :: stands for recursive which means that you want to go recursively down every folder and remove everything
Related articles
Andrew Fletcher
•
16 Jan 2025
get IP address from terminal OSX
When troubleshooting network issues or configuring devices, knowing your IP address can be essential. Whether you're connected via Wi-Fi, Ethernet, or tethering through a mobile provider, macOS offers powerful built-in tools to quickly identify your IP address. Here's a practical guide tailored to...
Andrew Fletcher
•
07 Jan 2025
Managing DDEV environment troubleshooting and setting up multiple Drupal projects
DDEV has become a popular tool for local web development, offering a streamlined approach to managing Docker-based environments. However, setting up and managing DDEV projects, particularly with the latest versions of Docker Desktop, can present challenges. This article guides you through resolving...
Andrew Fletcher
•
28 Dec 2024
Optimising file transfers by improving efficiency from cp to rsync
Transferring files between development and production environments is a critical task in the deployment process. However, I continue to come across multiple approaches that scale from awesome automation using pipelines to the basic of direct command line entry. Where the basic approaches rely on...