Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Easy, sure, but how?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
Lemma
Guru
Guru


Joined: 19 Apr 2002
Posts: 416
Location: Uppsala, Sweden

PostPosted: Wed Aug 27, 2003 12:23 pm    Post subject: Easy, sure, but how? Reply with quote

Hi
I want to write a script that checks once a day (using cron) if the files in a specific directory (and everything under that same directory :)) are older that a week and if so erase them. That and no more should be easy enough to do but I don't know how - any suggestions?
_________________
Always make it as simple as possible, but no simpler
/Einstein
Back to top
View user's profile Send private message
Lemma
Guru
Guru


Joined: 19 Apr 2002
Posts: 416
Location: Uppsala, Sweden

PostPosted: Wed Aug 27, 2003 12:33 pm    Post subject: Reply with quote

Sorry for posting here. This can and will (I hope) be moved to a more appropriate forum soon :oops:
_________________
Always make it as simple as possible, but no simpler
/Einstein
Back to top
View user's profile Send private message
liquidjoe
n00b
n00b


Joined: 06 Aug 2003
Posts: 23
Location: Germany - Bad Soden

PostPosted: Wed Aug 27, 2003 2:24 pm    Post subject: Reply with quote

man find could be helpful :wink:
Back to top
View user's profile Send private message
cselkirk
Apprentice
Apprentice


Joined: 09 Jun 2003
Posts: 199
Location: NL

PostPosted: Wed Aug 27, 2003 3:59 pm    Post subject: Re: Easy, sure, but how? Reply with quote

Lemma wrote:
I want to write a script that checks once a day (using cron) if the files in a specific directory (and everything under that same directory :)) are older that a week and if so erase them. That and no more should be easy enough to do but I don't know how - any suggestions?


Code:
#!/bin/sh

find /path/to/dir -ctime +7 -type f | xargs rm -f
# and if you want to also remove empty directories
find /path/to/dir -type d -empty | xargs rmdir


note, certain filesystem flags (such as notail, noatime on reiserfs) may cause problems.
_________________
cn=cselkirk,dc=xs4all,dc=nl
Back to top
View user's profile Send private message
delta407
Bodhisattva
Bodhisattva


Joined: 23 Apr 2002
Posts: 2876
Location: Chicago, IL

PostPosted: Wed Aug 27, 2003 4:27 pm    Post subject: Reply with quote

`find` is your friend -- it finds a list of files that match certain criteria and can do things with them. By default, it finds everything (files, directories, symlinks, device nodes, etc.) and prints them to stdout. However, its behavior is very flexible.

So, let's begin.

Code:
$ find

Default behavior. All right -- but you said you only wanted files, and in a particular directory.

Code:
$ find /path/to/directory -type f

Now we're finding files. Note that find, by default, checks subdirectories recursively. Let's say we don't want that. So, we tell it to check only one level down -- the files in the current directory.

Code:
$ find /path/to/directory -type f -maxdepth 1

Now we're finding files in the current directory. Let's tell it to only tell us about files that were last modified over seven days ago.

Code:
$ find /path/to/directory -type f -maxdepth 1 -mtime +7

Great! Now, recall that printing filenames to stdout is only the default behavior. (If there were no other options, this would be a great time to break out `xargs` and do some magic.) However, `find` can execute a command every time it finds something that matches -- a command like `rm`.

Code:
$ find /path/to/directory -type f -maxdepth 1 -mtime +7 -exec rm -v '{}' ';'

The quotes are necessary to keep your shell from potentially botching the parameters to rm. Note the -v switch -- it tells `rm` to print the names of the deleted files to stdout. (Feel free to remove it.)

That's all there is to it. Stick that in a shell script or maybe directly in your crontab and you should be all set.

Oh, and say you only wanted log files:
Code:
$ find /path/to/directory -type f -maxdepth 1 -mtime +7 -name '*.log' -exec rm -v '{}' ';'


Have fun. ;-)
_________________
I don't believe in witty sigs.
Back to top
View user's profile Send private message
delta407
Bodhisattva
Bodhisattva


Joined: 23 Apr 2002
Posts: 2876
Location: Chicago, IL

PostPosted: Wed Aug 27, 2003 4:28 pm    Post subject: Reply with quote

`find` is your friend -- it finds a list of files that match certain criteria and can do things with them. By default, it finds everything (files, directories, symlinks, device nodes, etc.) and prints them to stdout. However, its behavior is very flexible.

So, let's begin.

Code:
$ find

Default behavior. All right -- but you said you only wanted files, and in a particular directory.

Code:
$ find /path/to/directory -type f

Now we're finding files. Note that find, by default, checks subdirectories recursively. Let's say we don't want that. So, we tell it to check only one level down -- the files in the current directory.

Code:
$ find /path/to/directory -type f -maxdepth 1

Now we're finding files in the current directory. Let's tell it to only tell us about files that were last modified over seven days ago.

Code:
$ find /path/to/directory -type f -maxdepth 1 -mtime +7

Great! Now, recall that printing filenames to stdout is only the default behavior. (If there were no other options, this would be a great time to break out `xargs` and do some magic.) However, `find` can execute a command every time it finds something that matches -- a command like `rm`.

Code:
$ find /path/to/directory -type f -maxdepth 1 -mtime +7 -exec rm -v '{}' ';'

The quotes are necessary to keep your shell from potentially botching the parameters to rm. Note the -v switch -- it tells `rm` to print the names of the deleted files to stdout. (Feel free to remove it.)

That's all there is to it. Stick that in a shell script or maybe directly in your crontab and you should be all set.

Oh, and say you only wanted log files:
Code:
$ find /path/to/directory -type f -maxdepth 1 -mtime +7 -name '*.log' -exec rm -v '{}' ';'


Have fun. ;-)
_________________
I don't believe in witty sigs.
Back to top
View user's profile Send private message
Lemma
Guru
Guru


Joined: 19 Apr 2002
Posts: 416
Location: Uppsala, Sweden

PostPosted: Wed Aug 27, 2003 4:36 pm    Post subject: Reply with quote

Wow, are you a full time teacher ? ;-) This was awsome! Now, I have things to do - se'ya all later!
_________________
Always make it as simple as possible, but no simpler
/Einstein
Back to top
View user's profile Send private message
BonezTheGoon
Bodhisattva
Bodhisattva


Joined: 14 Jun 2002
Posts: 1408
Location: Albuquerque, NM -- birthplace of Microsoft and Gentoo

PostPosted: Wed Aug 27, 2003 8:30 pm    Post subject: Reply with quote

Moved this topic from Documentation, Tips & Tricks given it is none of those into the Other Things Gentoo forum.

Thanks.

Regards,
BonezTheGoon
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum