CodeStride
The Command Line

Making files and folders in Git & Command Line

Topic 4 of 32 · 10 minFree lesson

So far you've looked at files that were already there. Now you'll make your own. In the terminal you make folders and files with a few short commands, and write text into files without opening any program.

These commands print nothing when they work. That's normal: no news is good news. Use ls to check what you made.

Making a folder

mkdir, short for "make directory", makes a new folder with the name you give it:

Example 1 · Terminal
$ mkdir travel$ lsnotes photos projects recipes travel

The new travel folder sits next to the others. If a folder with that name is already there, mkdir says so and changes nothing:

Example 2 · Terminal
$ mkdir travelmkdir: cannot create directory 'travel': File exists

Folders inside folders

mkdir makes one folder, inside a folder that already exists. Ask for a folder two levels down, and it stops, because the folder in the middle isn't there yet:

Example 3 · Terminal
$ mkdir travel/2026/italymkdir: cannot create directory 'travel/2026/italy': No such file or directory

The -p option makes every missing folder on the way, in one go:

Example 4 · Terminal
$ mkdir -p travel/2026/italy$ ls travel2026$ ls travel/2026italy

-p never complains when a folder already exists, which makes it a safe choice.

Empty files

touch makes an empty file. You can name several files at once:

Example 5 · Terminal
$ touch travel/packing.txt travel/budget.txt$ ls -l traveltotal 4drwxr-xr-x 3 learner learner 4096 Mar 3 09:00 2026-rw-r--r-- 1 learner learner 0 Mar 3 09:00 budget.txt-rw-r--r-- 1 learner learner 0 Mar 3 09:00 packing.txt

Both files are 0 bytes, because nothing is in them yet. If a file already exists, touch leaves what's inside alone.

Writing text into a file

echo prints text on the screen. Add > and a file name, and the text goes into that file instead. The quotes keep the text together, and you'll learn more about them later.

cat shows what's inside a file. You'll meet it properly in the next topic:

Example 6 · Terminal
$ echo "Pack sunscreen" > travel/packing.txt$ cat travel/packing.txtPack sunscreen

> makes the file if it isn't there. If it is, > replaces everything in it:

Example 7 · Terminal
$ echo "Pack a hat" > travel/packing.txt$ cat travel/packing.txtPack a hat

The sunscreen line is gone. There's no warning and no undo, so take care with >.

Several lines at once

To write a few lines, use cat > file <<EOF. It reads every line you type next and puts them in the file, until you type a line that says EOF. The terminal shows > at the start of each line while it waits:

Example 8 · Terminal
$ cat > travel/packing.txt <<EOF> Sunscreen> A hat> Walking shoes> EOF$ cat travel/packing.txtSunscreenA hatWalking shoes

EOF stands for "end of file". It isn't written into the file: it only marks where your text stops.

Naming files

The part of a name after the last dot is its extension. It hints at what kind of file it is: .txt for plain text, .html for a web page, .jpg for a photo.

Example 9 · Terminal
$ touch travel/map.jpg travel/plan.html$ ls travel2026 budget.txt map.jpg packing.txt plan.html

The extension is only part of the name. map.jpg is still an empty file, not a picture, until something puts a photo in it.

Good file names are short and lowercase, with dashes or underscores instead of spaces, like road-trip.txt or road_trip.txt.

Common mistakePutting a space in a name. The shell splits arguments at spaces, so mkdir road trips makes two folders: road and trips.
Example 10 · Terminal
$ mkdir road trips$ lsnotes photos projects recipes road travel trips
TipMake a habit of running ls after you make something. These commands are silent when they work, so a quick look confirms the name came out the way you meant.

Try it yourself

Run the code in the editor and answer 8 practice questions on making files and folders. It's free, you only need an account.