Search code examples
androidrootsu

Copy Files and directories with su command on rooted device


Similar Questions are often asked here, but I can´t figure out, what exactly I have to do. This is not a duplicate, because the solutions on many threads here are not what I am looking for or doesn´t work for me. I want to copy files and directories to a folder inside the /data/ folder. So, I made a lot of search on SO and Googled for hours. But all the threads I found are not clear enough and don´t let me get what I want. I also found this Blogs, but that doesn´help me.

http://su.chainfire.eu/

http://forum.xda-developers.com/showthread.php?t=1694251

What I have tried

I am able to copy a directory to sd from the /data/ directory without need a su command and it works well just with java code. First question: Is root permission not neccessary for copying from /data/ folder to sd card? The next step I have done is, to execute su command like this:

Process pp  = Runtime.getRuntime().exec("su cat "+originDir+" > "+targetDir);

Where orginDir is the path on sd card from a file and the targetDir is the folder inside the /data/ directory as Strings, for example:

/data/test/testfile.txt

The problem here is, I can copy this file, but it has 0 Bytes. So what I am doing wrong?

What I want

  1. I want to copy a file from sd card to /data/ directory
  2. I want to create a folder on /data/ directory
  3. I want t o copy a whole directory to /data/ directory

I have searched a lot, but I can´t find a good tutorial for using su command to write files to that directory. Also, is there a list anywhere with all in Android available su commands that can be executed programmatically? Belive me, I am not a lazy developer, I have searched many things, but I hadn´t find a good tutorial for this.


Solution

  • Use ths copy linux command and add -r (recursive) switch to it cp -r /source/ /target/. It will create target if it does not exists

    But prior to that you need to make your system is writable

    mount -o remount,rw /system 
    cp -r /source/ /target/
    mount -o remount,ro /system #Don't forget to do this, even if everything else fails!!!!
    

    EDIT: According to comments below, this solution should only be used when targeting relatively new devices