Making files and folders in Git & Command Line
Topic 4 of 32 · 10 minFree lessonSo 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:
The new travel folder sits next to the others. If a folder with that name is already there, mkdir says so and changes nothing:
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:
The -p option makes every missing folder on the way, in one go:
-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:
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:
> makes the file if it isn't there. If it is, > replaces everything in it:
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:
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.
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.
mkdir road trips makes two folders: road and trips.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.