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
sudo apt update
sudo apt install -y make
make --version | head -n 1The last line prints GNU Make 4.4.1 or a later 4.x. Create a project folder with two small scripts to work with:
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/*.shStep 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:
target: prerequisites
<TAB>commandThe 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:
printf 'hello:\n\t@scripts/hello.sh\n' > Makefile
cat -A Makefilecat -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:
make helloIt 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 theMakefile, or the file name has a different capitalisation.makelooks forGNUmakefile,makefileandMakefile, in that order.make: 'hello' is up to date.: a file calledhelloexists in the folder, somakethinks 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:
touch hello
make hello || true
rm hellomake 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:
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
MAKEA 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..PHONYlists the targets that are commands. It already namesoutputsandhelp, which you add in the next two steps.- Inside a recipe,
$$fis how you write a shell$f.maketreats 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
cdon one line does not carry over to the next. To keep state, put the commands on one line joined with&∨.
Check it worked
make
make backup
ls backupsmake 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:
cat >> Makefile <<'MAKE'
build/%.out: scripts/%.sh
@mkdir -p build
bash $< > $@
outputs: build/hello.out build/report.out
MAKE
make outputsThe 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:
make outputs
touch scripts/hello.sh
make outputsThe 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:
cat >> Makefile <<'MAKE'
help: ## Show this list
@grep -E '^[a-z-]+:.*## ' $(MAKEFILE_LIST) | sed -E 's/:.*## /\t/'
MAKE
make helpThe list shows check, backup, clean and help with their descriptions. A variable set on the command line wins over the one in the file:
make backup BACKUP_DIR=old-backups
ls old-backupsThe 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:
make clean
rm -rf old-backups
lsThe folder is back to Makefile and scripts. Delete the whole demo with rm -rf ~/make-demo when you are done.
Next steps
- Run
make checkfrom a pipeline:/tutorials/run-node-tests-in-github-actionsshows 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/.
Related tutorials
- How to Schedule Jobs with Cron on LinuxYou will schedule a command to run every minute, then on a real timetable, read the cron syntax, and learn why a job that works in your shell can fail under cron.
- How File Permissions Work in Linux: chmod, chown and umaskYou will read Linux permission strings, change them with chmod and chown, set up a folder two users can share, and control the defaults with umask.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials