Creating Linux Files and Directories
How it works
mkdir DIRECTORY creates a directory. Use mkdir -p PATH to create missing parent directories; existing directories along the path are accepted, but a regular file blocking the path causes failure. touch FILE creates an empty file if it does not exist. On an existing file, touch normally updates access and modification timestamps instead of erasing contents. Quote names containing spaces, and use -- for GNU utilities before a filename that could look like an option. Creation requires appropriate access to the parent directory.
Prepare a safe practice environment
Use GNU/Linux and Bash as a normal user. From the extracted package root run bash lab-fixtures/files/setup.sh. It prints a fresh private directory under /tmp. Copy that path and enter it with cd (quote the path if needed); check pwd. Each code example below starts in a new lab with the same fixture: notes/hello.txt, notes/daily report.txt, notes/-draft, notes/.hidden, and empty directories archive and empty. Set export LC_ALL=C for the shown ordering and English diagnostics. Never run the modifying examples on your own files.
Build a small tree
mkdir -p projects/week1touch projects/week1/plan.txtls -1 projects/week1Prints plan.txt. Both missing directories and the empty file are created.
The first command prepares both directory levels, so the second has an existing parent in which to create the file. A new empty file has zero bytes; it still has a name and metadata. Listing its name confirms presence, not the contents or length.
Create a spaced filename
touch "notes/new idea.txt"ls -1 -- "notes/new idea.txt"Prints one path. Quotes keep the space inside the filename.
Touch an existing file
Still prints hello; touch has not cleared the file.
Common mistakes and safe habits
Do not use touch to empty a file. Do not rerun a lab in a location with unrelated data. mkdir -p is convenient but does not repair a path where an ordinary file occupies a required directory name.
Try it yourself
Create projects/week2 and two empty files summary.txt and next steps.txt inside it. List the new directory, then explain why touching an existing text file does not clear it.
Use a fresh lab for the practice. When finished, return to the lab root if you are in a subdirectory, then leave it with cd ... Remove only the identified temporary lab folder using your file manager if desired. Never remove an unrelated path.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Creating files and directories — Knowledge Check
Apply filesystem concepts in a fresh disposable Linux lab.
Which command creates missing parent directories?
What happens when touch is used on an existing normal file?
How can you create one file named new idea.txt?
What prevents mkdir -p a/b if a is an ordinary file?
Which prerequisite is needed to create a file in a directory?