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.
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.
Comments