LFCS 9 ๐ง Creating Files and Directories โ touch, mkdir
A filesystem is a tree of directories and files. Creating them is the first operation in any task that involves new data. The LFCS exam includes tasks that require creating a directory structure, initializing a file, or setting up a path for a service. The two commands for this are touch, which creates an empty file or updates a timestamp, and mkdir, which creates a directory. Both are simple, and both have options that matter.
Key point: touch creates an empty file if the file does not exist and updates the access and modification timestamps if it does. mkdir creates a directory. The -p option on mkdir creates parent directories as needed and does not error if the directory already exists. Both commands respect the umask, which determines the default permissions of the new file or directory.
Why file and directory creation matters
The path problem. A file cannot be created in a directory that does not exist. A service cannot write to a log directory that has not been created. The first step in many tasks is creating the directory structure that the rest of the task depends on.
The permission problem. A newly created file or directory receives permissions based on the umask. The default umask is 022, which gives a file 644 (rw-r–r–) and a directory 755 (rwxr-xr-x). If the task requires different permissions, they must be set explicitly with chmod. Knowing the default is knowing what to change.
The persistence problem. A directory created with mkdir in a temporary location does not survive a reboot. A directory created under /var or /srv or /opt does. The location matters as much as the creation.
The ownership problem. A file or directory created by a user is owned by that user. A service that runs as a different user cannot write to it unless the ownership or permissions allow it. Creating a directory for a service means creating it with the right owner or changing the owner afterward.
The exam problem. The LFCS tasks often begin with “Create a directory” or “Create a file.” The commands are simple, but the options and the permissions are where tasks succeed or fail. The -p option is the one that prevents the most common failure: creating a path whose parents do not exist.
a. touch โ create files and update timestamps
The touch command has two purposes. If the file does not exist, it creates an empty file. If the file exists, it updates the access and modification timestamps to the current time.
$ touch notes.txt
$ ls -l notes.txt
-rw-r--r-- 1 alice alice 0 Oct 8 10:00 notes.txt
The file is created with zero bytes. The permissions are 644 because the umask is 022. The owner and group are the user and the user’s primary group.
The command accepts multiple arguments and creates all of them.
$ touch file1.txt file2.txt file3.txt
$ ls
file1.txt file2.txt file3.txt
If a file already exists, touch updates its timestamps without changing its content.
$ touch notes.txt
$ ls -l notes.txt
-rw-r--r-- 1 alice alice 0 Oct 8 10:05 notes.txt
The modification time is updated to the current time. The content is unchanged. This is useful for making a file appear newer without editing it, or for updating a timestamp that a build system checks.
The -a option updates only the access time. The -m option updates only the modification time. The default updates both.
$ touch -a notes.txt # access time only
$ touch -m notes.txt # modification time only
The -t option sets a specific timestamp. The format is [[CC]YY]MMDDhhmm[.ss].
$ touch -t 202601081030 notes.txt
$ ls -l notes.txt
-rw-r--r-- 1 alice alice 0 Jan 8 10:30 notes.txt
The -d option sets a timestamp from a date string.
$ touch -d "2026-01-08 10:30:00" notes.txt
$ touch -d "yesterday" notes.txt
$ touch -d "2 hours ago" notes.txt
The -r option copies the timestamp from a reference file.
$ touch -r reference.txt notes.txt
The -c option does not create the file if it does not exist. It only updates the timestamp of an existing file.
$ touch -c nonexistent.txt
$ ls nonexistent.txt
ls: cannot access 'nonexistent.txt': No such file or directory
The touch command fails if the directory does not exist or the user does not have write permission.
$ touch /nonexistent/file.txt
touch: cannot touch '/nonexistent/file.txt': No such file or directory
$ touch /root/file.txt
touch: cannot touch '/root/file.txt': Permission denied
b. mkdir โ create directories
The mkdir command creates a directory.
$ mkdir projects
$ ls -ld projects
drwxr-xr-x 2 alice alice 4096 Oct 8 10:00 projects
The directory is created with permissions 755 because the umask is 022. The owner and group are the user and the user’s primary group.
The command accepts multiple arguments and creates all of them.
$ mkdir dir1 dir2 dir3
$ ls
dir1 dir2 dir3
If the directory already exists, mkdir fails with an error.
$ mkdir projects
mkdir: cannot create directory 'projects': File exists
The -p option creates parent directories as needed and does not error if the directory already exists.
$ mkdir -p projects/2026/january
$ ls -R projects
projects:
2026
projects/2026:
january
projects/2026/january:
The entire path is created. If projects already existed, the command creates the missing subdirectories and does not error on the existing ones.
$ mkdir -p projects/2026/january
$ mkdir -p projects/2026/january
# no error on the second run
The -p option is the one to use when the parent directories may or may not exist. It is the safe form.
The -m option sets the permissions explicitly, bypassing the umask.
$ mkdir -m 700 private
$ ls -ld private
drwx------ 2 alice alice 4096 Oct 8 10:00 private
The directory is created with 700 (rwx——). The umask is applied to the permissions in the -m option, so -m 777 with a umask of 022 produces 755. To set the exact permissions, the umask must be considered or the chmod command must be used afterward.
The -v option prints a message for each directory created.
$ mkdir -v dir1 dir2
mkdir: created directory 'dir1'
mkdir: created directory 'dir2'
The -p and -v options combine for a useful pattern.
$ mkdir -pv a/b/c/d
mkdir: created directory 'a'
mkdir: created directory 'a/b'
mkdir: created directory 'a/b/c'
mkdir: created directory 'a/b/c/d'
The mkdir command fails if the user does not have write permission in the parent directory.
$ mkdir /root/newdir
mkdir: cannot create directory '/root/newdir': Permission denied
c. Permissions, umask, and the created path
The permissions of a new file or directory are determined by the umask. The umask is a bitmask that removes permissions from the default.
$ umask
0022
The default permissions for a file are 666 (rw-rw-rw-) and for a directory are 777 (rwxrwxrwx). The umask removes the bits that are set in the mask. With a umask of 022, the file becomes 644 and the directory becomes 755.
$ umask 022
$ touch file.txt
$ mkdir dir
$ ls -ld file.txt dir
-rw-r--r-- 1 alice alice 0 Oct 8 10:00 file.txt
drwxr-xr-x 2 alice alice 4096 Oct 8 10:00 dir
A umask of 077 produces a file of 600 and a directory of 700.
$ umask 077
$ touch private.txt
$ mkdir privatedir
$ ls -ld private.txt privatedir
-rw------- 1 alice alice 0 Oct 8 10:00 private.txt
drwx------ 2 alice alice 4096 Oct 8 10:00 privatedir
The umask is a property of the shell process. It is inherited by child processes. It can be set in ~/.bashrc or /etc/profile to persist.
To create a file or directory with specific permissions, the chmod command is used after creation, or the -m option is used with mkdir.
$ mkdir -m 750 shared
$ ls -ld shared
drwxr-x--- 2 alice alice 4096 Oct 8 10:00 shared
The touch command does not have a -m option for permissions. The chmod command is required.
$ touch script.sh
$ chmod 755 script.sh
$ ls -l script.sh
-rwxr-xr-x 1 alice alice 0 Oct 8 10:00 script.sh
The path of the created file or directory is relative to the working directory unless it begins with /. The mkdir -p and touch commands resolve the path from the working directory.
$ cd /tmp
$ mkdir -p a/b/c
$ ls -ld /tmp/a/b/c
drwxr-xr-x 2 alice alice 4096 Oct 8 10:00 /tmp/a/b/c
The -p option creates the path relative to the working directory. The absolute path is the working directory plus the relative path.
A file or directory created in a temporary location like /tmp does not survive a reboot. A file or directory created under /var, /srv, /opt, or /etc does. The location determines the persistence.
Complete Example Session
# ============================================
# PART 1: TOUCH BASIC
# ============================================
$ touch notes.txt
$ ls -l notes.txt
-rw-r--r-- 1 alice alice 0 Oct 8 10:00 notes.txt
# ============================================
# PART 2: TOUCH MULTIPLE
# ============================================
$ touch file1.txt file2.txt file3.txt
$ ls
file1.txt file2.txt file3.txt
# ============================================
# PART 3: TOUCH UPDATE TIMESTAMP
# ============================================
$ touch notes.txt
$ ls -l notes.txt
-rw-r--r-- 1 alice alice 0 Oct 8 10:05 notes.txt
# ============================================
# PART 4: TOUCH SPECIFIC TIME
# ============================================
$ touch -t 202601081030 notes.txt
$ ls -l notes.txt
-rw-r--r-- 1 alice alice 0 Jan 8 10:30 notes.txt
# ============================================
# PART 5: TOUCH -C
# ============================================
$ touch -c nonexistent.txt
$ ls nonexistent.txt
ls: cannot access 'nonexistent.txt': No such file or directory
# ============================================
# PART 6: MKDIR BASIC
# ============================================
$ mkdir projects
$ ls -ld projects
drwxr-xr-x 2 alice alice 4096 Oct 8 10:00 projects
# ============================================
# PART 7: MKDIR -P
# ============================================
$ mkdir -p projects/2026/january
$ ls -R projects
projects:
2026
projects/2026:
january
projects/2026/january:
# ============================================
# PART 8: MKDIR -M
# ============================================
$ mkdir -m 700 private
$ ls -ld private
drwx------ 2 alice alice 4096 Oct 8 10:00 private
# ============================================
# PART 9: MKDIR -PV
# ============================================
$ mkdir -pv a/b/c/d
mkdir: created directory 'a'
mkdir: created directory 'a/b'
mkdir: created directory 'a/b/c'
mkdir: created directory 'a/b/c/d'
# ============================================
# PART 10: UMASK EFFECT
# ============================================
$ umask 077
$ touch private.txt
$ mkdir privatedir
$ ls -ld private.txt privatedir
-rw------- 1 alice alice 0 Oct 8 10:00 private.txt
drwx------ 2 alice alice 4096 Oct 8 10:00 privatedir
The ten parts covered basic touch, multiple files, updating a timestamp, a specific time, touch -c, basic mkdir, mkdir -p, mkdir -m, mkdir -pv, and the umask effect.
Quick Reference
touch Options
| Option | Effect |
|---|---|
touch file | Create or update timestamp |
touch -a file | Access time only |
touch -m file | Modification time only |
touch -t [[CC]YY]MMDDhhmm[.ss] file | Set specific time |
touch -d "date" file | Set time from string |
touch -r ref file | Copy time from ref |
touch -c file | Do not create |
mkdir Options
| Option | Effect |
|---|---|
mkdir dir | Create directory |
mkdir dir1 dir2 | Create multiple |
mkdir -p a/b/c | Create parents |
mkdir -m 700 dir | Set permissions |
mkdir -v dir | Verbose |
mkdir -pv a/b/c | Both |
Default Permissions
| Umask | File | Directory |
|---|---|---|
| 022 | 644 | 755 |
| 002 | 664 | 775 |
| 077 | 600 | 700 |
| 027 | 640 | 750 |
Path Persistence
| Location | Survives Reboot |
|---|---|
/tmp | No |
/var/tmp | Usually |
/home | Yes |
/etc | Yes |
/var | Yes |
/srv | Yes |
/opt | Yes |
Best Practices
โ Do This:
# Use -p to create parent directories
mkdir -p /srv/app/logs # โ
# Use -p to avoid errors on existing directories
mkdir -p /var/log/myapp # โ
# Check the permissions after creation
ls -ld /srv/app # โ
# Set permissions explicitly when needed
mkdir -m 750 /srv/shared # โ
# Use touch to create a file before editing
touch /etc/myapp/config.conf # โ
# Use touch -c in scripts to avoid creating files
touch -c /var/log/myapp.log # โ
# Verify the umask when permissions surprise you
umask # โ
โ Don’t Do This:
# Don't forget -p when the parent may not exist
mkdir /srv/app/logs # fails if /srv/app does not exist # โ
# Don't assume the default permissions are what you want
mkdir /srv/shared # 755, not 750 # โ ๏ธ
# Don't create files in /tmp and expect them to persist
touch /tmp/important.txt # lost on reboot # โ
# Don't use touch to create a directory
touch /srv/app # creates a file, not a directory # โ
# Don't ignore the umask when setting -m
mkdir -m 777 dir # with umask 022, becomes 755 # โ ๏ธ
# Don't create a file in a directory you cannot write to
touch /root/file.txt # permission denied # โ
# Don't leave the umask changed in the current shell
umask 077 # affects all subsequent commands # โ ๏ธ
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
mkdir fails | Parent does not exist | Use -p |
| Permissions wrong | Umask | Check umask, use chmod |
| File not created | Directory does not exist | Create the directory first |
| Timestamp not updated | Used -c | Remove -c |
touch creates a file instead of a directory | Used the wrong command | Use mkdir |
| Directory not persistent | Created in /tmp | Create under /var, /srv, etc. |
-m produces wrong permissions | Umask applied | Account for the umask or use chmod |
Real-World Examples
1. Create a File
touch notes.txt
2. Create Multiple Files
touch file1.txt file2.txt file3.txt
3. Update a Timestamp
touch existing.txt
4. Create a Directory
mkdir projects
5. Create a Nested Path
mkdir -p /srv/app/logs
6. Create with Permissions
mkdir -m 750 /srv/shared
7. Verbose Creation
mkdir -pv /srv/app/{logs,cache,data}
8. Set a Specific Time
touch -t 202601081030 notes.txt
9. Copy a Timestamp
touch -r reference.txt notes.txt
10. Check the Umask
umask
Visual
File vs Directory Creation
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ touch file.txt โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ file.txt (0 bytes) โ โ
โ โ -rw-r--r-- โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ mkdir dir โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ dir/ โ โ
โ โ drwxr-xr-x โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
mkdir -p Path Resolution
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ mkdir -p /srv/app/logs โ
โ โ
โ Before: โ
โ / โ
โ โโโ srv โ
โ โ
โ After: โ
โ / โ
โ โโโ srv โ
โ โโโ app โ
โ โโโ logs โ
โ โ
โ Each missing parent is created. โ
โ Existing directories are not modified. โ
โ No error if the path already exists. โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Umask Effect
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ FILE DEFAULT: 666 (rw-rw-rw-) โ
โ DIR DEFAULT: 777 (rwxrwxrwx) โ
โ โ
โ umask 022: โ
โ 666 - 022 = 644 (rw-r--r--) file โ
โ 777 - 022 = 755 (rwxr-xr-x) directory โ
โ โ
โ umask 077: โ
โ 666 - 077 = 600 (rw-------) file โ
โ 777 - 077 = 700 (rwx------) directory โ
โ โ
โ The umask removes bits. โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Timestamp Update
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ BEFORE: โ
โ -rw-r--r-- 1 alice alice 0 Oct 8 09:00 notes.txt โ
โ โ
โ touch notes.txt โ
โ โ
โ AFTER: โ
โ -rw-r--r-- 1 alice alice 0 Oct 8 10:05 notes.txt โ
โ โ
โ Content unchanged. Timestamps updated. โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Summary
| Command | Purpose |
|---|---|
touch file | Create file or update timestamp |
touch -a file | Update access time |
touch -m file | Update modification time |
touch -t TIME file | Set specific time |
touch -c file | Do not create |
mkdir dir | Create directory |
mkdir -p a/b/c | Create parents |
mkdir -m MODE dir | Set permissions |
mkdir -v dir | Verbose |
umask | Show default permission mask |
Key takeaways:
touchcreates an empty file if it does not exist and updates the timestamps if it does. The file is created with zero bytes and permissions determined by the umask. The-coption prevents creation.mkdircreates a directory. The-poption creates parent directories as needed and does not error if the directory already exists. It is the safe form to use in scripts and on the exam.- The umask determines the default permissions. A file gets
666minus the umask, and a directory gets777minus the umask. The common umask of022produces644files and755directories. The-moption onmkdirsets permissions explicitly, but the umask is still applied. - The location determines persistence. A file or directory created under
/tmpdoes not survive a reboot. A file or directory created under/var,/srv,/opt, or/etcdoes. - The directory must exist before a file can be created in it.
touch /nonexistent/file.txtfails because the directory does not exist.mkdir -pcreates the directory first. - Permissions and ownership matter for services. A service that runs as a different user cannot write to a directory unless the permissions or ownership allow it. Create the directory with the right owner or change it afterward.
Remember: Creating files and directories is the first operation in many tasks. The commands are simple, but the details matter: the -p option for paths, the umask for permissions, the location for persistence. Use mkdir -p when the parent directories may not exist. Check the permissions after creation. And remember that a file created in /tmp is temporary. The exam tasks often require a directory structure that the rest of the task depends on, and getting the structure right is the prerequisite for everything that follows.
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!