0%

The Command Line · practice

Create, Copy, Move, and Remove

You need to reorganize some files from a , and you may have arrived here directly from another course. Four operations cover nearly all of it: create, copy, move, and remove. Each one changes the tree, so each one is worth a look afterward to see that it did what you meant.

The beside this lesson holds a disposable hosted project at /workspace, separate from your own computer. Do the examples and graded practice there. Before changing anything, you need to check your location, navigate, list a directory, and read a file with a quoted path. If you have not done those yet, follow these lessons in order, then return here:

Copy

A copy leaves the original alone and makes a second item somewhere else. Start from /workspace:

mkdir destination/scratch
cp README.md destination/scratch/readme-demo.md
cat destination/scratch/readme-demo.md

cp takes the source first and the destination second. Nothing in the line reminds you which is which, so that order is yours to remember; getting it backwards is the one mistake this command invites.

Reading the copy afterward costs one command and catches a mistyped destination, which is otherwise invisible until much later.

Move, which is also rename

Moving changes an item’s path. That covers both jobs people think of as separate: give it a new directory and the file moves, give it a new name in the same directory and the file is renamed. There is one command for both, because to the filesystem they are the same edit.

touch destination/scratch/draft.txt
mv destination/scratch/draft.txt destination/scratch/final.txt
ls -1 destination/scratch

The listing shows final.txt and no draft.txt. That is the observable difference from a copy: this same-filesystem example now has the destination path and no source path.

One thing to watch: shells do not share one collision rule. PowerShell’s Move-Item normally reports an error when the target filename already exists, while common mv implementations may replace an existing file. The safe rule works in both places: inspect the destination before moving, and choose a new target name when anything important is already there.

A path with spaces still needs quoting, exactly as it did when you were reading files:

cp "notes with spaces.txt" destination/scratch/notes-demo.txt

Without the quotes the shell splits the name at the spaces and the command goes looking for a file called notes.

Remove

Treat removal as an operation with no undo. It commonly bypasses the Recycle Bin or Trash, and confirmation behavior varies by command and configuration. You made final.txt a moment ago and do not need it, so this exact disposable path is safe practice:

rm destination/scratch/final.txt

rmdir removes a directory, and only an empty one. If something is still inside, it refuses and says so, which makes it a useful way to find out whether a directory you think is empty actually is.

Ctrl+C may interrupt a command that is still running; it does not undo a removal or any other change that already finished.

The habit worth keeping is not timidity, it is precision: name the exact thing you mean. Every removal here names one disposable target in full, so the complete path is visible before you press Enter.

Practice: build a checked exercise directory

Graded from the workspace, starting at /workspace. Use Reset first if an earlier attempt left destination/file-operations behind.

Work through four milestones, looking at the result after each one:

  1. Orient and protect the sources. Confirm you are at /workspace, and that README.md and notes with spaces.txt are both present.

  2. Prepare and copy. Create destination/file-operations. Copy README.md into it as README-copy.md, read the copy, then copy notes with spaces.txt into it as notes-copy.txt.

  3. Move the copy. Rename notes-copy.txt to renamed-notes.txt. The supplied original stays at the workspace root because later lessons still read it.

  4. Remove only disposable evidence. Inside destination/file-operations, create and list an empty directory called empty, then remove it using the directory-removal route explained above for your shell. Create and list an empty file called delete-me.txt in that same directory, then remove that exact file. Finish by verifying that the two required files remain and the two disposable paths are gone.

Stay inside /workspace, name one target at a time rather than matching several at once, and remove nothing except destination/file-operations/empty and destination/file-operations/delete-me.txt from step 4. Leave README.md and the supplied notes file unchanged.

If directory removal reports existing contents or asks to remove children, stop instead of confirming. Inspect the exact path; use Reset if you cannot confidently recover this disposable workspace. When the final state matches the four milestones, press Check my work.

You move draft.txt to final.txt in the same directory. What does the directory hold afterward?

Steps 2 and 3 ask you to copy the notes file and rename the copy, rather than renaming the original. Why?

You can create, copy, move, rename, and remove, and you know which of those leave the original behind. Next, a value that lives in the session rather than in a file.