Since the permission policy on my device is a bit paranoid (cannot adb pull application data), I wrote a script to copy files recursively.
Note: this recursive file/folder copy script is intended for Android!
copy-r:
#! /system/bin/sh src="$1" dst="$2" dir0=`pwd` myfind() { local fpath=$1 if [ -e "$fpath" ] then echo $fpath if [ -d "$fpath" ] then for fn in $fpath/* do myfind $fn done fi else : echo "$fpath not found" fi } if [ ! -z "$dst" ] then if [ -d "$src" ] then echo 'the source is a directory' mkdir -p $dst if [[ "$dst" = /* ]] then : # Absolute path else # Relative path dst=`pwd`/$dst fi cd $src echo "COPYING files and directories from `pwd`" for fn in $(myfind .) do if [ -d $fn ] then echo "DIR $dst/$fn" mkdir -p $dst/$fn else echo "FILE $dst/$fn" cat $fn >$dst/$fn fi done echo "DONE" cd $dir0 elif [ -f "$src" ] then echo 'the source is a file' srcn="${src##*/}" if [ -z "$srcn" ] then srcn="$src" fi if [[ "$dst" = */ ]] then mkdir -p $dst echo "copying $src" '->' "$dst/$srcn" cat $src >$dst/$srcn elif [ -d "$dst" ] then echo "copying $src" '->' "$dst/$srcn" cat $src >$dst/$srcn else dstdir=${dst%/*} if [ ! -z "$dstdir" ] then mkdir -p $dstdir fi echo "copying $src" '->' "$dst" cat $src >$dst fi else echo "$src is neither a file nor a directory" fi else echo "Use: copy-r src-dir dst-dir" echo "Use: copy-r src-file existing-dst-dir" echo "Use: copy-r src-file dst-dir/" echo "Use: copy-r src-file dst-file" fi
Here I provide the source of a lightweight find for Android because on some devices this utility is missing. Instead of myfind one can use find, if it is defined on the device.
Installation:
$ adb push copy-r /sdcard/
Running within adb shell (rooted):
# . /sdcard/copy-r files/ /sdcard/files3
or
# source /sdcard/copy-r files/ /sdcard/files3
(The hash # above is the su prompt, while . is the command that causes the shell to run the specified file, almost the same as source).
After copying, I can adb pull the files from the sd-card.
Writing files to the app directory was trickier, I tried to set r/w permissions on files and its subdirectories, it did not work (well, it allowed me to read, but not write, which is strange), so I had to do:
String[] cmdline = { "sh", "-c", "source /sdcard/copy-r /sdcard/files4 /data/data/com.example.myapp/files" }; try { Runtime.getRuntime().exec(cmdline); } catch (IOException e) { e.printStackTrace(); }
in the application's onCreate().
PS just in case someone needs the code to unprotect application's directories to enable adb shell access on a non-rooted phone,
setRW(appContext.getFilesDir().getParentFile()); public static void setRW(File... files) { for (File file : files) { if (file.isDirectory()) { setRW(file.listFiles()); // Calls same method again. } else { } file.setReadable(true, false); file.setWritable(true, false); } }
although for some unknown reason I could read but not write.
./adb shell. If you are saying you have noadbavailable, how can you access the shell?pullandpushare recursive. Check out here.