17 17. Creating, Copying, Moving, and Deleting
So far you have only looked around. This page covers the commands that change things: mkdir and touch create, cp copies, mv moves and renames, and rmdir and rm delete. You will practice in a new folder called playground inside the practice files, and delete it at the end.
As on the previous page, the outputs come from Maria’s Mac (with her practice files in ~/Documents/bash-tutorial-files). Where Git Bash words an error message differently, both versions are shown.
17.1 Set up a playground
cd ~/Documents/bash-tutorial-files
mkdir playground
cd playgroundExperimenting in a throwaway folder is a good habit: if something goes wrong, nothing important is affected.
17.2 Creating folders: mkdir
mkdir (make directory) creates one or more folders. With the -p option (parents), it also creates any missing folders along the path:
mkdir reports
mkdir -p archive/2026/october
ls -FOutput:
archive/ reports/
ls archive/2026Output:
october
Without -p, mkdir refuses to create a folder inside a folder that doesn’t exist yet. It also refuses to create a folder that already exists:
$ mkdir backups/2026
mkdir: backups: No such file or directory
$ mkdir reports
mkdir: reports: File exists
Git Bash words these as mkdir: cannot create directory followed by the name and the same reason. mkdir -p never complains about a folder that already exists.
17.3 Creating empty files: touch
touch creates an empty file - or several at once. If a file already exists, touch leaves its contents alone and only updates its “last modified” time.
touch todo.txt notes.txt
lsOutput:
archive notes.txt reports todo.txt
Usually you create files with an editor or a program; touch is just a quick way to make an empty one. To put text into a file from the command line, see Writing to Files: echo, >, >>, and cat.
17.4 Copying: cp
cp source destination copies a file. The destination can be a new name, or an existing folder (then the copy keeps its name):
cp todo.txt todo-backup.txt
cp ../documents/notes/ideas.txt .
lsOutput:
archive notes.txt todo-backup.txt
ideas.txt reports todo.txt
The first command makes a copy with a new name. The second copies ideas.txt from the practice files into . - the current folder.
To copy a folder and everything in it, add -r (recursive: “and everything inside, all the way down”):
cp -r ../projects/website website-copy
ls website-copyOutput:
index.html styles.css
Without -r, cp refuses to copy a folder:
$ cp ../projects/website website-copy
cp: ../projects/website is a directory (not copied).
(Git Bash: cp: -r not specified; omitting directory '../projects/website'.)
cp and mv overwrite without asking
If a file with the destination name already exists, cp and mv replace it silently - its old contents are gone. Add -i (interactive) to be asked first, for example cp -i todo.txt notes.txt. Answer y to overwrite or n to keep the existing file.
17.5 Moving and renaming: mv
mv (move) does two jobs, because moving and renaming are really the same operation: giving a file a new path (a new folder, a new name, or both). Unlike cp, it moves folders without needing -r.
mv todo-backup.txt old-todo.txt
mv old-todo.txt archive
mv notes.txt archive/2026/october/october-notes.txt
mv website-copy archive
ls -FOutput:
archive/ ideas.txt reports/ todo.txt
ls -F archiveOutput:
2026/ old-todo.txt website-copy/
Line by line, the four mv commands:
- rename
todo-backup.txttoold-todo.txt(same folder, new name); - move
old-todo.txtinto the existing folderarchive; - move and rename
notes.txtin one step: it ends up inarchive/2026/octoberasoctober-notes.txt; - move a whole folder,
website-copy, intoarchive.
The rule for both cp and mv: if the last argument is an existing folder, the item goes inside it and keeps its name; otherwise the last argument is the new name.
17.6 Deleting: rmdir and rm
17.6.1 rmdir: remove an empty folder
rmdir reportsThis works because reports is empty. rmdir refuses to delete a folder that contains anything - a built-in safety check:
$ rmdir archive
rmdir: archive: Directory not empty
(Git Bash: rmdir: failed to remove 'archive': Directory not empty.)
17.6.2 rm: remove files
rm ideas.txtrm (remove) deletes files immediately. Add -i to be asked first:
$ rm -i todo.txt
remove todo.txt? y
Type y and press Enter to delete, or n to keep the file. (Git Bash asks rm: remove regular empty file 'todo.txt'?.)
17.6.3 rm -r: remove a folder and everything in it
On its own, rm refuses to delete a folder:
$ rm archive
rm: archive: is a directory
(Git Bash: rm: cannot remove 'archive': Is a directory.) With -r, it deletes the folder and every file and folder inside it, all the way down:
rm -r archive
lsThis time ls prints nothing: the playground is empty.
rm is forever
- There is no Trash or Recycle Bin. Files deleted with
rmdo not go to the Mac Trash or the Windows Recycle Bin. There is no undo. -fmeans “force”: never ask, and stay quiet about missing files. You will often seerm -rfin instructions and AI answers. It deletes a folder and everything in it without a single question, so use it only when you are sure.- One typo can be a disaster.
rm -rf ~ /old-stuff(do not type this) has a stray space after~, so it means “delete my entire home folder, and also/old-stuff”. Never run anrm -rfyou don’t fully understand - whether it comes from a website, a classmate, or an AI. - Habits that help: run
pwdandlsfirst; userm -iwhen in doubt; name exactly the files you mean; and keep your projects in Git (see What Is Version Control?), so that committed work can be recovered.
17.6.4 When an AI agent deletes files
As of Oct 6, 2026 · Official docs: Checkpointing · Choose a permission mode
Claude Code can run rm, mv, and cp too. Depending on its permission mode, it asks you first, lets an automatic safety check review the command, or - in the mode that auto-accepts edits - runs commands such as rm and mv inside your project without asking. Its checkpoints let you rewind changes it made with its own file-editing tools, but its documentation states that they do not undo file changes made by shell commands like these. Permission modes and Claude Code’s other safeguards are covered in Permissions, Modes, and Safety.
17.7 Names with spaces need quotes
Back in the (now empty) playground, try to create a folder called new folder:
$ mkdir new folder
$ ls -F
folder/ new/
$ mkdir new folder
$ ls -F
folder/ new/
Bash splits a command line at spaces, so mkdir received two names and created two folders. Remove them both and try again with quotes:
$ rmdir new folder
$ mkdir "new folder"
$ ls -F
new folder/
The quotes turn new folder into a single argument. You need them every time you use that name: cd "new folder", rmdir "new folder". (Git Bash displays the name as 'new folder'/; the quotes in the listing just show that it is one name.) Tab completion helps: it types a backslash before the space for you (new\ folder/), which works too. Quotes are explained fully in Writing to Files and Single Quotes vs. Double Quotes. The simplest fix is to avoid spaces in names you create.
Finally, clean up. The last rmdir works because the playground is empty again:
rmdir "new folder"
cd ..
rmdir playground17.8 Command summary
| Command | What it does |
|---|---|
mkdir name |
Create a folder. |
mkdir -p a/b/c |
Create nested folders; no error if they already exist. |
touch file |
Create an empty file (or update its “last modified” time). |
cp file new-name / cp file folder |
Copy a file to a new name / into a folder. |
cp -r folder new-folder |
Copy a folder and everything in it. |
mv old-name new-name |
Rename a file or folder. |
mv item folder |
Move a file or folder into a folder. |
rmdir folder |
Delete an empty folder. |
rm file |
Delete a file - permanently. |
rm -r folder |
Delete a folder and everything in it - permanently. |
-i (with cp, mv, rm) |
Ask before overwriting or deleting. |
Practice Questions
Unless a question says otherwise, Maria is in /Users/maria/Documents/bash-tutorial-files (the practice files; see the tree on Files, Folders, and Paths).
Easy
Question 1 · Multiple choice
Which command creates a new, empty folder named invoices?
touch invoicesmkdir invoicescp invoicesmv invoices
Question 2 · Short answer
What does touch report.txt do if report.txt does not exist? And if it already exists?
Question 3 · True or false
Files you delete with rm go to the Trash (Mac) or Recycle Bin (Windows), so you can get them back.
Question 4 · Matching
Match each command with its effect.
| Command | Effect | |
|---|---|---|
1. mkdir -p a/b/c |
A. delete a folder and everything inside it | |
2. cp -r site site-backup |
B. delete an empty folder | |
3. rm -r site |
C. create nested folders in one step | |
4. rmdir site |
D. ask before deleting a file | |
5. rm -i notes.txt |
E. copy a folder and everything inside it |
Question 5 · Multiple choice
Which command do you use to rename a file?
rncpmvrename-file
Question 6 · Short answer
What does the -r option stand for, and with which two commands on this page do you need it for folders?
Medium
Question 7 · Write the command
Write one command that creates the folders mobile-app and mobile-app/src inside projects (neither exists yet).
Question 8 · Write the command
Maria is in projects/data-analysis. Write the command that renames data.csv to sales-2026.csv.
Question 9 · Write the command
Write one command that copies pictures/family.jpg into the documents folder, keeping its name.
Question 10 · Write the command
Before experimenting, Maria wants a complete backup of the projects folder, called projects-backup. Write the command.
Question 11 · Predict the output
In an empty folder, Maria runs:
mkdir quarterly reports
lsWhat does ls show, and why? How should she have typed the command to get one folder named quarterly reports?
Question 12 · Predict the output
Maria runs rmdir documents. What happens, and why?
Question 13 · Fill in the blank
rm music fails with the message “______” because music is a folder. To delete it together with its contents you would type rm ______ music.
Question 14 · Compare
What is the difference between cp notes.txt archive and mv notes.txt archive (where archive is an existing folder)?
Hard
Question 15 · Scenario
Maria’s todo.txt contains her to-do list. She meant to make a backup but typed:
cp notes.txt todo.txtWhat happened to her to-do list? How could she have protected herself?
Question 16 · Find the bug
Sam wants to delete an empty folder named old reports and types:
rmdir old reportsWhat does Bash actually try to do? Describe a case where this could delete the wrong thing, and write the correct command.
Question 17 · Scenario
An AI chatbot tells you to “clean up” by running:
rm -rf old-siteExplain each part of the command. What would you check before running it?
Question 18 · Short answer
Explain why rm -rf ~ /old-stuff is so dangerous, even though the person who typed it only wanted to delete ~/old-stuff.
Question 19 · Ordering
Maria wants to keep a backup of documents/notes/ideas.txt in a new folder backup, then rename the original to ideas-v1.txt, and finally check the result. Put these commands in a working order:
ls backup documents/notesmv documents/notes/ideas.txt documents/notes/ideas-v1.txtmkdir backupcp documents/notes/ideas.txt backup
Question 20 · Compare
Both rmdir photos and rm -r photos can delete a folder. When is rmdir the safer choice?
Question 21 · Scenario
During a Claude Code session, Claude ran the shell command rm draft.txt. Later you realize you needed that file. Can Claude Code’s checkpoints (its rewind feature) bring it back? What could?