Exercise 0.3: Creating Files and Directories
Building Your Workspace
Purpose
So far, you have only looked at existing files and folders. A programmer, however, constantly creates new ones to organize projects, write code, and store documentation. In this exercise, you will learn how to create directories with mkdir and empty files with touch (or its equivalent).
What You’ll Accomplish
By the end of this exercise, you will have successfully:
- Created new directories using
mkdir. - Created new, empty files from the command line.
- Built a simple project folder structure.
Creating Directories
The mkdir (make directory) command is used to create a new folder. This command is universal across macOS, Linux, and Windows.
Your Task 1: Make a Directory
First, use
cd ~to make sure you are in your home directory.Run the following command to create a directory for your practice work:
mkdir cli-practiceRun
ls(ordir). You should now seecli-practicein the list of directories!Use
cd cli-practiceto move into your new folder.
Creating Empty Files
Often, you just need to create a blank file that you can edit later. The command for this differs slightly between operating systems.
Your Task 2: Create a File
Follow the instructions for your operating system to create a file named notes.txt inside your cli-practice directory.
The standard command is touch. If the file doesn’t exist, touch creates it. If it does exist, touch simply updates its modification timestamp.
touch notes.txtIn PowerShell, the New-Item cmdlet is the most direct way to create a new file.
New-Item notes.txtAlternatively, a common shortcut (that also works in CMD) is to use output redirection:
echo. > notes.txtIn the classic Command Prompt, there is no direct touch equivalent. The standard method is to use output redirection to create an empty file.
echo. > notes.txtThis command tells the shell to print nothing (echo.) and redirect that empty output (>) into a new file named notes.txt.
After running the command, use ls (or dir) to confirm that notes.txt now exists in your cli-practice directory.
Verification: Build a Project Structure
Let’s combine your skills to build a standard project folder structure.
Make sure you are inside your
cli-practicedirectory.Create a main project folder named
my-projectand navigate into it.mkdir my-project cd my-projectInside
my-project, create two subdirectories:src(for source code) anddocs(for documentation).mkdir src mkdir docsCreate an empty code file inside the
srcdirectory.# On macOS/Linux touch src/main.py # On Windows New-Item src/main.pyCreate an empty documentation file inside the
docsdirectory.# On macOS/Linux touch docs/README.md # On Windows New-Item docs/README.mdFinally, use
lsto see your newsrcanddocsfolders. Then, usels srcandls docsto see the files inside them.
Reflect and Review
In your Microsoft Teams Student Notebook, answer the following:
- What is the command to create a new directory called
assignments? - What command would you use on your OS to create an empty file called
index.html? - After creating a file or directory, what command should you always run to verify it exists?