Programming and Linux Tutorials for Makers

linux command line tutorials

Start these linux command line tutorials with navigation: learn pwd, ls and cd first, then pick Bash for glue tasks, Python for hardware logic or Go for a single compiled program. Your first decision is what the script must do.

Makers rarely need to master every tool. A short, runnable script that reads a sensor, moves a file or restarts a service is usually enough, and each tool below suits a different job. The sections follow the order in which the decisions come up, and the spoke guides go deeper on each one, starting with python for electronics beginners for readers who already know they want Python.

Choosing where to start

Pick your starting point by the job in front of you, not by which language is fashionable. Three criteria settle most choices, and each points to a different part of this guide.

  • What the script has to do. If the job is moving files, checking disk space or chaining commands that already exist, stay in the shell. If it has to talk to a sensor, parse data or call a web service, a general-purpose language earns its place.
  • Where it will run. A board with a keyboard-less, headless setup means you will live in a terminal over a network connection whatever language you choose, so shell basics come first either way.
  • How long it has to live. A throwaway check can be a one-line command. A script that logs data for months deserves arguments, logging and a proper editor, which are covered near the end.

If you are undecided, start with the shell. Every other option on this page assumes you can open a terminal, move between folders and read what a command printed. Python is the natural second step for hardware work, and Go is the better third step when you want one program you can copy to a board and run.

Shell basics: moving around the terminal

The shell is the best first stop because every other tool is launched from it. Ubuntu’s command line tutorial for beginners notes that most Linux systems use the same default shortcut to open a terminal, Ctrl-Alt-T. On a headless board you skip that step and land in a terminal as soon as you connect.

Syntax colouring and line numbers make loops like the one above easier to read; most terminal editors can show both.

Opening a terminal and reading the prompt

Once the terminal is open, type pwd to print the folder you are in. Case matters here. Ubuntu’s documentation points out that typing PWD instead of pwd produces an error, and the UC Riverside HPCC manual repeats the general rule that the Unix and Linux command line is case sensitive. The same applies to file names: a script called Sensor.sh and one called sensor.sh are different files.

A plain laptop and a dark editor are all the setup these tutorials assume; the code runs wherever a terminal does.

Two small habits save a lot of typing. The UC Riverside HPCC Linux basics manual explains that the Tab key completes a command or file name when only one option matches, and the Up arrow scrolls backwards through your command history. Ctrl+a jumps the cursor to the beginning of the command line, which helps when you need to fix the start of a long command. Use Tab constantly; it prevents typos in long paths.

A root-level listing: entries such as bin and lib are shown as links into usr, and a green-highlighted tmp entry stands out.

Navigating directories with cd

To navigate directories in Linux from the terminal, combine three commands: pwd to see where you are, ls to see what is there, and cd to move. Ubuntu’s tutorial describes cd as the command that changes the working directory.

  • Move into a folder. Type cd followed by the folder name, for example cd projects. Because this is a relative path, the place you end up depends on your current working directory.
  • Move up. Type cd .. with a space between cd and the two dots. Ubuntu’s tutorial stresses that, unlike in DOS, you cannot type cd.. as one command.
  • Go home. Type cd on its own. Ubuntu describes this as a quick shortcut back to your home directory.
  • Use full paths. The directory separator is a forward slash, not the backslash familiar from Windows. A path that starts with a slash does not depend on where you currently are, which makes it the safer form inside scripts.

A good rule: use relative paths when you are exploring by hand, and absolute paths when you write something that will run later from a different folder, such as a scheduled job.

Checking size before you copy or mount

Disk space becomes a real question on small boards with small memory cards. The HPCC manual lists du -sh . as a way to show the size of the current directory in a human-readable form. Run it inside a project folder before you copy the folder to a USB stick. When the stick itself is the problem, the walkthrough on how to mount a usb drive in linux covers the rest.

Networking is the other early hurdle on a headless board. You cannot connect to it until you know its address, and the guide on how to find ip address of raspberry pi collects the commands for that. Once you can reach the board, key-based login saves repeated password entry, and how to set up ssh keys on linux walks through it.

Working with files and commands safely

File commands are small, but a few of them cannot be undone, so learn what each one does before you run it in a folder that matters.

  • Copy before you change. The freeCodeCamp beginner guide describes cp as the command to copy files. Copying a working script before you edit it is the cheapest backup you will ever make.
  • Create without editing. According to the same guide, touch creates a file and does not open it in edit mode. It is useful when a script expects a file to exist, such as a log, before anything has written to it.
  • Count what a file holds. The wc command displays the number of lines, words and bytes of a file. Pointing it at a data log is a fast sanity check that your sensor script is still writing.
  • Delete deliberately. Removing files is covered in the questions at the end, because it is where beginners lose work.

When you need to edit rather than create, the editor choice matters more than it first appears. The freeCodeCamp guide calls vim an advanced and powerful text editor, which is a fair summary: it rewards practice but is a poor first experience over a slow connection when you only want to change one line. The comparison of nano vs vim helps you decide which to learn first, and the practical answer for most makers is to be comfortable with the simpler editor before you invest in the powerful one.

Go work happens mostly in text panes like these: an editor, a build command and the program’s output side by side.

Bash for gluing projects together

Choose Bash when the task is made of commands you already run by hand. A Bash script is a text file that repeats those commands in order, so everything you learned above transfers directly. Bash is also the shell where the history, Tab completion and line-editing keys from the HPCC manual apply, so your interactive habits and your scripts use the same skills.

A minimal script that reports on every log file in a folder looks like this:

for file in *.log; do
  echo “$file”
  wc “$file”
done

Save it in a file, give it a descriptive name and run it with bash followed by the file name. Quoting the file variable keeps names containing spaces from breaking the command. The wc call prints lines, words and bytes for each file, as described earlier, so the script gives you a one-glance summary of an entire data folder.

When Bash stops being the right tool

Bash is a poor fit once a script has to parse structured data, keep complicated state or handle errors from a network call. That is the signal to move the logic into Python and keep Bash only for launching it. A common pattern is a short shell wrapper that changes into the project folder and starts a Python program, which also keeps the interesting logic testable.

If the script needs to run when the board powers up, the guide on how to run script on startup raspberry pi explains the options. Test the script by hand first. A script that fails when you run it manually will fail silently at boot, where you cannot see its output.

Python for hardware and data

Python is the default choice for most electronics scripting, mainly because it takes little code to express an idea. The linux.org forum discussion of learning Go notes that Go is a little more verbose than Python or Ruby, and that Python is great for web-based projects through frameworks such as Django. For a maker, the practical meaning is that a Python script is faster to write and change at the bench, which matters when you are adjusting a threshold for the tenth time.

Python code that turns text into a dictionary is typical of sensor work, where readings arrive as strings and need structuring.

Dependencies without a mess

The first practical problem with Python is not the language but the packages. Installing libraries system-wide on a board you also use for other things leads to conflicts that are tedious to untangle. Keep each project’s packages separate; the guide to a python virtual environment raspberry pi shows the setup, and it is worth doing before your first pip install rather than after the first failure.

Talking to devices and services

Hardware projects tend to use Python in three ways, and each has its own spoke guide:

  • Serial links to microcontrollers. If an Arduino-style board is sending readings over USB, the pyserial tutorial covers opening the port and reading lines.
  • Message-based sensor networks. For publishing readings to a broker so other devices can subscribe, see the paho mqtt python example.
  • Web services. To send readings to a dashboard or API, the python requests post json example shows how to build and send the payload.

Choose serial when the device is plugged in beside the computer, MQTT when several devices must share data, and a web request when the data must leave your network. Do not mix them in one script until each works alone.

Go for compiled, self-contained programs

Go suits makers who want a single program that runs reliably without an interpreter and, ideally, without a pile of installed packages. The GoLang-Basics repository on GitHub notes that Go uses a tracing garbage collector to manage memory automatically, so you are not tracking allocations by hand as you would in C. The trade-off, again from the linux.org discussion, is that Go is more verbose than Python, so quick experiments take longer to write.

Running your first Go file

The GoLang-Basics repository shows that go run compiles and runs a file in one step, for example go run hello/hello.go for a hello.go file in a hello directory. That one command is all you need while learning, and it is the Go equivalent of running a Python script directly.

How to write a for loop in Go

The same repository lists loops among Go’s control structures, next to if-else and switch. In practice, Go’s looping uses the for keyword in every form. For a maker, the most useful form walks through a list of items, such as a set of pin names or sensor labels:

package main

import “fmt”

func main() {
  pins := []string{“red”, “green”, “blue”}
  for _, name := range pins {
    fmt.Println(name)
  }
}

Save this as hello/hello.go and run it with go run hello/hello.go. The range clause hands the loop each item in turn, and the underscore discards the position because this loop does not need it. When you need to repeat until something changes, write for followed only by a condition, such as for !done, and the loop runs while that condition holds. A bare for with no condition runs until your code breaks out of it, which is the shape of a typical sensor-polling loop.

Putting Go on a board

Go is where you should look if you want to drive pins from a compiled program. The guide to golang raspberry pi gpio covers that directly. If you would rather build on a laptop and copy the result across, cross compile go for arm explains how to produce a binary for the board’s processor family. Compiling on the laptop is the right choice when the board is slow or short of storage.

Making scripts reliable

A script becomes a tool once it accepts inputs, reports what it is doing and is easy to edit. These three upgrades matter more than any language feature, and they apply to Python and Bash scripts alike.

Most maker scripts are written and rewritten at a desk before they ever run unattended on a board.
  • Take options from the command line. Hard-coded port names and file paths force you to edit the script every time something moves. The guide to python argparse tutorial shows how to accept them as arguments, with help text for free.
  • Log instead of printing. A script running at boot has no screen to print to. The python logging tutorial explains how to write messages to a file, and the same guide covers stepping through a failing script with the pdb debugger.
  • Know your editor. You will edit scripts over a remote connection, so choose one editor and learn its save and quit commands before you need them in a hurry.

Do these in order of pain. Logging pays back first, because the first unattended failure will otherwise leave you guessing.

One more option sits outside the shell and the scripting languages: a desktop window around your data. If the goal is a graphical front end for serial readings, the electron serialport tutorial approaches it with web technology instead. That is a different decision from the scripts above, so treat it as a later step once the data path already works.

A decision path for your next hour

Use this list to choose a single next step rather than reading everything at once.

  • You cannot yet move around a terminal comfortably. Stay in the shell section. Practise pwd, ls and cd until the prompt stops feeling unfamiliar, then add Tab completion.
  • Your task is combining commands that already work. Write a Bash script, keep it short and run it by hand before scheduling it.
  • Your task involves a sensor, a serial port or a web service. Use Python, set up a virtual environment first, then pick the spoke that matches your data path.
  • You want one program to copy to a board. Learn Go, start with go run and a simple for loop, then look at cross-compiling.
  • Your script already works but fails mysteriously. Add logging before you add features.

If two options both fit, take the one with less code for the first version. You can always rewrite a working Bash or Python prototype in Go later, and the prototype tells you exactly what the final program has to do.

Frequently asked questions

How do I go back to the previous directory?

Type cd – to return to where you were before the last directory change, as the UC Riverside HPCC Linux basics manual describes. It is handy when you jump between a project folder and a log folder.

How do I see hidden files?

Run ls -a. The HPCC manual describes this option as listing all files, including hidden files whose names begin with a dot.

Is rm -rf safe to use?

Use it only when you are certain of the path. The HPCC manual says the option turns confirmation off and advises caution, and freeCodeCamp’s beginner guide notes it will not ask for confirmation before deleting a folder. Check your location with pwd and list the folder with ls before you delete anything.

How do I leave a manual page?

Press q. The man command shows the manual page for a program, and the HPCC manual notes that the q key exits it. It is the quickest way to check an option you half remember.