How to copy and edit files in the Android shell?

There is no cp command in the Android shell. Android also lacks sed or grep or vi . I do not have adb daemon. There is an mv command, but it refuses to work if the source is in a read-only device.

  • What if I need to copy some directories from a read-only device?
  • How to change a line in a text file (for example, "PATH = / cache" to "PATH = / mnt / asec")?
+64
android linux shell sh android-ndk
Jan 17 2018-11-11T00:
source share
11 answers

The most common answer to this question is simple: to connect several applications (busybox?) With the APK (provided that you want to use it in the application). As far as I know, the / data section is not mounted by noexec, and even if you do not want to deploy a full-fledged APK, you can change ConnectBot sources to create an APK using the command-line toolkit.

For command line tools, I recommend using crosstool-ng and creating a set of statically related tools (related to uClibc). They may be large, but they will definitely work.

+16
Jan 31 '11 at 2:21
source share

To copy dirs, it seems you can use adb pull <remote> <local> if you want to copy the / dir file from the device and adb push <local> <remote> to copy the file / directory to the device. Alternatively, to copy the file, you can use a simple trick: cat source_file > dest_file . Please note that this does not work for user inaccessible paths.

To edit the files, I did not find a simple solution, just some possible workarounds. Try this , it seems that you can (after installation) use it to edit files such as busybox vi <filename> . Nano seems to be usable .

+90
Feb 01 '11 at 11:26
source share

You can do this without root privileges:

 cat srcfile > /mnt/sdcard/dstfile 
+31
Mar 15 2018-12-15T00:
source share

You can use cat > filename to use standard input to write to a file. At the end you should put EOF CTRL+D

+9
Jul 30 '13 at 10:52
source share

Also, if the goal is to only access files on the phone. There is a file explorer available from the perspective of Eclipse DDMS. It allows you to copy a file from and to the device. Therefore, you can always get the file, modify it and return it back to the device. Of course, it only allows access to files that are not write protected.

If you do not see File Explorer, from the point of view of DDMS, go to "Window" → "Show View" → "Explorer".

+4
Feb 01 '11 at 21:05
source share

Since the permissions policy on my device is a bit paranoid (cannot adb pull application data), I wrote a script to recursively copy files.

Note: this recursive copy of the script file / folder is for Android!

copy-p:

 #! /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 the easy find for Android, because on some devices this utility is missing. Instead of myfind you can use find if it is defined on the device.

Installation:

 $ adb push copy-r /sdcard/ 

Running inside adb shell (root):

 # . /sdcard/copy-r files/ /sdcard/files3 

or

 # source /sdcard/copy-r files/ /sdcard/files3 

(Hash # above is a su prompt, and . Is a command that forces the shell to run the specified file, almost the same as source ).

After copying, I can adb pull files from the SD card.

Writing files to the application directory was more difficult, I tried to set r / w permissions on files and its subdirectories, this 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 onCreate () application.

PS in case someone needs a code to remove protection from application directories to enable access to the adb shell on a non-root 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.

+3
Feb 11 '15 at 8:45
source share

If you have root access, install busybox ( Google for instructions ).

+2
Jan 18 2018-11-11T00:
source share

I could suggest just installing Terminal-ide on your device, available in the gaming market . It is free, does not require root, and provides a convenient * nix environment such as cp, find, du, mc and many other utilities that install in binary form with the click of a button.

+1
Dec 27 '15 at 19:15
source share

Android supports the dd command.

 dd if=/path/file of=/path/file 
+1
Mar 09 '19 at 22:39
source share

I tried to follow on the Mac.

  1. Launch Terminal and go to the folder where adb is located. On Mac, usually in /Library/Android/sdk/platform-tools .
  2. Connect the device now with ./adb status developer mode and check the status of the device using the ./adb status command. "./" must begin with the prefix "adb".
  3. Now we may need to find out the location of the destination folder on our device. You can check this with adb shell . Use the ./adb shell command to enter the adb shell. Now we have access to the device folder using the shell.
  4. You can list all folders using the ls -la .
  5. Usually we find the /sdcard folder on our device. (You can select any folder here.) Suppose my destination is /sdcard/3233-3453/DCIM/Videos and the source is ~/Documents/Videos/film.mp4
  6. Now we can exit the adb shell to access the file system on our machine. Team: ./adb exit
  7. Now ./adb push [source location] [destination location]
    those. ./adb push ~/Documents/Videos/film.mp4/sdcard/3233-3453/DCIM/Videos
  8. Voila.
0
Oct 28 '18 at 11:25
source share

If you just want to add a file, for example, add several lines to the configuration file, the shell built-in commands are enough:

 adb shell cat >> /path/to/file <<EOF some text to append a second line of text to append EOF exit 

Above, replace /path/to/file file you want to edit. You need to have write permission to the file, which means root access if you are editing a system file. Secondly, replace some text to append and a second line of text to append lines you want to add.

0
Jul 19 '19 at 0:17
source share



All Articles