Linux Foundations / Work With Files and Paths

Copy and move a small workspace safely

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

Keep an Original or Change Its Location?

You are preparing a workshop. An incoming agenda needs a working copy, the notes need to go into a folder for printing, and an approved agenda already exists in the reference folder. Moving everything would lose the incoming agenda's original location. Copying everything over the reference folder could replace the approved version.

The decision starts with the intended result:

  • Copy when the original must remain and you need another file containing the same text. Use cp.
  • Move when the file should have a new location or name. Use mv.

A source is the file you are taking content from or relocating. A destination is the path where the result belongs. A path identifies a file or a directory; a directory is a folder containing named entries.

For the ordinary files in this exercise, a successful copy leaves the source at its original path. Changing one copy later does not automatically update the other. A successful move leaves the file at its destination and removes its old pathname. Neither command decides which version of your notes matters. You make that decision before running it.

Read the Destination Before Acting

Both commands take options, then a source and a destination. An option changes how a command behaves; an argument is a value you pass to it. These are examples of the commands you will use after creating the workspace:

cp -i -- inbox/agenda.txt ready/
cp -i -- inbox/agenda.txt reference/draft-agenda.txt
mv -i -- 'inbox/workshop notes.txt' ready/

The order matters: source first, destination last.

Destination in this exerciseWhat the command does
Existing directory ready/Places the file inside it using the source's final name: ready/agenda.txt or ready/workshop notes.txt
A new filename such as reference/draft-agenda.txtUses that exact filename; its parent directory must already exist
An existing destination fileMay replace that file's content; our -i option asks before replacement

The final slash in ready/ makes the directory intention explicit. Create that directory first. Without the slash, a missing ready could instead be treated as a new filename. Do not infer the destination's type from its name.

-i means interactive: ask before overwriting an existing destination. It does not ask for every new file and it does not make an unwanted overwrite recoverable. Inspect first, and answer deliberately.

-- marks the end of options for these GNU commands. The following values are paths, even if a filename begins with a hyphen. It does not check ownership, prevent overwrites or replace quoting.

The single quotes around 'inbox/workshop notes.txt' keep the space inside one argument. Bash, the shell that interprets your terminal input, removes those quotes before passing the pathname to mv. The quotes are not part of the filename. Without them, the space would separate arguments.

Copying inbox/agenda.txt creates ready/agenda.txt and keeps the source. Moving workshop notes.txt transfers it from inbox to ready, then renaming gives ready/print-notes.txt. At reference/agenda.txt, declining the overwrite keeps the approved file; a separate draft-agenda.txt receives the copy.

Read each arrow from the earlier path to the result. Copy keeps the source in inbox. The dashed old path in the move scene no longer exists after the two moves, which end at ready/print-notes.txt. The last scene protects reference/agenda.txt: decline the collision, then choose a fresh reference/draft-agenda.txt destination to keep both versions.

Exercise: Prepare the Workshop Files

Goal: make a printable workspace while preserving the incoming agenda and the approved reference. Verify the names, locations and contents, not merely the absence of an error message.

Starting state: use the disposable Ubuntu 24.04 guest and its normal account, with Bash 5.2, GNU coreutils 9.4 and nano available from the course setup. The earlier lessons on paths and file editing supply the normal prerequisites. The short bridge below also explains the commands this exercise needs. It does not install a guest or replace that setup.

Execution and effects: run each command line separately at the guest's shell prompt. A prompt is the shell's indication that it is ready for input; do not copy the prompt or illustrative output as commands. If a command reports an unexpected error, stop that group and inspect it before continuing. All changes stay inside ~/linux-practice/copy-and-move-files/, which must be an ordinary course-owned directory inside the guest, with no host-shared folders. This exercise uses no administrator commands, network services or paid resources.

1. Establish a Fresh, Owned Directory

id -u prints your numeric user ID. A result of 0 means the root administrator account; switch to the normal course account before continuing. Other numeric values vary between installations. Record the ordinary username from the already taught id -un for the ownership checks below.

id -u
id -un

~ at the beginning of an unquoted path means your home directory. mkdir creates a directory. Its -p option creates missing parents and accepts an already-existing directory; here it is used only for the shared practice parent. cd changes your current directory, and pwd prints its full path. ls -a lists the directory's entries, including hidden names; . means this directory and .. means its parent.

First reuse the navigation lesson's entry checks, without trailing slashes:

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

Each existing entry must have directory type d and your recorded ordinary owner. A leading l means a symbolic link: stop for that, another unexpected type or owner, or an unexplained inspection error. Only a missing practice parent may be created below. mkdir -p and an expected-looking pwd do not replace these checks. They are observations of current state, not a guarantee against concurrent changes; stop and recheck if anything changes or is unclear.

mkdir -p ~/linux-practice
ls -ld ~/linux-practice

Require the expected owned directory before entering it:

cd ~/linux-practice
pwd
ls -a

The pwd result should be your guest home followed by /linux-practice. For example, /home/learner/linux-practice is illustrative; your account name may differ. Require that actual course practice location and record its full path. A failed cd or different result means stop before creation.

Inspect the exercise entry itself before following or creating it:

ls -ld -- copy-and-move-files

Require a missing-name diagnostic for the fresh route. If it exists, do not overwrite its contents: use Resume or Reset, which first checks its directory type and owner. Leave any unrecognized entry untouched.

mkdir copy-and-move-files
ls -ld -- copy-and-move-files

Continue only after error-free creation and the expected owned directory record. A File exists message is a reason to inspect, not add -p.

cd copy-and-move-files
pwd
ls -a

Require the recorded shared-root path followed by /copy-and-move-files and only . and ... Record this exercise path before creating the children:

mkdir inbox
mkdir reference
mkdir ready
ls -ld -- inbox reference ready

Require three ordinary owned directories before following their paths. From now until cleanup, run commands in this exercise root. Paths such as inbox/agenda.txt are relative paths: they start from that current directory.

2. Supply and Inspect the Text

nano opens the specified text file in the terminal editor. For each file: run its command, type the supplied text, press Ctrl+O to write it, press Enter to accept the displayed filename, then press Ctrl+X to exit. Nano displays ^ for Ctrl in its shortcut labels. Hold Ctrl while pressing the letter; do not type the caret. You return to the shell after exiting.

Open the incoming agenda:

nano inbox/agenda.txt

Enter these two lines, then save and exit:

Workshop: Linux foundations
Start: 10:00

Open the notes, keeping the quoted path together:

nano 'inbox/workshop notes.txt'

Enter this one line, then save and exit:

Bring: a notebook

Create the deliberately different approved agenda:

nano reference/agenda.txt

Enter this one line, then save and exit:

Approved agenda: keep this version

These are complete synthetic inputs. Type the text inside each block, without the surrounding Markdown fence. No names, credentials or external files need replacing. If nano opens existing text during this fresh setup, stop and check that you are editing the intended owned file.

Use the preceding editor lesson's file-entry checks before reading the inputs:

ls -ld -- inbox/agenda.txt 'inbox/workshop notes.txt' reference/agenda.txt

Require regular type -, one link and your recorded ordinary owner for each file. Keep those conditions for existing sources and destinations throughout the task; a link, extra hard link or changed/unrecognized entry means stop and inspect before acting. cat displays a file's content without changing it. Inspect each input:

cat inbox/agenda.txt
cat 'inbox/workshop notes.txt'
cat reference/agenda.txt
ls -a ready

Each cat should show its supplied text. The final listing should show only . and ... The two agendas have the same final filename but different paths and different contents. That distinction is central to the task.

3. Copy into an Existing Directory

Predict: after copying into ready/, which path contains the copy? Does the incoming agenda remain?

cp -i -- inbox/agenda.txt ready/
ls -a inbox
ls -a ready
cat inbox/agenda.txt
cat ready/agenda.txt

No prompt is expected because ready/agenda.txt was absent. Both displayed agendas should contain the two supplied workshop lines. The two directory listings establish that the source remains and a destination was added. Silence from cp alone would not establish either fact.

4. Decline a Collision, Then Keep Both Versions

Before copying to the reference directory, inspect the file that already has the destination name:

cat reference/agenda.txt
cp -i -- inbox/agenda.txt reference/

Illustrative prompt:

cp: overwrite 'reference/agenda.txt'?

Type n and press Enter at this prompt, not as a separate shell command. This preserves the approved file. Prompt wording and quotation marks can vary with language settings. On the course's English-language examples, y confirms replacement and n declines. If you do not recognize a prompt, use Ctrl+C to cancel rather than guess.

cat reference/agenda.txt

It must still say Approved agenda: keep this version. A declined overwrite has intentionally made no change.

Keep the incoming version under a different, unused reference name:

ls -a reference
cp -i -- inbox/agenda.txt reference/draft-agenda.txt
cat reference/draft-agenda.txt
cat reference/agenda.txt

The new draft-agenda.txt contains the incoming two-line agenda. The approved agenda.txt remains different. A fresh name solves this collision without choosing an unwanted replacement. If the supposedly new name already exists, decline any prompt and inspect it before deciding what to do.

5. Move the Notes, Then Rename Them

First move the notes into the existing ready/ directory:

mv -i -- 'inbox/workshop notes.txt' ready/
ls -a inbox
ls -a ready
cat 'ready/workshop notes.txt'

The spaced filename remains the same, but its parent directory changes. inbox should now contain only agenda.txt, apart from . and ... The notes are at ready/workshop notes.txt.

Now give that file a useful printing name:

mv -i -- 'ready/workshop notes.txt' ready/print-notes.txt
ls -a ready
cat ready/print-notes.txt

This is another move, with a new filename in the same directory. The content still says Bring: a notebook. There should be no workshop notes.txt entry in ready after the rename. A move can change a location, a name, or both in one command.

6. Verify the Whole Result

Run these read-only checks from the exercise root:

pwd
ls -a inbox
ls -a reference
ls -a ready
cat inbox/agenda.txt
cat ready/agenda.txt
cat reference/draft-agenda.txt
cat reference/agenda.txt
cat ready/print-notes.txt

Success: the inventory and contents match this table. Listing order and displayed quote marks around names can vary; inspect the actual names.

DirectoryFiles remainingMeaning
inboxagenda.txtThe original incoming agenda is preserved
referenceagenda.txt, draft-agenda.txtThe approved text survives beside the incoming copy
readyagenda.txt, print-notes.txtThe printable agenda is copied; the notes are relocated and renamed

The three incoming-agenda files contain the same two supplied lines. The approved agenda retains its distinct line. Exactly one notes file remains in these three directories, at ready/print-notes.txt.

If Something Differs

EvidenceLikely explanationSafe next check or action
No such file or directory for a sourceWrong current directory, mistyped path, or a move already completedRun pwd; list the named source and destination directories with ls -a. A completed move is not a reason to recreate the source blindly.
A new file is named ready instead of appearing inside a directoryThe destination directory was missing and its trailing slash was omittedUse the narrow recovery branch below; preserve any unrecognized file.
Unexpected overwrite promptThe destination already exists, perhaps from an earlier attemptAnswer n, inspect its content with cat, then resume the next unfinished step or choose the specified unused name.
More than one apparent source, or an error mentioning part of a spaced nameQuotes were omitted or copied incorrectlyCheck the whole path; use ordinary single quotes around the spaced pathname.
Approved agenda now contains the incoming textReplacement was allowed or -i was omittedThe copy command cannot restore the previous contents. In this synthetic lab, restore the supplied approved line with nano after confirming the exact owned path. For real work, recovery would require another copy or backup.
Permission deniedWrong account/location or an unexpected ownership conditionStop; check the guest account and pwd. Do not use administrator privileges as a shortcut for this normal-user exercise.

If Ready Became a File

Use this branch only if your mistaken copy created a file named ready where you intended a directory. It is not part of the normal exercise path. Reapply the parent/exercise entry checks from setup before entering the exercise, then require its recorded pwd path. Inspect the mistaken entry itself before reading or removing it:

pwd
ls -ld -- ready

If the entry is a directory, this branch does not apply. Require a regular file, one link and your ordinary owner; stop for a link or other difference. Then read it with the taught cat ready. If it displays the incoming agenda, continue only when you know this is the unwanted copy created by your own attempt. Matching text alone does not prove ownership; leave an unrecognized file untouched.

rm -i -- ready asks before removing that one named file. Removal does not use the desktop Trash and cannot be undone by this command. For this confirmed unwanted copy only, run:

rm -i -- ready

Type y and press Enter at its removal prompt. Continue only after removing the file:

mkdir ready
ls -ld -- ready

Require the newly created ordinary owned directory before inspecting inside it:

ls -a ready

The new directory should contain only . and ... Resume the unfinished copy, preserving the source and other directories. The normal instructions create ready first and use a trailing slash in ready/, so a missing directory would be reported instead of silently becoming a new filename.

Independent Task: Prepare a Second Session

Keep the guided workspace until you finish this task. You will use the same decisions on fresh inputs, and combine the notes' move and rename into one operation.

From ~/linux-practice/copy-and-move-files/, require the recorded pwd path and inspect the root with ls -a. If independent already exists, use your earlier state or the reset instructions; do not merge fresh setup into unknown contents.

mkdir independent
ls -ld -- independent

Require error-free creation and the known directory type/owner before creating its children:

mkdir independent/inbox
mkdir independent/reference
mkdir independent/ready
ls -ld -- independent/inbox independent/reference independent/ready

Require those same directory checks before following any child path. Run each nano command separately. Supply the corresponding complete one-line text, then save and exit using the keys taught above.

Editor commandText to enter
nano independent/inbox/schedule.txtPractice session: 14:00
nano 'independent/inbox/speaker notes.txt'Check: projector
nano independent/reference/schedule.txtApproved schedule: keep this version

Use the taught ls -ld -- inspection on those exact three file paths first; require regular type, one link and your recorded ordinary owner. Then use cat on them to confirm the inputs. Use ls -a independent/ready to confirm it contains only . and ... Keep the exercise root as your current directory; the longer relative paths below start there.

Attempt these goals before reading the hints or answer:

  1. Preserve independent/inbox/schedule.txt and place a copy at independent/ready/schedule.txt.
  2. Try the interactive copy into independent/reference/, decline the collision, and confirm the approved schedule is unchanged.
  3. Keep another incoming copy at independent/reference/draft-schedule.txt, without replacing the approved schedule.
  4. Move independent/inbox/speaker notes.txt directly to independent/ready/speaker-brief.txt in one move command.
  5. Inspect all three directories and read every resulting file. Explain why the schedule remains in the inbox while the speaker notes do not.

Acceptance checks: the inbox contains only the incoming schedule; reference contains its approved schedule and a separate draft; ready contains the incoming schedule and speaker-brief.txt. Both incoming copies match the source line. The approved line and the notes line are unchanged. No speaker notes.txt remains in the inbox.

Graduated Hints

Use one hint at a time, then try the task again.

  1. Decide which original paths must remain. Those operations need copying; the notes should leave their old path.
  2. An existing destination directory keeps the source's final filename. An explicit new destination filename lets you choose a different name.
  3. The protected reference file shares the schedule's final filename. Inspect it, decline the interactive replacement, then use draft-schedule.txt. Quote the entire source path containing speaker notes.txt.

Check Your Understanding

Consider the fresh guided starting state, before any copy or move.

  1. Predict the difference between copying inbox/agenda.txt to ready/ and copying it to reference/draft-agenda.txt. Which originals remain?
  2. Why does copying to reference/ produce a collision even though the command never spells out reference/agenda.txt?
  3. What does -- protect against, and what separate problem do the quotes around 'inbox/workshop notes.txt' solve?
  4. After declining an overwrite, is an unchanged destination evidence that the task failed? What should you inspect?
  5. Why do both the directory listing and the file's text matter when checking the notes' move?

Answer and Reasoning

Independent Task

Run from the exercise root after supplying the independent starting files. Do not rerun a move if inspection shows that it already completed.

cp -i -- independent/inbox/schedule.txt independent/ready/
cat independent/reference/schedule.txt
cp -i -- independent/inbox/schedule.txt independent/reference/

At the overwrite prompt, type n and press Enter. Continue after checking that the approved content remains:

cat independent/reference/schedule.txt
cp -i -- independent/inbox/schedule.txt independent/reference/draft-schedule.txt
mv -i -- 'independent/inbox/speaker notes.txt' independent/ready/speaker-brief.txt

The two successful copies retain their source. The declined copy protects the existing approved text. The move's destination provides both the new parent directory and the new filename, so a second rename is unnecessary.

Inspect the actual result:

ls -a independent/inbox
ls -a independent/reference
ls -a independent/ready
cat independent/inbox/schedule.txt
cat independent/ready/schedule.txt
cat independent/reference/draft-schedule.txt
cat independent/reference/schedule.txt
cat independent/ready/speaker-brief.txt

The first three cat commands each show Practice session: 14:00. The next shows Approved schedule: keep this version, and the last shows Check: projector. The listings establish that the old notes name is absent and the new name is present.

You could spell out independent/ready/schedule.txt as the copy destination instead of independent/ready/; both satisfy this task once the directory exists. Reordering independent copies is also valid. Replacing the approved file, leaving a duplicate notes file in the inbox, or failing to check content does not meet the stated result.

Understanding Check

  1. Both copies retain inbox/agenda.txt. The existing ready/ directory produces ready/agenda.txt; the explicit new name produces reference/draft-agenda.txt.
  2. With an existing directory as destination, cp appends the source's final filename. That resolves to the already-existing reference/agenda.txt.
  3. -- ends option parsing, so a following hyphen-leading pathname is treated as a path. Quotes keep the spaced pathname in one shell argument. Neither mechanism prevents replacing existing content; -i and your decision address that.
  4. Declining deliberately leaves the destination unchanged. Read that file to verify the intended version survived, and inspect any other destination you meant to create. A prompt response alone is insufficient evidence.
  5. The listing checks the location and absence of the old name. Reading the destination checks that the intended text arrived. A correctly named but wrongly populated file would fail the task.

Cleanup

Clean up only after the guided task, independent attempt and checks. Keep any notes you want outside the disposable exercise directory before removing it.

rm removes named files; this command-line removal does not put them in a desktop Trash. -i asks before each removal. Confirm only the exact files you created here, then type y and press Enter for each prompt. -- again ends options. rmdir removes an empty directory and refuses a nonempty one, which gives you another chance to inspect unexpected contents.

First recheck the shared root and exercise entries without trailing slashes:

ls -ld ~/linux-practice
ls -ld ~/linux-practice/copy-and-move-files

Require the known directory types and ordinary owner, then enter separately:

cd ~/linux-practice/copy-and-move-files
pwd

Require the exact recorded exercise path. Before following child paths, inspect their entries from parent to child:

ls -ld -- inbox reference ready independent

Require ordinary owned directories, then inspect the nested entries:

ls -ld -- independent/inbox independent/reference independent/ready

Require the same conditions before listing their contents. Stop for changed or unrecognized state; these observations do not prevent concurrent replacement.

ls -a
ls -a inbox
ls -a reference
ls -a ready
ls -a independent/inbox
ls -a independent/reference
ls -a independent/ready

For the completed result, inspect the ten file entries themselves before removal:

ls -ld -- inbox/agenda.txt reference/agenda.txt reference/draft-agenda.txt ready/agenda.txt ready/print-notes.txt
ls -ld -- independent/inbox/schedule.txt independent/reference/schedule.txt independent/reference/draft-schedule.txt independent/ready/schedule.txt independent/ready/speaker-brief.txt

Require known regular single-link files and the ordinary owner; recheck their supplied contents with cat. Preserve anything unexpected. Only after those checks, remove these ten known files:

rm -i -- inbox/agenda.txt
rm -i -- reference/agenda.txt
rm -i -- reference/draft-agenda.txt
rm -i -- ready/agenda.txt
rm -i -- ready/print-notes.txt
rm -i -- independent/inbox/schedule.txt
rm -i -- independent/reference/schedule.txt
rm -i -- independent/reference/draft-schedule.txt
rm -i -- independent/ready/schedule.txt
rm -i -- independent/ready/speaker-brief.txt

Then remove empty children before their parents:

rmdir -- inbox
rmdir -- reference
rmdir -- ready
rmdir -- independent/inbox
rmdir -- independent/reference
rmdir -- independent/ready
rmdir -- independent
ls -a

Require the exercise now empty. Return to the shared root separately:

cd ~/linux-practice
pwd

Require its recorded path before inspecting the final entry:

ls -ld -- copy-and-move-files

Only for that known owned directory with verified emptiness:

rmdir -- copy-and-move-files
ls -a

The exercise name should be absent from the final listing. Keep the parent linux-practice and any other course directories. If removal is refused, inspect the reported directory; do not escalate to recursive deletion.

Resume or Reset

For an interrupted attempt, reapply setup's no-trailing-slash ls -ld checks to the home, shared root and exercise entry before following them. Require known ordinary directories and your recorded owner, then enter the exercise and require its exact recorded pwd. Inspect each existing inbox, reference, ready and independent entry with ls -ld -- before listing inside it. Check independent before its nested children; inspect only names that were actually created. A recognized missing child can be created with the taught mkdir only after its parent is checked. Treat the mistaken regular ready file only through its specific recovery branch.

For each existing source, destination or cleanup file, use the same taught file-entry checks before cat, editing or mutation: regular type, one link and your ordinary owner. Match the names and contents to the last completed step before continuing. Stop on a link, unknown owner or changed/unrecognized state; inspections are observations, not a guarantee against concurrent changes.

  • If a copy's intended destination already has the correct text, leave it and continue. Decline a repeated overwrite prompt.
  • If the notes are already in ready, use that current path. If they are already named print-notes.txt, the guided move work is complete.
  • If nano did not save a required file, recreate only that missing owned input from the supplied text.

To reset instead, use the cleanup procedure for the listed files that actually exist. An interrupted move may leave a notes file at one of these earlier paths; inspect with cat, then remove that exact file with rm -i -- only if it is yours:

Possible earlier fileWhy it may remain
inbox/workshop notes.txtThe first guided move has not completed
ready/workshop notes.txtThe move completed but the rename did not
independent/inbox/speaker notes.txtThe independent move has not completed

Quote each spaced path when passing it to a command. Skip files or directories that were never created; an expected missing-path message during inspection does not require a broader command. Empty directories can then be removed with the same child-before-parent rmdir sequence. Recreate the fresh starting state only after the old owned exercise directory is gone. Leave any unrecognized file untouched and resolve it before attempting the reset.

References

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