Skip to lesson

Creating, copying and deleting files

I'm on:

Showing steps for Windows. They are marked “On Windows” below.

Five short commands make, show, copy, move and delete files: mkdir, cat, cp, mv and rm. They are faster than clicking when you work with many files, but they act at once and never ask twice, so read each one before you press Enter.

Make folders: mkdir

Every new project starts with folders. mkdir (make directory) creates a folder. Give it a path, such as marks-app/src, and it can create both folders in one go. Priya works in her college-projects folder.

On Windows

In PowerShell, mkdir always creates the missing folders on the way, and prints the folder it made:

Windows PowerShell
PS C:\Users\Priya\college-projects> mkdir marks-app\src    Directory: C:\Users\Priya\college-projects\marks-app


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        21-10-2026     10:15                src
  • PowerShell created marks-app first, then src inside it. The table shows the last folder made.
  • If the folder already exists, PowerShell says so in red and changes nothing.

Make a file and look inside: cat

You create a file from the terminal and type its text in an editor. Then cat (short for concatenate, "join") prints the file's text, so you can check it without opening the editor.

On Windows

New-Item creates an empty file, and notepad opens it in Notepad:

Windows PowerShell
PS C:\Users\Priya\college-projects> New-Item marks.txtPS C:\Users\Priya\college-projects> notepad marks.txtOutput    Directory: C:\Users\Priya\college-projects


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        21-10-2026     10:18              0 marks.txt
  • Length 0 means the file is empty. Notepad opens it: type Priya 86, Ravi 58 and Kabir 91 on three lines, press Ctrl+S and close Notepad.
  • touch, from Mac guides, is not recognized in PowerShell; New-Item does the same job.

Example 1 · Show the file

Windows PowerShell
PS C:\Users\Priya\college-projects> cat marks.txtPriya 86
Ravi 58
Kabir 91

What this code does

  • cat printed every line you typed in Notepad, so the file was saved correctly.
  • For a long file, show only the start or end: cat marks.txt -Head 2 prints the first 2 lines, cat marks.txt -Tail 1 the last line.

Copy, move and rename: cp and mv

Recursive means "this folder and everything inside it, at every level". Commands such as cp and rm need the -r option to work on a whole folder instead of a single file.

Before changing marks.txt, Priya keeps a backup. cp (copy) makes a second file and keeps the first; mv (move) moves a file, and a move to a new name is a rename.

Left: cp marks.txt backup.txt. Before, the folder has marks.txt; after, it has marks.txt and a new backup.txt. Right: mv marks.txt old-marks.txt. Before, the folder has marks.txt; after, it has only old-marks.txt. Copy keeps the original; move or rename does not.

Example 2 · A backup, then a rename

Windows PowerShell
PS C:\Users\Priya\college-projects> cp marks.txt marks-backup.txtPS C:\Users\Priya\college-projects> mv marks-backup.txt old-marks.txtPS C:\Users\Priya\college-projects> lsOutput    Directory: C:\Users\Priya\college-projects


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        21-10-2026     10:15                marks-app
-a----        21-10-2026     10:20             27 marks.txt
-a----        21-10-2026     10:20             27 old-marks.txt

What this code does

  • cp made marks-backup.txt with the same 27 bytes. mv then renamed it to old-marks.txt, so marks-backup.txt no longer exists.
  • cp and mv print nothing when they work; only ls printed here.

mv old-marks.txt marks-app moves the file into the marks-app folder. To copy a whole folder, add -r: cp -r marks-app marks-app-backup.

Both cp and mv replace a file with the same name without asking. Check with ls before you copy over something you need.

Delete: rm has no undo

rm (remove) deletes a file: rm old-marks.txt. Files deleted in the terminal do not go to the Recycle Bin or the Trash. They are gone at once.

Example 3 · Deleting a folder needs -r

Windows PowerShell
PS C:\Users\Priya\college-projects> rm marks-appConfirm
The item at C:\Users\Priya\college-projects\marks-app has children and the Recurse parameter was not specified. If
you continue, all children will be removed with the item. Are you sure you want to continue?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):

What this code does

  • marks-app is a folder with something inside ("children"), so PowerShell asks first. Type N and press Enter to keep it. Pressing just Enter means Yes.
  • When you are sure, rm -r marks-app deletes the folder and everything in it without asking. rm -rf, from Mac guides, is an error in PowerShell.

Before any rm -r, run pwd and ls to be sure where you are and what will go. Type the name exactly, and never run rm -r on ~ or on a folder you have not checked.

One extra space can be a disaster: rm -r ~ /college-projects gets two names, ~ (your whole home folder) and /college-projects. Read every delete command twice before pressing Enter.

Next step

Check your understanding

8 quick questions