learn.colinkim.dev

Creating, moving, and removing files

Learn to create, copy, move, and delete files and directories safely — including recursive operations, glob patterns, and how to preview before doing something destructive.

Once you can navigate, the next step is changing things: creating files, organizing directories, and removing what you no longer need.

Create an empty file — touch

touch notes.txt

touch updates the timestamps on a file. If the file does not exist, it creates an empty one.

touch a.txt b.txt c.txt    # create multiple files at once

Create a directory — mkdir

mkdir my-folder
mkdir -p a/b/c             # create nested directories at once

The -p flag creates parent directories as needed. Without it, mkdir a/b/c fails if a/b does not already exist.

Copy files — cp

cp notes.txt notes-backup.txt
cp -r my-folder/ my-folder-backup/    # copy a directory recursively
  • -r (or -R) copies directories recursively. Required for directories.
  • -i prompts before overwriting an existing file.
cp -i source.txt dest.txt    # asks for confirmation if dest.txt exists

Move and rename — mv

mv notes.txt documents/notes.txt    # move
mv old-name.txt new-name.txt        # rename

mv both moves and renames. They are the same operation — the filesystem sees a path change.

Like cp, use -i to prompt before overwriting.

Remove files — rm

rm notes.txt
rm -r my-folder/          # remove a directory and everything inside it
rm -i important.txt       # prompt before removing
  • -r removes directories recursively.
  • -f forces removal without prompting, even if the file does not exist.
  • -i prompts before each removal.

Remove an empty directory — rmdir

rmdir empty-folder

rmdir only removes empty directories. If the directory has contents, it fails. Use rm -r instead for non-empty directories.

Glob patterns

Glob patterns let you match multiple files at once:

rm *.txt                 # remove all .txt files in the current directory
cp *.md backup/          # copy all markdown files
ls src/**/*.ts           # all .ts files in src/ and subdirectories (with globstar)

Common patterns:

  • * — matches any characters (except /)
  • *.txt — all files ending in .txt
  • *.* — all files with an extension
  • ?.txt — any single character followed by .txt (e.g. a.txt)

In bash/zsh, ** matches across directories when globstar is enabled:

shopt -s globstar        # bash: enable **
# zsh supports ** by default

Previewing before destructive actions

Before running a command that deletes or overwrites, preview what it would affect:

# Instead of this:
rm *.log

# Run this first:
ls *.log                 # see what would be removed
rm -i *.log              # then remove with confirmation prompts

Or use echo to preview a command:

echo rm *.log            # prints the expanded command without running it

A safe workflow example

mkdir project
cd project
touch README.txt
mkdir src
cp README.txt src/README-backup.txt
ls -la                   # verify everything looks right
mv src/README-backup.txt src/README.txt
rm README.txt            # remove the original

The main idea to carry forward

touch, mkdir, cp, mv, and rm are the basic file manipulation toolkit. Use -i when you want safety, -r when working with directories, and always preview with ls before running destructive commands. Glob patterns let you operate on many files at once.

Quick Check

One answer

Which command should you use when you want the original file to stay where it is and also create a copy somewhere else?

Choose the best answer and use it to track your progress through the lesson.

Progress

Quick checks

No quick checks in this lesson.

Mark lesson manually or answer quick checks to track progress.