Find a course

Module 2: Working with FilesLesson 6 of 1310 min

Editing Text Files and Archiving

Edit configuration files in nano and survive vim, make safe backups before changes, and bundle and compress files with tar and gzip.

Step 1 of 12Learn

Everything is a text file

On a Linux server, nearly every setting lives in a plain text file: the web server's configuration, the list of users, scheduled jobs, the SSH settings. Changing how the system behaves usually means opening a text file in /etc, editing a line and restarting a service. So you need a terminal text editor.

nano is the friendliest. Run nano notes.txt and start typing. The bottom two lines show shortcuts, where ^ means Ctrl:

  • Ctrl+O writes (saves) the file; press Enter to confirm the name.
  • Ctrl+X exits; if there are unsaved changes, nano asks whether to save.
  • Ctrl+W searches ("where is"), Ctrl+K cuts a line, Ctrl+U pastes it.

That's enough to edit any config file. Before you edit an important one, make a copy: cp sshd_config sshd_config.bak. If your change breaks something, mv the backup back into place and you are exactly where you started. Many teams also add a dated suffix, such as sshd_config.2026-03-14, so they can tell backups apart.

System files in /etc belong to root, so you will open them with sudo nano /etc/hosts. You will learn exactly what sudo does in the permissions module.

Step 2 of 12Learn

Surviving vim

Sooner or later a command will drop you into vim (or its ancestor vi), because it is installed on almost every Unix-like system and is the default editor for tools like git and crontab. Vim is powerful, but it surprises newcomers because it has modes.

  • Normal mode is where vim starts. Keys are commands, not text: x deletes a character, dd deletes a line, u undoes. Typing a sentence here causes chaos.
  • Insert mode is for typing text. Press i to enter it; "-- INSERT --" appears at the bottom. Press Esc to go back to normal mode.
  • Command-line mode starts when you press : in normal mode. This is where you save and quit.

The survival kit:

Keys (from normal mode)Effect
istart inserting text at the cursor
Escreturn to normal mode
:w then Enterwrite (save)
:q then Enterquit (fails if there are unsaved changes)
:wq then Entersave and quit
:q! then Enterquit and throw away changes
/wordsearch for "word"

If you are ever stuck, press Esc twice and type :q!. You lose unsaved edits but you are out. You can set a friendlier default editor for tools like git with export EDITOR=nano.

Step 3 of 12Practice · Diagram

The three modes of vim

Most vim confusion comes from typing in the wrong mode. Tap each mode.

In which mode does pressing dd delete a whole line?

Tap the shape that answers the question, then press Check.

The three modes of vimParts diagram: vim, Normal mode, Insert mode, Command-line mode.vimNormal modeInsert modeCommand-linemode
vim
Normal mode

Where vim starts. Keys are commands: x, dd, u. Esc always brings you back here.

Insert mode

Press i from normal mode. Keys type text. "-- INSERT --" shows at the bottom.

Command-line mode

Press : from normal mode. Type w, q, wq or q! and press Enter.

Step 4 of 12Practice · Put in order

You opened config.yml in vim and need to change one value and save. Put the steps in order.

Drag the items, or use the arrow buttons, to put them in order.

  1. Move the cursor to the value you want to change
  2. Type the new value
  3. Press Esc to return to normal mode
  4. Type :wq and press Enter
  5. Press i to enter insert mode

Step 5 of 12Flashcards

Editor survival card

Tap a card to flip it.

Step 6 of 12Practice · Choose one

In vim you typed some text, pressed Esc, and now want to leave without saving your changes. What do you type?

Step 7 of 12Learn

Bundling and compressing with tar

To back up a directory, send a project to a colleague or download software, you will meet archives. On Linux the standard tool is tar (originally "tape archive"). It bundles many files and directories into one file and, with an extra option, compresses it.

The options read like a recipe:

  • c create an archive, x extract one, t list its contents
  • z compress or decompress with gzip
  • v verbose: print each file name as it goes
  • f the next argument is the archive file name

So:

$ tar -czvf site-backup.tar.gz /var/www/site    # create
$ tar -tzf site-backup.tar.gz                   # look inside
$ tar -xzvf site-backup.tar.gz -C /tmp/restore  # extract into /tmp/restore

The .tar.gz (or .tgz) extension means "a tar bundle compressed with gzip". gzip file on its own compresses a single file into file.gz and removes the original; gunzip file.gz reverses it. Text such as logs, code and CSV often shrinks to a quarter of its size or less, while photos and videos, which are already compressed, barely shrink at all.

Two habits: list an archive with -t before extracting it, so you know whether it will spill hundreds of files into the current directory, and use -C to extract into a specific folder.

Step 8 of 12Practice · Chart

How much gzip shrinks different files

Size after compression as a percent of the original. Text has lots of repetition; photos and video are already compressed.

Which file type shrinks the most?

Tap the bar that answers the question, then press Check.

How much gzip shrinks different filesBar chart with 5 categories: Server log (text): 15%; CSV data: 25%; Source code: 30%; JPEG photos: 98%; MP4 video: 99%.0%20%40%60%80%100%Server log (text)CSV dataSource codeJPEG photosMP4 video15%25%30%98%99%
Illustrative typical results; real ratios depend on the content.
How much gzip shrinks different files
ItemSize after gzip
Server log (text)15%
CSV data25%
Source code30%
JPEG photos98%
MP4 video99%

Step 9 of 12Note

Step 10 of 12Practice · Fill in the blanks

Fill in the blanks

To create a gzip-compressed archive called logs.tar.gz from the directory logs, run `tar - logs.tar.gz logs. To extract it, you would replace the letter c` with .

Step 11 of 12Practice · True or false

Compressing a folder of JPEG photos with gzip will usually make it about four times smaller.

Step 12 of 12Practice · What would you do?

What would you do?

You need to change the port in /etc/myapp/app.conf on a production server. The service must keep working if you make a mistake. What is the best approach?