Top 10 Raspberry Pi Linux Commands You Should Know
Plus a bonus copy and paste function
Getting started with aRaspberry Pican 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 onLinux, 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 commandlsstands for “listing.” This is the most basic Linux command you’ll use on your Pi. Enterlsin the terminal, pressEnter, and it will return a list of all the files and folders in the current directory.
2. Changing Your Pi’s Password
Thepasswdcommand 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, enterpasswdin the terminal.
It will prompt you to enter your current password, so type that in and pressEnter. Next, type your new password and hitEnter. Next, it will ask you to confirm your new password. Type it in again, pressEnter, 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 thesudocommand. Sudo is a Linux command that stands forSuperuserDo. 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,entersudofollowed by the command you want to execute.
To shutdown your Pi, entersudo shutdown. When you hitEnter, it will ask you for the Pi’s root password. This command will shutdown your Pi in one minute. Usesudo shutdown 0to shutdown immediately.
To restart your pi, usesudo shutdown -r. By default, your Pi will reboot in one minute. If you want it to reboot instantly, you can usesudo shutdown -r 0, where0stands for zero minutes orright now.
4. Changing Directories
Thecdcommand stands for—you guessed it—change directory. It changes the current working directory, which is whatever directory you’re currently in. Typecd/[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 typecd ..which will move you up one directory in the folder hierarchy. Or you could usecd ~. That moves you to the logged-in user’s home directory, andcd /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 previouscdcommand.
5. Copying Files on Your Pi
Thecpcommand 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 namedtest.txtin the current directory and rename it totest2.txt, the command would becp test.txt test2.txt. Both the original file and the renamed copy of the file will be in the current directory. Use thelscommand to see the new file.
6. Renaming Files on Your Pi
To rename a file, use themvcommand. For example, if you use themv test.txt test2.txtcommand, 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. Entermv [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’shomefolder. As usual, if you get a “permission denied” message, addsudoto the beginning of the command.
If the file you want to move isnotin the current directory, you can use a command like this:mv /usr/lib/test.txt ~/. That command would move thetest.txtfile from theusr/libdirectory to the user’shomedirectory.
By the way, you can also rename the filewhileyou’re moving it. Entermv ~/test.txt /usr/lib/test2.txt. In this example, we’ve renamed the text.txt file to test2.txt and moved it from thehomedirectory to theusr/libfolder.
8. Editing Text Documents
The Linux command line text editor is callednano. To run nano, typenano [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, usesudo nano [filepath]. (If you need permission, the editor will tell you so you can close it out and re-run the command withsudo.)
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 holdctrldown when you use that command.
To save a file, pressctrl+o. If you want, you can change the file name. PressingEnterwill save the file. If you want to exit, pressctrl+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 enteringyand pressingEnter.Or you can enternand pressEnterto 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 thewhereiscommand. This command locates any installed package. Enterwhereis [package name].
For example, if you’re looking for your C++ compiler calledgcc, you would type inwhereis gccand 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 displaygcc:.
10. Apt-Get
This is one of the most fun Raspberry Pi Linux commands. Theapt-getcommand 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 typesudo apt-get install [name of the package you want to install].
Here’s the command for if you want to installhtop(an interactive process monitor that will display your Pi’s CPU utilization, memory usage, etc.), you would typesudo 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, useCTRL+ Cto copy it, andCTRL+ Vto paste it into the Pi’s terminal.
Youcan, however, useCTRL+Cto copy the password from Windows and thensingle right-clickin the terminal window. That single right-click pastes text from your clipboard into the terminal. Then, pressEnter.
Be warned: you won’t see any evidence that you have pasted anything into the terminal, but it’s there for sure!
Maggie Marystone is a freelance technology writer, human rights worker, and storyteller based in Chicago.Read Maggie’s Full Bio
Welcome to Help Desk Geek- a blog full of tech tips from trusted tech experts. We have thousands of articles and guides to help you troubleshoot any issue. Our articles have been read over 150 million times since we launched in 2008.
HomeAbout UsEditorial StandardsContact UsTerms of Use
Copyright © 2008-2024 Help Desk Geek.com, LLC All Rights Reserved