50

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
  • 11,553
  • 8
  • 64
  • 88
Sandeep
  • 18,356
  • 16
  • 68
  • 108

8 Answers8

37

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
  • 446
  • 3
  • 13
Deepak Negi
  • 893
  • 10
  • 19
  • 9
    provided there is no better answer, I feel sad for you getting down-voted :) Solved my issue with `echo "..." > filename`. – Giszmo Dec 17 '15 at 06:08
  • I can use vi on a Sony Xperia running JB. – jiggunjer Feb 18 '16 at 04:28
  • ``echo 'right click past the content of the file' > filename.txt`` use ' insted of " for multi ligne. – Joseph Garrone Sep 15 '18 at 09:38
  • Technically you are not answering the question, as your solution does not edit files but creates/appends content to them. A correct answer would be the one given by @Veener. – jonathask Sep 05 '22 at 19:14
29

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
  • 435
  • 8
  • 16
Veener
  • 4,771
  • 2
  • 29
  • 37
7

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
  • 27,481
  • 8
  • 94
  • 152
  • 1
    Clever! For me this is the easiest and quickest way to simply change a value in the shared preferences file. – dkoukoul Nov 10 '22 at 10:29
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,819
  • 3
  • 33
  • 60
6

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

Lohmar ASHAR
  • 1,639
  • 14
  • 18
  • How do you enter `bash`? – julien_c Feb 13 '18 at 21:55
  • 5
    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

Actually you can IF your device uses toybox for it's utilities and it has sed installed and you know how to use sed. Actually you don't need toybox but you do need an editor and I found that my system uses toybox which supplies sed.

My Samsung doesn't support even ADB but my LG has what I needed.

Run adb shell, use run-as to get the right permissions and edit away.

$ which toybox
/system/bin/toybox
$ which sed
/system/bin/sed
steven smith
  • 1,519
  • 15
  • 31
  • 1
    I use `toybox vi` to edit files. – DataDino Apr 21 '23 at 04:26
  • Lucky you :) Unfortunately I get "toybox: Unknown command vi" for this, :( "toybox" gives a complete list of the commands the installed toybox contains and on my phone, vi is not among them. Neither is my old friend "ed". Sigh... – – steven smith Apr 22 '23 at 16:34
0

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
  • 1,015
  • 2
  • 10
  • 29
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
  • 148
  • 1
  • 9