Learn by reading through in order

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
The nested structure mkdir -p builds
mkdir -p project/src/bin creates all at onceprojectproject/srcproject/src/bininside thisinside this
mkdir -p creates project, project/src, and project/src/bin all at once, even without the parents.

① Create a single directory named work as a working folder, and confirm it was created with ls.

② Next, create a nested three-level directory — src inside project, and bin inside that — all at once, then confirm the three levels exist with ls -R project. (Run it correctly and an explanation will appear.)

Linux console
0 / 4 completed
Loading Linux Terminal...

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 a throwaway directory named demo and confirm it exists with ls.

② Then delete the empty demo directory and confirm with ls that demo is gone.

Linux console
0 / 3 completed
Loading Linux Terminal...

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.

CommandEffect
mkdir nameCreates a single directory
mkdir -p parent/child/grandchildCreates the parents and all at once
rmdir nameDeletes only empty directories
touch nameCreates an empty file
touch notes.txt                 # create an empty file
echo 'first line' > notes.txt   # write
cat notes.txt                   # display
Create → write → display
touch hello.txtan empty file is createdecho ... > hello.txtHello, Linux! goes incat hello.txtshown on screen
touch makes an empty file, > writes, and cat displays.

① Create an empty file named hello.txt and confirm it exists with ls.

② Write the single line Hello, Linux! into that file, then display its contents with cat hello.txt to confirm.

Linux console
0 / 4 completed
Loading Linux Terminal...

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.

SymbolMeaning
>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
> overwrites, >> appends
echo A > ff contains: Aecho B > ff contains: B(A is gone)echo C >> ff contains: B / C> (overwrite)>> (append)
> 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).

① Write the single line first into note.txt.

② Then append the line second to the same note.txt, and confirm with cat that it now has two lines.

③ Finally, overwrite note.txt with reset, and confirm with cat that the contents change to just reset.

Linux console
0 / 4 completed
Loading Linux Terminal...
QUIZ

Knowledge Check

Answer each question one by one.

Q1What happens when you run mkdir -p a/b/c?

Q2What can rmdir delete?

Q3Which symbol adds a new line at the end while keeping an existing file's contents?