Deleting files in specific sub-directories created at random

S van Dyk

I would like to create a script that would delete files older than 5 days from a certain set of subfolders. I'm aware of the following command:

find /path/to/files* -mtime +5 -exec rm {} \;

The problem that i'm facing at this point, needs some clarification of the structure of my folders:

/cassandra/data/data/cpms/basket/snapshots
/cassandra/data/data/customer/customer/snapshots
/cassandra/data/data/profile/customer/snapshots

I want my script to only look in the snapshots folders and clear out files only from there, the problem is that at any point, the application can create a new folder under the 'data' directory, or under a current existing directory. i.e.

/cassandra/data/data/cpms/new_basket/snapshots

OR

/cassandra/data/data/new_category/new_folder/snapshots

So to hard code the directories will be irrelevant as soon as the application creates a new structure, it won't work effectively anymore until I update the code.

Is there a way to do a command like this:

find /cassandra/data/data <look in *this* directory for a folder named snapshots [it might still be 1 or 2 levels down] in the folders below data> & delete only files older than 5 days in snapshot folders found?

So to clarify [I hope], my script should go into the /cassandra/data/data directory, find all the folders named 'snapshots' and delete only files from those folders.

Wayne Vosberg

Assuming you are in the top level directory where you want to start searching for snapshot folders (note - replaced your '-mtime 5 -exec rm' with just a -ls so you can verify it is finding the folders properly):

find . -name snapshot -a -type d | while read a ; do find "$a" -ls ; done

or, even easier:

find . -path '*/snapshot/*' -a -type f -a -mtime 5 -exec ls -l {} \;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Git: ignore files but not sub-directories in a directory

From Dev

assigning files in a directory to sub-directories

From Dev

Move files over a certain size to specific directory (and exclude specifc sub-directories)

From Dev

Get List of Files in Sub Directories for FAKE

From Dev

Renaming all files within different sub directories

From Dev

Efficently find files in specific directories

From Dev

loop through sub directories, to sample files

From Dev

Excluding certain files and directories when deleting files

From Dev

Deleting specific files in command line

From Dev

Excluding certain files and directories when deleting files

From Dev

Deleting specific files in command line

From Dev

Finding a specific file in several sub-directories

From Dev

Remove all files recursively without deleting directories

From Dev

Recursively move files within sub directories

From Dev

Find files in specific directories

From Dev

Create sub-directories and organize files by date

From Dev

How to Delete files which are in sub directories

From Dev

can't delete files from directories created on a specific period of time

From Dev

Copy files to sub-directories

From Dev

Finding and deleting files with a specific date

From Dev

How to split a directory of files into sub-directories

From Dev

how to unzip all files in sub directories

From Dev

Is there a Windows command-line utility to list largest files exceeding specific size in sub-directories?

From Dev

Finding files in sub directories

From Dev

find matching files in one directory and sub directories

From Dev

Rename files into sub directories

From Dev

Remove sub-folders without deleting files

From Dev

SHELL - Deleting files under home directory and its sub-directories in a single command line

From Dev

Deleting multiple sub directories with in multiple directories

Related Related

  1. 1

    Git: ignore files but not sub-directories in a directory

  2. 2

    assigning files in a directory to sub-directories

  3. 3

    Move files over a certain size to specific directory (and exclude specifc sub-directories)

  4. 4

    Get List of Files in Sub Directories for FAKE

  5. 5

    Renaming all files within different sub directories

  6. 6

    Efficently find files in specific directories

  7. 7

    loop through sub directories, to sample files

  8. 8

    Excluding certain files and directories when deleting files

  9. 9

    Deleting specific files in command line

  10. 10

    Excluding certain files and directories when deleting files

  11. 11

    Deleting specific files in command line

  12. 12

    Finding a specific file in several sub-directories

  13. 13

    Remove all files recursively without deleting directories

  14. 14

    Recursively move files within sub directories

  15. 15

    Find files in specific directories

  16. 16

    Create sub-directories and organize files by date

  17. 17

    How to Delete files which are in sub directories

  18. 18

    can't delete files from directories created on a specific period of time

  19. 19

    Copy files to sub-directories

  20. 20

    Finding and deleting files with a specific date

  21. 21

    How to split a directory of files into sub-directories

  22. 22

    how to unzip all files in sub directories

  23. 23

    Is there a Windows command-line utility to list largest files exceeding specific size in sub-directories?

  24. 24

    Finding files in sub directories

  25. 25

    find matching files in one directory and sub directories

  26. 26

    Rename files into sub directories

  27. 27

    Remove sub-folders without deleting files

  28. 28

    SHELL - Deleting files under home directory and its sub-directories in a single command line

  29. 29

    Deleting multiple sub directories with in multiple directories

HotTag

Archive