 |
 |
 |
 |
| Using Fedora General support for current versions. Ask questions about Fedora and it's software that do not belong in any other forum. |

12th April 2005, 02:02 AM
|
|
Guest
|
|
Posts: n/a

|
|
|
Deleting files according to date
Here's the scenario:
I have a directoriy (~/myfiles) and I'd like to create a bash script that looks at the files in that directory and finds the date the files were last accessed and deletes them if the last accessed date is more than 30 days ago and then goes back and moves some files to another directory if the filename has a number before the file extension (ie, car2.png or scrnsht8.png).
I will be creating a cronjob to run this bash script once it is completed. FYI, this is part of a larger project and this bit is the last thing I need to learn.
Any advice?
|

12th April 2005, 03:29 AM
|
|
Registered User
|
|
Join Date: Feb 2004
Posts: 845

|
|
|
For the first part, you can use the find command like this:
find ~/myfiles -atime +30 -exec rm {} \;
where {} denotes each of the files in turn that satisfies find's criterion. See the man page for details.
|

12th April 2005, 03:41 AM
|
|
Registered User
|
|
Join Date: Feb 2004
Posts: 845

|
|
|
Actually, you might want to replace ~/myfiles with something like ~/myfiles/* in the above command, since otherwise one of the files that find considers would be ~/myfiles itself, which rm wouldn't remove since it's a directory, but if it tried you'd get an error message. To experiment with find, you can replace
-exec rm {} \;
with
-print
which will just list all the files that satisfy the criterion you give it.
|

12th April 2005, 03:42 AM
|
|
Guest
|
|
Posts: n/a

|
|
Thank you very much, robatino, that takes care of the first part of the command. I appreciate it 
I will experiment with -print.
Last edited by ianmac; 12th April 2005 at 03:46 AM.
|

12th April 2005, 04:12 AM
|
|
Registered User
|
|
Join Date: Mar 2005
Posts: 74

|
|
|
As for the second part, you can continue with "find". You just have to use a pattern search. With your example, you could use a simple wildcard match:
find ~/myfiles -name "*[0-9].png" -exec mv \{\} destination_dir \;
or, in case you want to be more restrictive/specific, you can use a regexp pattern like
find ~/myfiles -regex ".*[^0-9]+[0-9]+.png"
|

12th April 2005, 04:19 AM
|
|
Guest
|
|
Posts: n/a

|
|
Quote:
|
Originally Posted by BostonWatcher
As for the second part, you can continue with "find". You just have to use a pattern search. With your example, you could use a simple wildcard match:
find ~/myfiles -name "*[0-9].png" -exec mv \{\} destination_dir \;
or, in case you want to be more restrictive/specific, you can use a regexp pattern like
find ~/myfiles -regex ".*[^0-9]+[0-9]+.png"
|
Thank you! I was reading man find and I like that app, it's very nice 
Thank you both for the help.
|

12th April 2005, 04:36 AM
|
|
Guest
|
|
Posts: n/a

|
|
so, does this look correct?
Code:
#!/bin/bash
FILES="/home/user/myfiles"
for file in $FILES
do
find $FILES -atime +30 -exec rm {} \;
done
for file in $FILES
do
find $FILES -name "*[0-9].png" -exec mv \{\} /home/user/Documents \;
done
exit 0
|

12th April 2005, 06:09 AM
|
|
Registered User
|
|
Join Date: Feb 2004
Posts: 845

|
|
|
To avoid processing $FILES itself in the first find command, you can use
find $FILES -mindepth 1 -atime +30 -exec rm {} \;
and I don't think you need to backquote { and } in the second command. On the bash command line, find needs the ; to be backquoted, but not {} (try it).
|

12th April 2005, 06:12 AM
|
|
Guest
|
|
Posts: n/a

|
|
ok, fixed it, works fine 
Thank you all for the help.
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Current GMT-time: 11:52 (Thursday, 20-06-2013)
|
|
 |
 |
 |
 |
|
|