LINUX BASIC COMMANDS

Photo of author

By Jackson Taylor

Home » Electronics Topics » Raspberry Pi » LINUX BASIC COMMANDS

As an Amazon Associate we earn from qualifying purchases.

The terminal is the fastest way to get things done on Linux, and it is the main way most people work on a Raspberry Pi. You type a command, press Enter, and the system responds. This page lists the basic Linux commands you will use most often, with short explanations and examples you can try right away.

The commands below work on most Linux distributions, including Raspberry Pi OS, Debian and Ubuntu.

Moving around: files and directories

Start with these three commands. They tell you where you are and let you move around the file system.

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video
  • pwd: print the working directory, which is the folder you are currently in.
  • ls: list the files and folders in the current directory. Add -l for a detailed list, or -a to include hidden files.
  • cd: change directory. For example, cd /home/pi takes you to the pi user’s home folder. cd .. goes up one level, and cd on its own returns you to your home directory.

A typical first session is to type pwd to see where you are, ls to see what is there, then cd foldername to step into one of the folders.

Creating, copying, moving and deleting

These commands manage your files and folders. Linux does not ask for confirmation before deleting, so read them carefully.

  • mkdir directoryname: make a new directory with that name.
  • rmdir directoryname: remove a directory, but only if it is empty.
  • rm filename: remove (delete) a file. There is no recycle bin in the terminal, so a deleted file is gone.
  • cp oldfile newfile: copy oldfile to newfile.
  • mv oldfile newfile: move oldfile to newfile. Because moving a file to a new name is the same as renaming it, this is also how you rename files.

To make a folder in the Raspberry Pi home directory, for example one for FTP files, use:

See also
DETECTING EDGES OF PICTURE

sudo mkdir /home/pi/FTP

The sudo at the start is needed only when the location requires administrator rights. Inside your own home folder you can usually run mkdir without it.

A note on deleting folders

The rm command on its own removes files only. To delete a folder along with everything in it, you can add the -r option, as in rm -r foldername. Double-check the name before you press Enter, especially if you are running it with sudo.

Reading and searching file contents

  • cat textfile: display the contents of a text file in the terminal. For example, cat a.py shows the code in a Python file named a.py.
  • grep: a search tool that finds lines matching a pattern, and it understands regular expressions. For example, grep error log.txt prints every line in log.txt that contains the word “error”.
  • echo: print whatever you type back in the terminal. For example, echo hello prints hello. It is often used in scripts.

For long files, cat scrolls everything past you at once. In that case, a viewer such as less filename lets you page through the file and press q to quit.

System and help commands

  • date: read the system date and time.
  • man command: show the manual page for a command. Try man ls to see every option that ls supports. Press q to leave the manual.
  • exit: quit the terminal session.

When you are unsure what a command does, man is the first place to look. Many commands also accept –help, which prints a shorter summary.

Running programs and using sudo

./program runs a program in the current directory. The ./ tells the shell to look in the current folder instead of the system paths. If a script will not run, it may not be marked as executable. You can fix that with chmod +x program.

Python scripts are usually started by naming the interpreter, for example python3 a.py.

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works
See also
Drowsiness or eye detection in opencv python raspberry pi

sudo runs a command with the rights of the root (administrator) user. You need it for tasks such as installing software or changing system files. Use it only when a command needs it, because root can change or delete almost anything.

Changing a password

The command sudo passwd sets a new password for the root account. To change the password of the user you are logged in as, for example the pi user, run passwd without sudo. You will be asked for the current password and then the new one twice. The characters do not show on screen as you type, which is normal.

Installing software and libraries

On Debian-based systems, including Raspberry Pi OS and Ubuntu, you install software with the apt package manager. The general form is:

sudo apt-get install packagename

Before installing, it is a good habit to refresh the package list with sudo apt-get update, so you get the current version of the package.

Here are two examples. The first installs Python, and the second installs the matplotlib plotting library for Python:

  • sudo apt-get install python
  • sudo apt-get install python-matplotlib

These package names come from the Python 2 era. On current releases, Python 3 is the standard version, and the matching packages are usually named python3 and python3-matplotlib. If apt says it cannot find a package, try the python3 name, or search with apt-cache search name.

Quick reference table of commands

  1. pwd: where am I?
  2. ls: what is here?
  3. cd: go to another folder
  4. mkdir / rmdir: create or remove a folder
  5. cp / mv / rm: copy, move or rename, delete
  6. cat / grep: read or search a file
  7. man: read the manual
  8. sudo apt-get install: add software

Tips for using the terminal

  • Press the Tab key to auto-complete file and folder names. It saves typing and avoids typos.
  • Press the Up arrow to bring back previous commands.
  • Linux is case-sensitive, so File.txt and file.txt are different files.
  • If a file or folder name has spaces, put it in quotes.
See also
SMART ECO BIN (GIVES YOU CHOCOLATE IF YOU DEPOSITE WASTE BOTTLE)

Once these commands feel natural, you can move on to scripting and hardware projects. If you also work with modems, our guide to AT commands for GSM modems shows another command-driven way of talking to a device.

Frequently asked questions

What does sudo mean in Linux?

It stands for “superuser do”. It runs a single command with administrator (root) rights, which is needed for installing software and changing system settings.

How do I rename a file in Linux?

Use mv oldname newname. The same command moves a file to another folder when you give it a different path.

What is the difference between rm and rmdir?

rm deletes files. rmdir removes only empty directories. To delete a folder that has files in it, use rm -r with care.

Are Raspberry Pi commands different from Linux commands?

No. Raspberry Pi OS is a Linux distribution, so all the commands on this page work the same way there as on other Debian-based systems.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works