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
•
21 Apr 2024
This command will help you spot any unusually large directories
The red flag was when I saw the server disk space is showing a site is taking up 57992.5 MB, where locally the site size is showing 957MB. There is something serious happening here and I need to establish the problem quickly. When dealing with a significant discrepancy in site size...
Andrew Fletcher
•
05 Apr 2024
Drupal 9 to 10 upgrade an example of the actual steps taken
Continuing on from the Drupal 9 to Drupal 10 upgrade outline written earlier... Upgrading Drupal 9 to Drupal 10. However, the point of difference is this article works through the actual action steps of the upgrade.Planning and testing is critical for the Drupal to version 10 process to...
Andrew Fletcher
•
18 Mar 2024
Resolving CVE-2022-48624 less issue
To resolve the CVE-2022-48624 vulnerability on Ubuntu using Nginx, it's crucial to understand that the issue lies within the "less" package, not Nginx itself. The vulnerability affects "less" before version 606, where close_altfile in filename.c in less omits shell_quote calls for LESSCLOSE,...