Delete files older than a specific time

semsaudade

New member
Is it possible to create a macro to delete all files older than one week (for example) in a given folder? Thank you!
 

Jacob L

Moderator (Lawsonator)
Needs two variables:
All
Current

In shell script action:

https://stackoverflow.com/questions...files-that-are-more-than-a-week-old-in-linux#
find /path/to/files* -mtime +7

Tick save to a variable: All https://stackoverflow.com/questions...-files-that-are-more-than-a-week-old-in-linux

See if this uses commas to separate the folders and files by commas by adding this to the end of the command:

| tr '\n' ','
This is called piping.

Use text manipulation action to replace comma with \n on the All variable

and use text manipulation action to extract text with ^+ from All to Current

Then use replace all in text manipulation action to replace All variable with Current

Then trim whitespace for all variables with text manipulation and use the Current variable in the command:

rm -r Current

Use the three dots menu to select your variable

DO A TRST FOLDER/FILES FIRST AND TAKE BACKUPS
 

Jacob L

Moderator (Lawsonator)
Please test this macro. Every device is different and will respond in different ways. Take a backup before running the macro to prevent accidental data loss. You can change the path variable to change the path which is being checked for old files. You can test if you want by turning off automatic date and time in the settings and change the date to something more than a week ago, create some files and then use auto date and time to put you back to the current time, which then makes the files older than a week.


If the link fails, head to the store and search id=10020
 

Jacob L

Moderator (Lawsonator)
What version of MacroDroid have you got? I think only users who have upgraded can use the store

Store appears at the bottom of the MacroDroid app. The attachment will import as a macro for MacroDroid.
 

Attachments

  • Delete_files_older_than_7_days.macro
    6.5 KB · Views: 37

semsaudade

New member
Thanks a lot! I upgraded macrodroid, downloaded your macro and changed the variable path to /storage/emulated/0/WhatsApp/Media/WhatsApp Images/
But I got the message "no such directory".
So sorry😐
 

tanutanu

Well-known member
If your target path contains white space, quote like "WhatsApp Images" or escape like WhatsApp\ Images.

Try one of below commands in shell script action. It's much simpler.
find /storage/emulated/0/WhatsApp/Media/WhatsApp\ Images/ -mtime +7 -delete
find /storage/emulated/0/WhatsApp/Media/WhatsApp\ Images/ -mtime +7 | xargs rm -f
 

tanutanu

Well-known member
In this case, whole and a part quoting are the same thing. Normally, a white space is recognized by shell itself as a delimiter of arguments. So you have to quote or escape at least the single space in your path.
In another case, with meta characters,
A: find "~/path/as/you/like/with space"
B: find ~/path/as/you/like/"with space"
A and B is completely different. So I recommend you quote as short as possible basically. UNIX shell is different than DOS cmd.
 

Dm114

Well-known member
In this case, whole and a part quoting are the same thing. Normally, a white space is recognized by shell itself as a delimiter of arguments. So you have to quote or escape at least the single space in your path.
In another case, with meta characters,
A: find "~/path/as/you/like/with space"
B: find ~/path/as/you/like/"with space"
A and B is completely different. So I recommend you quote as short as possible basically. UNIX shell is different than DOS cmd.
What would be the difference between A and B syntax?
 

tanutanu

Well-known member
What would be the difference between A and B syntax?
Don't tell me before you try, please;) Its easy to understand if you have enough experience to use UNIX like OS.
On Android device, you can access to toybox mksh via any terminal emulators if not.
 

Dimlos

Well-known member
Shell script to delete the latest screenshot.
Confirmed to work on Galaxy S23.
 

Attachments

  • Macro.jpg
    Macro.jpg
    393.9 KB · Views: 21
  • ShellScript.jpg
    ShellScript.jpg
    426.8 KB · Views: 19
Top