Getting started with a Raspberry Pi can be daunting. Even if you find a good guide to follow when you set up your Pi for the first time, there’s so much to learn. Raspberry Pis run on Linux, and, if you’ve never used the Linux operating system before, it can seem strange and complicated.
While you already know how to perform basic tasks like viewing folders and files on your PC or Mac, doing those things on your Pi works differently, especially if you’re running a version that doesn’t have a graphical user interface (GUI). Below, we’ll take you through common Raspberry Pi Linux terminal commands that you’ll need to know to use your Pi.
1. Listing the Contents of the Current Directory
The command ls stands for “listing.” This is the most basic Linux command you’ll use on your Pi. Enter ls in the terminal, press Enter, and it will return a list of all the files and folders in the current directory.
2. Changing Your Pi’s Password
The passwd command probably should be among the first Linux commands you use on your Raspberry Pi. You’re not running your Pi with the default password, are you? That’s not good. To change your Pi’s password, enter passwd in the terminal.
It will prompt you to enter your current password, so type that in and press Enter. Next, type your new password and hit Enter. Next, it will ask you to confirm your new password. Type it in again, press Enter, and you have successfully changed your Pi’s password.
3. Restarting or Shutting Down Your Pi
Restarting or shutting down your Pi requires root access, so you have to use the sudo command. Sudo is a Linux command that stands for SuperuserDo. It allows you to execute a Raspberry Pi Linux command with elevated privileges—which you’ll need for things like installing programs or rebooting the machine. To use sudo, enter sudo followed by the command you want to execute.
To shutdown your Pi, enter sudo shutdown. When you hit Enter, it will ask you for the Pi’s root password. This command will shutdown your Pi in one minute. Use sudo shutdown 0 to shutdown immediately.
To restart your pi, use sudo shutdown -r. By default, your Pi will reboot in one minute. If you want it to reboot instantly, you can use sudo shutdown -r 0, where 0 stands for zero minutes or right now.
4. Changing Directories
The cd command stands for—you guessed it—change directory. It changes the current working directory, which is whatever directory you’re currently in. Type cd /[path of the directory you want to go to]. Here’s an example: cd /usr/lib. Typing that command in the terminal will take you to the user/lib folder on your Pi.
Alternatively, you could type cd .. which will move you up one directory in the folder hierarchy. Or you could use cd ~. That moves you to the logged-in user’s home directory, and cd / will move you to the root folder. Lastly, cd – takes you to the previous folder you were in. Think of that command as undoing the previous cd command.
5. Copying Files on Your Pi
The cp command copies files and directories. In general, the Raspberry Pi Linux command will look like this: cp [source file location] [destination file location].
When you copy files, you can rename them at the same time. If you want to copy a file named test.txt in the current directory and rename it to test2.txt, the command would be cp test.txt test2.txt. Both the original file and the renamed copy of the file will be in the current directory. Use the ls command to see the new file.
6. Renaming Files on Your Pi
To rename a file, use the mv command. For example, if you use the mv test.txt test2.txt command, the renamed file will be located in the current directory.
7. Moving Files or Folders
Moving a file from one folder to another works similarly to renaming a file. Enter mv [filename] [destination folder]. This assumes that the file you want to move is in the current directory. Here’s an example: mv test.txt ~/. That command will move the test.txt file from the current directory to the user’s home folder. As usual, if you get a “permission denied” message, add sudo to the beginning of the command.
If the file you want to move is not in the current directory, you can use a command like this: mv /usr/lib/test.txt ~/. That command would move the test.txt file from the usr/lib directory to the user’s home directory.
By the way, you can also rename the file while you’re moving it. Enter mv ~/test.txt /usr/lib/test2.txt. In this example, we’ve renamed the text.txt file to test2.txt and moved it from the home directory to the usr/lib folder.
8. Editing Text Documents
The Linux command line text editor is called nano. To run nano, type nano [path to the text file you want to open or create]. Some folders require permission to create or edit a file. If that’s the case, use sudo nano [filepath]. (If you need permission, the editor will tell you so you can close it out and re-run the command with sudo.)
If you use nano to open an existing file, it will open the file for editing. If you’re creating a new file, Linux will open an empty editor with no text in it. You can use the arrow keys and the keyboard to type anything you want. Note that there’s a menu of commands at the bottom of the terminal window. They all start with a ^. In Linux, that means you should hold ctrl down when you use that command.
To save a file, press ctrl+o. If you want, you can change the file name. Pressing Enter will save the file. If you want to exit, press ctrl+x. If you exit and there are changes you haven’t saved, it’ll ask you if you want to save them. Choose to save by entering y and pressing Enter. Or you can enter n and press Enter to discard the changes.
9. Finding the Location of an Installed Program
To find the location of an installed program on your Pi, you’ll use the whereis command. This command locates any installed package. Enter whereis [package name].
For example, if you’re looking for your C++ compiler called gcc, you would type in whereis gcc and the terminal will display the full path to the executable, anywhere it exists on your machine. In the screenshot below, the package has been found in two places. If it doesn’t find the package anywhere, it would display gcc:.
This is one of the most fun Raspberry Pi Linux commands. The apt-get command will find the package you want, download it, and install it, all in a single command. Sweet! When you install files, you need elevated permissions, so type sudo apt-get install [name of the package you want to install].
Here’s the command for if you want to install htop (an interactive process monitor that will display your Pi’s CPU utilization, memory usage, etc.), you would type sudo apt-get install htop.
BONUS: How to Copy Text and Paste It Into Your Pi’s Terminal Window
Windows copy/paste shortcuts don’t work in Linux. Let’s say you are remotely connected to your Pi from your PC and you want to copy your Pi’s password from your password manager on Windows. You can’t just select the password, use CTRL + C to copy it, and CTRL + V to paste it into the Pi’s terminal.
You can, however, use CTRL + C to copy the password from Windows and then single right-click in the terminal window. That single right-click pastes text from your clipboard into the terminal. Then, press Enter.
Be warned: you won’t see any evidence that you have pasted anything into the terminal, but it’s there for sure!