Skip to content

Tutorial 03

Lee Burton edited this page Jul 13, 2025 · 2 revisions

📝 Reading & Editing Text Files

Now that you can navigate around your computer, you may want to read the contents of a file or edit it yourself.


📖 Reading Files

🐱 cat – Concatenate and Print

Prints the contents of a file to the screen.

$ cat filename

🔝 head – Show the First Lines

Shows the first 10 lines of a file by default. You can show a custom number using -n:

$ head -n 15 filename

🔚 tail – Show the Last Lines

Shows the last 10 lines by default. You can also follow a file that is being updated in real-time using -f:

$ tail -n 15 filename
$ tail -f filename

🔍 grep – Search Within Files

Searches for a string or pattern inside a file.

$ grep searchstring filename

For multiple words, wrap the search string in quotes:

$ grep "I am looking for this search string" filename

🔢 wc – Word Count

Returns stats about the file: lines, words, and characters.

$ wc filename

To count only the number of lines:

$ wc -l filename

🔄 diff – Compare Files

Compares two files line by line.

$ diff file1 file2

Typical output:

  • < means the line is in file1 but not in file2
  • > means the line is in file2 but not in file1

Use the -u flag for a unified view (easier to read):

$ diff -u file1 file2

✍️ Editing Files

🧙 vim – The Powerful Text Editor

Vim is a powerful and efficient text editor available on nearly all Linux systems.

Open or create a file:

$ vim filename

Vim Modes:

  • Command mode (default): you can navigate and issue commands.
  • Insert mode: press i to start editing the text.
  • Press Esc to return to command mode.

Common Vim Commands:

  • i — enter insert mode
  • Esc — exit insert mode
  • u — undo last change
  • /007 — search for "007"
  • 20G — go to line 20
  • :w — save
  • :q! — quit without saving
  • :wq — save and quit
  • :sort — sort all lines alphabetically
  • :sort u — sort and remove duplicates

💡 Vim has a steep learning curve, but it's incredibly powerful once you get the hang of it!


⚙️ sed – Stream Editor

Used to automatically edit files from the command line without opening a text editor.

Replace all instances of oldstring with newstring and save to a new file:

$ sed 's/oldstring/newstring/' filename > newfilename

🎉 Congratulations!

You’ve now learned how to view, search, and edit files from the command line!
You're ready to move on and learn how to connect to remote computers, including the Tel Aviv University server, in Tutorial 4.

Clone this wiki locally