Git & Command Line Tutorial · Command Line Basics · Lesson 3 of 20

Creating, Copying, Moving and Deleting

Create files and folders, copy and move them, rename, and delete safely, including the commands that can't be undone.

Create a free account to save this page, take notes and track your progress. Sign in

The essential commands

Command Does
mkdir name / mkdir -p a/b/c Create a folder (with parents)
touch file.txt Create an empty file (or update its timestamp)
cp src dest / cp -r folder copy Copy a file / a folder
mv old new Move or rename
rm file / rm -r folder Delete (no recycle bin!)
rmdir folder Delete an empty folder

Careful: rm deletes permanently. rm -rf deletes folders without asking; double-check the path, especially with variables or *. Use rm -i to be asked before each deletion while you're learning.

Wildcards: *.txt matches every .txt file, report-?.pdf matches one character, {a,b} expands to both names.

mkdir, cp, mv, rm and wildcards
mkdir demo && cd demo        # start in an empty folder
mkdir -p site/css site/img
touch site/index.html site/css/style.css
echo "draft notes" > notes.txt

cp notes.txt notes-backup.txt          # copy a file
mv notes.txt site/about.txt            # move (and rename) it
cp -r site site-copy                   # copy a whole folder
touch site/img/{logo,banner,photo}.png # brace expansion makes three files

ls -F site site/img
rm site-copy/css/style.css
rmdir site-copy/css                    # works only on empty folders
rm -r site-copy                        # remove a folder and everything in it
ls -F

Output

site:
about.txt
css/
img/
index.html

site/img:
banner.png
logo.png
photo.png
notes-backup.txt
site/

Run this on your computer — see “Get Started” for setup.

Test yourself

How do you rename report.txt to final.txt?

  1. rename report.txt final.txt
  2. cp report.txt final.txt
  3. mv report.txt final.txt
  4. rn report.txt final.txt
Show answer

Answer: mv report.txt final.txt

Was this page helpful?