[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 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. |
Code:
find /c/home/test/D/ -maxdepth 1 -type d -mtime +3 -exec rm -rf '{}' \; 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. |
...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 |
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.
|
Crap, you're right! Thanks for correcting me.
|
Quote:
|
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... |
Quote:
|
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. :) |
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 |
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.
|
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. |
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 {} \; |
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 |
All times are GMT -5. The time now is 01:12 PM. |