I have multiple log file appended every day since 2 years, want to cleanup the logs older than 90days in each log file. Can i include dates for the past few year? Like 90..1000 to delete files older than 3 months to past few years?

  • What is the data format? I have logs with 2023-04-09T04:02:02.876067-04:00, 2023-04-09T08:13:45Z, [Oct 23 09:22:37.912], [09/Apr/2023:09:50:53 -0400], and [Sun Apr 09 04:02:26.077019 2023] and that is with checking only a few files. In looking closer, your question is unclear. Do you have a single log file that is appended to or do you have a directory with multiple files?
    – doneal24
    Apr 12, 2023 at 16:45
  • logrotate is probably a better solution for what you are trying to accomplish.
    – Bib
    Apr 12, 2023 at 18:51
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Apr 16, 2023 at 18:46

1 Answer 1


Managing log files by hand is complex. How is your log file used? Is it periodically updated or does a long running process (daemon) have it open constantly? If the latter, you'll have to do something (signal, or restart) with the daemon. Read the man page for the daemon.

When the logfile (big.log) is not open by any process (check via sudo lsof $PWD/big.log), use the date command (man date) with the "--date="today - 90 days"" and "+" formatting options to produce 90 days ago's in the same format as used in big.log.

# Warning: bash pseudo-code due to fuzziness of spec
# Warning: run any derived code through https://shellcheck.net
# You: Backup big.log, Just In Case

longago="$(date --date="today - 90 days" +"%...")"
# find the line number of the first occurence of the long ago date. 
# only want the 1st match, 
# only want the line number (up to the colon)
firstkeep="$(grep -n "$longago" big.log | head -n1 | cut -d: -f1)"
# left as an exercise: what if there are no logs on $longago?
# simple check? 
[[ -z "$firstkeep" ]] && \
    error ...
# now, just keep from $firstkeep to the end
tail -n "$firstkeep" big.log

# now, check small.log (add tests)
wc -l big.log small.log

# when you're satisfied with small.log

# If the first mv works, do the second
mv big.log big.log.old && mv small.log big.log

# finally, when you're satisfied with the new, smaller big.log.
df.;rm big.log.old;df .

All of these commands have man pages. Read them as needed along with man bash.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .