Working with Files: touch, cp, mv, mkdir

beginner
12 min

Working with Files: touch, cp, mv, mkdir

Now that you can move around the filesystem, it is time to shape it. In this lesson you will create files and folders, copy and move them, rename things, and delete safely. These five commands—touch, mkdir, cp, mv and rm—are the daily bread of anyone working on a Linux system.

Creating Empty Files with touch

touch creates an empty file if it does not exist, or updates the modification time if it does:

bash
touch notes.txt touch index.html style.css app.js # create several at once ls -l notes.txt

Why would you want an empty file? Placeholders for a project skeleton, log files a script will append to, or simply a quick way to test other commands.

Creating Directories with mkdir

bash
mkdir projects # one directory mkdir docs images scripts # several at once mkdir -p site/assets/css # -p creates the whole nested path

The -p (parents) flag is the one to remember: it builds every missing level of a path in one go and never complains if the directory already exists. Without it, mkdir site/assets/css fails unless site/assets already exists.

Copying with cp

cp copies files and directories. The last argument is always the destination:

bash
cp notes.txt backup.txt # copy to a new name cp notes.txt ~/backups/ # copy into a directory, same name cp index.html style.css site/ # copy multiple files into a folder cp -r site site-backup # -r copies a directory recursively

Two flags will save you pain:

  • cp -i asks before overwriting an existing file (interactive).
  • cp -r is required for directories; without it cp refuses to copy a folder.

Moving and Renaming with mv

Linux does not have a separate rename command—mv does both jobs:

bash
mv draft.txt final.txt # rename a file mv final.txt ~/documents/ # move it elsewhere mv old-site/ archive/old-site/ # move a whole directory (no -r needed) mv -i report.txt done/ # ask before overwriting

Moving within the same disk is instant regardless of file size, because Linux only rewrites the directory entry, not the data itself.

Deleting with rm and rmdir — Carefully

bash
rm scratch.txt # delete a file rm -i *.log # confirm each deletion rmdir empty-folder # delete a directory only if it is empty rm -r old-project # delete a directory and everything inside

There is no trash can on the command line. rm deletes immediately and permanently. Build these habits from day one:

  1. Run ls on the target first so you see exactly what will match.
  2. Prefer rm -i while learning.
  3. Never run rm -rf on a path you have not double-checked, and be extremely careful with variables or wildcards in the path.

Wildcards Preview

The shell expands * to match any characters, which works with all of these commands:

bash
cp *.jpg images/ # copy every .jpg file mv report-*.txt done/ # move report-1.txt, report-2025.txt, ...

We cover patterns in depth in the searching lesson; for now, test any wildcard with ls before using it with rm.

Practice: Build a Project Skeleton

bash
mkdir -p myapp/{src,tests,docs} touch myapp/src/main.py myapp/README.md cp myapp/README.md myapp/docs/ mv myapp/docs/README.md myapp/docs/overview.md ls -R myapp

The {src,tests,docs} brace expansion creates three sibling folders in one command—a taste of how expressive the shell can be.

Key Takeaways

  • touch creates empty files; mkdir -p builds whole directory paths.
  • cp copies (-r for folders); mv both moves and renames, no -r needed.
  • rm is permanent—check with ls first and use -i while learning.
  • Wildcards like *.txt let one command act on many files.

Next lesson: Viewing Files: cat, less, head, tail — read file contents without opening an editor.