Delete files in a folder after a certain time (base = creation date)

994 views
Skip to first unread message

[ Scolaro]

unread,
Jan 15, 2014, 9:49:18 AM1/15/14
to tas...@googlegroups.com
Hi,

I have a rooted phone (Samsung Note 2), Tasker and Secure Settings.
One of my task actions backs up some files in the middle of the night via FolderSync, but since the amount of files in the backup folders constantly increases (e. g. the WhatsApp database files) I have to go to the folders myself on a regular basis to delete the older stuff manually.

I've been searching for ages, but there doesn't seem to be a way to tell Tasker to delete files in a certain folder after a certain time (3 days or so) based on their creation date. Sadly I have no experience at all with scripts, but looked around and tried to copy/adjust one with the following text:

@! find /storage/sdcard0/WhatsApp/Databases/* -mtime +3 -exec rm -rf {} \;

(Source)

After backing up the folder I tried running the script in a Tasker action via Secure Settings --> Run Command, but it's not working.
Any idea if this would/should actually do what I'm trying to accomplish and how to set it up in Tasker?

Thank you very much in advance!

Scolaro

nanobrain

unread,
Jan 15, 2014, 10:08:38 AM1/15/14
to tas...@googlegroups.com
You don't need the !@ part for Secure Settings

[ Scolaro]

unread,
Jan 15, 2014, 10:33:27 AM1/15/14
to tas...@googlegroups.com
Many thanks for the reply! Okay, my line in the script file "delete_me" now reads:

find /storage/sdcard0/WhatsApp/Databases/* -mtime +3 -exec rm -rf {} \;

Here's the very simple profile text (I'm just using 1st Dec while fiddling around with the tasker action):

Profile: Delete Old Files (2)
Day: The 1st of Dec
Enter: Delete Old Files (15)
A1: Secure Settings [ Configuration:sh /sdcard0/delete_me Package:com.intangibleobject.securesettings.plugin Name:Secure Settings Timeout (Seconds):0 ]

Unfortunately it's still not working...

-----------

Bob Hansen

unread,
Jan 15, 2014, 11:49:56 AM1/15/14
to tas...@googlegroups.com
Remove the semicolon (;) from the end of the command.

find /storage/sdcard0/WhatsApp/Databases/* -mtime +3 -exec rm -rf {} \



[ Scolaro]

unread,
Jan 16, 2014, 3:51:32 AM1/16/14
to tas...@googlegroups.com
Thank you, Bob! But still... nothing.

If that line is correct now, though, I'm almost positive the mistake is somewhere in the Run Command setup (because I really played that one by ear).

Basically under "Configuration" in Tasker's Action Edit screen I hit the edit pen icon and am transferred to the Run Command screen in Secure Settings.
I gave the Run Command action itself (field at the top of the screen) the random name "Los", but am not sure if it matters what I put in here. Also, the Command Name I use is "sh /sdcard0/delete_me". (That's the command that should start the script, isn't it?)
The only ticked off box in the list above is "Wait for process".


Bob Hansen

unread,
Jan 16, 2014, 9:22:24 AM1/16/14
to tas...@googlegroups.com
When ever your setting up a run command it is best to use a terminal emulator app and create and test the command in that and then copy and paste it into the run shell command. I use Android Terminal Emulator.

This worked for me testing on text files.

Find Files Older Than (181)
        <find /storage/sdcard0/*.txt -mtime +1 -exec rm -rf {} \>
        A1: Run Shell [ Command:find /storage/sdcard0/*.txt -mtime -1 Timeout (Seconds):0 Use Root:Off Store Output In:%out Store Errors In: Store Result In: ]
        A2: Flash [ Text:%out Long:Off ]

It is important that the files to be deleted not be in use or read only, otherwise the command would fail.
Also, I would suggest using a file extension in the path because a * alone might match something that can't be deleted like a sub directory.

[ Scolaro]

unread,
Jan 18, 2014, 10:59:14 AM1/18/14
to tas...@googlegroups.com
Thanks for the hint - I've installed Android Terminal Emulator and fiddled around with it all morning. It runs simple Unix commands fine, but not the edited delete_me file, even though I added the file extension (.crypt, --> file permissions: rw-rw-r--).

For testing purposes I started from scratch, created a new folder called "Test" and put in a few .txt files, then copied the command line from your last posting and added the appropriate folder, hence creating a new delete_me file with the following content:

find /storage/sdcard0/Test/*.txt -mtime +1 -exec rm -rf {} \

I copied that file into the sdcard0 folder, then started the Terminal Emulator and wrote
sh /storage/sdcard0/delete_me

Output was: "-exec requires an argument"

After typing
/storage/sdcard0/delete_me

the output was: "/storage/sdcard0/delete_me: can't execute: Permission denied"

I'm a bit stuck, because the info I do understand about Unix commands seems to be far below the level of what I'm trying to achieve. If you don't want to invest the time to help me, I understand. In the end, it's a stupid little first world problem and I can still delete these files manually. ;)

nanobrain

unread,
Jan 18, 2014, 1:41:10 PM1/18/14
to tas...@googlegroups.com
Second problem first: in Unix the file extension doesn't matter, the access permission determine if a file is executable, i.e your file needs to have at least rwxrw-r-- permissions. 

Your first try didn't work because despite what Bob Hansen said earlier you need the semicolon after the \ 

You can add !#/bin/sh as the first line of your script so you don't need to specify sh before calling it.

Hope that helps.

Bob Hansen

unread,
Jan 18, 2014, 1:41:22 PM1/18/14
to tas...@googlegroups.com
The command "find /storage/sdcard0/Test/*.txt -mtime +1 -exec rm -rf {} \" works fine for me. Do you have BusyBox installed? 

If not. try installing it and then try the command again.

nanobrain

unread,
Jan 18, 2014, 1:46:59 PM1/18/14
to tas...@googlegroups.com
Hah, it only works for me with semicolon :) Different shells? I do have busybox installed
Reply all
Reply to author
Forward
0 new messages