Skip to content
Ameen Abozaid
PublishedBEG-006-L03

Working with files and folders

  • Lesson 3 of 8
  • 10 min read
  • Beginner
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 "pwd and ls before 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

JobBashPowerShell
Make a foldermkdir namemkdir name or New-Item name -ItemType Directory
Make an empty filetouch nameNew-Item name -ItemType File
Copycp source destinationCopy-Item source destination (alias cp)
Move or renamemv source destinationMove-Item source destination (alias mv)
Deleterm nameRemove-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

text
$ mkdir sandbox
$ cd sandbox

To make several levels at once, Bash needs the -p ("parents") option:

text
$ mkdir -p garden/plants/herbs

Without -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

text
$ touch notes.txt            # Bash
PS> New-Item notes.txt -ItemType File     # PowerShell

touch 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

text
$ cat notes.txt
PS> Get-Content notes.txt

Both 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

text
$ cp notes.txt backup.txt         # copy a file under a new name
$ cp notes.txt garden/            # copy into a folder, keeping the name

The original stays where it was. To copy a folder and everything inside it, Bash needs -r ("recursive", meaning "include what is inside"):

text
$ cp -r garden garden-copy
PS> Copy-Item garden garden-copy -Recurse

If 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.

text
$ mv notes.txt garden/            # move into a folder
$ mv notes.txt ideas.txt          # same folder, new name: a rename

Renaming 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

text
$ rm ideas.txt
PS> Remove-Item ideas.txt

Deletion 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 name removes an empty folder only. It is the safest tool.
  • rm -r name in Bash deletes a folder and everything inside it.
  • Remove-Item name -Recurse is 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:

  1. pwd. Where am I?
  2. ls (or Get-ChildItem). What is here? Is the thing I want to delete listed, and is nothing else about to go with it?
  3. 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.

text
PS> Remove-Item garden -Recurse -WhatIf

Quoting 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)

text
$ mkdir sandbox
$ cd sandbox
$ mkdir -p garden/plants
$ touch garden/plants/rose.txt
$ ls garden/plants
rose.txt

You 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:

text
$ 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.txt

The 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.

text
$ pwd
/home/ana/sandbox
$ ls
garden
$ rm -r garden
$ ls

The 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:

text
trip
├── tickets
│   └── flight.txt
└── photos

Then 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 (-p in Bash for nested ones). Files: touch (Bash) or New-Item (PowerShell).
  • cp copies, mv moves and renames, and rm deletes. Folders need -r for cp and rm (-Recurse in PowerShell).
  • Source first, destination second.
  • Terminal deletion should be treated as permanent. Check pwd and ls first, and use -WhatIf or rm -i while 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

  1. What does mv a.txt b.txt do when b.txt does not exist?
  2. What option copies a whole folder in Bash?
  3. Why is rm riskier than deleting in a file manager?
  4. What does Remove-Item folder -WhatIf do?
  5. What two checks should come before a delete?
Show answers
  1. It renames a.txt to b.txt.
  2. -r, as in cp -r folder copy.
  3. It does not use the recycle bin and does not ask for confirmation, so the file is effectively gone.
  4. It shows what would be deleted without deleting anything.
  5. pwd to see the current folder, and ls to see what is in it.

References

  • Microsoft Learn, Remove-Item. Supports: the -Recurse and -WhatIf parameters, the confirmation prompt for a folder with contents when -Recurse is missing (which -Confirm:$false does not suppress), and that the aliases rm and rmdir exist on Windows only while del, erase, rd, and ri exist 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, mv renaming, touch updating a modification time, PowerShell's mkdir creating parents, and the claim that Remove-Item and rm bypass 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.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.