My folder structure looks like that:
./build
./module/build
./source
All 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?
Try:
find . \! \( -wholename "./build/*" -o -wholename ./build \) -delete
If you run:
rm -rf /tmp/tmp2
mkdir /tmp/tmp2
cd /tmp/tmp2
mkdir -p build module/build source
touch .hidden build/abc build/abc2 source/def module/build/ghi
find . \! \( -wholename "./build/*" -o -wholename ./build \) -delete
find .
your output will be:
./build
./build/abc
This 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.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments