The Command Line · practice
Create, Copy, Move, and Remove
You need to reorganize some files from a
The /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:
New-Item -ItemType Directory -Path ./destination/scratch
Copy-Item -LiteralPath ./README.md -Destination ./destination/scratch/readme-demo.md
Get-Content -LiteralPath ./destination/scratch/readme-demo.md
-LiteralPath is the source and -Destination is where the copy goes. Naming both is what stops you getting them the wrong way round, which is the one mistake this command invites.
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.
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.
New-Item -ItemType File -Path ./destination/scratch/draft.txt
Move-Item -LiteralPath ./destination/scratch/draft.txt -Destination ./destination/scratch/final.txt
Get-ChildItem -LiteralPath ./destination/scratch
touch destination/scratch/draft.txt
mv destination/scratch/draft.txt destination/scratch/final.txt
ls -1 destination/scratch
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:
Copy-Item -LiteralPath './notes with spaces.txt' -Destination ./destination/scratch/notes-demo.txt
cp "notes with spaces.txt" destination/scratch/notes-demo.txt
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 final.txt a moment ago and do not need it, so this exact disposable path is safe practice:
Remove-Item -LiteralPath ./destination/scratch/final.txt -WhatIf
Remove-Item -LiteralPath ./destination/scratch/final.txt
The first line does nothing except describe what the second would do. -WhatIf previews the named operation, but does not reserve or protect the target. Check that the path names the disposable item you intended.
For an empty directory, use Remove-Item -LiteralPath with that directory’s exact path. Unlike Unix rmdir, this is not an empty-directory-only safeguard: if PowerShell asks whether to remove children, answer No and inspect the directory. Do not add -Recurse or -Force in this exercise.
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.
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:
Orient and protect the sources. Confirm you are at
/workspace, and thatREADME.mdandnotes with spaces.txtare both present.Prepare and copy. Create
destination/file-operations. CopyREADME.mdinto it asREADME-copy.md, read the copy, then copynotes with spaces.txtinto it asnotes-copy.txt.Move the copy. Rename
notes-copy.txttorenamed-notes.txt. The supplied original stays at the workspace root because later lessons still read it.Remove only disposable evidence. Inside
destination/file-operations, create and list an empty directory calledempty, then remove it using the directory-removal route explained above for your shell. Create and list an empty file calleddelete-me.txtin 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.