Linux Labs

First steps / Stage 7 of 15

Creating and organising files

Not running Starting usually ~20 s
Leave the lesson

Step 2 of 10 · theory · 3 min

A directory and an empty file

Create the directory in advance, before moving files, so they have somewhere to land.

mkdir creates a directory. Its parent must already exist: mkdir reports/2026 fails if there is no reports. With -p the command creates the whole chain and quietly skips directories that already exist. You can give several names: each directory appears.

bash
mkdir archive              # one directory
mkdir docs logs            # two directories in one command
mkdir -p reports/2026/q3   # the whole chain at once
ls -l                      # a directory's line starts with d

touch creates an empty file when no file has that name. When the file exists, touch only sets its modification time to now. A file with text in it comes from the > redirection of the paths lesson.

bash
touch notes.txt                # no file yet — an empty one appears
ls -l notes.txt                # size 0, time is now
echo '# Project' > README.md   # a file with one line of text
Check yourself 1 question

notes.txt holds three lines of notes. You run touch notes.txt and then cat notes.txt. What do you see?

  • For an existing file, touch changes only the modification time. Compare ls -l notes.txt before and after: the size is the same.

  • A directory cannot hold two files with the same name: ls -l shows one notes.txt.

  • An existing file is the ordinary case for touch: it silently updates the time. Run touch notes.txt twice in a row — there is no message.

touch leaves an existing file's contents alone and only sets its modification time to now. Check ls -l notes.txt before and after: the time changes, the size does not.

Key points

  • mkdir -p creates the missing directories of a chain and does not complain about existing ones.
  • touch creates an empty file, and for an existing file changes only the modification time.
Ubuntu 24.04
Loading…