LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 09: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, 09:39 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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, 09: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, 09:45 AM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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, 09: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, 10:02 AM   #6
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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 10:27 AM.
 
Old 03-17-2014, 10:36 AM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,691

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
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, 11:32 AM   #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, 01:28 PM   #9
Mark Nagel
LQ Newbie
 
Registered: Jul 2006
Posts: 11

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 01:30 PM.
 
Old 03-18-2014, 06:22 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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, 08:18 AM   #11
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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, 11:47 AM   #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



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

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

All times are GMT -5. The time now is 01:57 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
Open Source Consulting | Domain Registration