File Management

By the end of this lesson you'll create files and folders, copy and move them, rename and delete them safely, read their contents, and act on dozens of files at once with wildcards — the everyday muscle-memory of working in a terminal.

Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.

Part of the free Cli course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

1️⃣ Creating Files and Directories

Every project starts as empty files and folders. touch makes an empty file (or, if it already exists, just updates its "last modified" time). mkdir makes a directory. The flag you'll use constantly is -p : it creates every missing parent folder in a path, so one command can build a deep structure. Read this worked example, run it, then check the output matches.

2️⃣ Reading File Contents

Before you copy or delete something, you usually want to look inside it. cat dumps a file's entire contents to the screen — perfect for short files. For long files (like logs) that would scroll off the top, use less : it opens a scrollable viewer you move through with the arrow keys, and you quit by pressing q .

Fill in the three blanks marked ___ using the hints in the comments, then run it and check your output against the expected lines.

3️⃣ Copy, Move, Rename, and Delete

These four jobs are the core of file management. cp duplicates (original stays); add -r to copy a directory. mv does double duty — give it a new name in the same folder and it's a rename ; give it a different folder and it's a move . rm deletes, and there is no undo. rmdir removes empty folders only — a built-in safety check.

Unlike dragging to the trash, rm erases files with no recovery . The combination rm -rf is the most dangerous command in this lesson: -r means "recurse into directories" and -f means "force, never ask and never complain". Together they will silently wipe out an entire folder tree. Never run rm -rf / , rm -rf ~ , or rm -rf * casually, and beware a stray space — rm -rf / tmp/junk tries to delete your whole system instead of /tmp/junk . The habit that keeps you safe: run the command as ls first to see exactly what matches, and reach for rm -i (interactive) when deleting anything you care about.

4️⃣ Wildcards: Acting on Many Files at Once

A wildcard (or "glob") is a pattern the shell expands into a list of matching filenames before the command even runs. * matches any run of characters, ? matches exactly one character, and [abc] matches one character from the set. This is how you copy or delete dozens of files in a single line — and exactly why previewing with ls first is so important.

Preview every glob with ls before you destroy anything. The shell expands *.tmp the same way for ls and rm , so ls *.tmp shows you the exact list rm *.tmp would delete. Get into the habit of running the ls version, reading the list, and only then swapping in rm or mv . This one reflex prevents the most common "oh no" moment for beginners.

Fill in each ___ with the right glob, then run it and compare your result to the expected output.

No blanks this time — just a brief and an outline. Work through the steps in your terminal, then check your folder against the expected ls output in the comments. This is exactly the kind of setup-and-cleanup you'll do at the start of real projects.

Practice quiz

Which command creates an empty file?

  • mkdir
  • touch
  • cp
  • rm

Answer: touch. touch creates an empty file (or updates a file's timestamp).

Which mkdir flag creates missing parent folders in one go?

  • -p
  • -r
  • -a
  • -f

Answer: -p. mkdir -p builds the whole nested path, parents included.

What does the cp command do?

  • Moves a file
  • Deletes a file
  • Copies a file (original stays)
  • Renames a file

Answer: Copies a file (original stays). cp copies; the original stays in place.

To copy a whole directory with cp you must add which flag?

  • -i
  • -p
  • -a
  • -r

Answer: -r. cp -r (recursive) is required to copy a directory and its contents.

What does mv do when the destination is a new name in the same folder?

  • Renames the file
  • Copies the file
  • Deletes the file
  • Creates a folder

Answer: Renames the file. Same folder plus a new name is a rename.

After running rm on a file, where does it go?

  • The trash
  • The recycle bin
  • A backup folder
  • Nowhere - it is permanently deleted

Answer: Nowhere - it is permanently deleted. rm deletes immediately and permanently; there is no trash.

Which command removes only an empty directory?

  • rm -r
  • rmdir
  • mv
  • touch

Answer: rmdir. rmdir only removes an empty directory - a built-in safety check.

In a wildcard pattern, what does * match?

  • Exactly one character
  • Only digits
  • Any run of characters (zero or more)
  • Only letters

Answer: Any run of characters (zero or more). * matches any run of zero or more characters.

In a wildcard pattern, what does ? match?

  • Exactly one character
  • Zero or more characters
  • A whole word
  • A directory

Answer: Exactly one character. ? matches exactly one character.

Before deleting files with a glob like rm *.tmp, what is the safe habit?

  • Add the -f flag
  • Preview with ls *.tmp first
  • Run it twice
  • Reboot the machine

Answer: Preview with ls *.tmp first. ls expands the glob the same way, so preview with ls before rm.