Use find to find certain directory and delete all files in it except one directory

Zettt

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
Stéphane Chazelas
find . -depth -print0 | perl -0lne '
  if ("$_/" =~ m{/Caches(/.*)}s && $1 !~ m{/Snapshots/}) {rmdir $_ or unlink $_}'

If your find doesn't support -print0, you can replace it with -exec printf '%s\0' {} +.

The idea is to print the list of files NUL-terminated (as 0 is the only byte that can't occur in a file path) and use perl's -n with -0 option to run some code for each of those filenames (with $_ set to the filename).

With -depth, files are printed before their parent directory. We remove only files or directories (assuming they are empty which is why it's important to process the list in depth order) if their path contains /Caches/ not followed by /Snapshosts/.

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 all files except in a certain subdirectory with find

From Dev

find all files excluding one directory

From Dev

In Linux terminal, how to delete all files in a directory except one or two

From Dev

Why does 'find -delete ' delete all files in a directory recursively

From Dev

How to delete all files in a directory except some?

From Dev

Using find to list all files under certain directory

From Dev

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

From Dev

How do I use grep to find all the zip files in a directory?

From Dev

How do I use grep to find all the zip files in a directory?

From Dev

Delete all files except files with the extension pdf in a directory

From Dev

Delete all files except files with the extension pdf in a directory

From Dev

BAT file: Delete all files in directory older than 5 days except one file

From Dev

How can I delete all the files in the current directory except one file?

From Dev

How do I remove all files from wtihin a certain directory except for a child directory of that directory?

From Dev

How to, using CMD/Powershell, find all files in a directory (including subdirectories), and then display files that have certain extensions

From Dev

How to delete all files in a directory except some globs using Puppet?

From Dev

How to delete all directories inside a directory except for files, (safely)?

From Dev

How to delete all files inside a directory except some extensions?

From Dev

PHP - find all all files within directory that match certain string and put in array

From Java

Find the files existing in one directory but not in the other

From Dev

find matching files in one directory and sub directories

From Dev

Find files in directory that have one of two keywords

From Java

Find all files in a directory with extension .txt in Python

From Dev

find all files in a directory without specified permissions

From Dev

How to find all executable files in a windows directory?

From Dev

How to find all zero byte files in directory

From Dev

Find all files in a directory and its subdirectory

From Dev

Windows find all files under a directory

From Dev

Delete all files in a directory

Related Related

  1. 1

    Delete all files except in a certain subdirectory with find

  2. 2

    find all files excluding one directory

  3. 3

    In Linux terminal, how to delete all files in a directory except one or two

  4. 4

    Why does 'find -delete ' delete all files in a directory recursively

  5. 5

    How to delete all files in a directory except some?

  6. 6

    Using find to list all files under certain directory

  7. 7

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

  8. 8

    How do I use grep to find all the zip files in a directory?

  9. 9

    How do I use grep to find all the zip files in a directory?

  10. 10

    Delete all files except files with the extension pdf in a directory

  11. 11

    Delete all files except files with the extension pdf in a directory

  12. 12

    BAT file: Delete all files in directory older than 5 days except one file

  13. 13

    How can I delete all the files in the current directory except one file?

  14. 14

    How do I remove all files from wtihin a certain directory except for a child directory of that directory?

  15. 15

    How to, using CMD/Powershell, find all files in a directory (including subdirectories), and then display files that have certain extensions

  16. 16

    How to delete all files in a directory except some globs using Puppet?

  17. 17

    How to delete all directories inside a directory except for files, (safely)?

  18. 18

    How to delete all files inside a directory except some extensions?

  19. 19

    PHP - find all all files within directory that match certain string and put in array

  20. 20

    Find the files existing in one directory but not in the other

  21. 21

    find matching files in one directory and sub directories

  22. 22

    Find files in directory that have one of two keywords

  23. 23

    Find all files in a directory with extension .txt in Python

  24. 24

    find all files in a directory without specified permissions

  25. 25

    How to find all executable files in a windows directory?

  26. 26

    How to find all zero byte files in directory

  27. 27

    Find all files in a directory and its subdirectory

  28. 28

    Windows find all files under a directory

  29. 29

    Delete all files in a directory

HotTag

Archive