How to delete everything inside a directory, without an specific folder and it's content

Sommereder

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?

Anthon

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Delete everything but a specific folder

From Dev

How to delete all files inside a specific folder using php

From Dev

How to delete a specific number of files inside a folder with fish shell?

From Dev

How to delete all files and directory except one named directory from a specific folder in centos

From Dev

How do delete everything before an specific symbol?

From Dev

There's any issue if I don't place my models into a specific folder inside the app directory?

From Dev

PHP Delete all folder content without deleting root folder

From Dev

Find directory and delete it's content but not itself

From Dev

Find directory and delete it's content but not itself

From Dev

How to delete folder from subversion tags directory?

From Dev

how delete squiggly folder in sub directory

From Dev

In a batch file, how would I find the specific folder it is in without the rest of its directory?

From Dev

How to delete the file inside data folder?

From Dev

How to delete a folder with weird files inside?

From Dev

Delete all folders inside a folder except one with specific name

From Dev

delete parentheses and everything inside with bash

From Dev

MS Access: how to delete everything in a string after two specific characters

From Dev

How can I delete everything before/after a specific occurrence of a character?

From Dev

MS Access: how to delete everything in a string after two specific characters

From Dev

Mule ESB: How to take all the files in a folder inside Bucket of Amazon S3 ( get object content)

From Dev

how to delete folder inside a folder using laravel 4

From Dev

WPF How to modify resource/content file inside my project folder at runtime (NOT in bin directory)? Implementing "Recent changes" section

From Dev

Folder/ Directory delete with progress

From Dev

How to delete a specific set of ??.png files in a directory?

From Dev

delete a folder and its content

From Dev

Find and delete folder but not content

From Dev

How to create a text file inside a specific directory?

From Dev

How to create a text file inside a specific directory?

From Dev

How to let users view a public folder's calendar content without letting them see details within appointments?

Related Related

  1. 1

    Delete everything but a specific folder

  2. 2

    How to delete all files inside a specific folder using php

  3. 3

    How to delete a specific number of files inside a folder with fish shell?

  4. 4

    How to delete all files and directory except one named directory from a specific folder in centos

  5. 5

    How do delete everything before an specific symbol?

  6. 6

    There's any issue if I don't place my models into a specific folder inside the app directory?

  7. 7

    PHP Delete all folder content without deleting root folder

  8. 8

    Find directory and delete it's content but not itself

  9. 9

    Find directory and delete it's content but not itself

  10. 10

    How to delete folder from subversion tags directory?

  11. 11

    how delete squiggly folder in sub directory

  12. 12

    In a batch file, how would I find the specific folder it is in without the rest of its directory?

  13. 13

    How to delete the file inside data folder?

  14. 14

    How to delete a folder with weird files inside?

  15. 15

    Delete all folders inside a folder except one with specific name

  16. 16

    delete parentheses and everything inside with bash

  17. 17

    MS Access: how to delete everything in a string after two specific characters

  18. 18

    How can I delete everything before/after a specific occurrence of a character?

  19. 19

    MS Access: how to delete everything in a string after two specific characters

  20. 20

    Mule ESB: How to take all the files in a folder inside Bucket of Amazon S3 ( get object content)

  21. 21

    how to delete folder inside a folder using laravel 4

  22. 22

    WPF How to modify resource/content file inside my project folder at runtime (NOT in bin directory)? Implementing "Recent changes" section

  23. 23

    Folder/ Directory delete with progress

  24. 24

    How to delete a specific set of ??.png files in a directory?

  25. 25

    delete a folder and its content

  26. 26

    Find and delete folder but not content

  27. 27

    How to create a text file inside a specific directory?

  28. 28

    How to create a text file inside a specific directory?

  29. 29

    How to let users view a public folder's calendar content without letting them see details within appointments?

HotTag

Archive