الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

الفصل 8الاسم في الدورة: 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 الدروس
  • 17 الأوامر
  • 1 المعامل

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

المهارات في هذا الفصل

  • إنشاء الملفات النصية وتحريرها باستخدام vim و 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

تذكّر

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

ملاحظة

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

الأوامر في هذا الدرس

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

الأوامر في هذا الدرس

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

في العمل

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

انتبه

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

تدرّب

كل الأوامر في هذا الفصل