Matthias Braun
Matthias Braun

Reputation: 34303

Check if directory exists on Android SD card with Bash

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?

Upvotes: 3

Views: 5880

Answers (2)

Nicolae Natea
Nicolae Natea

Reputation: 1194

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

Upvotes: 4

pkk
pkk

Reputation: 3691

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.

Upvotes: 3

Related Questions