View previous topic :: View next topic |
Author |
Message |
Lemma Guru
Joined: 19 Apr 2002 Posts: 416 Location: Uppsala, Sweden
|
Posted: Wed Aug 27, 2003 12:23 pm Post subject: Easy, sure, but how? |
|
|
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 |
|
|
Lemma Guru
Joined: 19 Apr 2002 Posts: 416 Location: Uppsala, Sweden
|
Posted: Wed Aug 27, 2003 12:33 pm Post subject: |
|
|
Sorry for posting here. This can and will (I hope) be moved to a more appropriate forum soon _________________ Always make it as simple as possible, but no simpler
/Einstein |
|
Back to top |
|
|
liquidjoe n00b
Joined: 06 Aug 2003 Posts: 23 Location: Germany - Bad Soden
|
Posted: Wed Aug 27, 2003 2:24 pm Post subject: |
|
|
man find could be helpful |
|
Back to top |
|
|
cselkirk Apprentice
Joined: 09 Jun 2003 Posts: 199 Location: NL
|
Posted: Wed Aug 27, 2003 3:59 pm Post subject: Re: Easy, sure, but how? |
|
|
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 |
|
|
delta407 Bodhisattva
Joined: 23 Apr 2002 Posts: 2876 Location: Chicago, IL
|
Posted: Wed Aug 27, 2003 4:27 pm Post subject: |
|
|
`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.
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 |
|
|
delta407 Bodhisattva
Joined: 23 Apr 2002 Posts: 2876 Location: Chicago, IL
|
Posted: Wed Aug 27, 2003 4:28 pm Post subject: |
|
|
`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.
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 |
|
|
Lemma Guru
Joined: 19 Apr 2002 Posts: 416 Location: Uppsala, Sweden
|
Posted: Wed Aug 27, 2003 4:36 pm Post subject: |
|
|
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 |
|
|
BonezTheGoon Bodhisattva
Joined: 14 Jun 2002 Posts: 1408 Location: Albuquerque, NM -- birthplace of Microsoft and Gentoo
|
Posted: Wed Aug 27, 2003 8:30 pm Post subject: |
|
|
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 |
|
|
|
|
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
|
|