Home > OS >  How to delete files older than X days and print a list of removed files
How to delete files older than X days and print a list of removed files

Time:08-12

Suppose the following call in a script:

find $dir/$CACHE_DIR_SUBPATH -type f -mtime  $RETENTION_DAYS -delete

Is there a neat way to print the file names that are going to be deleted or is the best option to call find twice like so:

find $dir/$CACHE_DIR_SUBPATH -type f -mtime  $RETENTION_DAYS
find $dir/$CACHE_DIR_SUBPATH -type f -mtime  $RETENTION_DAYS -delete

CodePudding user response:

You should simply introduce into the find command line the -print option.

In others words:

find $dir/$CACHE_DIR_SUBPATH -type f -mtime  $RETENTION_DAYS -print -delete

The alternative way maybe:

find $dir/$CACHE_DIR_SUBPATH -type f -mtime  $RETENTION_DAYS -exec echo "Deleting file '{}'..."; rm {} \;
  • Related