70

Is there a way to edit a file if there are no vi, vim, joe, mcedit? In another words is there a way to edit a file using just the shell commands?

11
  • 1
    I don't have nano, either! Commented Aug 20, 2012 at 19:26
  • 4
    Make a copy of the file you want to edit. Then do cat file, followed by cat >file and arrange the parts with cut+paste and/or typing, finish with ctrl-d.
    – ott--
    Commented Aug 20, 2012 at 20:08
  • 2
    You haven't said if ed is present or not... Commented Aug 21, 2012 at 2:02
  • 9
    This also happened to me, in docker.. It is very minimalistic. Now how the heck do i do something like that. The VM is ephemeral, i cannot ssh nor ftp, i have to use redirection and hacks like sed to change some files. It was a great challenge. See how reliant we are on editors :P
    – code ninja
    Commented Jun 26, 2014 at 21:33
  • 3
    If this is a container or accessed via some other host you can use echo <<EOF > new.file [paste here] EOF. Edit contents of file on host PC.
    – pztrick
    Commented Aug 22, 2017 at 23:16

10 Answers 10

28

In that instance, I'd try transferring files out and editing them on another computer, then transfer them back. If you have ssh, you should have scp (I hope), so you should be able to push files in and out. If not, you can also look for ftp to transfer files in and out.

If not, then I think your best option is to try and make use of cat, grep, sed, echo, and I/O redirection (especially append with >>). And lots of temporary files.

Though if you have access to perl (or something similar), you can run it with no arguments and it will let you input a script source from standard input. Once you press ctrl+d, it can then run the script. You could use that method to create a file. It would be more powerful than hacking something on the command line as I mentioned before.

9
  • no sshd but I have telnetd Commented Aug 20, 2012 at 19:37
  • Then can you ftp? Commented Aug 20, 2012 at 19:38
  • 1
    I can use cat, grep, echo, and I/O redirection (especially append with >>) but no sed Commented Aug 20, 2012 at 19:42
  • 2
    Ftp works :) now I need to see where on this router do I have something that is writable. Commented Aug 20, 2012 at 19:50
  • 4
    If you are connected to your device via telnet, I'd seriously think about just catting the file to the terminal (with a sufficiently large backscroll buffer), copy/pasting the entire thing into a local editor, making your changes, and then whipping up something that will turn that into a series of echo commands that will write a replacement file.
    – afrazier
    Commented Aug 20, 2012 at 19:50
30

Use a terminal that lets you copy and paste with the mouse, and assemble your files that way?

e.g.  cat > myfile
(use terminal to copy/paste)
^D

If it is a binary file, use this to turn it into text which you can copy with the mouse:

 openssl base64 -in <infile>

then on another computer, openssl again to decode it using the -d switch, edit however you like (e.g. hexedit) then re-encode, and on the box with no editor, openssl again to decode it and paste from your terminal. do

3
  • sadly no cursor while booting process is broken, like in grub or initramfs. Commented May 10, 2021 at 18:00
  • 1
    This is an excellent answer ... in many cases like when inside a container where you cannot even install vi ( obviously only during development ) Commented Aug 13, 2022 at 19:57
  • You can get KVM devices and USB keyboard emulators and keyboards that can play macros etc etc if you're working on "bare metal", and keep in mind that both HP and Dell (and some other brand) servers do include remote consoles, all of which support sending keys to a console too.
    – cnd
    Commented Aug 15, 2022 at 0:23
10

One way would to be to output the result of an echo.

echo "foo" > bar.txt

This will make a file titled bar.yxy with the containing text, "foo".

2
  • 4
    That's not really editing.
    – DavidPostill
    Commented Jul 23, 2016 at 22:50
  • 3
    Gets the job done, untill you can get an editor on it.
    – Landon
    Commented Jul 25, 2016 at 7:40
7
cat file.txt

copy output and edit content in your regular editor

cat <<- "EOF" > file.txt //
// paste edited content
EOF
4

Assuming you're SSH'ing into your router, you can also use various utilities to transfer the file back and fourth to your computer/router. You can download a copy on your PC, modify it, and then SSH it back to the router via SFTP (see Putty or WinSCP if you're a Windows user).

If you're using a custom firmware on the router/gateway, however, you may be in luck. There are various Optware packages containing simple (and small) text editors. Depending on your needs, you could get the nano package, or just go for busybox which contains vi.

4

I imagine you could do what you need with grep and perl - look for the line you want with grep, edit that line with perl (perl can act like a big replacement for sed) and then confirm you didn't make more changes than you intended by doing diff filename filename.new. If so, make the changes permanent - mv filename.new filename

2

Install vi - as weird as it sounds, you can often copy an existing "vi" from another machine and it will run OK on your box. I've found a "/data" partition on a lot of devices where you can save it to, and run it from.

I've done this. It works.

If you want to improve your chances of getting it working first-try, find any existing executable on your box and run this over it:

readelf -h </bin/filename>

then check that the "vi" you get form elsewhere uses the same chipset, bitsize, etc.

Works for other programs too (tcpdump is another one I've succeeded with)

2

I was working on Router with BusyBox installed and it didn't provided any text editor. One of the ways I was able to achieve it was,

  1. Output the file content using cat
  2. Edit the files on your system/pc
  3. Upload the updated file on internet such as Transfer.sh
  4. Download the updated file on the router/device using curl command, in my case I had to use the --ignore switch as well to bypass certificate/https warning.
  5. Remove [RM] the old file.
  6. Rename/Move [MV] the new file with the old files' name.
1

Did you wipe out /bin or something? Otherwise maybe you could hack something together with the text utilities in the GNU Coreutils that should be standard on a linux system.

0

If you have perl installed, you could give my ed-clone perled a try: https://github.com/nitardus/perled. It is an early beta, more of a challange to implement ed in under 150 lines of pure perl, but perhaps it could be helpful, too...

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Mar 4 at 17:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .