TExcelTAD
Home › Terminal & Command Line › Creating Files & Folders

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:

cmd · try it yourself
@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 /s
C:\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 > file creates an empty file: nul is Windows' "nothing" device.
  • > creates or overwrites a file with a command's output; >> appends.
  • Don't put a space before >: echo hi > f.txt writes hi with 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 ^:

cmd · try it yourself
@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:

powershell · try it yourself
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 -Name
css
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:

powershell · try it yourself
$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):

bash · try it yourself
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 | sort
website
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:

bash · try it yourself
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):

zsh · try it yourself
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, or set -o noclobber in 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?

  1. mkdir a/b/c
  2. mkdir -p a/b/c
  3. touch a/b/c
  4. md a/b/c

What does >> do in all four shells?

  1. Overwrites the file
  2. Appends output to the end of the file
  3. Reads the file
  4. Creates a folder

Which command creates an empty file in CMD?

  1. touch empty.txt
  2. type nul > empty.txt
  3. New-Item empty.txt
  4. mkdir empty.txt