0

I want to remove all the folders/directories in the current folder (and its contents) without touching the files in current folder.

find -mindepth 1 -maxdepth 1 -type d -exec rm -r {}

Traceback:

find: missing argument to `-exec'
2

2 Answers 2

1

The post How to delete only directories and leave files untouched has this very simple solution :

To delete all directories and subdirectories and leave only files in the working directory, I have found this concise command works for me:

rm -r */

It makes use of bash wildcard */ where star followed by slash will match only directories and subdirectories.

The above rm command will not remove folders whose names start with a dot (.), which is perhaps what most people would prefer.

The formulation recommended by user Romeo Ninov is also found in this post:

find . -maxdepth 1 -mindepth 1 -type d

then

find . -maxdepth 1 -mindepth 1 -type d -exec rm -rf '{}' \;

To add an explanation:

find starts in the current directory due to . and stays within the current directory only with -maxdepth and -mindepth both set to 1. -type d tells find to only match on things that are directories.

find also has an -exec flag that can pass its results to another function, in this case rm. the '{}' \; is the way these results are passed. See this answer for a more complete explanation of what {} and \; do

4
  • "bash wildcard */ where star followed by slash will match only directories and subdirectories" – Directories in the current working directory, yes, but not subdirectories. Subdirectories will be deleted because of -r, not because the wildcard matches them. By default * does not match dot files, so this solution does not fulfill the "all the folders/directories" requirement. Sadly the answer you cited is superficial. It is highly upvoted because usually it matches expectations, still superficial. May 26, 2023 at 19:51
  • @KamilMaciorowski: I think that not deleting dot folders is a good thing.
    – harrymc
    May 26, 2023 at 20:16
  • @KamilMaciorowski: Rather than argue, I added this information in my answer.
    – harrymc
    May 27, 2023 at 7:54
  • In 999‰ of cases (including this one), you want + instead of ;. With a massive number of small directories, this can make a significant difference. Jun 6, 2023 at 14:35
1

I don't know how to combine these two find incantations

# delete the contents of directories
find . -depth -mindepth 2 -print -delete

# then delete the directories
find . -mindepth 1 -type d -print -delete
2
  • Interesting approach. You cannot combine these exact two because -mindepth (like e.g. -depth) is a "global" primary that affects how the "traversing engine" of find works. AFAIK FreeBSD find (but not GNU find common in Linux) supports -depth -n|n|+n (not related to sole -depth) that is actually a test performed for each file separately. This (along with -o) allows us to do different things to files at different levels. With FreeBSD find we could rewrite the commands using -depth +1 and -depth 1 instead of the respective -mindepth …, then we could combine them. May 27, 2023 at 20:14
  • 1
    (cont) The problem is AFAIK FreeBSD find does not support -delete, so we would need to use -exec rm … instead. And then there is no reason not to use rm -r in the first place. May 27, 2023 at 20:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .