Quantcast
Channel: User Kamil Maciorowski - Super User
Viewing all articles
Browse latest Browse all 652

Answer by Kamil Maciorowski for how to delete all files that are old then X days and ended with number/s

$
0
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.


Viewing all articles
Browse latest Browse all 652

Trending Articles