Creating Files & Directories
5 min read·Jan 1, 2025
On Unix-like operating systems, the two most common types of files are regular files and directories.
Regular files are raw files used to store all kinds of data such as text, images, audio, video, scripts, programs, etc.
Directories, also called folders, are a filesystem structure which contains references to other files and possibly other directories, generally called entries.
Creating regular files
To create one or more empty regular files, you can use the touch command as follows:
$ touch file ...
Where file ... is a list of paths to the files you want to create, separated by a space character.
Example
For example, this command will create a new empty regular file named index.html into the current directory:
$ touch index.html
This command will create a new empty regular files named guests.txt into the Documents directory:
$ touch ~/Documents/guests.txt
And this command will create two new empty regular files named login.js and signup.js into the controllers directory:
$ touch ./controllers/login.js ./controllers/signup.js
File extensions in Unix
A file extension is a string of characters placed after the file name and separated by a dot character used to identify the characteristic of the file contents or its intended use (e.g., .txt, .pdf, .mp4).
Unlike on Windows, regular files can be created with or without a file extension, as it doesn't have any special meaning for the operating system.
For instance, a regular file named invoice or invoice.pdf will be treated the same way by the operating system.
Creating directories
To create one or more empty directories, you can use the mkdir command — which stands for make directory — as follows:
$ mkdir directory ...
Where directory ... is a list of paths to the directories you want to create, separated by a space character.
Note: A directory contained inside another directory is called a subdirectory and the directory containing it is called the parent directory.
Example
This command will create a new directory named projects into the home directory:
$ mkdir ~/projects
This command will create two new directories named api and blog into the projects directory:
$ mkdir ~/projects/api ~/projects/blog
Creating nested directories
To create multiple nested directories at once — which means a directory within a directory — you can use the mkdir command with the -p flag as follows:
$ mskdir -p directory1/.../directoryN
Where directory1/.../directoryN is the path to the directories you want to create.
Upon execution, the mkdir command will create every single directory in the specified path.
Note: By default, no error will be reported if one or more intermediary directories already exist.
Example
This command will create a new directory named projects, that contains a directory named website, that contains a directory named src:
$ mkdir -p ./projects/website/src
This command will only create the components directory as the other directories already exist:
$ mkdir -p ./projects/website/src/components
Summary
Here's a summary of what you've learned in this lesson:
- The
touchcommand is used to create empty regular files. - The
mkdircommand is used to create directories. - The
mkdir -pcommand is used to create nested directories.
Enjoying the courses?
I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:
Support BackendBrewery