LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   [SOLVED] Scriting BASH delete old file with ctime. (https://www.linuxquestions.org/questions/linux-newbie-8/%5Bsolved%5D-scriting-bash-delete-old-file-with-ctime-4175451864/)

Serialkiller520 02-27-2013 02:54 AM

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

lleb 02-27-2013 06:56 AM

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.

unSpawn 02-27-2013 08:09 AM

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

colucix 02-27-2013 08:31 AM

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.

unSpawn 02-27-2013 08:39 AM

Crap, you're right! Thanks for correcting me.

linosaurusroot 02-27-2013 09:39 AM

Quote:

Originally Posted by lleb (Post 4900750)
Code:

find /c/home/test/D/ -maxdepth 1 -type d -mtime +15 -exec rm '{}' \;

Won't you want "rm -r" ?

Serialkiller520 02-27-2013 11:17 AM

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

lleb 02-27-2013 07:55 PM

Quote:

Originally Posted by linosaurusroot (Post 4900878)
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.

Serialkiller520 03-01-2013 01:23 AM

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

Serialkiller520 03-01-2013 01:32 AM

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

linosaurusroot 03-01-2013 02:19 AM

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.

Serialkiller520 03-01-2013 03:33 AM

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.

pablobhz 03-01-2013 04:51 AM

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 {} \;

Serialkiller520 03-04-2013 02:40 AM

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.


All times are GMT -5. The time now is 01:12 PM.