See all UNIX related articles/faq
I have log files created whenever I patch or rebuild Linux containers. They are taking disk space. The logs are helpful if something fails. Otherwise, after a few weeks, those log files have no usage for me. So here is a quick tip on how to use the find command to delete all *.log files created in the last 90 days except for the previous 7 or 10 days. The commands mentioned in these tips are compatible with the Linux, FreeBSD, and macOS versions of the find command.

Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Linux terminal
Category File Management
OS compatibility FreeBSD Linux macOS
Est. reading time 2 minutes
Advertisement

How to use find command to delete all *.log files created in last 90 days except for last 7 days

Here is how all files looked with the ls command:
$ ls -l *.log

-rw-r--r-- 1 root root  3118 Mar 28 10:47 incus.patch.2024-03-28.log
-rw-r--r-- 1 root root  3162 Mar 30 06:50 incus.patch.2024-03-30.log
-rw-r--r-- 1 root root  5112 Apr  1 22:00 incus.patch.2024-04-01.log
-rw-r--r-- 1 root root  3162 Apr  3 11:39 incus.patch.2024-04-03.log
-rw-r--r-- 1 root root 12368 Apr  7 12:34 incus.patch.2024-04-07.log
-rw-r--r-- 1 root root  4052 Apr 11 10:48 incus.patch.2024-04-11.log
-rw-r--r-- 1 root root  3118 Apr 14 20:53 incus.patch.2024-04-14.log
-rw-r--r-- 1 root root  5571 Apr 19 09:51 incus.patch.2024-04-19.log
-rw-r--r-- 1 root root  3248 Apr 23 20:49 incus.patch.2024-04-23.log
-rw-r--r-- 1 root root  3248 Apr 28 18:36 incus.patch.2024-04-28.log
-rw-r--r-- 1 root root  3248 Apr 30 10:33 incus.patch.2024-04-30.log
-rw-r--r-- 1 root root  3118 May  4 10:53 incus.patch.2024-05-04.log
-rw-r--r-- 1 root root  4560 May  6 10:10 incus.patch.2024-05-06.log
-rw-r--r-- 1 root root  9401 May  8 23:04 incus.patch.2024-05-08.log
-rw-r--r-- 1 root root 16948 May 11 09:20 incus.patch.2024-05-11.log
-rw-r--r-- 1 root root  4935 May 15 05:51 incus.patch.2024-05-15.log

Find command examples to list files

First, let us list the files, type:
# find . -type f -name "*.log" -mtime +7 -mtime -90 -ls
Outputs:

   266651      8 -rw-r--r--   1 root     root         5112 Apr  1 22:00 ./incus.patch.2024-04-01.log
   264177      4 -rw-r--r--   1 root     root         3248 Apr 30 10:33 ./incus.patch.2024-04-30.log
   264809     16 -rw-r--r--   1 root     root        12368 Apr  7 12:34 ./incus.patch.2024-04-07.log
   263935      4 -rw-r--r--   1 root     root         3118 Apr 14 20:53 ./incus.patch.2024-04-14.log
   263964      8 -rw-r--r--   1 root     root         4560 May  6 10:10 ./incus.patch.2024-05-06.log
   263951      4 -rw-r--r--   1 root     root         3248 Apr 28 18:36 ./incus.patch.2024-04-28.log
   296716      4 -rw-r--r--   1 root     root         3162 Mar 30 06:50 ./incus.patch.2024-03-30.log
   273856      4 -rw-r--r--   1 root     root         3118 Mar 28 10:47 ./incus.patch.2024-03-28.log
   266656      4 -rw-r--r--   1 root     root         4052 Apr 11 10:48 ./incus.patch.2024-04-11.log
   266661      8 -rw-r--r--   1 root     root         5571 Apr 19 09:51 ./incus.patch.2024-04-19.log
   263952      4 -rw-r--r--   1 root     root         3118 May  4 10:53 ./incus.patch.2024-05-04.log
   266653      4 -rw-r--r--   1 root     root         3162 Apr  3 11:39 ./incus.patch.2024-04-03.log
   264283      4 -rw-r--r--   1 root     root         3248 Apr 23 20:49 ./incus.patch.2024-04-23.log

This find command searches the current directory including its subdirectories for files (-type f) with the .log extension (-name "*.log") that were last modified more than 7 days ago (-mtime +7) but less than 90 days ago (-mtime -90). It then lists (-ls) using the ls command outputs. This ensures that I don’t end up deleting unwanted log files. If you need case insensitive search replace the -name "*.log" with -iname "*.log":
# find . -type f -iname "*.log" -mtime +7 -mtime -90 -ls
How to use find command to delete all log files created in last 90 days except for last 7 days

Deleting files

Now that the results are as per my needs, I will replace the -ls (list files) action in the find command with -delete to delete those files. There is no confirmation with this option, and log files will be deleted permanently:
# find . -type f -name "*.log" -mtime +7 -mtime -90 -delete
Verify it using the df command and du command:
# df /
# ls -l

Outputs:

total 60
-rwxrwx--- 1 root root  1717 Mar 28 10:38 apply.incus.updates.sh
drwxr-xr-x 3 root root  4096 May  7 09:08 bin
-rw-r--r-- 1 root root  9401 May  8 23:04 incus.patch.2024-05-08.log
-rw-r--r-- 1 root root 16948 May 11 09:20 incus.patch.2024-05-11.log
-rw-r--r-- 1 root root  4935 May 15 05:51 incus.patch.2024-05-15.log
-rw-r--r-- 1 root root   490 Feb 16 09:01 incuse.init
-rw------- 1 root root  2661 Apr 19 06:59 mbox

Summing up

The find command is a really powerful tool if used carefully on Linux and Unix-like systems such as macOS or FreeBSD. Please read find command manual using the man command or ls command:
$ man find
$ find --help

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

0 comments… add one

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.