Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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:
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:
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 .
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".
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.