A new file is a new name and a new inode
When you create a file, the kernel allocates an inode, records the file type, the owner, the permissions, and the three timestamps, and adds a name to a directory that points at the inode. No data blocks are allocated until something is written. That is why a fresh file has size zero, and why an empty file costs almost nothing on disk. The inode number is assigned by the filesystem, and the name is only a directory entry that can later be renamed or removed.
The permissions of a new file come from the process that creates it, minus the umask, which a later unit in this module covers. The owner is the effective user of the process. Nothing about the new file depends on the directory that holds it, except that the directory must be writable and searchable by the creator.
touch: create or update
The command touch has two jobs. If the named file does not exist, it creates an empty regular file. If the file exists, it updates the access and modification times to the current time without changing the contents. This second behavior is the reason touch is used to mark a file as newer, for example to tell a build tool that a target is up to date. It also explains why running touch on an existing file is harmless.
The option -c means do not create the file if it is missing, which is useful when you want to update times only when a file is already present. The option -d sets a time you choose, and the option -r copies the times of another file. Each of these updates the change time, because the inode has been modified.
cd /workspace
touch notes.txt
stat -c '%s %n' notes.txt
echo "first line" > notes.txt
touch notes.txt
stat -c '%s %n' notes.txtThe first size is zero. After the write and the touch, the size is still the length of the text, because touch leaves the contents alone. The times change, but the data does not.
mkdir and the -p option
The command mkdir creates a directory. By default it fails if any part of the path before the last name is missing, and it fails if the final name already exists. The option -p changes both rules. It creates every missing parent along the path, and it succeeds without error when the directory already exists. Running mkdir -p twice is therefore safe, which is what makes it a good first line in a script.
The option -m sets the permission bits of the new directories. It applies to the final directory only when you use -p, and it is a shortcut for a chmod that follows the mkdir. The option -v prints each directory as it is created, which is a good way to confirm what a command did.
cd /workspace
mkdir -p projects/2026/notes
mkdir projects/2026/notes
echo "exit status: $?"The first command succeeds whether or not the path already exists. The second fails, because without -p mkdir refuses to create a directory that is already there, and the status prints as one.
Atomic creation with noclobber
A common bug in scripts is to write a file that another process is also writing. The shell offers a safety option, set -o noclobber, which stops the redirection operator from overwriting an existing file. With it on, the command echo a > lock fails if lock exists. The override is >|. This does not make creation atomic across all programs, but it turns an accidental overwrite into a visible error.
For a lock file, the lesson is the same as for any check-then-create sequence: testing for a file and then creating it leaves a window in which another process can act. The safe pattern uses one operation that fails if the name already exists, such as a noclobber redirect or a directory created with mkdir, which is atomic for the name it creates.
Names, spaces and special characters
Any byte except the slash and the NUL byte can appear in a file name. Spaces, newlines, and leading dashes are all legal, and they cause trouble. A name beginning with a dash looks like an option to a command, so the usual defense is the end-of-options marker, two dashes. Writing touch -- -notes creates a file named dash notes. Quote names with spaces, and prefer short names without special characters when you choose them yourself.
Use mkdir -p at the top of a script that writes into a directory, and check the result with test -d before you write. Doing so makes the script safe to run again after a partial failure.
Timestamps and what touch sets
A new file gets all three timestamps set to the moment of creation. The access time records reads, the modification time records changes to the data, and the change time records changes to the inode, which includes the creation itself. Because touch sets the modification and access times and then changes the inode, the change time also moves forward. This is why a file created with an old date from touch -d still shows a recent change time, a fact that the stat unit explains in detail.
Build systems use these times to decide whether a target is out of date. A target older than its source needs rebuilding, and touch on the target marks it as current. Use this carefully, because touching a file tells the build that it is up to date whether or not the contents match the sources.
Directories as containers of names
A directory is itself a file whose contents are a table of names. Creating a directory therefore creates an inode and two entries, the dot entry for itself and the dot-dot entry for its parent. The parent gains one link for each subdirectory, which is why its link count rises with the number of children. This detail is visible in long listings, where the second column of a directory counts its subdirectories plus two.
Directory names and the working path
A directory name is a directory entry like any other, so the same rules apply to it. A name can be long, but the kernel limits each component to 255 bytes and a full path to a larger fixed size, so very deep trees fail with a name-too-long error. Keep names short and meaningful, and avoid spaces in names that scripts will handle, because every script that touches the directory must then quote the path.
Before you create a tree in a script, decide where it lives. A relative path depends on the current directory, and a script started from cron or a service may start somewhere unexpected. Build the path from a known base, and check that the base exists with test -d before any mkdir runs. This turns a stray directory in the wrong place into an error you see immediately.
Practice in the lab
The lab creates nested directories, touches a file that already has content, and checks an exit status. Each result is saved in /workspace, so the checker can see the directories and files you made.