LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-01-2009, 03:50 PM   #1
JavaNinja
Member
 
Registered: Sep 2008
Posts: 90

Rep: Reputation: 15
Edit script to not remove certain files


Hello all,

Google led me to a post by d3funct which is exactly what I needed but I need to modify it now.

Code:
find /var/log -mtime +60 -type f -exec rm -rf {} \;
This will delete files in the directory /var/log that are older than a day?

How can I adapt it so that it only deletes files that are of type ".flv" - I can guess and test but that will mess things up ^^ - is it something to do with the "-name '*.flv'" option??

Thanks all
 
Old 01-01-2009, 04:16 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,825
Blog Entries: 1

Rep: Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221
This should do:
Quote:
find /var/log -mtime +60 -type f -name \*.flv -exec rm -rf {} \;
 
Old 01-01-2009, 04:18 PM   #3
cmnorton
Member
 
Registered: Feb 2005
Distribution: Ubuntu, CentOS
Posts: 585

Rep: Reputation: 35
Create a sandbox directory and experiment

-name "*.flv"

Add that to your find command.

I do not understand why copying all the /var/logs into a sandbox (junk, play, experiment) directory will mess things up. Try things out there, and then implement the command in your "production" directory.
 
Old 01-01-2009, 04:37 PM   #4
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-14.2 on Lenovo Thinkpad W520
Posts: 7,763

Rep: Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691
Code:
This will delete files in the directory /var/log that are older than a day?
No, but older than 60 days. Read "man find".

Be very careful when using find with the option "exec rm -rf". This is especially important, because:
- find is recursive: it will not only find the files meeting the conditions you set up in /var/log itself, but also in all sub directories.
- "rm -rf" is recursive too: you will erase all concerned sub-directories and their content.

So may I suggest you first write instead:
Code:
find /var/log -daystart -maxdepth 0 -mtime +1 -type f -name "*.flv"
and double-check the output to make sure you won't erase files you need.

Then and only then you can add "|xargs rm -f" (without the quotes) to your command..
 
Old 01-01-2009, 04:40 PM   #5
JavaNinja
Member
 
Registered: Sep 2008
Posts: 90

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by cmnorton View Post
I do not understand why copying all the /var/logs into a sandbox (junk, play, experiment) directory will mess things up. Try things out there, and then implement the command in your "production" directory.
I am noob, and everything I usually touch on the linux machine gets messed up. I've probably had reboot hundreds of times (and I am not even sure that fixed the problem)!

Thanks guys, this is the command I will use:
Code:
find /var/log -mtime +60 -type f -name \*.flv -exec rm -rf {} \;
 
Old 01-01-2009, 04:49 PM   #6
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-14.2 on Lenovo Thinkpad W520
Posts: 7,763

Rep: Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691
Did you read my answer ? Remember the warning.
 
Old 01-01-2009, 04:50 PM   #7
JavaNinja
Member
 
Registered: Sep 2008
Posts: 90

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Didier Spaier View Post
No, but older than 60 days. Read "man find".
Ok, got it.

Quote:
Originally Posted by Didier Spaier View Post
Be very careful when using find with the option "exec rm -rf".
lol these are the kind of warnings I need, thanks Didier.

Quote:
Originally Posted by Didier Spaier View Post
So may I suggest you first write instead:
Code:
find /var/log -daystart -maxdepth 0 -mtime +1 -type f -name "*.flv"
Ah I need the maxdepth option since this script shouldn't need to go into sub directories. Thats a good precaution.

Ok will check the above, the final command will be:
Code:
find /var/log -daystart -maxdepth 0 -mtime +1 -type f -name "*.flv" |xargs rm -f
I don't quite get the -daystart:
Code:
 -daystart Measure times (for -amin, -atime,  -cmin,  -ctime,  -mmin,  and -mtime)  from  the beginning of today rather than from 24 hours ago.
So it will start counting from the beginning of today (12AM) and anything that was 24hrs ago from then gets deleted?

Many thanks Didier.
 
Old 01-01-2009, 04:57 PM   #8
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-14.2 on Lenovo Thinkpad W520
Posts: 7,763

Rep: Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691
Quote:
So it will start counting from the beginning of today (12AM) and anything that was 24hrs ago from then gets deleted?
This is how I understand it, but it isn't very important. You could write "-time +2" instead.
 
Old 01-06-2009, 05:29 AM   #9
JavaNinja
Member
 
Registered: Sep 2008
Posts: 90

Original Poster
Rep: Reputation: 15
I am using this in a cron job:
Code:
find /home/get/public_html/videos -daystart -maxdepth 0 -mtime +1 -type f -name "*.flv" |xargs rm -f
But this desn't do anything! I tried to run this command in a folder that was full of .flv files and were 2 or 3 days old and it didn't give any output:
Code:
find /home/get/public_html/videos -daystart -maxdepth 0 -mtime +1 -type f -name "*.flv"
What am I doing wrong?
 
Old 01-06-2009, 06:22 AM   #10
linuxlover.chaitanya
Senior Member
 
Registered: Apr 2008
Location: Nagpur, India
Distribution: Cent OS 5/6, Ubuntu Server 10.04
Posts: 4,629

Rep: Reputation: Disabled
Are you running the cron job as the user that has enough permissions to delete the files from the directory.
 
Old 01-06-2009, 06:43 AM   #11
JavaNinja
Member
 
Registered: Sep 2008
Posts: 90

Original Poster
Rep: Reputation: 15
[SOLVED]

Code:
find /home/get/public_html/videos -daystart -maxdepth 1 -mtime +1 -type f -name "*.flv" |xargs rm -f
Yes, I have all permissions, it was because -maxdepth was set to 0, it should be 1 for it to acutally search the directory.

Last edited by JavaNinja; 01-06-2009 at 06:45 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script to remove backup ~ files hallamigo Linux - General 3 09-13-2010 04:47 PM
Help with script to batch edit text files OnoTadaki Programming 5 10-15-2007 03:44 PM
script - remove files after specified time bskrakes Linux - General 12 09-30-2006 12:30 PM
edit wav files via command line in a script legolin Linux - Software 4 12-21-2005 11:09 AM
Script to remove old files. cmfarley19 Linux - General 2 03-24-2003 02:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:12 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration