Creating Files & Folders
You can create folders, empty files and small text files without leaving the terminal. That's handy for setting up a project skeleton, writing a quick config file, or preparing test data, and it's the first step of most scripts.
| Task | CMD | PowerShell | bash / zsh |
|---|---|---|---|
| Create a folder | mkdir logs (md) |
New-Item -ItemType Directory logs (mkdir logs) |
mkdir logs |
| Create nested folders | mkdir a\b\c |
New-Item -ItemType Directory a/b/c -Force |
mkdir -p a/b/c |
| Create an empty file | type nul > empty.txt |
New-Item empty.txt |
touch empty.txt |
| Write a line (overwrite) | echo text> f.txt |
Set-Content f.txt 'text' |
echo "text" > f.txt |
| Append a line | echo text>> f.txt |
Add-Content f.txt 'text' |
echo "text" >> f.txt |
| Write several lines | (echo a& echo b)> f.txt |
here-string @' … '@ |
heredoc <<'EOF' |
| Update a file's date only | copy /b f.txt +,, |
(Get-Item f.txt).LastWriteTime = … |
touch f.txt |
Windows (CMD)
mkdir (or its short name md) creates every missing folder in the path, so nested folders need no extra option. Several names can be given at once:
@echo off
mkdir website
cd website
mkdir css js img\icons
type nul > index.html
echo body { margin: 0; }> css\style.css
echo console.log("hi");> js\app.js
dir /b /o:n /sC:\Users\learner\website\css
C:\Users\learner\website\img
C:\Users\learner\website\index.html
C:\Users\learner\website\js
C:\Users\learner\website\css\style.css
C:\Users\learner\website\img\icons
C:\Users\learner\website\js\app.js
type nul > filecreates an empty file:nulis Windows' "nothing" device.>creates or overwrites a file with a command's output;>>appends.- Don't put a space before
>:echo hi > f.txtwriteshiwith a trailing space.
To write several lines, group the echo commands in parentheses and redirect once. Characters with a special meaning in CMD (< > | & ^) must be escaped with ^:
@echo off
(
echo # Settings
echo name=demo
echo mode=fast ^& safe
) > settings.ini
echo port=8080>> settings.ini
type settings.ini# Settings
name=demo
mode=fast & safe
port=8080
mkdir on a folder that already exists prints an error (A subdirectory or file logs already exists.), so scripts check first: if not exist logs mkdir logs.
Windows (PowerShell)
New-Item creates both files and folders. -ItemType Directory makes a folder, -Force creates missing parent folders and doesn't complain if the folder exists, and -Value gives a new file its content. mkdir is a built-in function that calls New-Item -ItemType Directory:
New-Item -ItemType Directory website/css, website/js -Force | Out-Null
New-Item website/index.html | Out-Null
Set-Content website/css/style.css 'body { margin: 0; }'
New-Item website/js/app.js -Value 'console.log("hi");' | Out-Null
Get-ChildItem website -Recurse -Namecss
js
index.html
css/style.css
js/app.js
New-Item prints a table describing the new item; | Out-Null hides it. For multi-line text, use a here-string: everything between @' and '@ (each on its own line) is taken literally. With @" … "@ variables are expanded:
$app = 'demo'
@"
# Settings
name=$app
mode=fast & safe
"@ | Set-Content settings.ini
Add-Content settings.ini 'port=8080'
Get-Content settings.ini# Settings
name=demo
mode=fast & safe
port=8080
Note that Set-Content overwrites. New-Item refuses to overwrite an existing file unless you add -Force, which is a safer default.
Linux & macOS (bash/zsh)
mkdir -p creates parent folders as needed and is silent if the folder exists. Brace expansion creates several similar names in one go, and touch creates empty files (or updates the date of existing ones):
mkdir -p website/{css,js,img/icons}
touch website/index.html
echo 'body { margin: 0; }' > website/css/style.css
echo 'console.log("hi");' > website/js/app.js
find website | sortwebsite
website/css
website/css/style.css
website/img
website/img/icons
website/index.html
website/js
website/js/app.js
{css,js,img/icons} is expanded by the shell into three arguments before mkdir runs, so the command really is mkdir -p website/css website/js website/img/icons. This works in bash and zsh but not in CMD or PowerShell.
For several lines, a heredoc sends the lines between <<'EOF' and EOF to a command's input. With quotes around 'EOF' the text is taken literally; without them, $variables are expanded:
app=demo
cat > settings.ini <<EOF
# Settings
name=$app
mode=fast & safe
EOF
echo "port=8080" >> settings.ini
cat settings.ini# Settings
name=demo
mode=fast & safe
port=8080
printf is another precise way to write files, since it doesn't add anything you didn't ask for (\n is a newline):
printf '%s\n' 'line one' 'line two' > notes.txt
printf 'no newline at the end' > raw.txt
wc -l notes.txt raw.txt 2 notes.txt
0 raw.txt
2 total
wc -l counts newline characters, so raw.txt counts as 0 lines even though it contains text.
Note: In zsh, the option
noclobber(setopt noclobber, orset -o noclobberin bash) makes>refuse to overwrite an existing file. Use>|when you really mean to overwrite.
Opening an editor
For anything longer than a few lines, open a text editor from the terminal: notepad file.txt (Windows), code file.txt (VS Code, any OS), nano file.txt (Linux and macOS, easy for beginners), or open -e file.txt (TextEdit on macOS). See Terminal Text Editors.
Learn more: mkdir & rmdir · touch · CMD mkdir & rmdir · Heredocs
Test yourself
Which bash command creates a/b/c even if a doesn't exist yet?
mkdir a/b/cmkdir -p a/b/ctouch a/b/cmd a/b/c
What does >> do in all four shells?
- Overwrites the file
- Appends output to the end of the file
- Reads the file
- Creates a folder
Which command creates an empty file in CMD?
touch empty.txttype nul > empty.txtNew-Item empty.txtmkdir empty.txt