Creating, Copying, Moving, and Deleting Files

Learn the everyday commands for managing files and folders: touch, mkdir, cp, mv and rm, plus wildcards for working with many files at once.

~9 min Beginner

Before you start

Make a practice folder so nothing important is at risk, and move into it:

mkdir ~/practice
cd ~/practice

mkdir is explained below. ~/practice is a path that starts in your home directory. All the commands in this lesson take arguments that are file or folder names, written after the command and separated by spaces.

Creating files: touch

touch notes.txt
touch a.txt b.txt c.txt

touch creates an empty file if the name does not exist yet. You can give it several names at once. If the file already exists, touch leaves the contents alone and only updates the "last modified" time.

Creating folders: mkdir

mkdir projects
mkdir -p projects/web/css

mkdir makes a directory. The -p option means "create the parent folders too". Without it, mkdir projects/web/css fails if projects/web does not exist yet. With -p all the missing levels are created in one go.

Copying: cp

cp copies a file. The pattern is always cp source destination.

cp notes.txt backup.txt
cp notes.txt projects/
cp -r projects projects-backup
  • The first command copies notes.txt to a new file called backup.txt.
  • The second copies notes.txt into the folder projects. A destination that ends in / is treated as a folder.
  • The third copies a whole folder. The -r option means recursive: copy the folder and everything inside it. Without -r, cp refuses to copy a folder.

After a copy, the original is still there. If the destination file already exists, cp replaces it without asking. Add -i to be asked first.

Moving and renaming: mv

mv notes.txt ideas.txt
mv ideas.txt projects/
mv projects/ideas.txt .
  • The first command renames notes.txt to ideas.txt. Linux has no separate rename command: moving a file to a new name in the same place is renaming.
  • The second moves ideas.txt into the projects folder.
  • The third moves it back to the current folder. A single dot . means "here".

Unlike cp, mv does not leave the original behind. It also works on folders without -r. Like cp, it overwrites an existing destination without asking unless you add -i.

Deleting: rm and rmdir

rm ideas.txt
rm -i important.txt
rm -r old-project
rm -rf old-project
rmdir empty-folder
  • rm deletes a file.
  • -i asks for confirmation before each deletion.
  • -r deletes a folder and everything inside it.
  • -f means force: never ask, and ignore files that do not exist. Combined as -rf it removes a whole folder silently.
  • rmdir deletes a folder only if it is empty, which makes it safer.
There is no Recycle Bin on the command line. Files removed with rm are gone for good. Be especially careful with rm -rf, and never run a delete command you copied from the internet without understanding it.

Wildcards: matching many files

A wildcard is a special character that stands for other characters in a file name. The shell expands the pattern into the list of matching names before it runs the command.

  • * matches any number of characters, including none. *.txt means every file ending in .txt.
  • ? matches exactly one character. file?.txt matches file1.txt and file2.txt but not file10.txt.
  • [abc] matches one character from the list. report[123].pdf matches report1.pdf, report2.pdf and report3.pdf.
ls *.txt
cp *.jpg photos/
mv report-?.pdf archive/
rm *.tmp

The safe way to use wildcards with rm

Before you delete with a pattern, run ls with the exact same pattern. You will see exactly what the shell is about to pass to rm.

ls *.tmp
rm *.tmp

If the list from ls is not what you expected, change the pattern before you delete anything.

Brace expansion and a full example

Curly braces let you write several names at once. {docs,images} expands into two separate words.

mkdir -p ~/demo/{docs,images}
cd ~/demo
touch docs/readme.txt images/logo.png
cp docs/readme.txt docs/readme-copy.txt
mv docs/readme-copy.txt docs/readme-old.txt
ls -R

ls -R lists the current folder and all folders inside it, so you can check the result.

Quick reference

  • touch: create an empty file
  • mkdir: create a folder (-p for nested folders)
  • cp: copy (-r for folders, -i to confirm overwrites)
  • mv: move or rename
  • rm: delete (-r for folders, -i to confirm, -f to force)
  • rmdir: delete an empty folder
Sign up to track your progress Mark lessons as read and pick up where you left off Sign up free to unlock this practice Your first practice lesson is free to try. Create an account to unlock the rest and save your progress