Let’s talk about reading and modifying files.
This post is part of the "what is sudo?" series, which aims to help understand Linux and text consoles.
The cat
command
Using the cat
command, you can view the text contents of a file:
cat my_file.txt
You can also use cat
to append to a file, and to overwrite a file.
The code below appends “Hello, world” to the file my_file.txt
.
cat >> my_file.txt
Hello, world
[CTRL+C]
The code below overwrites my_file.txt
with the words “Hello, world”.
cat > my_file.txt
Hello, world
[CTRL+C]
The vi
command
Note, sometimes this command is
vim
(forvi
“improved”).
Vi is a text editor. You can open files, edit them, and save them.
It is powerful, but it takes some getting used to.
Open files like so:
vi my_file.txt
Here’s a quick list of commands:
i
gets you into ‘Insert’ mode. Think of this as your normal text editor mode, where you can write characters, use your backspace, etc…v
gets you into Visual mode, where you can select characters.V
gets you into Visual (line) mode, where you can select lines.ESC
gets you out of any mode.- While not in a mode,
:w
saves the file to disk. - While not in a mode,
:q
quits the current file.
You can chain the last two commands like so: :wq
, so save and quit.
If you want to learn how to use Vi well, I recommend this: http://vim-adventures.com