My folder structure looks like that:./build./module/build./sourceAll I want to keep is ./build and it's content.The command find . \! -path ./build -delete does not delete ./build, but all of it's content.How to avoid that?
How to delete everything inside a directory, without an specific folder and it's content
shell;find
Try:find . \! \( -wholename ./build/* -o -wholename ./build \) -deleteIf you run:rm -rf /tmp/tmp2mkdir /tmp/tmp2cd /tmp/tmp2mkdir -p build module/build sourcetouch .hidden build/abc build/abc2 source/def module/build/ghifind . \! \( -wholename ./build/* -o -wholename ./build \) -deletefind .your output will be:./build./build/abcThis is much safer than trying to parse the output of ls, where you have to take care of file or directory names with spaces or even worse with newlines embedded, find handles those correctly.