• Uncategorized

About linux : How-to-delete-files-based-on-file-name-pattern-duplicate

Question Detail

I have files with naming convention:

FILE_NAME=$APP_NAME"_"$PARAM_COUNTRY"_"$YYYY"_"$MM"_"$DD".info"

so in unix my file will be created like this:

run.sh_JP_2018_03_19.info
run.sh_JP_2018_03_20.info
run.sh_JP_2018_03_21.info
run.sh_AP_2018_03_21.info
run.sh_AP_2018_03_21.info

So if today’s date is ‘2018_03_21′ I would like to have a command which will delete the files older than this date having the string’JP’ in it. This command should work even if I execute it after few days. How do I do it?

Question Answer

This command:

find -type f -name "run.sh_JP_20[0-9][0-9]_[0-9][0-9]_[0-9][0-9].info" -mtime +1 -delete

will ignore the announced date in the filename, but look for the modification date of the file and only roughly for something like a dateformat in the filename, but this will be in most times be enough, if you don’t have names like run.sh_JP_2018_53_79.info laying around.

It will delete files at least 1 day old. Consult the find manual for variations like -ctime and -atime and your OS/filesystem for available flags.

The -delete flag for find works with Linux/Gnu-find.

Three days from now, you would just modify it to -mtime +4 -delete.

Dates and pattern matching soon get’s unmaintainable.

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.