Create and edit text files

1. Understand and Use Essential Tools

πŸ“˜Red Hat Certified System Administrator (RHCSA – EX200)


In the RHCSA exam, you will often need to create, view, and modify text files on a Linux system. Text files are everywhere in Linux: configuration files, scripts, logs, and notes. Knowing how to manage them efficiently is essential.


1. Understanding Text Files in Linux

  • A text file is a file that contains readable characters (letters, numbers, symbols) instead of binary data.
  • Examples in IT environments:
    • /etc/passwd β†’ stores user accounts.
    • /etc/fstab β†’ stores filesystem mount information.
    • Log files in /var/log/ β†’ stores system logs.
    • Script files in /usr/local/bin β†’ custom automation scripts.

Key point: Most configuration and script files in Linux are plain text files.


2. Creating Text Files

There are multiple ways to create text files in Linux:

2.1 Using touch

  • Command:
touch filename.txt
  • What it does: Creates an empty text file or updates the timestamp if it already exists.
  • Example (IT environment):
touch /home/admin/new_config.txt

This creates an empty file where you can later add configuration data.

2.2 Using echo

  • Command:
echo "Some content" > filename.txt
  • > will overwrite the file.
  • Example:
echo "PermitRootLogin no" > /etc/ssh/sshd_config
  • >> will append to the file instead of overwriting:
echo "AllowUsers admin" >> /etc/ssh/sshd_config

2.3 Using cat

  • Command:
cat > filename.txt
  • After running, you can type the content directly. Press CTRL+D to save and exit.
  • Example:
cat > /tmp/server_list.txt
server1.example.com
server2.example.com

2.4 Using text editors

  • In Linux, you often need to edit configuration files, so text editors are important.

Common editors in RHCSA:

EditorHow to openKey Features
vi / vimvi filenamePowerful, default on most Linux systems.
nanonano filenameEasy for beginners, shows on-screen shortcuts.
geditgedit filenameGUI editor (if using desktop Linux).

3. Editing Text Files

3.1 Using vi or vim

vi is commonly tested in RHCSA because it is available on all Linux systems.

Basic steps in vi:

  1. Open a file:
vi /etc/hosts
  1. Switch to insert mode to type:
  • Press i β†’ you can now type/edit text.
  1. Exit insert mode:
  • Press ESC.
  1. Save changes and exit:
  • :w β†’ write/save.
  • :q β†’ quit.
  • :wq or ZZ β†’ save and quit together.
  • :q! β†’ quit without saving.

Example: Adding a new server IP in /etc/hosts:

192.168.1.50   webserver.example.com

3.2 Using nano

  • Command:
nano /etc/hostname
  • After opening:
    • Type your text.
    • Save: CTRL+O β†’ press Enter.
    • Exit: CTRL+X.

nano is easier for beginners than vi.


4. Viewing Text Files

Sometimes you just need to check file content:

CommandDescription
cat filenameShow entire content
less filenameScroll up/down through content
more filenameSimilar to less but less advanced
head filenameShow first 10 lines by default
tail filenameShow last 10 lines (useful for logs)
tail -f filenameContinuously monitor logs (real-time updates)

Example: Monitoring /var/log/messages for new log entries:

tail -f /var/log/messages

5. Practical IT Scenarios

  • Editing a config file:
vi /etc/ssh/sshd_config

Disable root login:

PermitRootLogin no
  • Creating a script:
nano /usr/local/bin/backup.sh

Add:

#!/bin/bash
tar -czf /backup/home_backup.tgz /home

Save and exit β†’ make it executable:

chmod +x /usr/local/bin/backup.sh
  • Checking log files:
tail -f /var/log/secure

Helps monitor authentication attempts in real-time.


6. Key Exam Tips

  1. Know at least vi commands – it’s mandatory for RHCSA.
  2. Practice creating files with touch, echo, and cat.
  3. Be comfortable viewing and appending text using >>.
  4. Know how to check file content using cat, less, head, tail.
  5. Always check permissions if editing system files:
ls -l /etc/hosts
sudo vi /etc/hosts

7. Summary Table

TaskCommand/MethodNotes
Create empty filetouch file.txtFast, simple
Create file with contentecho "text" > file.txtOverwrites existing
Append textecho "text" >> file.txtAdds to end
Create/edit interactivelycat > file.txtType content manually
Edit with vivi file.txtDefault editor, powerful
Edit with nanonano file.txtBeginner-friendly
View filecat file.txtShows all content
View large fileless file.txtScrollable view
Monitor logstail -f file.logReal-time monitoring

βœ… Conclusion:
In the RHCSA exam, you must be able to create, edit, view, and append text files efficiently, mostly using vi, nano, or basic shell commands. These skills are tested not just for text files, but because most Linux administration tasks (configuring services, creating scripts, monitoring logs) depend on them.


Buy Me a Coffee