LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   delete files leaving current date. (https://www.linuxquestions.org/questions/linux-newbie-8/delete-files-leaving-current-date-4175594480/)

arun natarajan 11-30-2016 12:19 AM

delete files leaving current date.
 
Hi,

am trying make a script that deletes all files just leaving the current day from current the directory
Eg: if today is Nov 30, then should delete all files dated less than Nov 29. i.e 29,28,27,26,25,.....etc

from below examples you can able to see that
-mtime 1 is bringing both 28th and 29th dated files
-mtime +1 is bringing files beyond 28th.

is there any way to get this done by using awk ?


[root@localhost d1]# date
Wed Nov 30 11:46:15 IST 2016

[root@localhost d1]# ls -ltr
drwxr-xr-x. 2 root root 4096 Nov 8 13:06 d2
-rw-r--r--. 1 root root 0 Nov 28 01:01 f34
-rw-r--r--. 1 root root 0 Nov 28 01:01 f3
-rw-r--r--. 1 root root 0 Nov 28 01:01 f1
-rw-r--r--. 1 root root 0 Nov 28 23:01 f6
-rw-r--r--. 1 root root 0 Nov 29 01:01 f5
-rw-r--r--. 1 root root 0 Nov 29 01:01 f4
-rw-r--r--. 1 root root 0 Nov 29 23:01 f7
-rw-r--r--. 1 root root 0 Nov 30 10:23 f2
-rw-r--r--. 1 root root 0 Nov 30 10:24 201611280101.01
-rw-r--r--. 1 root root 75 Nov 30 11:35 qw

[root@localhost d1]# find -type f -mtime 1 -exec ls -ltr {} \;
-rw-r--r--. 1 root root 0 Nov 29 01:01 ./f5
-rw-r--r--. 1 root root 0 Nov 29 01:01 ./f4
-rw-r--r--. 1 root root 0 Nov 28 23:01 ./f6

[root@localhost d1]# find -type f -mtime +1 -exec ls -ltr {} \;
-rw-r--r--. 1 root root 0 Nov 28 01:01 ./f34
-rw-r--r--. 1 root root 0 Nov 28 01:01 ./f3
-rw-r--r--. 1 root root 0 Nov 28 01:01 ./f1

Turbocapitalist 11-30-2016 12:53 AM

You should be able to do it all with "find". What have you tried regarding combining -daystart with -mtime?

astrogeek 11-30-2016 01:02 AM

Please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.

You might try -mtime 0 with -daystart, and negation to delete all not in that list. See man find for full details.


All times are GMT -5. The time now is 10:19 AM.