Ten most useful Raspberry Pi commands.
Getting started with the Raspberry Pi can be tricky. Even if you find a good guide to follow when setting up your Pi for the first time, there is still a lot to learn. The Raspberry Pis runs on Linux, and if you’ve never used a Linux operating system before, it might seem strange and daunting.
While you already know how to perform basic tasks like viewing folders and files on your PC or Mac, doing these tasks on your Pi works differently, especially if you’re using a version that doesn’t have a graphical user interface (GUI). Below, we’ll introduce you to the common Raspberry Pi Linux terminal commands you need to know in order to use your Pi.
1. Listing the Contents of the Current Directory
The ls command stands for “enumeration”. This is the simplest Linux command that you will use on your Pi. Type ls into terminal, hit Enter and it will return a list of all files and folders in the current directory.
2. Changing Your Pi’s Password
The passwd command should probably be one of the first Linux commands you use on your Raspberry Pi. You’re not using the Pi with the default password, are you? This is not good. To change your Pi’s password, enter the password in the terminal.
You will be prompted for your current password, enter it and press Enter. Then enter your new password and press Enter. Then it will ask you to confirm the new password. Re-enter it, hit Enter and you’ve successfully changed your Pi’s password.
3. Restarting or Shutting Down Your Pi
Root access is required to restart or shutdown your Pi, so you need to use the sudo command. Sudo is a Linux command that stands for SuperuserDo. It allows you to execute an elevated Raspberry Pi Linux command that you will need for things like installing programs or rebooting your machine. To use sudo type sudo followed by the command you want to run.
To shutdown the Pi, enter sudo shutdown. When you hit Enter, it will ask you for the root Pi password. This command will turn off your Pi after one minute. Use sudo shutdown 0 for immediate shutdown.
To restart pi, use sudo shutdown -r. By default, your Pi will restart in one minute. If you want it to restart instantly, you can use sudo shutdown -r 0, where 0 means zero minutes or right now .
4. Changing Directories
The cd command means – you guessed it – change directory. It changes the current working directory, that is, the directory you are currently in. Enter cd / [ the path to the directory you want to change to ] . Here’s an example: cd / usr / lib. Typing this command in a terminal will take you to the user / lib folder on your Pi.
Alternatively, you can enter cd .., which will move you one directory up in the folder hierarchy. Or you can use cd ~. This will move you to the home directory of the logged in user, and cd / will move you to the root folder. Finally, cd – takes you to the previous folder you were in. Think of this command as canceling the previous cd command.
5. Copying Files on Your Pi
The cp command copies files and directories. In general, a Raspberry Pi Linux command would 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 to the current directory and rename it to test2.txt, the command will 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 the file, use the mv command. For example, if you use the command mv test.txt test2.txt, the renamed file will be located in the current directory.
7. Moving Files or Folders
Moving a file from one folder to another works in the same way as renaming a file. Enter mv [file name] [destination folder]. This assumes that the file you want to move is in the current directory. Here’s an example: mv test.txt ~ /. This 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 ~ /. This command will 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 when is moved. Enter mv ~ / test.txt /usr/lib/test2.txt. In this example, we 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 start nano, enter nano [path to the text file you want to open or create]. Some folders require permission to create or edit a file. In this case, use sudo nano [filepath]. (If you need permission, the editor will tell you so you can close it and rerun the command with sudo.)
If you use nano to open an existing file, it will open the file for editing. you if create a new file, Linux will open an empty editor with no text. You can use the arrow keys and keyboard to type whatever you want. Note that there is a command menu at the bottom of the terminal window. They all start with the ^ character. On Linux, this means that you must hold down the Ctrl key while using this command.
Press ctrl + o to save the file. You can change the file name if you like. Pressing Enter will save the file. If you want to exit, press ctrl + x. If you exit and there are changes that you have not saved, you will be prompted to save them. Select save by typing y and pressing Enter. Or you can type n and press Enter to discard your changes.
9. Finding the Location of an Installed Program
To find the installed software on the Pi, use the whereis command. This command finds any installed package. Enter whereis [package name].
For example, if you are looking for a C ++ compiler called gcc, you must type whereis gcc and the terminal will display the full path to the executable anywhere on your computer. In the screenshot below, the package was found in two places. If the package is not found anywhere, gcc: will be displayed.
This is one of the funniest Raspberry Pi Linux commands. The apt-get command will find the package you want, download it and install it – all with one command. Sweet! You will need elevated permissions to install the files, so enter sudo apt-get install [the name of the package you want to install].
Here is the command, if you want to install htop (an interactive process monitor that will display your Pi’s CPU usage, memory usage, etc.), you must enter sudo apt-get install htop.
BONUS: How to Copy Text and Paste It Into Your Pi’s Terminal Window
Windows shortcuts for copy and paste do not work on Linux. Let’s say you’re connected to the Pi remotely from your computer and want to copy the Pi’s password from the Windows password manager. You can’t just choose a password, use CTRL + C to copy it, and CTRL + V to paste it into the Pi’s terminal.
However, you can use CTRL + C to copy the password from Windows and then right-click in the terminal window. This single right-click pastes text from the clipboard into the terminal. Then press Enter.
Be warned: you won’t see any evidence that you inserted something into the terminal, but there certainly is!
Ten most useful Raspberry Pi commands
Ten most useful Raspberry Pi commands.