LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Edit script to not remove certain files (https://www.linuxquestions.org/questions/linux-newbie-8/edit-script-to-not-remove-certain-files-694295/)

JavaNinja 01-01-2009 02:50 PM

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 :)

sycamorex 01-01-2009 03:16 PM

This should do:
Quote:

find /var/log -mtime +60 -type f -name \*.flv -exec rm -rf {} \;

cmnorton 01-01-2009 03:18 PM

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.

Didier Spaier 01-01-2009 03:37 PM

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..

JavaNinja 01-01-2009 03:40 PM

Quote:

Originally Posted by cmnorton (Post 3393722)
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 {} \;

Didier Spaier 01-01-2009 03:49 PM

Did you read my answer ? Remember the warning.

JavaNinja 01-01-2009 03:50 PM

Quote:

Originally Posted by Didier Spaier (Post 3393738)
No, but older than 60 days. Read "man find".

Ok, got it.

Quote:

Originally Posted by Didier Spaier (Post 3393738)
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 (Post 3393738)
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.

Didier Spaier 01-01-2009 03:57 PM

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.

JavaNinja 01-06-2009 04:29 AM

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?

linuxlover.chaitanya 01-06-2009 05:22 AM

Are you running the cron job as the user that has enough permissions to delete the files from the directory.

JavaNinja 01-06-2009 05:43 AM

[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.


All times are GMT -5. The time now is 05:44 AM.