Skip to main content
Back to module
Git & the Command Line · The Command Line

Creating and moving files

Reading33mLesson 2 of 3 in this module

What you'll learn

  • Create, copy, move and delete files safely
  • View files with cat, less, head and tail
  • Preview wildcard matches with ls before acting on them
  • Check the path before rm, because the terminal has no recycle bin

Create, view, copy, move, rename and delete files and folders from the terminal without losing anything by accident.

Why it matters

Setting up a project, organising data files, cleaning up build output, moving logs on a server: all of it is file management, and the terminal does it faster and more repeatably than clicking. It is also less forgiving. The terminal has no recycle bin: a deleted file is gone. This lesson covers the commands and the habits that keep you safe.

Safety rule for this course: practise only inside ~/cli-practice. Never use sudo for these commands, and never aim rm at your home folder or /.

Making folders and files

$ cd ~/cli-practice
$ mkdir project                  # one folder
$ mkdir -p project/data/raw      # -p creates the parent folders too
$ touch project/notes.txt        # empty file (or updates its timestamp)
$ echo "first line" > project/todo.txt   # file with content (more on > next lesson)

Looking at files

$ cat project/todo.txt     # print the whole file
first line
$ less bigfile.txt         # page through: Space = next page, q = quit
$ head -n 5 data.csv       # first 5 lines
$ tail -n 5 app.log        # last 5 lines

cat is fine for short files; use less or head for long ones.

Copying: cp

$ cp project/todo.txt project/todo-backup.txt    # copy a file
$ cp project/todo.txt project/data/              # copy into a folder (keeps the name)
$ cp -r project project-copy                     # -r copies a folder and everything in it

Moving and renaming: mv

mv does both. Moving to a new name in the same folder is renaming:

$ mv project/notes.txt project/meeting-notes.txt   # rename
$ mv project/todo-backup.txt project/data/          # move into a folder

Careful: if the destination file already exists, cp and mv overwrite it without asking. Add -i (interactive) to be asked first: mv -i a.txt b.txt.

Deleting: rm and rmdir

$ rm project/data/todo-backup.txt   # delete a file, permanently
$ rmdir project/data/raw            # delete an EMPTY folder
$ rm -r project-copy                # delete a folder and everything inside
$ rm -i *.tmp                       # ask before each deletion

rm -r is the most dangerous everyday command. Before running it:

  1. Run pwd to confirm where you are.
  2. Run ls with the same path or pattern first (ls project-copy) to see exactly what would go.
  3. Prefer specific paths over wildcards; use -i when unsure.

You will see rm -rf (force, no questions) in tutorials. Avoid it while learning.

Wildcards

The shell expands patterns before the command runs:

PatternMatches
*.csvevery name ending in .csv
report-*names starting with report-
data?.txtdata1.txt, dataA.txt (exactly one character)
$ ls *.csv          # preview first
$ mv *.csv data/    # then act

Worked example: organise a messy folder

$ cd ~/cli-practice
$ mkdir -p tidy && cd tidy
$ touch jan.csv feb.csv mar.csv notes.txt draft.tmp old.tmp
$ mkdir data
$ ls *.csv
feb.csv  jan.csv  mar.csv
$ mv *.csv data/
$ ls *.tmp
draft.tmp  old.tmp
$ rm -i *.tmp
remove draft.tmp? y
remove old.tmp? y
$ ls -R
.:
data  notes.txt

./data:
feb.csv  jan.csv  mar.csv

&& runs the second command only if the first succeeds, so cd tidy only happens if mkdir worked. ls -R lists folders recursively.

Common mistakes

  • rm in the wrong folder. Check pwd first.
  • A space in a path: rm my file.txt tries to delete two files, my and file.txt. Quote it: rm "my file.txt".
  • Overwriting with mv/cp by accident. Use -i.
  • Copying a folder without -r (you get "is a directory" or "omitting directory").

Quick recap

  • mkdir -p, touch and echo ... > create; cat, less, head, tail view.
  • cp copies (-r for folders); mv moves and renames.
  • rm deletes permanently; rm -r deletes folders. Preview with ls, check pwd, use -i.
  • Wildcards (*, ?) select many files: always preview them first.

Try it

Open the Do tab to build and reorganise a small project folder.

Key terms (5)

mkdir
The command that makes a folder; -p also creates any missing parent folders.
cp
The command that copies files; -r copies a folder and everything inside it.
mv
The command that moves a file or folder, or renames it when the destination is a new name.
rm
The command that deletes files permanently; -r deletes folders, and -i asks before each deletion.
wildcards
Patterns such as *.csv or data?.txt that the shell expands to matching file names before the command runs.

Recap

  • mkdir -p, touch and echo ... > create; cat, less, head and tail view
  • cp copies (-r for folders); mv moves and renames, and both overwrite without asking unless you add -i
  • rm deletes permanently and rm -r deletes folders: check pwd, preview with ls, use -i
  • Wildcards (* and ?) select many files, so always preview them first
  • Quote paths that contain spaces
Also in this lesson: