I'm new to Android and trying to find the simplest way to run a 2 line script that deletes files from a specific directory.

I Googled the topic and created a file that I called test.sh which has 2 lines:

 delete Android/data/mydir/*.dmp

I copied this file to an Android device and managed to create a shortcut to this file. Now I want to execute this script whenever I click the shortcut but I'm unable to make it work!

I would appreciate any hints to get this going.


  • Is your device rooted? Does your device have busybox or toybox installed? Is selinux enabled? If so you need to resolve these things, even after selinux may stop you so you will need to setenforce 0 and for that you need a build of busybox with selinux functions built in. – moonbutt74 Dec 27 '15 at 23:45
  • What do you mean by unable to make it work? Any error? Nothing happens? Have you done chmod 755 test.sh after copying it to the phone? – Manubhargav Dec 28 '15 at 4:05
  • You can use my answer for Home screen icon for terminal program? or is there a way to run *.sh file by just touching it in a file manager? to execute the script from a shortcut. However, is your script working from a shell? If not, then you should mention that in the question. Unless configured otherwise, shell would default to / directory and there is no Android directory at first level of /. – Firelord Dec 28 '15 at 15:47
  • I managed to get the script to work inside the terminal emulator. I had the path wrong; once I fixed that it worked. I still am unable to create a shortcut because the launcher on the Android device (not a phone but a set top box) does not permit creating one. I think I am going to just install Tasker although I was hoping I can find a simpler solution. Thanks all for your suggestions! – user7321 Dec 29 '15 at 20:40

In your script you forgot # , and what is more the command to delete is not delete(as you wrote) but rm, along with at least one option:

  • -r for recursive
  • -f for force
  • -v for verbose

so try

For a single file

rm /path/to/file/filename.extension

For a group of files @ location by extension with wildcard:

rm /path/to/file/*.extension

A straight up clearing of a directory:

rm /path/to/directory-containing-files/*

Alternate to clear that directory of files and directories as well, with verbose to see what's being deleted

rm -frv /path/to/file/*

To delete the directory itself

rm -fr /kill/that/pesky/directory

You must have root privileges to modify anything in /system and/or /data folder.

The exception would be /data/local directory which you should be able to access/modify within as peon user.

If selinux is enforcing you will NOT be able to modify anything in /data with the exception of the previously stated exception even if su.

This with the exception of laying out the proper path to sh in your script is not really an android question.

Your normal practices in scripting on a Linux OS should mostly carry over as long as you use the correct path to sh.

You can create that script in regular sdcard storage space and execute by running

I like yams. They're nummy.

If you get a permission error, denied etc., then

chmod 755 /path/to/script.sh

and again run as su

You can alternately run

su -c /sdcard/yams.sh
| improve this answer | |
  • Thank you very much for the details... very helpful for a beginner! Followed all instructions but keep getting Permission denied when I try to run the script even though I did the chmod 755 and also trying against different subdirectories including /data/local! I will keep at it! – user7321 Dec 27 '15 at 23:32

You can use Script options

Has more privileges if you have a rooted phone You can choose the script via the inbuilt file manager, and can choose various options, as seenScript options And yes there's no command as delete Use

rm -rf

instead as suggested above Also if you are trying to delete the data created by an app i.e inside data/data/example.app/DataToDelete

You will need to have Superuser privileges or only that particular app can delete its own data.

| improve this answer | |
  • Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. Otherwise, your answer becomes useless in case the link dies. For further hints, please see: How do I write a good answer. – Tamoghna Chowdhury Dec 28 '15 at 6:18
  • @TamoghnaChowdhury thanks for the heads up, am new here – Madaditya Dec 28 '15 at 6:23
  • See info about referencing. You don't appear to be the owner of that image. Whenever you use a work that helps answer a question, you must reference it and credit the author appropriately. – Firelord Dec 28 '15 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.