Q1What happens when you run mkdir -p a/b/c?
Creating Files & Directories — mkdir, touch, cat
Make folders with mkdir, create files with touch and echo's > / >>, and check their contents with cat — all hands-on in a browser terminal.
Create directories — mkdir
mkdir creates a new directory (folder).
Just pass the name you want and it makes one.
Add -p and it creates a whole nested hierarchy at once, even when the parent directories don't exist yet.
mkdir logs # create logs
ls # check
mkdir -p src/app/utils # create 3 levels at once
ls -R src # list src recursively
mkdir -p creates project, project/src, and project/src/bin all at once, even without the parents.Remove an empty directory — rmdir
rmdir deletes an empty directory.
Because it only removes a directory once it's confirmed empty, it's a safe operation that makes accidentally deleting the files inside unlikely.
Deleting a directory along with its contents is covered in a later article.
mkdir empty_box # create an empty directory
rmdir empty_box # empty, so it can be removed
ls # confirm it's gone
rmdir only works when empty
rmdir deletes only empty directories.
If there are files or folders inside, it won't delete them and the directory stays.
Deleting a directory together with its contents is handled safely in a later article.
Create and write files — touch / echo
touch creates an empty file.
echo 'text' > filename writes text into a file, and cat shows its contents on screen.
Create, write, and view — you'll use these three together.
| Command | Effect |
|---|---|
mkdir name | Creates a single directory |
mkdir -p parent/child/grandchild | Creates the parents and all at once |
rmdir name | Deletes only empty directories |
touch name | Creates an empty file |
touch notes.txt # create an empty file
echo 'first line' > notes.txt # write
cat notes.txt # display
touch makes an empty file, > writes, and cat displays.Overwrite and append — > and >>
> empties the file before writing (overwrite).
When you want to add lines while keeping the existing content, use >> (append).
Mix these two up and you can wipe out data you needed.
| Symbol | Meaning |
|---|---|
> | Empties the file and writes (overwrite) |
>> | Appends to the end (keeps existing content) |
echo 'line 1' > log.txt # overwrite (create new)
echo 'line 2' >> log.txt # append
cat log.txt # line 1 and line 2
echo 'reset' > log.txt # overwrite (previous content is gone)
cat log.txt # reset only
> clears and overwrites; >> appends to the end.> clears the contents
> empties the file's contents before writing.
When you want to add lines to existing content, always use >> (append).
Knowledge Check
Answer each question one by one.
Q2What can rmdir delete?
Q3Which symbol adds a new line at the end while keeping an existing file's contents?