I'm having the following problem. I have a directory structure and want to delete everything that is within a directory named Caches
. Everything should be delete except all directories named Snapshots
. I can't figure out how to do this with find or any other command I know.
The reason I'm asking: On iOS every app has its own Caches directory. These sometimes don't get cleared properly, depending on the app. With the solution to this answer, one would be able to clear Caches on iOS, and therefore optimize the disk space, when the devices' drive is mounted on another computer, e.g. with FUSE (iExplorer).
This is what I have so far:
find . 2>/dev/null -type d -name "Caches" -maxdepth 3 -print
This returns something like:
./Library/Caches
When I do a ls ./Library/Caches
I see all contents and the Snapshots
directory, which I want to exclude because ultimately I want to -delete
everything except this one.
I want something like this:
Before: After:
. .
├── a ├── a
│ ├── a │ ├── a
│ └── Caches │ └── Caches
│ ├── a │ └── a
│ │ └── Snapshots │ └── Snapshots
│ │ └── a │ └── a
│ ├── b └── b
│ │ └── a └── c
│ └── c
└── b
├── c
└── Caches
├── a
│ └── foo
│ └── a
└── b
└── a
--remove-files
and--exclude
options, and then delete the tar files (as bonus, if you accidentally removed a file you didn't intend to, and notice it before deleting the tar file, you can simply restore it from that archive)../Library/Caches/not_Snapshots/Caches/Snapshots/
? Do you want to delete that directory because it is inside./Library/Caches/not_Snapshots/
or save it because it is aSnapshots
directory?