This question is essentially a continuation of this question. I have a directory with a large number of subdirectories. How can I delete part of the files in these directories that are stored there for more than a certain time and with a specific pattern in the name. For example, deleting all files older than 3 months, whose name begins with 'start' ends like this: end.zip .

  • find <нужные параметры> | xargs rm find <нужные параметры> | xargs rm . And the parameters can be found in man find . - Nick Volynkin
  • | xargs rm is optional. find can call itself ( majestio.info/viewtopic.php?id=55 ) - Majestio
  • one
    @NickVolynkin, | xargs rm | xargs rm - absolutely superfluous essence. in the presence of a "live" option -delete . - aleksandr barakin
  • @alexanderbarakin a, did not know. Then it's even easier) - Nick Volynkin

1 answer 1

deleting all files older than 3 months, the name of which starts with start ends like this: end.zip

This can be done with the find program:

  1. deletion

    for this is the option -delete . first it makes sense to run without it, to analyze the list of found files.

  2. all files

    for this is the option -type f .

  3. older than 3 months

    depending on what “older” means. if the modification time of the file status is meant, then the option -ctime +количество_суток days will do; if the time of modification of the file's contents, then the option -mtime +количество_суток days will do ( and the creation time itself is not saved in any file systems, since it is not required by the standard posix ).

  4. whose name begins with start ends like this: end.zip

    for this is the option -name маска . something like -name start\*end.zip or -name 'start*end.zip' .


total see the list:

 $ find /путь -type f -ctime +90 -name start\*end.zip 

remove:

 $ find /путь -type f -ctime +90 -name start\*end.zip -delete 
  • And if the task is a little more difficult. The string that consists of start and end.zip is not in the file name, but in its content? - faoxis
  • one
    it will not “complicate”, but “ask a completely different question.” - aleksandr barakin