-1
-rw-rw-r--. 1 root root 26753164 Nov  6 10:09 chart_20190924.bin
-rw-rw-r--. 1 root root 26752800 Nov  6 10:09 chart_20190925.bin
-rw-rw-r--. 1 root root 26751824 Nov  6 10:09 chart_20190926.bin
-rw-rw-r--. 1 root root 26753048 Nov  6 10:09 chart_20190927.bin
-rw-rw-r--. 1 root root 26753047 Nov  6 10:09 chart_20190928.bin
-rw-rw-r--. 1 root root 26751753 Nov  6 10:09 chart_20190929.bin
-rw-rw-r--. 1 root root 26751752 Nov  6 10:09 chart_20190930.bin
-rw-rw-r--. 1 root root 26756171 Nov  6 10:09 chart_20191001.bin
-rw-rw-r--. 1 root root 26762749 Nov  6 10:09 chart_20191002.bin
-rw-rw-r--. 1 root root 26763022 Nov  6 10:09 chart_20191003.bin
-rw-rw-r--. 1 root root 26763021 Nov  6 10:09 chart_20191004.bin
-rw-rw-r--. 1 root root 26763021 Nov  6 10:09 chart_20191005.bin
-rw-rw-r--. 1 root root 26767792 Nov  6 10:09 chart_20191101.bin
-rw-rw-r--. 1 root root 26768566 Nov  6 10:09 chart_20191102.bin
-rw-rw-r--. 1 root root 26768565 Nov  6 10:09 chart_20191103.bin
-rw-rw-r--. 1 root root 26768565 Nov  6 10:09 chart_20191104.bin

this is my log files

so i want to delete files without this files

-rw-rw-r--. 1 root root 26767792 Nov  6 10:09 chart_20191101.bin
-rw-rw-r--. 1 root root 26768566 Nov  6 10:09 chart_20191102.bin
-rw-rw-r--. 1 root root 26768565 Nov  6 10:09 chart_20191103.bin
-rw-rw-r--. 1 root root 26768565 Nov  6 10:09 chart_20191104.bin

so i tried this three code but it didn't work

find . -mtime +15 -name 'chart_*' -exec rm -rf {} \;
find . -maxdepth 1 -mtime -10 -type f -name "chart_*" -delete
find . -type f -mtime +15 -exec rm -f {} +

so could someone give some advice or solution how to delete files with out last one week thank you

hognkun
  • 181
  • 1
  • 2
  • 14
  • `-mtime` looks at file modification time not filename. All your files claim to have been created today and so none will match `-mtime +15` and all of them will match `-mtime -10`. – jhnc Nov 06 '19 at 01:54
  • `limit=$(date -d 'today -7 days' '+chart_%Y%m%d.bin'); find -name 'chart_*.bin' | while read f; do [[ $(basename "$f") < $limit ]] && rm -i "$f"; done` – jhnc Nov 06 '19 at 02:02
  • is this shell script? so if i use it i will make .sh files? – hognkun Nov 06 '19 at 02:04
  • There is no requirement to use a specific file name for shell scripts. Usually your scripts should have no extension at all. Anything you can type at the prompt can be put in a script (though [sometimes you have to understand how a command interacts with the terminal](/q/37586811)). – tripleee Nov 06 '19 at 04:00

1 Answers1

0

If you don't mind overwriting the modification times of the log files, how about:

#!/bin/bash

dir="."
for f in "$dir"/chart_*.bin; do
    date="${f#*chart_}"       # trim the substring before "chart_"
    date="${date%.bin}0000"   # trim the substring after ".bin" and append time
    touch -m -t "$date" "$f"  # update the mtime
done

find "$dir" -type f -name "chart_*.bin" -mtime +7 -exec rm -f {} \;

It first adjusts the mtime of the file to the filename-embedded date then performs the ordinary find and delete procedure.

Apparently it requires two steps and not so elegant as @jhnc's solution. A possible advantage (if any) may be it does not use the date -d command which is platform dependent.

Hope this helps.

tshiono
  • 21,248
  • 2
  • 14
  • 22