3

I would like to check whether a directory exists on the SD card of an Android device using Bash.

I am aware that a similar question was answered here: How can I check if a directory exists in a Bash shell script?

The difference is that when I do

if [ -e /sdcard/myDir ]; then
     # magic
fi

it is checked whether /sdcard/myDir exists on my computer and not on the phone. How can I check if the folder exists on the phone?

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171

2 Answers2

4

You could have done the following:

if [ `adb shell "if [ -e /sdcard/myDir ]; then echo 1; fi"` ]; then 
   echo "Folder exists";
else
   echo "Folder does not exist";
fi
AlexB
  • 7,302
  • 12
  • 56
  • 74
Nicolae Natea
  • 1,185
  • 9
  • 14
3

If I understood you correctly try:

adb shell

... and then type your shell commands on the device. I'm not really sure if bash is available on standard Android device. I'd bet that there are only simple busybox tools installed.

Also note, that there is very limited set of directories that you will be able to access this way on non-rooted device.

UPDATE: More precisely, if you need to execute some kind of shell script on the remote device, first prepare the script, say foo.sh and then execute:

adb push foo.sh /sdcard/
adb shell sh /sdcard/foo.sh

That should do the trick.

pkk
  • 3,691
  • 2
  • 21
  • 29
  • I have a script on my PC that's supposed to query whether the folder exists or not. I think the command should be something along the lines of `if [ adb shell -d $myFolder]; then adb shell rm -r "$myFolder" fi` – Matthias Braun Aug 17 '11 at 16:43
  • In this case, create foo.sh file, that contains: `if [ -d $myfolder]; then adb shell rm -r $myfolder fi` and push & run it on the device as described. – pkk Aug 17 '11 at 18:47