How can I edit a file if there is no vi, vim, joe, etc?
Solution 1
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.
Solution 2
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".
Solution 3
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
Solution 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
Solution 5
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
.
Related videos on Youtube
Eduard Florinescu
Coding my way out of boredom. “If the fool would persist in his folly he would become wise.” (William Blake)
Updated on September 18, 2022Comments
-
Eduard Florinescu over 1 year
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?
-
Breakthrough over 11 yearsYou don't even have the
nano
binary on your system? :S -
Eduard Florinescu over 11 yearsI don't have nano, either!
-
Eduard Florinescu over 11 yearsIts a router, but also I've seen also a custom gateway in this situation :)
-
Breakthrough over 11 yearsAre you on DD-WRT?
-
Eduard Florinescu over 11 yearsNo, the default firmware
-
ott-- over 11 yearsMake a copy of the file you want to edit. Then do
cat file
, followed bycat >file
and arrange the parts with cut+paste and/or typing, finish withctrl-d
. -
dmckee --- ex-moderator kitten over 11 yearsYou haven't said if
ed
is present or not... -
code ninja almost 10 yearsThis 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
-
pztrick over 6 yearsIf 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. -
Sridhar Sarnobat over 5 yearsthis is the case with my Docker image :(
-
Prabowo Murti over 2 years@codeninja yes you're right. Happened to us too. Great we still have
cat
-
-
Eduard Florinescu over 11 yearsno sshd but I have telnetd
-
Eduard Florinescu over 11 yearsI can use cat, grep, echo, and I/O redirection (especially append with >>) but no sed
-
user3359503 over 11 yearsWell, if you know exactly what you want to write into your file, you should be able to
echo
each line and append it to the end of the file you want. Withoutsed
, editing would be a bit difficult, unless you have/knowawk
. Personally, I don't, but it's another alternative tosed
. -
Eduard Florinescu over 11 yearsFtp works :) now I need to see where on this router do I have something that is writable.
-
afrazier over 11 yearsIf 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 over 11 yearsAll that will only work for pure text files though. Binary... not so much. (Oh, you have ftp? That makes it easy!)
-
Eduard Florinescu over 11 years@afrazier that is the solution, but found found ftp working
-
DavidPostill almost 8 yearsThat's not really editing.
-
Landon almost 8 yearsGets the job done, untill you can get an editor on it.
-
Attie almost 7 yearsI'd also look out for
netcat
/nc
- there's often a very basic version included inbusybox
-
Adi Prasetyo about 3 yearssadly no cursor while booting process is broken, like in grub or initramfs.