LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 02-27-2013, 03:54 AM   #1
Serialkiller520
LQ Newbie
 
Registered: Feb 2013
Location: france
Distribution: Mainly Debian Squeeze.
Posts: 6

Rep: Reputation: Disabled
Smile [SOLVED] Scriting BASH delete old file with ctime.


Hi all, I'm actually having an issue with a script I have to make. I first try commands before making the script and even with commands it doesn't work.

By the way sorry for my english, I'm french.

I have to delete on a ReadyNAS Pro directories that are older than X days. I've checked and "mtime" seems to be the best way to found them.

Easy way... However, whatever I try it doesn't work.
First I had issues with the spaces in the name. When I tried to resolve it, I have an error in find must precede expression...
I tried aproximately 15 differents syntaxes that i found on the web, but none worked as I wanted.

Here is my current command:

Code:
find /c/home/test/D/ -maxdepth 1 -type d -mtime +3 print0 | xargs -0 rm -r >> /c/home/test/log/delete 2>> /c/home/test/log/errordel
In my final script I will replace full paths with variable.

What is wrong with that command?
I've been working on that since 3 days without succeding in anything..

Thanks in advance.

Edit: I didn't check on the forum if someone else had this issue because after all my search on web I really don't know where came from my probleme.

Last edited by Serialkiller520; 03-04-2013 at 03:40 AM. Reason: problem solved
 
Old 02-27-2013, 07:56 AM   #2
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
Code:
find /c/home/test/D/ -maxdepth 1 -type d -mtime +3 -exec rm -rf '{}' \;
should do the trick nicely.

if it works please let me know and mark post as [SOLVED]

edited to change to the correct number of days and to add the proper -rf flag to command.

Last edited by lleb; 02-27-2013 at 08:56 PM.
 
Old 02-27-2013, 09:09 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529
...and if you've got a "modern" version of 'find' then this should work too:
Code:
find /c/home/test/D/ -maxdepth 1 -type d -mtime +3 -delete
 
Old 02-27-2013, 09:31 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Just a quick note: the -delete action removes only empty directories. If the output includes all the files inside the directory they will be removed first, since -delete implies -depth and the directory will result empty before deletion. However, here we have -type d and the content of the directory (if any) is left untouched.
 
Old 02-27-2013, 09:39 AM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529
Crap, you're right! Thanks for correcting me.
 
Old 02-27-2013, 10:39 AM   #6
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 979
Blog Entries: 2

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by lleb View Post
Code:
find /c/home/test/D/ -maxdepth 1 -type d -mtime +15 -exec rm '{}' \;
Won't you want "rm -r" ?
 
Old 02-27-2013, 12:17 PM   #7
Serialkiller520
LQ Newbie
 
Registered: Feb 2013
Location: france
Distribution: Mainly Debian Squeeze.
Posts: 6

Original Poster
Rep: Reputation: Disabled
Hi all, thanks to you for your responses.
I won't be able to tets your answers until thursday cauz' i'm working in a remote site for 2 days.

Effectively a "rm -r" should match better.

In fact, i'm using cobian backup for saving a dataserver's repertories. It means my repertories are full of repertories and files and repertories... Moreover the names of my repertories are like: "MYFOLDERTOSAVE 2012-02-28 10h50 (Incremental)" which is very boring. I can't change that automated naming so I've to work thinking of these spaces (maybe blank characters, don't know the word).

I want to delete old files cauz' the NAS is limited up to 1To that represent about a month of saves.

Maybe i should have started with that explanation first before giving you a script built with parts of many other scripts in many website...
 
Old 02-27-2013, 08:55 PM   #8
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
Quote:
Originally Posted by linosaurusroot View Post
Won't you want "rm -r" ?
yes you would need the -r for removing the directories... my bad, i typically will use rm -rf when i remove things. sorry tossed that up rather fast this morning.
 
Old 03-01-2013, 02:23 AM   #9
Serialkiller520
LQ Newbie
 
Registered: Feb 2013
Location: france
Distribution: Mainly Debian Squeeze.
Posts: 6

Original Poster
Rep: Reputation: Disabled
Hi all,

back to work and I wanted to try but I've just one more question.

"-maxdepth n" isn't it for searching only to n directory from where the commands start? Because I have many subfolders.
May I try "-mindepth n" instead or am I misunderstanding the command?

Whatever your answer could be, i try your command on my test nas and let you know result.
 
Old 03-01-2013, 02:32 AM   #10
Serialkiller520
LQ Newbie
 
Registered: Feb 2013
Location: france
Distribution: Mainly Debian Squeeze.
Posts: 6

Original Poster
Rep: Reputation: Disabled
ok... I tried lol

So what happened:

find: /c/home/test/D/: No such file or directory

so I tried without the last "/" after "D" like this:

Code:
find /c/home/test/D -maxdepth 1 -type d -mtime +3 -exec rm -rf '{}' \; >> /c/home/test/log/delete 2>> /c/home/test/log/errordel
Then my repertory D simply disapear with all its content ^^
 
Old 03-01-2013, 03:19 AM   #11
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 979
Blog Entries: 2

Rep: Reputation: 235Reputation: 235Reputation: 235
Using -ok instead of -exec is sometimes a good idea; or even run with -print before using another command to see what it expects to work on.

Last edited by linosaurusroot; 03-01-2013 at 03:20 AM.
 
1 members found this post helpful.
Old 03-01-2013, 04:33 AM   #12
Serialkiller520
LQ Newbie
 
Registered: Feb 2013
Location: france
Distribution: Mainly Debian Squeeze.
Posts: 6

Original Poster
Rep: Reputation: Disabled
Yah I know it but as I'm not a pro in bahs commands lines i didn't want to complicate my task. I never used those parameters and even don't know about -ok.

Whatever it's not a real problem, it's a NAS i used for testing no any ral data have been deleted. It's jsut I'll have to wait few more days for the repertory to get new files and folders.
 
Old 03-01-2013, 05:51 AM   #13
pablobhz
LQ Newbie
 
Registered: Aug 2012
Posts: 7

Rep: Reputation: Disabled
This is mine
Working for a few months (almost 1yr :P)

find /home/fileserver/backups/Setorial/Diario -type f -mtime +5 >> /home/fileserver/backups/logs/Removidos/Removidos-$INICIO.log
find /home/fileserver/backups/Setorial/Diario -type f -mtime +5 -exec rm {} \;
 
Old 03-04-2013, 03:40 AM   #14
Serialkiller520
LQ Newbie
 
Registered: Feb 2013
Location: france
Distribution: Mainly Debian Squeeze.
Posts: 6

Original Poster
Rep: Reputation: Disabled
So this morning I check again what "-xtime" to use.
In fact, "-ctime" is the appropriate parameter for my script.

What I type:
Code:
find /c/home/test/D -maxdepth 1 -type d -ctime +1 -exec rm -rf '{}' \; >> /c/home/test/log/delete 2>> /c/home/test/log/errordel
And it work very fine. Thanks the all of you for helping. Just have to write down and comment my script.

Last edited by Serialkiller520; 03-04-2013 at 03:41 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
[SOLVED] bash - delete lines that starts with one particular lettere from a file Hoxygen232 Linux - Newbie 2 02-05-2013 03:19 PM
[SOLVED] bash delete part of each line in file nushki Programming 8 04-29-2011 01:42 AM
[SOLVED] Bash How to delete only one line from a file wizard211990 Programming 16 04-11-2011 08:43 AM
using find to compare file mtime with another file's mtime TheFueley Linux - Newbie 1 12-23-2008 09:06 PM
Need a bash shell script which will delete lines from file scjohnie Linux - Newbie 1 09-13-2008 09:51 PM


All times are GMT -5. The time now is 03:04 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration