User avatar
expandables
Posts: 654
Joined: Fri Jun 27, 2014 7:34 pm
Location: Neverland with Michael Jackson

Bash Script help

Tue Jun 30, 2015 5:01 am

Hi i have a script that deletes files in 30 days it works but i want to know how can i delete a file on a specific date .

Code: Select all

#!/bin/sh
 find /home/pi/delete/*.log -mtime +30  -exec rm {} \;
:mrgreen:
By thinking like an engineer you can create a raspberry pi.
Michael Jackson enthusiast.
I got the PI model B, B+ and PI 2 model B.
When will I get the A? I don't know.

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: Bash Script help

Tue Jun 30, 2015 6:47 am

expandables wrote:Hi i have a script that deletes files in 30 days it works but i want to know how can i delete a file on a specific date .
Its better to rotate your log files rather than deleting them; then, maybe after so many rotations (or so many weeks) delete the oldest rotation.
marcus
:ugeek:

User avatar
DougieLawson
Posts: 39301
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Bash Script help

Tue Jun 30, 2015 7:51 am

expandables wrote:

Code: Select all

#!/bin/sh
 find /home/pi/delete/*.log -mtime +30  -exec rm {} \;
touch -t 201506010000 /home/pi/delete/filetime # June 1st @ midnight
find /home/pi/delete/*.log -type f ! -newer /home/pi/delete/filetime -delete
rm /home/pi/delete/filetime

You'll need to test that on a cp -a copy of your directory.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
expandables
Posts: 654
Joined: Fri Jun 27, 2014 7:34 pm
Location: Neverland with Michael Jackson

Re: Bash Script help

Tue Jun 30, 2015 3:39 pm

Ok thanks that's what i wanted but any clue why i need to do cp -a compare to just doing cp?
By thinking like an engineer you can create a raspberry pi.
Michael Jackson enthusiast.
I got the PI model B, B+ and PI 2 model B.
When will I get the A? I don't know.

User avatar
rpdom
Posts: 17275
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Bash Script help

Tue Jun 30, 2015 3:44 pm

cp -a will preserve all the timestamps and permissions on the files. No point in testing using files where the timestamp has been changed.

It'll also copy any sub-directories, but I don't think that's needed in this case.

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Bash Script help

Tue Jun 30, 2015 4:09 pm

"cp -a" is the general "big kahuna" version of "cp", that is the general solution to "How do I correctly copy a subdirectory tree?" So, it is worth learning and understanding.

However, in this specific case, all you really need is "-p", which just preserves the timestamps (which is all we care about in this specific instance).

BTW, "-a" is not standard (I'd wager it is Linux-only, but someone will probably come along and nitpick that). I often get caught trying to use it on, e.g., OSX, where it doesn't work - so, you have to drop back to "-p" (which is standard).
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

User avatar
expandables
Posts: 654
Joined: Fri Jun 27, 2014 7:34 pm
Location: Neverland with Michael Jackson

Re: Bash Script help

Tue Jun 30, 2015 5:11 pm

How can i do a script that checks if 30 days has pass from a specific date and then delete a file?
By thinking like an engineer you can create a raspberry pi.
Michael Jackson enthusiast.
I got the PI model B, B+ and PI 2 model B.
When will I get the A? I don't know.

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Bash Script help

Tue Jun 30, 2015 5:15 pm

expandables wrote:How can i do a script that checks if 30 days has pass from a specific date and then delete a file?
I suppose something along the lines of:

$ touch -d '2014-12-25 30 days' /tmp/glozi
$ ls -lsa /tmp/glozi
0 -rw-r--r-- 1 pi pi 0 Jan 24 00:00 /tmp/glozi
$ find -newer /tmp/glozi ...

At least I think that's what you are getting at...
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

User avatar
rpdom
Posts: 17275
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Bash Script help

Tue Jun 30, 2015 5:18 pm

Hmm, quite easy I guess.

Code: Select all

#!/bin/bash

now=$( date +%s)
then="2015-05-31"
testdate=$( date -d "$then + 30 days" +%s )
if [ $now -gt $testdate ]
then
  echo "Now is more than 30 days from $then"
fi

User avatar
expandables
Posts: 654
Joined: Fri Jun 27, 2014 7:34 pm
Location: Neverland with Michael Jackson

Re: Bash Script help

Tue Jun 30, 2015 7:28 pm

Thanks rpdom it works :mrgreen:
By thinking like an engineer you can create a raspberry pi.
Michael Jackson enthusiast.
I got the PI model B, B+ and PI 2 model B.
When will I get the A? I don't know.

Return to “Beginners”