Fedora Linux Support Community & Resources Center

Go Back   FedoraForum.org > Fedora 17/18 > Using Fedora
FedoraForum Search

Forgot Password? Join Us!

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

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 12th April 2005, 02:02 AM
ianmac
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?
Reply With Quote
  #2  
Old 12th April 2005, 03:29 AM
robatino Offline
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.
Reply With Quote
  #3  
Old 12th April 2005, 03:41 AM
robatino Offline
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.
Reply With Quote
  #4  
Old 12th April 2005, 03:42 AM
ianmac
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.
Reply With Quote
  #5  
Old 12th April 2005, 04:12 AM
BostonWatcher Offline
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"
Reply With Quote
  #6  
Old 12th April 2005, 04:19 AM
ianmac
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.
Reply With Quote
  #7  
Old 12th April 2005, 04:36 AM
ianmac
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
Reply With Quote
  #8  
Old 12th April 2005, 06:09 AM
robatino Offline
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).
Reply With Quote
  #9  
Old 12th April 2005, 06:12 AM
ianmac
Guest
 
Posts: n/a
ok, fixed it, works fine
Thank you all for the help.
Reply With Quote
Reply

Tags
date, deleting, files

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting files in KDE wombat53 Using Fedora 5 8th March 2009 09:57 PM
Problem when deleting files Mystinar Using Fedora 2 31st July 2006 12:25 PM
Deleting 1.5 million files dhav Using Fedora 6 12th June 2006 09:06 PM
Deleting kernel files Ridgerunner Using Fedora 3 10th March 2006 09:18 PM
Deleting tmp files satimis Using Fedora 7 1st November 2004 04:32 AM


Current GMT-time: 11:52 (Thursday, 20-06-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat