Skip to main content

CI/CD and automation · Beginner · 12 min

How to Automate Repeated Commands with a Makefile

By Padding · Updated 15 Nov 2026
GNU Make 4 · Ubuntu 26.04.1 LTS

You will write a Makefile that gives a project short, memorable commands (make check, make backup, make clean), learn how targets, prerequisites and variables work, and avoid the tab and .PHONY mistakes that trip up most first Makefiles.

On this page

Every project grows a handful of commands that you type again and again: run the tests, build the files, make a backup, clean up. After a month nobody remembers the exact flags. A Makefile writes them down once and gives each a short name, so make check always means the same thing for you, for a colleague and for a CI job.

make is more than a task runner. It was built to rebuild only what is out of date, by comparing file modification times, and that is what makes it quick to run. In this tutorial you will use both sides of it, on a small project of shell scripts. GNU Make 4.4.1 is what Ubuntu 26.04 installs.

Step 1 · Install make and create a project

Bash
sudo apt update
sudo apt install -y make
make --version | head -n 1

The last line prints GNU Make 4.4.1 or a later 4.x. Create a project folder with two small scripts to work with:

Bash
mkdir -p ~/make-demo/scripts
cd ~/make-demo
cat > scripts/hello.sh <<'SH'
#!/bin/bash
echo "Hello from hello.sh"
SH
cat > scripts/report.sh <<'SH'
#!/bin/bash
echo "Files in this project: $(find . -type f | wc -l)"
SH
chmod +x scripts/*.sh

Step 2 · Write your first target

A Makefile is made of rules. A rule has a target (usually a file name), the prerequisites it depends on, and a recipe, the shell commands that make the target. This is the shape:

Text
target: prerequisites
<TAB>command

The recipe lines must start with a tab character, not spaces. This is the most common mistake: an editor that turns tabs into spaces makes make stop with *** missing separator. Stop. If you use a terminal editor, nano writes real tabs. If you paste the Makefile below into a terminal, check it with cat -A Makefile, which shows each tab as ^I.

Create a Makefile with one rule:

Bash
printf 'hello:\n\t@scripts/hello.sh\n' > Makefile
cat -A Makefile

cat -A prints hello:$ and ^I@scripts/hello.sh$, where ^I is the tab. The @ in front of a command stops make from printing the command before it runs it. Run the rule by naming its target:

Bash
make hello

It prints Hello from hello.sh. Without @ you would see the command line first, then its output.

Troubleshooting

  • Makefile:2: *** missing separator. Stop.: the recipe line starts with spaces, or has no tab. Replace the spaces at the start of the line with one tab.
  • make: *** No rule to make target 'hello'. Stop.: you are not in the folder with the Makefile, or the file name has a different capitalisation. make looks for GNUmakefile, makefile and Makefile, in that order.
  • make: 'hello' is up to date.: a file called hello exists in the folder, so make thinks the target is already built. The next step fixes this.

Step 3 · Mark command targets as phony

Most targets in a project like this are commands, not files. The trouble starts when a file with the same name appears. To see it, create a file named hello and run the target again:

Bash
touch hello
make hello || true
rm hello

make answers make: 'hello' is up to date. and does not run the recipe, because the target hello exists and has no prerequisites newer than it. Declare names that are commands with the special target .PHONY. A phony target always runs, whether or not a file with that name exists.

Rewrite the Makefile with several targets and a variable. Variables are set with = and used as $(NAME). A target after the colon is a prerequisite, so all runs check and report:

Bash
cat > Makefile <<'MAKE'
SCRIPTS := $(wildcard scripts/*.sh)
BACKUP_DIR := backups

.PHONY: all hello check report backup clean outputs help

all: check report

hello:
	@scripts/hello.sh

check: ## Syntax-check every script
	@for f in $(SCRIPTS); do bash -n "$$f" && echo "ok: $$f"; done

report:
	@scripts/report.sh

backup: ## Archive the scripts
	@mkdir -p $(BACKUP_DIR)
	@tar -czf $(BACKUP_DIR)/project.tar.gz scripts Makefile
	@echo "wrote $(BACKUP_DIR)/project.tar.gz"

clean: ## Remove backups and build output
	rm -rf $(BACKUP_DIR) build
MAKE

A warning about tabs: if you paste this Makefile through an editor or terminal that converts tabs to spaces, make stops with *** missing separator. Check with cat -A Makefile as before. Each recipe line must start with ^I, not with spaces, and if it does not, retype those starting characters as a real tab.

How it reads:

  • := assigns the value once, when the file is read. $(wildcard scripts/*.sh) expands to the list of scripts. Plain = re-evaluates every time the variable is used, which is rarely what you want for a shell glob.
  • .PHONY lists the targets that are commands. It already names outputs and help, which you add in the next two steps.
  • Inside a recipe, $$f is how you write a shell $f. make treats a single $ as its own variable, so you double it to pass one through to the shell.
  • Each recipe line runs in its own shell. A cd on one line does not carry over to the next. To keep state, put the commands on one line joined with && or ;.

Check it worked

Bash
make
make backup
ls backups

make with no target runs the first rule in the file, all, which runs check and then report. You see ok: scripts/hello.sh, ok: scripts/report.sh and the file count. make backup writes backups/project.tar.gz. A recipe stops at the first command that fails, and make then stops, so a failing check means the later steps do not run.

Step 4 · Let make skip work that is already done

For real files, make compares modification times. A rule runs only if the target is missing or older than a prerequisite. Add a rule that builds a file from the scripts, and write it as a pattern rule that works for any script. In a recipe, $@ is the target and $< is the first prerequisite:

Bash
cat >> Makefile <<'MAKE'

build/%.out: scripts/%.sh
	@mkdir -p build
	bash $< > $@

outputs: build/hello.out build/report.out
MAKE
make outputs

The first run prints the two bash ... commands, because the .out files do not exist yet. Run it again, then touch a script and run it a third time:

Bash
make outputs
touch scripts/hello.sh
make outputs

The second run prints make: Nothing to be done for 'outputs'.. After touch, only build/hello.out is rebuilt, because only its prerequisite changed. That is the feature that makes Makefiles fast on large projects.

Step 5 · Add a help target and pass variables

A project with many targets needs a list of them. Targets can carry a comment that make help prints. Add that, and also a variable you can override from the command line:

Bash
cat >> Makefile <<'MAKE'

help: ## Show this list
	@grep -E '^[a-z-]+:.*## ' $(MAKEFILE_LIST) | sed -E 's/:.*## /\t/'
MAKE
make help

The list shows check, backup, clean and help with their descriptions. A variable set on the command line wins over the one in the file:

Bash
make backup BACKUP_DIR=old-backups
ls old-backups

The archive now goes to old-backups/. Environment variables work the same way, but a variable set on the command line overrides them.

Step 6 · Clean up

make clean deletes whatever its recipe names. Read the recipe before you run it, because it runs as written. Yours removes the backups and build folders only:

Bash
make clean
rm -rf old-backups
ls

The folder is back to Makefile and scripts. Delete the whole demo with rm -rf ~/make-demo when you are done.

Next steps

  • Run make check from a pipeline: /tutorials/run-node-tests-in-github-actions shows a workflow that calls project commands.
  • Run a target on a schedule with /tutorials/schedule-jobs-with-cron.
  • The GNU Make manual is at gnu.org/software/make/manual/.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.