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 03-17-2014, 10:20 AM   #1
rodnikosh
LQ Newbie
 
Registered: Mar 2014
Posts: 4

Rep: Reputation: Disabled
delete files older than 7 days Except on saturdays


i use Linux Centos.
i use this command to delete files that are older than 7 days:

find * -mtime +7 -exec rm {} \;

on saturdays, i make full backups of programs i have,
so i don't want to delete any file that was created on saturdays.

how do i combine such a task in the command?
 
Old 03-17-2014, 10:39 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,027

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
this worx for me:
Code:
[schneidz@hyper rodnikosh]$ ll
total 0
-rw-rw-r--. 1 schneidz schneidz 0 Mar 17 10:37 akuma
-rw-rw-r--. 1 schneidz schneidz 0 Mar 17 10:37 chun.li
-rw-rw-r--. 1 schneidz schneidz 0 Mar 17 10:37 h4x0rz.bak
-rw-rw-r--. 1 schneidz schneidz 0 Mar 17 10:37 hello
-rw-rw-r--. 1 schneidz schneidz 0 Mar 17 10:37 hello.bak
-rw-rw-r--. 1 schneidz schneidz 0 Mar 17 10:37 l33t.bak
-rw-rw-r--. 1 schneidz schneidz 0 Mar 17 10:37 world
[schneidz@hyper rodnikosh]$ find * -type f ! -name "*.bak" -exec echo rm {} \;
rm akuma
rm chun.li
rm hello
rm world
 
1 members found this post helpful.
Old 03-17-2014, 10:42 AM   #3
rodnikosh
LQ Newbie
 
Registered: Mar 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
ohh , thank you , but i must have misled you.
all the files are *.bak, the ones that are in normal days and the ones that are on saturdays.
so i can't identify them by name...
 
Old 03-17-2014, 10:45 AM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,027

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
Code:
[schneidz@hyper rodnikosh]$ date -d "last saturday"
Sat Mar 15 00:00:00 EDT 2014
 
1 members found this post helpful.
Old 03-17-2014, 10:47 AM   #5
rodnikosh
LQ Newbie
 
Registered: Mar 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
thanks guys , but i need to keep all previous saturdays, not just the last one...
 
Old 03-17-2014, 11:02 AM   #6
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,027

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
heres an idea:
Code:
[schneidz@hyper rodnikosh]$ touch -t 201403010000 rodnikosh
[schneidz@hyper rodnikosh]$ stat rodnikosh
  File: `rodnikosh'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d	Inode: 1445575     Links: 1
Access: (0664/-rw-rw-r--)  Uid: (  500/schneidz)   Gid: (  500/schneidz)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2014-03-01 00:00:00.000000000 -0500
Modify: 2014-03-01 00:00:00.000000000 -0500
Change: 2014-03-17 11:03:06.556354133 -0400
 Birth: -
[schneidz@hyper rodnikosh]$ date -d "2014-03-01"
Sat Mar  1 00:00:00 EST 2014
edit: maybe you can run this in a loop:
Code:
[schneidz@hyper ~]$ date -d `stat rodnikosh | awk '/^Modify/ {print $2}'` "+%a"
Sat

Last edited by schneidz; 03-17-2014 at 11:27 AM.
 
Old 03-17-2014, 11:36 AM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 14,938

Rep: Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520
How are the backup files created?
How are the backup files named?
Are you just searching the current directory?

Some off the top ideas.
Append SAT to your backup file names and use the above posted find command that excludes SAT.
Create the Sat backup files in another directory. Limit the find command to not search that directory.
Loop through those files that are older then 7 days and check the atime/ctime. Delete those files not created on a Sat.
 
1 members found this post helpful.
Old 03-17-2014, 12:32 PM   #8
rodnikosh
LQ Newbie
 
Registered: Mar 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
Smile

Thank you all! that is a great idea.
i'll just backup the saturday backups into a different folder and operate the "rm" on the regular backups folder.
i just need to be carefull not to delete the script itself, since it is inside the folder of the backup and older than 7 day
 
Old 03-17-2014, 02:28 PM   #9
Mark Nagel
LQ Newbie
 
Registered: Jul 2006
Posts: 6

Rep: Reputation: 0
If you wish to keep the script in the same directory you could regen it via cron script (if file doesn't exist then make it [source elsewhere]). I would prefer to have any executable in some other centralized location, but that's just my preference.

I'm a bit out of touch with Linux/Unix, so please excuse me for referencing any obsolete commands/programs and such...

Last edited by Mark Nagel; 03-17-2014 at 02:30 PM.
 
Old 03-18-2014, 07:22 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Definitely keep the prog in a separate dir from data files eg backups.
Keeping the Sat backups in yet another dir eg 'weekly_archive' is also a good idea.
 
Old 03-18-2014, 09:18 AM   #11
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Another option using a tricky -exec action:
Code:
find . -mtime +7 ! -name script.sh -exec bash -c 'file="{}"; [[ $(date -r $file +%w) -ne 6 ]] && rm $file' \;
where script.sh is the name of the crontab script you want to protect from accidental removal.
 
1 members found this post helpful.
Old 03-20-2014, 12:47 PM   #12
griswald
LQ Newbie
 
Registered: Aug 2007
Posts: 11

Rep: Reputation: 0
If your system supports it, protect script.sh by making it immutable:
Code:
chattr +i script.sh
(To delete, first reverse the immutable bit with: chattr -i script.sh)
 
  


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 two days older files through script azheruddin Linux - Newbie 4 01-08-2013 01:18 PM
delete files when it's older 30 days Madison00 Linux - Newbie 8 01-07-2011 03:07 AM
[SOLVED] Delete old files older than 7 days rjo98 Linux - Newbie 3 09-18-2009 02:15 PM
Delete files older then 30 days stefaandk *BSD 1 01-07-2008 09:31 PM
delete files older than 30 days using cronjob latheesan Linux - Newbie 5 06-14-2005 03:40 PM


All times are GMT -5. The time now is 08:15 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration