0

we want to delete all files that ended with number with digit or more as 2 digit or three digit and so on

note - when files is ended with number , its tell us this file was rotate and actually backup with uniq number

example

-rw-r--r--  1 kafka hadoop    73668 Aug  5 07:56 controller.log.2018-08-05-07
-rw-r--r--  1 kafka hadoop    73668 Aug  5 08:56 controller.log.2018-08-05-08
-rw-r--r--  1 kafka hadoop   658893 Aug  7 11:53 controller.log.3
-rw-r--r--  1 kafka hadoop     1092 Aug  7 14:35 controller.log.2
-rw-r--r--  1 kafka hadoop  2750003 Aug  7 14:52 state-change.log.2018-08-07-14
-rw-r--r--  1 kafka hadoop     3678 Aug  7 14:52 log-cleaner.log.2018-08-07-14
-rw-r--r--  1 kafka hadoop   199094 Aug  7 14:52 controller.log.2018-08-07-14
-rw-r--r--. 1 kafka hadoop    24431 Aug  7 15:07 kafka.err
-rw-r--r--  1 kafka hadoop     1147 Aug  7 16:02 server.log.10
-rw-r--r--  1 kafka hadoop     1061 Aug  7 16:02 server.log.9
-rw-r--r--  1 kafka hadoop   135465 Aug  7 16:07 server.log.8
-rw-r--r--  1 kafka hadoop    10278 Aug  7 16:07 server.log.7
-rw-r--r--  1 kafka hadoop    10350 Aug  7 16:07 server.log.6
-rw-r--r--  1 kafka hadoop    10353 Aug  7 16:07 server.log.5
-rw-r--r--  1 kafka hadoop    10322 Aug  7 16:07 server.log.4
-rw-r--r--  1 kafka hadoop     1272 Aug  7 16:07 log-cleaner.log
-rw-r--r--  1 kafka hadoop   473322 Aug  7 16:07 controller.log.1
-rw-r--r--  1 kafka hadoop      170 Aug  7 16:07 controller.log

expected results:

-rw-r--r--. 1 kafka hadoop    24431 Aug  7 15:07 kafka.err
-rw-r--r--  1 kafka hadoop     1272 Aug  7 16:07 log-cleaner.log
-rw-r--r--  1 kafka hadoop      170 Aug  7 16:07 controller.log

I try this option but this option isn't work

 find . -type f  -mtime +6   -name '*[0-9]'  -print -delete

please help me to build the right syntax ,

King David
  • 1,001

2 Answers2

2

You need to use the -regex parameter for find.

find . -type f  -mtime +6 -regex '^.*[0-9]$' -print -delete

This will match and delete all files ending in numbers (beware that some extensions can end in numbers too, for example .mp4).

Also note that -mtime does not support floating point numbers and always rounds up to the next day. So if you want to delete files that were modified at least six days ago you supply -mtime +5.

confetti
  • 2,605
0

Please help me to build the right syntax.

What you used:

find . -type f  -mtime +6   -name '*[0-9]'  -print -delete

is the right syntax, with few caveats:

  1. -delete is not portable. In general, implementations of find may or may not support it. The portable equivalent is -exec rm {} \;.

  2. -name uses glob-like patterns. Depending on your locale, [0-9] may or may not mean exactly what you want: it may match a character from a set different than just ASCII decimal digits (e.g. it may match , but then maybe it doesn't match at the same time); and it's possible to create a locale where [0-9] cannot match anything. If you want to match any character your locale considers a decimal digit, use [[:digit:]]. If you want to match a character being one of the ASCII decimal digits, nothing more, then use [0123456789] explicitly.

The reason your command did not work for you is -mtime +6 did not match any of the files in question. The files were modified* on August 7, some on August 5. The question was posted on August 8. On that day the files were too recent to match -mtime +6. This has nothing to do with the syntax.

* I assume the example list of files is from ls -l, so it shows modification times.


That other answer advises replacing -name '*[0-9]' with -regex '^.*[0-9]$'. -regex is not portable, but where it works, -regex '^.*[0-9]$' is almost equivalent to -name '*[0-9]' (almost, because e.g. that -regex cannot match files with pathnames containing a newline character, while that -name can; such pathnames are rare though). [0-9] in -regex is similarly locale-dependent as [0-9] in -name.

That answer replaced your right syntax with a similarly right syntax.