Working with files and folders
On This Lesson 11 topics
Lesson Overview
You can now say where things are. This lesson lets you change them: create folders and files, copy them, move or rename them, and delete them, all from the terminal.
Back to the literal assistant. When you say "delete this folder", it deletes it. It does not move it to a recycle bin, it does not ask "are you sure?", and it does not notice that you meant a different folder. That is why this lesson pairs every command with a habit: check where you are, and look at what you are about to touch, before you press Enter.
You will learn five jobs and the commands for them in Bash and PowerShell: make a folder, make a file, copy, move or rename, and delete. You will also see how to look inside a small text file without opening an editor.
This lesson builds on the previous one: every command here takes paths, absolute or relative. If pwd, cd, and .. still feel uncertain, revisit Lesson 2 first. Use a practice folder for everything below, such as a new folder in your home called sandbox, so a mistake cannot hurt anything that matters. Commands and flags can differ slightly between systems, so check your own system's help if something behaves differently.
Learning Objectives
By the end of this lesson you will be able to:
- Create folders, including nested ones, from the terminal.
- Create empty files and view the text inside a small file.
- Copy a file or a whole folder.
- Move a file or folder, and rename it with the same command.
- Delete files and folders, and explain why terminal deletion is risky.
- Use the habit "
pwdandlsbefore delete", and preview a delete where the shell allows it. - Match each job to its Bash and PowerShell command.
Written Explanation
The five jobs and their commands
| Job | Bash | PowerShell |
|---|---|---|
| Make a folder | mkdir name | mkdir name or New-Item name -ItemType Directory |
| Make an empty file | touch name | New-Item name -ItemType File |
| Copy | cp source destination | Copy-Item source destination (alias cp) |
| Move or rename | mv source destination | Move-Item source destination (alias mv) |
| Delete | rm name | Remove-Item name (alias rm on Windows) |
Notice the pattern: copy, move, and delete name what to act on first, and where to put it second. "Copy this there. Move this there."
Windows PowerShell has no touch command. New-Item creates a file instead. Also, PowerShell aliases such as cp, mv, and rm exist on Windows but may not exist in PowerShell on Linux or macOS, so use the full Copy-Item, Move-Item, and Remove-Item names if an alias is missing.
Making folders: mkdir
$ mkdir sandbox
$ cd sandboxTo make several levels at once, Bash needs the -p ("parents") option:
$ mkdir -p garden/plants/herbsWithout -p, Bash refuses if garden/plants does not exist yet. PowerShell's mkdir creates missing parent folders by default, as far as I know, so mkdir garden/plants/herbs is enough there. If you are unsure, create the folders one level at a time. You can also create several sibling folders in one go by listing them: mkdir src docs.
Making files: touch and New-Item
$ touch notes.txt # Bash
PS> New-Item notes.txt -ItemType File # PowerShelltouch on a file that does not exist creates an empty one. On a file that exists it only updates its modification time and leaves the content alone. The file is empty until something writes into it. Lesson 4 shows how to write text into a file with >.
Looking inside a file: cat and Get-Content
$ cat notes.txt
PS> Get-Content notes.txtBoth print the file's text to the screen. cat also works as an alias in PowerShell. For a small file this is a quick check; do not use it on a huge or binary file, since you would flood the terminal.
Copying: cp and Copy-Item
$ cp notes.txt backup.txt # copy a file under a new name
$ cp notes.txt garden/ # copy into a folder, keeping the nameThe original stays where it was. To copy a folder and everything inside it, Bash needs -r ("recursive", meaning "include what is inside"):
$ cp -r garden garden-copy
PS> Copy-Item garden garden-copy -RecurseIf the destination file already exists, a copy may replace it without asking. Bash's cp -i asks first. PowerShell's behavior differs for existing files, so look at what is in the destination first.
Moving and renaming: mv and Move-Item
A move takes the original away from its old place and puts it in the new one. There is only one copy afterwards.
$ mv notes.txt garden/ # move into a folder
$ mv notes.txt ideas.txt # same folder, new name: a renameRenaming is moving to a new name. There is no separate rename command in Bash. If the destination is an existing folder, the file goes inside it. If it is a new name, the item is renamed. mv moves folders too, with no -r. PowerShell has Rename-Item as well, but Move-Item works for renames.
Deleting: rm and Remove-Item
$ rm ideas.txt
PS> Remove-Item ideas.txtDeletion from the terminal is permanent for practical purposes: the file does not go to the recycle bin or trash that you know from the desktop. I did not find that stated on Microsoft's documentation page for Remove-Item, so treat it as the working rule. Assume that nothing you delete here can be brought back.
Folders need extra words:
rmdir nameremoves an empty folder only. It is the safest tool.rm -r namein Bash deletes a folder and everything inside it.Remove-Item name -Recurseis the PowerShell equivalent. Without-Recurse, PowerShell asks you to confirm when the folder has contents. The Microsoft page notes this prompt cannot be silenced with-Confirm:$false.
Wildcards make deletion more dangerous. * means "anything". rm *.log removes every file ending in .log in the current folder. If you are in the wrong folder, you delete the wrong files. Never type rm -r * while unsure of your location.
The safe-delete routine
Before you delete anything, do these three things:
pwd. Where am I?ls(orGet-ChildItem). What is here? Is the thing I want to delete listed, and is nothing else about to go with it?- Read the command out loud before pressing Enter.
PowerShell offers a preview: add -WhatIf and the command shows what it would do without doing it. Bash's closest help is rm -i, which asks before each deletion. Use them while you are learning.
PS> Remove-Item garden -Recurse -WhatIfQuoting reminder
Names with spaces still need quotes: rm "old notes.txt". Forgetting them can make the shell treat two words as two files, which is a classic way to delete the wrong thing.
Examples
Example 1: Build a small tree (simple)
$ mkdir sandbox
$ cd sandbox
$ mkdir -p garden/plants
$ touch garden/plants/rose.txt
$ ls garden/plants
rose.txtYou created three folders, sandbox, garden, and plants, plus one empty file. On PowerShell, replace touch with New-Item garden/plants/rose.txt -ItemType File, and mkdir -p with plain mkdir.
Example 2: Copy, move, and rename (practical)
Continuing in sandbox:
$ cp garden/plants/rose.txt garden/rose-copy.txt
$ mv garden/plants/rose.txt garden/plants/tulip.txt
$ ls garden garden/plants
garden:
plants rose-copy.txt
garden/plants:
tulip.txtThe first command copied rose.txt one level up under a new name. The second renamed the original in place. At the end there is one tulip.txt and one rose-copy.txt. Notice that the file name in the second command's destination is a name, not a folder, so the item was renamed.
Example 3: Delete after a check (realistic)
You want to remove the whole garden folder at the end of the exercise.
$ pwd
/home/ana/sandbox
$ ls
garden
$ rm -r garden
$ lsThe pwd proved you were inside sandbox, not your home folder. The ls showed only the folder you meant to delete. Then rm -r garden removed it and everything in it, and the final ls printed nothing. On PowerShell the same job is Remove-Item garden -Recurse; add -WhatIf first on any delete you are unsure about.
Backend Insight
Backend engineers create, move, and delete files from the terminal constantly: setting up a project folder, copying a configuration file, rotating or cleaning old log files, replacing a deployed program with a new version. On a server there is no recycle bin, no undo, and often no second person watching, so the check-before-delete habit is a professional one.
You will also see scripts that do these steps automatically. A script is just a list of commands, so a typo in a path that was harmless on your laptop can delete the wrong folder on a server. Careful engineers print the target path first, test with a harmless dummy folder, and keep backups of anything important.
Common Mistakes
Mistake 1: Deleting without checking the current folder
rm -r * in your home folder instead of in sandbox is a disaster. Always run pwd and ls first.
Mistake 2: Forgetting -r or -Recurse for folders
cp garden garden-copy refuses because garden is a folder. Add -r (Bash) or -Recurse (PowerShell). mv is the exception: it moves folders without it.
Mistake 3: Copying onto an existing file
Copying a.txt onto b.txt may overwrite b.txt silently. ls first, or use cp -i.
Mistake 4: Mixing syntaxes
touch does not exist in Windows PowerShell, and New-Item does not exist in Bash. mkdir -p is Bash only. If the error says "not recognized" or "command not found", check which shell you are in.
Exercises
Try each exercise yourself before opening the solutions. Do all of them inside a new practice folder.
Level A: Foundation
A1. Which command creates an empty file in Bash? Which in PowerShell?
Show solution A1
A1. Bash: touch name. PowerShell: New-Item name -ItemType File.
A2. What is the difference between cp and mv?
Show solution A2
A2. cp leaves the original in place and creates a second copy. mv takes the original away and puts it in the new place, or gives it a new name if the destination is a new name in the same folder.
A3. Why should you run pwd before rm -r?
Show solution A3
A3. rm -r deletes a folder and its contents with no recycle bin, so you need to be sure which folder you are in and that the name you give points at the right one.
Level B: Application
B1. In sandbox, create the folders pets/cats and pets/dogs, then create a file tom.txt in cats.
Show solution B1
B1. mkdir -p pets/cats pets/dogs (PowerShell: mkdir pets/cats and mkdir pets/dogs), then touch pets/cats/tom.txt (PowerShell: New-Item pets/cats/tom.txt -ItemType File).
B2. Copy tom.txt into dogs, then rename the copy in dogs to rex.txt. Show the commands and the final listing of both folders.
Show solution B2
B2. cp pets/cats/tom.txt pets/dogs/ then mv pets/dogs/tom.txt pets/dogs/rex.txt. Listing: pets/cats has tom.txt, pets/dogs has rex.txt. PowerShell: Copy-Item pets/cats/tom.txt pets/dogs/ and Move-Item pets/dogs/tom.txt pets/dogs/rex.txt.
B3. Write the command that moves the whole pets folder into a new folder named archive, and say what archive must be like for the command to work as intended.
Show solution B3
B3. mv pets archive/. For this to work as intended, archive must already exist as a folder. If it does not exist, mv pets archive would rename pets to archive instead of moving it inside, so create it first with mkdir archive.
Level C: Challenge
C1. You type cp pets pets-backup and get an error. Explain why and fix it for Bash and PowerShell.
Show solution C1
C1. pets is a folder, and a plain copy handles files only. Bash: cp -r pets pets-backup. PowerShell: Copy-Item pets pets-backup -Recurse.
C2. A friend runs rm -r photos 2024 meaning to delete a single folder named photos 2024. What does the shell actually do, and how should the command be written? Why is this worse than a typical typo?
Show solution C2
C2. The shell splits on the space and sees two arguments: photos and 2024. It tries to delete a folder named photos and one named 2024, which may be different folders that you did want to keep. The correct command is rm -r "photos 2024". It is worse than a typo because the command is valid and runs successfully, so nothing warns you.
Mini Challenge
In a practice folder, build this tree using only the terminal, and make it in one sitting without using the mouse:
trip
├── tickets
│ └── flight.txt
└── photosThen copy flight.txt into photos with the name flight-backup.txt, rename the original to outbound.txt, and finally delete the whole trip folder.
Check your work: you ran pwd and ls immediately before the delete, and your history lists the commands in order. Run history to see them.
Summary & Next Lesson
Key takeaways
- Folders:
mkdir(-pin Bash for nested ones). Files:touch(Bash) orNew-Item(PowerShell). cpcopies,mvmoves and renames, andrmdeletes. Folders need-rforcpandrm(-Recursein PowerShell).- Source first, destination second.
- Terminal deletion should be treated as permanent. Check
pwdandlsfirst, and use-WhatIforrm -iwhile you learn. - Quote names with spaces.
Next lesson
You can now build and clean a folder tree. The next lesson, "Pipes and redirection", shows how to save a command's output to a file and how to send it into another command.
Self-check
- What does
mv a.txt b.txtdo whenb.txtdoes not exist? - What option copies a whole folder in Bash?
- Why is
rmriskier than deleting in a file manager? - What does
Remove-Item folder -WhatIfdo? - What two checks should come before a delete?
Show answers
- It renames
a.txttob.txt. -r, as incp -r folder copy.- It does not use the recycle bin and does not ask for confirmation, so the file is effectively gone.
- It shows what would be deleted without deleting anything.
pwdto see the current folder, andlsto see what is in it.
References
- Microsoft Learn, Remove-Item. Supports: the
-Recurseand-WhatIfparameters, the confirmation prompt for a folder with contents when-Recurseis missing (which-Confirm:$falsedoes not suppress), and that the aliasesrmandrmdirexist on Windows only whiledel,erase,rd, andriexist on all platforms. - man7.org, bash(1) manual page. Used for general shell background only.
- Not verified on an official page: the behavior of
mkdir -p,cp -r,cp -i,rm -i,mvrenaming,touchupdating a modification time, PowerShell'smkdircreating parents, and the claim thatRemove-Itemandrmbypass the recycle bin. These are standard, widely documented behaviors that I did not check on a page in this session, and the text marks the recycle-bin and PowerShell parent-folder points as hedged.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.