Can I edit files inside the Android ADB shell?

90,026

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

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.

Solution 5

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
Share:
90,026
Sandeep
Author by

Sandeep

An open source enthusiast. Interested in Robotics/embedded systems and Android OS. For queries, you can contact me on [email protected]

Updated on July 09, 2022

Comments

  • Sandeep
    Sandeep 6 months

    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.

  • Giszmo
    Giszmo about 7 years
    provided there is no better answer, I feel sad for you getting down-voted :) Solved my issue with echo "..." > filename.
  • jiggunjer
    jiggunjer almost 7 years
    I can use vi on a Sony Xperia running JB.
  • julien_c
    julien_c almost 5 years
    How do you enter bash?
  • Lohmar ASHAR
    Lohmar ASHAR almost 5 years
    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.
  • CODE-REaD
    CODE-REaD over 4 years
    @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).
  • Joseph Garrone
    Joseph Garrone over 4 years
    echo 'right click past the content of the file' > filename.txt use ' insted of " for multi ligne.
  • Tom Russell
    Tom Russell almost 4 years
    bash not available on my stock Marshmallow ROM.
  • frakman1
    frakman1 almost 3 years
    The question is specifically about editing a file with vi within an ADB shell
  • Tom Russell
    Tom Russell almost 3 years
    I noticed that. Thanks. Not sure what I was thinking. I'll edit it.