Linux Foundations / Work With Files and Paths

Create, inspect and edit text files

Lesson 7 of 37. Open to read. No account needed.

A file can exist without containing the words you meant to save. An editor can also show your words before they have been written to disk. This lesson separates those states: create an empty file, edit its contents, save it, and read the saved result with another command.

You will make three synthetic text files inside one owned exercise directory. The filenames contain spaces so you can connect quoting with actual pathname arguments. You will inspect a longer file in less, then independently correct one word in a second note while preserving its other lines. Cleanup comes after all those tasks and removes only the three named files and empty exercise directory.

Start with a checked, fresh directory

Complete Navigate absolute and relative paths first. Use the ordinary Bash account in the course guest, with nano and less from the checked package setup. No download, script, redirected output, administrator permission or host filesystem is needed. All text is supplied inline below; type the text itself, without the Markdown fence markers.

Reconfirm the account and shell with the taught checks. type inspects command origins without running the named tools. Require the expected distribution utilities; stop before file operations for an unexplained alias, function or executable location. Record the ordinary username for ownership checks.

id -u
id -un
ps -p $ -o comm=
type touch nano cat less rm rmdir

The user ID must be nonzero and the current shell must be Bash. Preserve the understood listing setup from earlier lessons. Use the navigation lesson's ls -ld entry checks, without trailing slashes, to inspect home and the shared practice root. Require directory type d and your recorded ordinary owner; stop for a link (l), unexpected type or owner.

ls -ld ~
ls -ld ~/linux-practice

The shared practice root must be the one retained from the prerequisite. Then inspect the new exercise name:

ls -ld ~/linux-practice/create-and-edit-files

A missing-name diagnostic is the expected fresh-start result. If it exists, follow the resume section rather than overwrite or erase it. When the name is confirmed absent, create only this directory and inspect its entry:

mkdir ~/linux-practice/create-and-edit-files
ls -ld ~/linux-practice/create-and-edit-files

Continue only after error-free creation and the expected directory/owner check. Enter it, then inspect your actual location and contents:

cd ~/linux-practice/create-and-edit-files
pwd
ls -a

Require your recorded shared practice-root path followed by /create-and-edit-files, and only . and .. in the directory. A failed cd or different result means stop before creating any files. Record the verified exercise path for later editing and cleanup checks.

One pathname can contain several words

Bash normally separates arguments at unquoted spaces. Straight single quotes keep the enclosed text as one literal argument. Thus 'workshop notes.txt' is one pathname argument; the quote characters are not part of the filename. Without that grouping, workshop and notes.txt would be separate arguments.

Here are the only file inputs this lesson uses:

FilenamePurpose
workshop notes.txtObserve an empty file, then save a heading and two lines.
session checklist.txtSupply longer text for pager navigation and search.
review notes.txtSave an independent note, then correct one word.

The same quoted pathname must be used when creating, editing, reading and removing a file. It is not enough to quote it in only the first command. Do not deliberately create the unquoted split-name mistake; you can predict its argument boundaries without making extra files.

An empty file is a real starting state

touch creates a missing file as an empty file. For an existing file, its ordinary purpose is to update timestamps; it does not replace existing text with supplied lesson text or empty the file. Our confirmed fresh directory makes the missing-file case explicit.

Predict: will the following command write the words “workshop notes” into the file, or merely use them as part of its name?

touch 'workshop notes.txt'

Inspect the new entry. As in the navigation lesson, ls -ld describes the entry itself. -- ends its options before the pathname. For these ordinary files, require a leading - for a regular file, a link count of 1 in the second field, and your username in the owner field. A larger link count means another directory entry names the same file, so editing would affect that shared file through either name. The size field should be zero at this point. A link, unexpected owner or extra hard link is outside this exercise; do not edit that object.

ls -ld -- 'workshop notes.txt'

cat reads a named file and writes its contents to the terminal. It does not open an editor or add text. Read the empty file:

cat 'workshop notes.txt'

You should return to the prompt without any file text printed. Together with the zero-size entry, that is the expected empty state. Do not mistake the filename for saved contents. Always supply the pathname in this exercise; cat without a file would instead wait for input from the terminal.

Save a heading and two lines with nano

nano opens a terminal editor. Its buffer is the text being edited in memory. Saving writes that buffer to the named file on disk. Changes visible on the editor screen are not sufficient proof that a later reader will see them.

Learn these controls before opening it:

In nanoAction
Ordinary typing / EnterInsert text / start the next line.
Arrow keysMove the cursor without changing text.
BackspaceRemove the character immediately before the cursor.
Ctrl+O, then EnterWrite the buffer to the displayed filename. Check the name before confirming it.
Ctrl+XExit the editor. If unsaved changes remain, read the save question rather than discard them accidentally.

The ^ notation in nano's shortcut bar means Ctrl: ^O means hold Ctrl and press O. These are keyboard actions, not characters to enter into the file.

Open the known empty file:

nano 'workshop notes.txt'

Enter exactly this heading and two lines. Press Enter after the final line too:

Workshop notes
Topic: paths and files
Next step: practise careful editing

Before saving, inspect the buffer for all three lines and the intended filename. Press Ctrl+O, check that the write-out name is workshop notes.txt, then Enter. Read nano's save message for an error or confirmation. Then press Ctrl+X.

If nano asks about saving modifications on exit, you still have an unsaved change. To keep it, choose the displayed Yes action and confirm the correct filename. Choose No only when you deliberately intend to discard your unsaved change. Do not treat an unexpected filename or write error as a successful save.

Back at the Bash prompt, inspect the disk contents independently:

cat 'workshop notes.txt'

The output must match the supplied heading and both lines. If it is empty or different, inspect the editor's filename, current directory and save result. Do not use touch as a way to insert the missing text.

Reopen the same file, confirm all three saved lines, then exit with Ctrl+X without changing them:

nano 'workshop notes.txt'

This combines two observations: cat read the saved bytes, and a fresh editor opening loaded the intended file. Seeing text in the original unsaved buffer would have established less.

Create and inspect a longer original checklist

Nano can also create a missing file when you save its new buffer; touch is not a prerequisite for every text file. Confirm the next name is absent in the exercise listing before opening it. If it already exists, inspect the resume state rather than replace it.

ls -a

Only when the intended new name is absent, open its new buffer:

nano 'session checklist.txt'

Enter this complete synthetic checklist, including its heading and final line. Save with Ctrl+O, verify the filename, Enter, then Ctrl+X:

Session checklist
01 Welcome the small study group.
02 Set notebooks beside the terminal.
03 Read the session goal aloud.
04 Identify the current directory.
05 Explain the home shortcut.
06 Compare two path starting points.
07 Inspect the supplied empty workspace.
08 Name the next file before editing.
09 Keep the filename words together.
10 Open the first note in the editor.
11 Enter the heading and both lines.
12 Review: check the text before saving.
13 Confirm the write-out filename.
14 Read the editor save message.
15 Leave the editor deliberately.
16 Inspect the saved note separately.
17 Reopen the same note for comparison.
18 Keep unrelated text unchanged.
19 Prepare the second note carefully.
20 Explain the difference between name and contents.
21 Read the longer checklist in a pager.
22 Move forward and back through the text.
23 Search for the next review point.
24 Review: compare the saved result with the input.
25 Return from the pager to Bash.
26 Complete the independent correction.
27 Check all three final note lines.
28 Inspect the owned cleanup targets.
29 Retain other lessons and the practice root.
30 Finish at the ordinary shell prompt.
End of session checklist.

less reads a file in a pager so you can move through it without editing it. Here it is opened directly on your own file, rather than selected by man. -- ends the program's options before the quoted pathname. The selected reader uses the same controls from the help lesson:

Reader actionKeys
Move forward / back a screenSpace / b
Find the first review entry aheadType /Review:, then Enter.
Find the next occurrencen
Return to the shellq

Run the file reader, locate both review entries, inspect the final line, then quit. On a large terminal more text may fit at once; that does not change the file or the meaning of the search keys.

less -- 'session checklist.txt'

Compare all saved lines with the supplied checklist as you read it. In particular, you should find the review text at entries 12 and 24 and the final End of session checklist. line. These checks verify that the longer input was saved and that you can navigate its reader. Use reader keys while it is open; wait for the shell prompt before the next command.

Diagnose a different result

ObservationEvidence and safe response
File exists but cat prints no textIt may be the empty touch result or an unsaved/wrong editor buffer. Check the exact path, filename and save confirmation.
Nano shows text but a new cat does notScreen contents and disk contents differ, or different files were addressed. Reconcile the actual filename and save result.
Two separate filenames appear after an unquoted name was usedBash passed separate arguments. Inspect the actual entries; do not delete unfamiliar files or repeat the unquoted command.
The quoted name is missingCheck pwd and the known exercise listing before creating anything else. A quote does not correct a wrong directory.
A save or entry inspection reports an access errorRecheck ordinary ownership, file type and exact path. Do not add administrator permission to this file task.
The terminal is showing editor or pager textUse that application's controls. Do not paste shell commands into a buffer or reader.

An ordinary write error, unknown file type, unexpected owner or extra hard link means stop before changing that object. The exercise supplies independent regular files with one link, not shared objects to edit through another name.

Independent task: correct one word, preserve two lines

Keep the guided files until this task is complete. In the same checked exercise directory, verify that review notes.txt is absent, then create it with nano. Its starting contents are exactly:

Review notes
Status: pending
Next: compare the saved lines
  1. Save and exit, then use cat to verify all three starting lines. This makes the original saved state observable before the correction.
  2. Reopen that same file. Change only the word pending to ready. Keep the heading and final line exactly as supplied.
  3. Save to the same quoted filename, exit, and inspect it with cat again.
  4. Explain why touch 'review notes.txt' would not make the text correction, and why the same quotes are needed when reading as when editing.

Success is one correctly named file containing the changed status line and the two unchanged surrounding lines. No exact file-size guess, automatic score or unexplained “save succeeded” claim substitutes for reading that result.

Hints

Hint 1 — reuse the editor workflow: a missing file can be created by nano when its buffer is saved. Read the first saved state before making the change.

Hint 2 — work inside the correct line: arrow keys move the cursor without editing. Backspace removes the character immediately before it; avoid deleting the line break or the Status: prefix.

Hint 3 — one possible cursor route: move to just after pending, remove those seven letters with Backspace, and type ready. Save and compare all three lines, not only the changed word.

Answer and Reasoning

After the checked fresh-name observation, the independent creation starts with:

nano 'review notes.txt'

Enter the supplied three-line starting note, save to that exact filename and exit. Verify the original saved state:

cat 'review notes.txt'

It must show the heading, Status: pending, and the supplied final line. Reopen the same file with the taught nano command. Move the cursor to the end of pending, remove those seven characters, and type ready. Ctrl+O, the correct filename confirmation with Enter, then Ctrl+X saves and leaves the editor.

Read the saved result:

cat 'review notes.txt'

The complete expected result is:

Review notes
Status: ready
Next: compare the saved lines

The first and third lines demonstrate preservation; the middle line demonstrates the intended correction. If a line break or another word changed, reopen the same file and repair it from the supplied input before calling the task complete.

The early prediction is that touch creates the named empty file; it does not write the filename as text. On an existing note, another touch updates timestamps rather than replacing pending with ready. The editor is what changes the buffer and saves the new contents.

Quotes keep each spaced pathname as one argument to touch, nano, cat, less or the later cleanup command. They are removed by Bash when it builds the argument; the actual filename contains the spaces, not quote characters. Correct grouping and correct current directory are separate requirements.

Cleanup only after all tasks and checks

The three notes are reproducible exercise data. The rm command removes named files; it does not mean “send to the desktop Trash.” Here -i asks for confirmation before each removal, and -- ends options before the exact quoted filenames. No recursive flag or wildcard belongs in this cleanup.

First recheck the shared root and exercise entries with the taught no-trailing- slash metadata checks. Require the known ordinary directory types and owner:

ls -ld ~/linux-practice
ls -ld ~/linux-practice/create-and-edit-files

Only after those checks, enter the exercise and inspect the location:

cd ~/linux-practice/create-and-edit-files
pwd

Require the exact exercise path recorded at setup. A failed change or different path means stop before any deletion. Inspect the three entries themselves:

ls -ld -- 'workshop notes.txt' 'session checklist.txt' 'review notes.txt'

Each must be a regular file (-), have link count 1, and belong to the recorded ordinary account. Reuse the taught cat and less checks to confirm they are still the supplied exercise contents. Keep unexpected or valuable changes rather than removing them to finish a lesson.

Only when the independent task and those checks are complete, remove the three named files. Read every prompt; answer y only for the intended owned file. If a target is unexpected or you are uncertain, answer n for that prompt and every remaining prompt, then stop at the returned shell prompt to inspect the state. Declining one file does not cancel the remaining confirmation prompts:

rm -i -- 'workshop notes.txt' 'session checklist.txt' 'review notes.txt'

Inspect the remaining directory:

ls -a

Continue only if it contains just . and ... If you declined a file, one removal failed, or something else remains, inspect and preserve that state; do not switch to a stronger removal command.

Return to the shared practice root and verify the change separately:

cd ~/linux-practice
pwd

Require the exact shared-root path recorded in the prerequisite. Only at that verified location, recheck the empty exercise entry itself:

ls -ld -- create-and-edit-files

Require the known directory type and ordinary owner. Then the already taught rmdir can remove only this confirmed empty directory:

rmdir -- create-and-edit-files
ls -a

The exercise name should be gone; the shared practice root and other lessons remain. A nonempty-directory refusal is a condition to inspect, not permission for broad deletion.

Resume from the saved state

If the exercise already exists, apply the same directory and file entry type/owner checks before following or editing anything. Inspect recognized files with cat or less. A known empty first file can resume at nano entry; a correctly saved first note can resume at the longer checklist. Create only a missing named input, using its supplied text. Do not run touch as a reset or overwrite a note merely to hide a previous mistake.

If still in nano, inspect the current buffer and filename, then deliberately save or discard the unsaved changes before returning to the shell. If still in less, quit with q. After an interrupted cleanup, inspect actual remaining names and contents before deciding which already taught step applies. Unknown files, links, extra hard links or unrecognized owners remain untouched.

Technical references

Use Revision to reconstruct the file states and Quick Reference for the taught editor, reader and cleanup cues.

Study this course with an account

Course access adds revision notes, the Quick Reference, saved progress, bookmarks and your own notes.

INR 99 plus 18% GST. 90 days of online access. No auto-renewal.

See course access

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy