Files and text: a summary of the essential commands, vim
This lesson is open to everyone. The other lessons open with a package: sign in or sign up, then choose a package.
Key points
- Navigation and files:
pwd,cd -,ls -lah,cp -a,mv,rm -i,mkdir -p,ln -s,find / -name ... 2>/dev/null. - Text:
grep -rn,head/tail -f,wc -l,sort | uniq -c,cut -d: -f1,sed -i. - Redirection:
>,>>,2>,&>,|,tee; to write to a root-owned file use| sudo tee -a. - On RHEL
viis actuallyvim(vim-minimal);nanomay be missing on a minimal installation. - vim:
itype,Esc,:wqsave,:q!discard;/wordsearch,dddelete line,uundo,:set nuline numbers.
The essential commands in one table
| Job | Command | Note |
|---|---|---|
| Where am I | pwd | |
| Go back to the previous directory | cd - | |
| Hidden files and sizes | ls -lah | |
| Create a directory tree | mkdir -p /srv/app/{logs,conf} | {} creates several names |
| Copy keeping permissions and dates | cp -a source target | -a = archive |
| Symbolic link | ln -s /srv/app/conf /etc/app | |
| Find a file | find /etc -name "*.conf" -mmin -60 | changed in the last 60 minutes |
| Find large files | find / -size +500M 2>/dev/null | discard errors |
| Find a word (in a directory) | grep -rn "Listen" /etc/httpd/ | -n line numbers |
| Follow a log live | tail -f /var/log/messages | on RHEL messages, not syslog |
| Count | grep -c Failed /var/log/secure | on RHEL secure, not auth.log |
| Cut columns | cut -d: -f1,7 /etc/passwd | |
| Most repeated | sort, then uniq -c, then sort -rn | joined with pipes |
| Replace in a file | sed -i 's/old/new/' file | check without -i first |
Log names differ on RHEL: Ubuntu's /var/log/syslog is /var/log/messages here, and /var/log/auth.log is /var/log/secure. RHCSA tasks often test this.
Redirection and root-owned files
[ali@srv-web01 ~]$ find /etc -name "*.repo" > ~/repolar.txt 2> ~/sehvler.txt [ali@srv-web01 ~]$ sudo grep -c "Failed password" /var/log/secure 3 [ali@srv-web01 ~]$ echo "10.20.0.20 srv-db01" | sudo tee -a /etc/hosts 10.20.0.20 srv-db01
sudo echo ... >> /etc/hosts does not work — the redirection is done by your shell, not by root. That is why | sudo tee -a is used.
The editor on RHEL: vim
A minimal RHEL installation may not have nano, but vi is always there (the vim-minimal package). For full vim (with syntax colouring): sudo dnf install -y vim-enhanced.
The minimum to remember:
| Mode / command | What it does |
|---|---|
i | Insert mode — start typing |
Esc | Back to Normal mode |
:w / :wq / :q! | Save / save and quit / quit without saving |
/word, n | Search, next |
dd, yy, p | Delete, copy, paste a line |
u, Ctrl+R | Undo, redo |
gg, G, :25 | Start of file, end, line 25 |
:set nu | Line numbers |
:%s/old/new/g | Replace in the whole file |
o | New line below and start typing |
Changing a configuration file safely
A working habit — every configuration change in these four steps:
[ali@srv-web01 ~]$ sudo cp -a /etc/ssh/sshd_config /etc/ssh/sshd_config.bak [ali@srv-web01 ~]$ sudo vim /etc/ssh/sshd_config [ali@srv-web01 ~]$ sudo sshd -t [ali@srv-web01 ~]$ sudo systemctl reload sshd
- A backup (
cp -akeeps permissions and the SELinux context). - The edit.
- The service's own syntax check (
sshd -t,httpd -t,nginx -t). reload— only after the check passes.
diff is useful to see what you changed: sudo diff /etc/ssh/sshd_config.bak /etc/ssh/sshd_config.
Lab: files and vim
In the Red Hat simulator (your own VM for the vim part), on srv-web01:
- Create the
/srv/app/{logs,conf}tree with one command and check withls -R /srv/app. - Find the files under
/etcending in.repo, send the list to~/repolar.txtand errors to/dev/null. - Count failed login attempts in
/var/log/secure. - Add
srv-db01to/etc/hostswithtee -aand check withping -c 1 srv-db01. - (On your own VM) open
vim ~/qeyd.txt, type three lines, look at the numbers with:set nu, delete the second line withdd, save with:wq.
[ali@srv-web01 ~]$ sudo mkdir -p /srv/app/{logs,conf} [ali@srv-web01 ~]$ find /etc -name "*.repo" > ~/repolar.txt 2>/dev/null [ali@srv-web01 ~]$ sudo grep -c "Failed password" /var/log/secure [ali@srv-web01 ~]$ ping -c 1 srv-db01
Check: ls /srv/app → conf logs; cat ~/repolar.txt is not empty; ping srv-db01 answers.
Check yourself
5 questions · answers are shown instantly · 3 correct answers complete the lesson
1On RHEL, which file stores failed SSH logins?
2Which editor is always present on a minimal RHEL installation?
3Why does sudo echo "x" >> /etc/hosts not work?
echo "x" | sudo tee -a /etc/hosts.4How does cp -a differ from plain cp?
5What is done after changing a configuration file and before reloading?
Comments
Sign in to comment.