Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

Hoofdstuk 8Naam in de cursus: Editing Text Files

Create, read and edit text files

On Linux almost every setting is a text file. You must be able to read a file fast, find a line in it, and change it safely with an editor that works on every server: vim. This chapter also shows nano (easier), and how to write a short file directly from the shell.

  • 7 Lessen
  • 17 Commando's
  • 1 Labs

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

Vaardigheden in dit hoofdstuk

  • Tekstbestanden maken en bewerken met vim en nano

8.1

Read a file fast

Use cat for short files, less for long ones, and head/tail for the start or the end.

cat /etc/hostname
server1.example.com
cat -n /etc/fstab
less /var/log/messages
head -n 5 /etc/passwd
tail -n 20 /var/log/secure
tail -f /var/log/messages
ToolUse it for
catShort files; -n adds line numbers, -A shows hidden characters (tabs, line ends)
lessLong files: Space = page down, /word = search, G = end, q = quit
head -n NThe first N lines
tail -n NThe last N lines
tail -fFollow a growing log live (stop with Ctrl+C)

8.2

Know the modes of vim

vim has modes. In normal mode keys are commands. In insert mode keys write text. In command mode (after :) you save and quit.

vim notes.txt
vimtutor
ModeHow you get thereWhat keys do
NormalEscMove, delete, copy, paste
Inserti, a, oType text
Command:Save, quit, search and replace
Visualv, VMark text, then copy or delete it

Onthoud

Lost? Press Esc two times. Then you are in normal mode again.

Notitie

vimtutor is a 30-minute hands-on course inside the terminal. Do it once — it is the fastest way to learn vim.

Commando's in deze les

8.3

Save and quit in vim

Every vim user must know these four commands.

CommandWhat it does
:wSave
:wq or :x or ZZSave and quit
:qQuit (only works when nothing changed)
:q!Quit and throw away the changes
:w newnameSave as a new file

8.4

Edit text in vim

Most editing happens in normal mode with short key commands. A number before a command repeats it (3dd = delete 3 lines).

KeysWhat they do
i / aInsert before / after the cursor
o / ONew line below / above, then insert
xDelete one character
dd / 3ddDelete (cut) the line / 3 lines
yy / 5yyCopy the line / 5 lines
p / PPaste below / above
u / Ctrl+RUndo / redo
gg / G / :25Go to the first line / last line / line 25
w / bNext word / word back
0 / $Start / end of the line
cwChange the word

Commando's in deze les

8.6

Use nano for quick edits

nano is easier for beginners: you type at once, and the key commands are shown at the bottom (^ = Ctrl).

nano /tmp/test.txt
dnf install nano
KeysWhat they do
Ctrl+O, EnterSave
Ctrl+XQuit (asks to save)
Ctrl+WSearch
Ctrl+\Search and replace
Ctrl+K / Ctrl+UCut the line / paste it
Alt+UUndo
Ctrl+_Go to a line number

Op het werk

Set your favourite editor for tools like visudo, crontab -e and systemctl edit: put export EDITOR=nano (or vim) in ~/.bashrc.

8.7

Write a file from the shell

For short files you do not need an editor: use echo with > / >>, or a here document (<<EOF).

echo "server1" > hostlist.txt
echo "server2" >> hostlist.txt
cat > /etc/motd << 'EOF'
Authorized users only.
Problems? Call the IT desk: ext. 4040
EOF
tee /etc/sysctl.d/90-test.conf << 'EOF'
vm.swappiness = 10
EOF
  • > makes the file new (old content is gone), >> adds to the end.
  • With 'EOF' in quotes the shell does not change $ signs in the text.
  • tee FILE writes the input to the file and shows it — and with sudo tee it works where sudo echo … > does not.
echo "nameserver 192.168.122.1" | sudo tee -a /etc/resolv.conf

Let op

sudo echo "text" > /etc/file fails with "Permission denied": the > is done by your shell, not by sudo. Use sudo tee instead.

Oefenen

Alle commando's in dit hoofdstuk