43

From android shell, is there a way I can edit a file?

In order to edit files like .rc and similar scripts, I am currently using adb pull to pull them, then edit them and then push them back adb push. This is certainly inefficient.

Is there a way I can edit these files in android shell (like the vim editor in Linux shell)

I have root permissions on my device. So if necessary, I can install root applications.

Lii
  • 10,777
  • 7
  • 58
  • 79
Sandeep
  • 17,036
  • 13
  • 64
  • 103

7 Answers7

32

You can create text files using:

 adb shell
 $ cat > filename.txt

You can add lines to a text files using:

 $ cat >> filename.txt

Both commands can be terminated using ctrl-D.

Zitoun
  • 166
  • 11
Deepak Negi
  • 843
  • 9
  • 18
21

You can install BusyBox from the F-Droid store and if you have rooted your device you are able to use vi to edit files.

adb shell
busybox vi /sdcard/Download/test.txt
ploth
  • 356
  • 8
  • 16
Veener
  • 4,180
  • 1
  • 25
  • 34
6

A simplified version of vi for Android is contained in Busybox. Assuming you're running Windows, once you've installed Busybox (and you do need root permissions to do so), I recommend you follow the instructions at https://stackoverflow.com/a/29033010/5025060 to accomplish full screen text editing on your Android device from your Windows PC screen.

Community
  • 1
  • 1
CODE-REaD
  • 2,513
  • 3
  • 27
  • 56
  • @Shubashree, I'm sorry my answer was not clear: I'm recommending you install Busybox on your ***Android*** device (click the *Busybox* link above to do so). – CODE-REaD Aug 03 '18 at 18:57
6

I've also noticed that the default shell is sh and after entering bash I could use nano.

Lohmar ASHAR
  • 1,554
  • 13
  • 17
  • How do you enter `bash`? – julien_c Feb 13 '18 at 21:55
  • 3
    I that case I've meant just type "bash" and hit "enter", but lately I've noticed that my advice is not always valid. Different phones come with different setups, some have bash some don't. I just try everything until I find something useful: "bash", "busybox", "toybox", last resort I use `adb pull` and `adb push` to move the file between the phone and my PC. – Lohmar ASHAR Feb 14 '18 at 22:35
  • 2
    `bash` not available on my stock Marshmallow ROM. – Tom Russell Feb 21 '19 at 05:07
1

While not answering the specific question, I will say there's an app called Termux in the Play Store that works for unrooted devices. It's only 200KB and supports vi.

Tom Russell
  • 984
  • 1
  • 8
  • 26
1

Deepak's answer (using cat > or >>) is good for creating a file from scratch or appending to a file.

To change a piece of an existing file, sed is another option, on phones that don't have vi:

$ sed -i 's/old_regex/replacement/' filename.ext
LarsH
  • 26,606
  • 8
  • 88
  • 145
0

Best way I found to edit files inside my app was using the Device File Explorer of AndroidStudio. Because the Shell just got me "Access Denied" errors.

Once there, you open the file with double-click and save it in your local environment, then upload this modificated version to the Android device. To do it just right click on the destination folder and select the Upload option (image below).

I hope this help somebody :)

enter image description here

Carlos Jiménez
  • 140
  • 1
  • 9