Skip to content

Module 0.5: Editing Files

Hands-On Lab Available
Ubuntu beginner 20 min
Launch Lab ↗

Opens in Killercoda in a new tab

Complexity: [QUICK] - Type what you see, save, done

Time to Complete: 25 minutes

Prerequisites: Module 0.3 - First Terminal Commands


After this module, you will be able to:

  • Create and edit files using nano from the terminal without a graphical editor
  • Write a simple bash script that combines multiple commands into one file
  • Make a script executable with chmod +x and explain why this step is necessary
  • Choose between nano and vim and explain when you’d use each

In the last module, you created files with touch — but they were empty. Empty files are like blank order tickets in a restaurant kitchen. Useful for reserving a spot, but not much else until someone writes on them.

In the real world, you’ll need to edit files constantly:

  • Configuration files tell programs how to behave (like the house rules in a kitchen)
  • Scripts are files that contain a sequence of commands (like a recipe card)
  • Kubernetes manifests are files that tell Kubernetes what to run (you’ll get there!)

Consider a real-world incident at a major tech company: a junior engineer needed to update a simple setting in a load balancer. They opened the nginx.conf file in nano, accidentally typed a few stray characters while trying to save, and saved the file. Within minutes, the entire site went down, costing thousands of dollars in revenue. Why? Because they were editing directly on the production server without realizing it, and a single typo in a configuration file can break a service.

You need to be able to open a file, write in it, save it, and close it — all from the terminal. No mouse. No graphical text editor. Just you and the keyboard. Learning to edit files safely in the terminal isn’t just about writing text; it’s about navigating the control room of your servers with precision.


“Can’t I just use Notepad or TextEdit?”

You can — on your own computer. But remember from Module 0.1: almost every server runs Linux, and servers usually don’t have a graphical interface. When you connect to a remote server (which you’ll learn in Module 0.7), there’s no mouse, no desktop, no Notepad. There’s just the terminal.

The terminal text editor is the chef’s knife of computing. It’s not the fanciest tool, but you’ll use it everywhere.


There are many text editors that run in the terminal. The two most famous are vim and nano.

We’re starting with nano because:

nanovim
Works the way you’d expectHas a steep learning curve
Type and it typesYou need to press i before you can type
Menu at the bottom shows you how to save and quitPeople famously get stuck and can’t figure out how to exit
Perfect for beginnersPowerful but overwhelming at first

There’s no shame in using nano. Many experienced engineers use it for quick edits. You’ll learn vim later when you’re ready for more power.

Fun fact: “How to exit vim” is one of the most searched programming questions on the internet. Over 2 million people have viewed that question on Stack Overflow. With nano, you’ll never have that problem.


Let’s create and edit a file. First, make sure you’re in your home directory:

Terminal window
cd ~

Now open nano with a new file:

Terminal window
nano hello.txt

Your screen will change completely. You’ll see something like this:

GNU nano 6.2 hello.txt
^G Help ^O Write Out ^W Where Is ^K Cut ^C Location
^X Exit ^R Read File ^\ Replace ^U Paste ^T Execute

Let’s break this down:

  • Top line: The editor name and your file name
  • Middle area: This is where you type (it’s blank because the file is new)
  • Bottom two lines: The menu showing available commands

This is the one thing that confuses everyone at first:

The ^ symbol means “hold the Ctrl key.”

So when you see ^O Write Out, that means: “Press Ctrl and O at the same time to save the file.”

What You SeeWhat You PressWhat It Does
^OCtrl + OSave the file
^XCtrl + XExit nano
^KCtrl + KCut the current line
^UCtrl + UPaste a cut line
^WCtrl + WSearch for text
^GCtrl + GShow help

That’s it. Six key combos and you can do everything you need.


This is the easy part: just type.

With nano open, type the following (press Enter at the end of each line):

Welcome to the Kitchen!
Today's special: Learning to edit files.
Chef says: You're doing great.

Your screen should now show:

GNU nano 6.2 hello.txt Modified
Welcome to the Kitchen!
Today's special: Learning to edit files.
Chef says: You're doing great.
^G Help ^O Write Out ^W Where Is ^K Cut ^C Location
^X Exit ^R Read File ^\ Replace ^U Paste ^T Execute

Notice the word Modified in the top line. That means you’ve made changes that haven’t been saved yet.


Let’s save. Press:

Ctrl + O

nano will ask you to confirm the file name:

File Name to Write: hello.txt

Press Enter to confirm. The “Modified” indicator disappears. Your file is saved.


To leave nano and return to the terminal:

Ctrl + X

If you’ve made changes since your last save, nano will ask:

Save modified buffer? Y Yes N No ^C Cancel

Press Y to save and exit, N to exit without saving, or Ctrl + C to cancel and stay in nano.


Now that you’re back in the terminal, let’s confirm the file was saved. The cat command prints a file’s contents to the screen:

Terminal window
cat hello.txt

Expected output:

Welcome to the Kitchen!
Today's special: Learning to edit files.
Chef says: You're doing great.

cat is short for “concatenate” (it can combine multiple files), but most people use it to quickly peek at a file’s contents. Think of it as reading a note without picking it up — you just glance at it.


To edit a file that already exists, just open it the same way:

Terminal window
nano hello.txt

Your text is there. Use the arrow keys to move around. Type to add text. Use Backspace/Delete to remove text.

Add a new line at the bottom:

PS: The pantry is fully stocked.

Save with Ctrl + O, Enter. Exit with Ctrl + X.


nano has simple cut and paste for entire lines:

  1. Move your cursor to the line you want to cut
  2. Press Ctrl + K to cut it (the line disappears)
  3. Move your cursor to where you want to paste it
  4. Press Ctrl + U to paste it

You can cut multiple lines by pressing Ctrl + K multiple times — they stack up. Then Ctrl + U pastes them all.

This is like the chef rearranging the order of steps in a recipe.


Working with a long file and need to find something? Press Ctrl + W, type what you’re looking for, and press Enter.

Ctrl + W

nano will prompt:

Search:

Type special and press Enter. The cursor jumps to the first occurrence of “special” in the file.

Press Ctrl + W and then Enter again (without typing anything) to find the next occurrence.


Now let’s do something powerful: write a script. A script is just a text file that contains commands the computer can run. It’s a recipe card for the terminal.

Terminal window
nano my-first-script.sh

Stop and think: This script starts with a strange line: #!/bin/bash. What do you think it does? It’s called a “shebang” — it’s the script’s way of telling the computer “I’m written in bash, use the bash program to run me.” Without it, the computer wouldn’t know how to interpret the file. You’ll see this line at the top of every shell script you encounter.

Type the following exactly:

#!/bin/bash
echo "Welcome to the kitchen!"
echo "Today's date is: $(date)"
echo "You are logged in as: $(whoami)"
echo "Your current directory is: $(pwd)"
echo ""
echo "Great job, chef! Your first script works!"

Let’s understand each line:

  • #!/bin/bash — This is called a “shebang.” It tells the computer “use the bash program to run this file.” Every script needs this as the first line. (Yes, “shebang” is a real term.)
  • echo — A command that prints text to the screen. Think of it as the kitchen yelling “Order up!”
  • $(date) — Runs the date command and inserts the result. The $() syntax means “run this command and give me the output.”
  • $(whoami) — Runs whoami, which tells you your username.
  • $(pwd) — You know this one — it prints your current directory.

Press Ctrl + O, Enter, then Ctrl + X.

Pause and predict: Before running chmod, try to run the script directly: ./my-first-script.sh. What happens? You should get “Permission denied.” This is intentional — new files are just data by default. The computer needs explicit permission to treat a file as a program. This is a security feature, not a bug.

Right now, the file is just text. The computer won’t run it because it doesn’t have permission to be executed. Let’s fix that:

Terminal window
chmod +x my-first-script.sh

chmod stands for change mode. The +x means “add execute permission.” You’re telling the restaurant manager: “This isn’t just a document — it’s a recipe that should be cooked.”

Terminal window
./my-first-script.sh

The ./ means “run this file from the current directory.” Expected output:

Welcome to the kitchen!
Today's date is: Sun Mar 23 14:30:00 UTC 2026
You are logged in as: yourname
Your current directory is: /Users/yourname
Great job, chef! Your first script works!

You just wrote and executed your first program. That’s not nothing — that’s everything.


You’ll encounter vim eventually. It’s incredibly powerful, used by many senior engineers, and is installed on virtually every Linux system. But vim has a famously steep learning curve because it works in “modes” — you can’t just open it and start typing.

We’re not ignoring vim. We’re saving it for when you have more context. Right now, nano gets the job done, and learning to walk before you run is how chefs train too.

When you’re ready, vim will be there. For now, nano is your friend.


  • nano is a clone of a clone. It was created as a free replacement for an editor called pstrict which was a replacement for pstrict … actually, nano is a free replacement for pico, which was the editor built into the Pine email client from the 1990s. nano stands for “nano’s ANOther editor” — a recursive acronym, which is a nerdy tradition in programming.

  • The oldest surviving text editor still in use is ed, created in 1969. It was written by Ken Thompson, one of the creators of Unix. ed is a “line editor” — you can’t see the whole file at once. You edit one line at a time. Engineers in the 1970s wrote entire operating systems using ed. Your experience with nano is luxurious by comparison.

  • Configuration files run the world. Nearly every piece of software reads a text configuration file when it starts up. Your web server, your database, Kubernetes itself — they all read text files to know how to behave. The ability to edit these files from the terminal is one of the most practical skills in all of computing.


MistakeWhy It’s a ProblemWhat to Do Instead
Pressing Ctrl+Z instead of Ctrl+X to exitCtrl+Z suspends nano (hides it) instead of closing it. The file stays open in the backgroundIf you accidentally suspend, type fg to bring nano back. Then use Ctrl+X to exit properly
Forgetting to save before exitingYour changes are lostAlways Ctrl+O to save before Ctrl+X to exit. Or just press Ctrl+X and say Y when asked to save
Not adding #!/bin/bash to scriptsThe system doesn’t know how to run the fileAlways make the first line of a bash script #!/bin/bash
Forgetting chmod +x before running a scriptYou get “Permission denied”Run chmod +x filename.sh to make the file executable
Using nano when you meant to use catYou accidentally open the file for editing when you just wanted to read itUse cat filename to view a file without editing it
Editing a file on a remote server thinking it is localYou might accidentally change production configuration instead of your local testing files, causing unexpected downtimeAlways check your prompt (like user@server) or run hostname to confirm which machine you are currently editing files on
Opening a binary file (like an image or compiled program) in nano and saving itNano will try to read the binary data as text, and saving it will corrupt the file permanentlyOnly use nano for plain text files (scripts, configs, logs). Use file filename to check the file type before opening

  1. You are editing a crucial configuration file and the nano menu at the bottom tells you to press ^O to Write Out. You try typing the caret symbol (^) and then the letter O, but it just types “^O” into your file. What went wrong?

    Answer You interpreted the `^` symbol literally instead of as a modifier key. In terminal applications like nano, the `^` symbol represents the Ctrl (Control) key on your keyboard. Therefore, `^O` means you should hold down the Ctrl key and press the letter O at the same time. This is a standard convention in Unix-like systems for showing keyboard shortcuts. By typing the characters individually, you were just adding raw text to your document instead of executing the save command.
  2. You’ve spent 15 minutes carefully writing a bash script in nano. You press Ctrl+X to exit, but you accidentally press ‘N’ when prompted to “Save modified buffer?”. What happens to your script, and what should you have done differently to ensure your work was safe?

    Answer Your script changes are permanently lost because pressing 'N' tells nano to discard all modifications made since the last save. Unlike modern graphical editors with autosave or history features, terminal editors do exactly what you tell them in the moment. To prevent this, you should form the habit of manually saving your file before attempting to exit. You do this by pressing Ctrl+O (Write Out) and hitting Enter to confirm the file name, which writes your changes to the disk immediately. Once saved, the "Modified" indicator at the top disappears, and you can safely exit with Ctrl+X.
  3. You download a script from a coworker that ends in .sh. You make it executable and try to run it, but your system throws an error saying it doesn’t know how to execute the file. Upon opening it in nano, you see the first line is simply echo "Starting backup". What crucial element is missing, and why does the system need it?

    Answer The script is missing its "shebang" line (e.g., `#!/bin/bash`) at the very top of the file. Without this line, the operating system's program loader doesn't know which interpreter program should be used to read and execute the subsequent instructions. The shebang acts as a strict directive, telling the system whether to pass the file's contents to bash, python, node, or another interpreter. Because it was missing, the system attempted to guess or use a default execution method, which failed because the context wasn't explicitly defined.
  4. You’ve written a perfect script to automate your server backups, saved it as backup.sh, and typed ./backup.sh to run it. Instead of your backup starting, the terminal sternly replies: Permission denied. Why did the system block your script, and how do you resolve this?

    Answer The system blocked your script because, by default, newly created text files only have read and write permissions, not execute permissions. This is a fundamental security feature in Linux designed to prevent arbitrary text files or downloaded data from being accidentally or maliciously run as programs. To resolve this, you must explicitly grant the file the right to be executed by running `chmod +x backup.sh`. This changes the file's mode, signaling to the operating system that you, the owner, vouch for this file and authorize it to run as a program.
  5. You wrote a script called backup.sh and tried to run it with ./backup.sh. You got “Permission denied.” Then you ran chmod +x backup.sh and tried again — now it says “line 1: syntax error.” What are the TWO things that went wrong, and in what order should you fix them?

    Answer The first issue was that your file lacked execute permissions, which is standard for newly created files as a security measure; this was correctly resolved using `chmod +x`. The second issue is that the script itself contains invalid instructions, most likely a malformed shebang on the first line (such as `#bin/bash` instead of `#!/bin/bash`). You must always address permission issues first because the system won't even attempt to read the syntax of a file it isn't allowed to execute. Once permissions are granted, the interpreter can read the file, encounter the bad syntax, and provide a helpful error message pointing directly to line 1. To fix the remaining issue, you should open the file in nano and correct the typo on the first line.

Create a “memo board” for the restaurant kitchen, practice editing, and write a script.

Terminal window
cd ~
nano kitchen-memo.txt

Type the following five lines:

=== KITCHEN MEMO BOARD ===
1. Morning prep starts at 6 AM
2. New menu items arriving Thursday
3. Remember: clean as you go
4. Staff meeting at 3 PM Friday

Save with Ctrl + O, Enter. Exit with Ctrl + X.

Verify your work:

Terminal window
cat kitchen-memo.txt

You should see all five lines printed exactly as you typed them.

Open it again:

Terminal window
nano kitchen-memo.txt

Add a sixth line at the bottom:

5. Chef says: great work today, team!

Save and exit (Ctrl + O, Enter, Ctrl + X).

Verify:

Terminal window
cat kitchen-memo.txt

All six lines should be there.

Terminal window
nano kitchen-report.sh

Type:

#!/bin/bash
echo "=== Kitchen Status Report ==="
echo "Date: $(date)"
echo "Chef on duty: $(whoami)"
echo ""
echo "--- Memo Board Contents ---"
cat kitchen-memo.txt
echo ""
echo "--- Files in current directory ---"
ls
echo ""
echo "Report complete. Kitchen is running smoothly!"

Save and exit.

Make it executable and run it:

Terminal window
chmod +x kitchen-report.sh
./kitchen-report.sh

Expected output (details will vary):

=== Kitchen Status Report ===
Date: Sun Mar 23 14:45:00 UTC 2026
Chef on duty: yourname
--- Memo Board Contents ---
=== KITCHEN MEMO BOARD ===
1. Morning prep starts at 6 AM
2. New menu items arriving Thursday
3. Remember: clean as you go
4. Staff meeting at 3 PM Friday
5. Chef says: great work today, team!
--- Files in current directory ---
hello.txt kitchen-memo.txt kitchen-report.sh my-first-script.sh
...
Report complete. Kitchen is running smoothly!
Terminal window
rm hello.txt kitchen-memo.txt kitchen-report.sh my-first-script.sh

Success criteria: You created a file with nano, edited it, wrote a bash script that reads from another file, made it executable, and ran it. You’re not just using the terminal anymore — you’re programming in it.


Next Module: Module 0.6: Git Basics — Learn to track your work with version control, the foundation of all modern software delivery.


You just used a tool that senior engineers use every day. You belong here.