LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-30-2006, 04:39 PM   #1
xtremeclones
Member
 
Registered: Jan 2006
Posts: 70

Rep: Reputation: 15
doing a locate with grep on a file older than *


hey guys, im trying to delete old junk and i have 1000's of .wav files.

I want to do a search all files on a directory that are older than 1 month old, how can i do so?

thank you!!
 
Old 10-30-2006, 05:12 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Read though the "info find" manual.
Something like: "find <path-to-directory> -maxdepth 1 -mtime +30 -iname "*.wav" -exec rm '{}' \;" should do the trick.

First test it without the "-exec ls -l '{}' \;" instead of "-exec rm '{}' \;" which will print out the files it has found.

You could instead use "-printf '%f %p'" instead to print the filename and modification time.

This LinuxJournal article has quite a few examples of using the find command: http://www.linuxjournal.com/article/1180

Last edited by jschiwal; 10-30-2006 at 05:14 PM.
 
Old 10-30-2006, 05:15 PM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
You can ask find to list files whose last modification date is more than some number of days, if that's close enough for you. Do this from the folder. Be aware that it will operate in all sub-directories as well!

Code:
find . -type f -mtime +30 -iname \*.wav
You can get rid of the "-iname \*.wav" to list all files - not just those called something.wav...

This will list the files. Good to do that first and check the results are OK. Then you can do the actual delete, by appending xargs, like this:

Code:
find . -type f -mtime +30 -iname \*.wav |xargs rm -f
Enjoy, but be careful!
 
Old 10-30-2006, 05:20 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
find is quite confusing to start with, but it's an amazingly powerful program. While I was typing, jschiwal wrote more or less the same command as me, but using the -exec option in find rather than passing the list to xargs.

The xargs way is better if you have a very large number of files because it spawns one rm process for a large number of files, whereas using -exec will spawn one rm process per file to be deleted. Spawn processes takes time, so for large numbers of files you may notice xargs is significantly quicker.

However, there is a price to pay - files with spaces and other weird characters in the filename (OK, spaces aren't so weird) may cause the xargs method to not function properly. You can fix this using -print0 with find, and -0 with xargs:

Code:
find . -type f -mtime +30 -iname \*.wav -print0 |xargs -0 rm -f
 
Old 10-31-2006, 12:49 AM   #5
xtremeclones
Member
 
Registered: Jan 2006
Posts: 70

Original Poster
Rep: Reputation: 15
hey guys,

thank you all for helping me with this. I ended up doing this
find /var/spool/asterisk/voicemail/default/ -type f -mtime +200 -iname \*.wav |xargs

and it worked great, if take out the |xargs it looks more uniformed however im totally feeling the |xargs and im deffinitely going to be using this from now on so thank you very much!!

now, basically if i want to only locate a file that starts with msg how do i put that i tried doing
find /var/spool/asterisk/voicemail/default/ -type f -mtime +200 -iname \msg*.wav |xargs but it did't work,
Incase you're wondering yes im deleting old voicemails from the system .

Again thank you very much for the help!!
 
Old 10-31-2006, 01:59 AM   #6
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Quote:
Originally Posted by xtremeclones
\msg*.wav
You're not supposed to escape the "m" which is a normal character. The "*" will get expanded by the shell on invocation of the find command, though, so it should be escaped.

In short, you should thus use:
-iname msg\*.wav
or
-iname 'msg*.wav'

For next time, I suggest you read up on quoting and escaping in "man bash".
 
Old 10-31-2006, 02:21 PM   #7
xtremeclones
Member
 
Registered: Jan 2006
Posts: 70

Original Poster
Rep: Reputation: 15
Worked like a charm..

Thank you so much!!
 
Old 10-31-2006, 05:43 PM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
I thought I would add a little bit of info. The -ctime argument (create time) uses the date of the inode for the file, so if you change the attributes of the file, such as changing the ownership or permissions, then the -ctime will be effected. That's why -mtime may be better to use.
 
Old 11-02-2006, 07:02 PM   #9
xtremeclones
Member
 
Registered: Jan 2006
Posts: 70

Original Poster
Rep: Reputation: 15
Awesome inside on the ctime.

thank you!!
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Grep and Locate xtremeclones Linux - General 7 07-12-2006 08:19 PM
locate | grep allelopath Linux - Software 3 03-24-2006 02:07 PM
Can't locate file.... thethinker101 Slackware 4 10-18-2004 04:48 PM
Using the results of locate for grep skibud2 Linux - Newbie 1 12-16-2003 01:58 PM
GREP a locate Hegemon Linux - Newbie 3 02-22-2002 04:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration