You want to try an edit while keeping an original note. First make a copy, then check both files after changing only the copy. A successful copy does not keep the files linked: later edits can make their contents differ.
File content is the stored data. The filename identifies where to find it. cat displays a file's content, cp copies it, and mv moves or renames it. Redirection is shell syntax that sends a command's output to a file instead of the screen.
Before you begin
- Recommended first
- Install Ubuntu LTS and open a terminal, Where practice is allowed, Where you are
- Estimated time
- 15 min reading, 15 min activity. Setup and optional practice may take longer.
- Where you work
- Use Bash in your owned Ubuntu practice environment as an ordinary user. The supplied text files use UTF-8, a text encoding; where shown, LC_ALL=C requests consistent text ordering. Work only under your own new ~/yanosec-lab directory, never a host share or personal/system file. Refuse an existing exercise destination rather than overwriting it. No account, paid platform, or remote target is required. Optional extensions take about 10–15 minutes and are not part of the time above. A virtual machine is optional. You can also finish the reasoning from the printed evidence.
- Without a lab
- The page supplies the files, source and text records needed for the activity. Without a shell, follow the numbered steps on paper, label each result as predicted, and use the separate evidence supplied by the question. Running commands can wait until your lab is available. Reading works without JavaScript. Answer checking needs JavaScript and a secure context, such as HTTPS; saving a correct result also needs usable browser storage. Missing tools, managed or mobile hosts, or a deferred guest setup never require buying a service. Keep owned lab files for review; do not use broad deletion.
After this lesson you can:
- Create and inspect text in a fresh lab directory.
- Copy a file and predict replacement of only the disposable copy.
- Distinguish copying, moving and shell redirection.
01Example
This prediction assumes practice is a new directory and note.txt is new. printf supplies red and a newline. The shell sends that output into the path after >. cat then reads the file so you can compare saved content with the intended message.
Commands to type, one at a time:
mkdir practice
printf '%s\n' red > practice/note.txt
cat practice/note.txt
Predicted result of cat:
red
The prompt returns after each successful command.
The text red is output, not another command.02Overwrite and append are different
The operator > creates its destination if absent, or replaces its current content if present. The operator >> adds output at the end instead. Both belong to the shell. They do not become part of the message printf displays.
Redirecting can open and empty a destination before the command runs. Even a later failure can therefore leave an old file empty. Use only the fresh activity files, and check the destination after > before pressing Enter.
cp -i asks before replacing an existing destination; -i is the option requesting that prompt. If a prompt appears in this fresh-directory activity, decline it and inspect your location. mv -i can also ask before replacement, but moving changes the original location and is not a backup.
Keep directory, stored bytes and terminal stdout separate while following mkdir, redirection and cat.Model. A teaching model drawn by hand. It shows the idea, not real tool output.
Read the full explanation below.
Playback is optional. Step through at your own pace.
Instruction being read
mkdir practiceModeled tree
practice absentabsent
practice/note.txt
file absentabsent
Predicted terminal stdout
not yet modeledunobserved
Before mkdir: practice and note.txt are absent.
Follow the explanation
- Before mkdir: practice and note.txt are absent.
- mkdir creates the modeled directory, not file content.
- > routes red plus LF into note.txt; the red line is not terminal stdout at this step.
- cat reads stored red plus LF into terminal stdout.
Read every state and value
1. Before mkdir: practice and note.txt are absent.
- Current command
- mkdir
- Data in each location
- Pane: Modeled tree; Text: practice absent; Status: absent · Pane: practice/note.txt; Text: file absent; Status: absent · Pane: Predicted terminal stdout; Text: not yet modeled; Status: unobserved
- Links
- None
2. mkdir creates the modeled directory, not file content.
- Current command
- mkdir
- Data in each location
- Pane: Modeled tree; Text: practice/; Status: present · Pane: practice/note.txt; Text: file absent; Status: absent · Pane: Predicted terminal stdout; Text: empty; Status: empty
- Links
- From: mkdir; To: Modeled tree; Label: creates directory
3. > routes red plus LF into note.txt; the red line is not terminal stdout at this step.
- Current command
- write
- Data in each location
- Pane: Modeled tree; Text: practice/note.txt; Status: present · Pane: practice/note.txt; Text: red ; Status: present · Pane: Predicted terminal stdout; Text: empty; Status: empty
- Links
- From: write; To: practice/note.txt; Label: redirected stdout
4. cat reads stored red plus LF into terminal stdout.
- Current command
- cat practice/note.txt
- Data in each location
- Pane: Modeled tree; Text: practice/note.txt; Status: present · Pane: practice/note.txt; Text: red ; Status: present · Pane: Predicted terminal stdout; Text: red ; Status: predicted
- Links
- From: cat practice/note.txt; To: practice/note.txt; Label: reads · From: practice/note.txt; To: Predicted terminal stdout; Label: displayed content
03Try the original and copy
Begin in your owned ~/yanosec-lab directory. Create a new files directory with mkdir files, then enter it with cd files only after creation succeeds. A conflicting name means you should stop and select a fresh name consistently.
The worksheet lists each command separately. On paper, keep two columns headed original.txt and copy.txt. Update only the column named as a destination. This gives the same content reasoning without writing any files.
Keep an original while changing a copy
The command labels below distinguish an example from the activity.
On paper, track original.txt and copy.txt as separate saved files.
The word after > names the destination to replace; it is not text
that printf prints. Continue only in a new owned exercise directory.
Start in a new owned directory.
Worked: printf '%s\n' red > note.txt -> predicted red newline
Activity commands (one line at a time, only in a fresh files directory):
printf '%s\n' amber > original.txt
cp -i original.txt copy.txt
cat original.txt
cat copy.txt
printf '%s\n' violet > copy.txt
cat original.txt
cat copy.txt
Predicted final files: original.txt = amber; copy.txt = violet.
No command here deletes original.txt. Do not overwrite unrelated files.
Question initial note.txt: first
Question action: printf '%s\n' second > note.txt
What one word remains in that disposable file?
- Check pwd and ls -a in the new files directory. It must be your fresh empty exercise destination before using >.
- Enter printf '%s\n' amber > original.txt. Read it with cat original.txt; expected amber on one line.
- Enter cp -i original.txt copy.txt. Read cat copy.txt and cat original.txt separately; expected matching amber content. If a replacement prompt appears unexpectedly, decline and inspect the destination.
- Enter printf '%s\n' violet > copy.txt, then read both again. Expected: copy violet, original amber. Explain why a copy does not synchronize later changes automatically.
- Record the paths and observed results, or trace those same bytes on paper. Return to the lab root; retain the files for review. No broad deletion or system change is necessary.
04Optional challenge
Allow about ten extra minutes. After the activity, predict printf '%s\n' third >> copy.txt. Write the complete resulting copy, including line breaks. Compare that with using > and explain why writing to original.txt would defeat the preservation goal.
05Fresh cases for the checks
Use these fictional cases for the checks below. They have different inputs from the worked examples. Read them here or make notes; no installation or external service is needed.
File card A: copy, replace and readOwned fixture. An original file made for this course. Follow the activity's instructions in your own lab.
In a fresh owned directory, source.txt contains Indigo followed by a newline.
trial.txt is absent. Commands run successfully in order:
cp -i source.txt trial.txt
printf '%s\n' Saffron > trial.txt
cat source.txt
cat trial.txt
No other write or synchronization occurs.
File card B: a separate moveOwned fixture. An original file made for this course. Follow the activity's instructions in your own lab.
Before: draft.txt contains plan and a newline. final.txt is absent.
Command: mv -i draft.txt final.txt
The command succeeds; there are no other files or changes in this case.
File card C: inspect before pressing EnterOwned fixture. An original file made for this course. Follow the activity's instructions in your own lab.
Owned original seed.txt contains fern and a newline; keep this original unchanged.
Disposable scratch.txt contains test and a newline.
A learner proposes:
printf '%s\n' moss > seed.txt
The command has NOT been run. Assume the path is writable.
File card D: an independent append caseOwned fixture. An original file made for this course. Follow the activity's instructions in your own lab.
Before: record.txt contains exactly one line: north
That line ends with a newline. Next command succeeds:
printf '%s\n' south >> record.txt
06Explain a preservation plan
Optional self-check: describe how you would try new text while keeping an owned original unchanged. Explain the roles of cp, mv, cat, > and >> in your plan.
Review your explanation: check pwd and a fresh destination before writing; keep source and copy states separate; use cat to inspect them; distinguish moving from making a second copy; and check every redirection destination. This explanation is self-reviewed.
07Common mistakes
If the original changed, inspect which destination followed >. Stop and preserve the evidence; do not overwrite more files to reconstruct what happened. Re-create only a new controlled lab for another attempt.
If cat cannot read the named file, inspect pwd, spelling and ls before retrying. A filename argument is interpreted from the current directory unless absolute.
If a copy prompt appears, the destination may not be fresh. Decline replacement and choose a new lab directory. The case contains no observed error text or guaranteed live outcome.
References
- Ubuntu 24.04 packaged manual: bash(1)
Shell grammar, quoting, builtins, redirection and job control; use only the sections introduced in this lesson. Use the named command and option sections. This manual describes Ubuntu 24.04 packages; your installed version can differ. The lesson supplies the facts needed for its activity.
- Ubuntu 24.04 packaged manual: cp(1)
Use the named command and option sections. This manual describes Ubuntu 24.04 packages; your installed version can differ. The lesson supplies the facts needed for its activity.
- Ubuntu 24.04 packaged manual: printf(1)
Use the named command and option sections. This manual describes Ubuntu 24.04 packages; your installed version can differ. The lesson supplies the facts needed for its activity.