pLumo has adequately given a few good alternative solutions in their answer. I'm providing a totally different approach.
You will notice that the find
command, the way you're using it with *
describing the top-level search paths, is only really used for finding the first directory among the names that *
expands to.
You may do this with a shell loop too, which results in code that is easier to read and to see what it does:
shopt -s dotglob nullglob
for name in *; do
if [ -d "$name" ] && [ ! -h "$name" ]; then
rm -rf -- "$name"
break
fi
done
This loop, which assumes it's running in a bash
shell, iterates over all names matched by *
(including hidden names). The body of the loop tests whether the current name is a directory (without being a symbolic link to a directory), and if so deletes the directory recursively before exiting the loop.
A similar effect could be had in the zsh
shell with the much simpler command
rm -rf -- *(D/[1])
This removes the first directory that *
expands to. The globbing qualifier (D/[1])
restricts the match of *
to only directories (/
), but includes hidden names (D
), and returns only the first matching name ([1]
). If there are no directories in the current directory, you would get a "no matches found" error message from the shell.
-exec
is a flag tofind
the|
is a "pipe" between commands that connects the output of one command to the input of the next. So after the pipe is a separate command. – user1794469 Jan 21 '19 at 13:51