1

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?

Djeah
  • 320
  • 8
  • 21
  • Tip: curly braces will save you from double quote madness. `FILE_NAME=${APP_NAME}_${PARAM_COUNTRY}_${YYYY}_${MM}_${DD}.info` – John Kugelman Mar 21 '18 at 11:57
  • are all the files have same naming format: `run.***.info`? – RomanPerekhrest Mar 21 '18 at 12:03
  • `date` could help. If the file were produced by the time it is named after, you can just use `find` – Laurent G Mar 21 '18 at 12:03
  • 3
    It will be a lot easier if you put the date string in the beginning of the filename. That way alphabetical sort will give you sort by date. Then just split the sorted file list at the point you want. – Ville Oikarinen Mar 21 '18 at 12:06
  • 1
    what about just doing `find /path/to/files -type f -name "run.*.info" -mtime +1 -exec rm {} \;` – nbari Mar 21 '18 at 12:35
  • Stack Overflow is not a code writing service. Please show your code. Since Stack Overflow hides the Close reason from you: *Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/).* – jww Mar 22 '18 at 03:38
  • 1
    Possible duplicate of [Bash script delete files older than n days ago based on the timestamp in filename?](https://stackoverflow.com/q/35353801/608639), [Delete files older than 10 days using shell script in Unix](https://stackoverflow.com/q/13489398/608639), [Shell script to delete directories older than n days](https://stackoverflow.com/q/13868821/608639), [find files older than X days in bash and delete](https://stackoverflow.com/q/20238183/608639), etc. – jww Mar 22 '18 at 03:39
  • @JohnKugelman Thanks.. Yes I modified it.. – Djeah Mar 22 '18 at 15:13

1 Answers1

2

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.

user unknown
  • 35,537
  • 11
  • 75
  • 121