LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-30-2016, 12:19 AM   #1
arun natarajan
Member
 
Registered: Jun 2014
Posts: 111

Rep: Reputation: Disabled
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
 
Old 11-30-2016, 12:53 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,008
Blog Entries: 3

Rep: Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633
You should be able to do it all with "find". What have you tried regarding combining -daystart with -mtime?
 
Old 11-30-2016, 01:02 AM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,205
Blog Entries: 24

Rep: Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to delete files in slot of 1000 leaving 7 days of history ankitpandey Linux - General 2 01-23-2012 12:11 PM
Delete all files of certain date. fatz2000 Programming 2 12-22-2009 09:06 AM
Delete files based on current date -1 Winanjaya Linux - Newbie 3 06-23-2008 04:38 AM
delete files by date noir911 Programming 4 10-04-2006 07:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration