The official operating system recommended for the Raspberry Pi is Raspbian, which is a free operating system based on Debian Linux that can be installed on all versions of the Raspberry Pi. This means that if you are not familiar with Linux, you may not be able to tinker with this delightful gadget freely.
Linux is inherently designed to be operated via CLI (command-line interface), so learning to use a Raspberry Pi requires knowledge of Linux commands. This article introduces the 20 most commonly used commands (see the table below) to help you easily see the colorful world in the deep black CLI.
▲ 20 Essential Linux Commands for Raspberry Pi Beginners
Assuming you have completed the installation of the Raspberry Pi operating system, open the terminal that comes with the system, and the displayed content is as follows:
▲ Raspberry Pi Raspbian Linux Terminal
The default command prompt is as follows:
pi@raspberrypi:~ $
This indicates that you are logged in as user “pi” on the host named “raspberrypi” and are in the “pi” user’s home directory. You can enter various Linux commands after this and press the Enter key to execute them.
Of course, you can also log in as the superuser “root”, and the command prompt will appear as follows:
root@raspberrypi:~ #
Unlike the “pi” user, the command prompt is #, indicating that this is the “root” user. The Raspberry Pi does not enable the root account by default, and if you want to execute commands as root, you can refer to the sudo command below.
ls lists the files in the current directory
▲ ls command
You can try entering the ls command directly and pressing Enter, and then entering the ls -l command and pressing Enter to see the difference. Obviously, when you add the -l option, the files in the directory are displayed in a listing format, allowing you to clearly see the file type, owner, creation time, and other information. The first column will show the following information:
drwxr-xr-x
The first letter d indicates that the file is a directory; if it is a regular file, the first letter will display a hyphen “ – ”.
The remaining 9 characters are grouped in sets of three, each representing the file’s permissions for different users. The first group rwx indicates that the file’s owner has read (read), write (write), and execute (execute) permissions for the file; the second group r-x indicates that users in the same group have read and execute permissions (“ – ” indicates lack of relevant permissions); the third group r-x indicates that other users have read and execute permissions.
There is also ls -a to list all files, including hidden ones; Linux hidden file names start with “ . “.
Some Linux distributions provide the ll command, which is equivalent to ls -l. The Raspberry Pi does not have the ll command by default, but you can modify the .bashrc file in the user’s home directory to find the following two lines:
#alias ll=’ls -l’
#alias la=’ls -Al’
Remove the # in front of them, save, and re-enter the terminal to use the ll command.
pwd outputs the current directory
Sometimes you may want to know what directory you are in, and you can print it out using the pwd command. pwd is the abbreviation for print working directory.
For example, if you are in the following directory:
/home/pi/projects/rebot/bin
But the terminal only displays:
pi@raspberrypi: /bin $
You only know you are in the bin directory from the terminal display, but do not know the complete path information. This is when you need the pwd command.
cd changes the directory
You can cd to any directory you want to go to, as long as you know its path, which can be an absolute path or a relative path.
Assuming you are in the following directory:
/home/pi/projects/robot/bin/
You want to go to the /home/pi/projects/ directory, you can use either the absolute path:
$ cd /home/pi/projects
or the relative path:
$ cd ../../
Where ../ refers to the parent directory; in this example, going up two levels from the bin directory leads to the projects directory.
If there is a lib directory at the same level as the bin directory under the robot directory, you can go to the lib directory from the bin directory using:
$ cd ../lib
Which is the lib directory under the parent directory of bin.
Additionally, entering the cd command will take you directly back to the pi user’s home directory /home/pi/
Entering the cd .. command will take you back to the parent directory of the current directory.
mkdir creates a new directory
If you want to create a directory named temp in the current directory, you can use:
$ mkdir temp
If you want to create this temp directory under /home/pi/projects, you can use:
$ mkdir /home/pi/projects/temp
Provided that the projects directory must exist, and you must have write permissions for it.
rmdir removes a directory
If you want to remove the temp directory, you can use:
$ rmdir temp
or:
$ rmdir /home/pi/projects/temp
But this requires that temp must be an empty directory; if there are other contents in temp, you may need to use:
$ rm -fr temp
The system will delete temp and all its contents.
cat displays or concatenates the contents of files
If you find a file named GirlsPhoneNum in the current directory, you may be eager to see it, so you can use:
$ cat GirlsPhoneNum
The entire content of the file will be printed in the terminal.
If you find another file named BoysPhoneNum and want to view both files together, you can use:
$ cat GirlsPhoneNum BoysPhoneNum
Yes, now you have the phone numbers of both boys and girls.
rm deletes a file
If you think the boy’s phone number is distracting you from seeing more important things, you can delete it:
$ rm BoysPhoneNum
Now you won’t find it even if you empty the recycle bin.
mv moves/renames files/directories
If you don’t want to delete the boys’ phone numbers but want to move them somewhere else, for example, to /home/pi/boys, you can:
$ mv BoysPhoneNum /home/pi/boys
Provided that the boys directory must exist; if it does not exist, you need to create it using the mkdir command.
If you are worried that your girlfriend will see the many girls’ numbers stored on your computer, you can rename it:
$ mv GirlsPhoneNum MyTeachersNum
cp copies files/directories
You can also copy the girls’ numbers to a safe place to prevent your girlfriend from deleting them:
$ cp GirlsPhoneNum /home/pi/Study/Linux
If you want to copy an entire directory to /home/pi:
$ cp -r GirlPhotos/ /home/pi
echo displays input content in the terminal
You can try the following command to see what it outputs:
$ echo Hello Raspberry Pi
date reads the system date/time
There is no system tray in the terminal to check the date and time; if you want to see the time, you need to use date.
grep searches for regular expressions and prints them
This is a feature-rich command that can search for files or directories using regular expressions and can also search through the output of some commands. Its usage could fill a book, but we only need to understand some of the simplest uses for now.
For example, if you want to know if there is a number for JingJing in the girls’ phone number file, you can:
$ grep JingJing GirlsPhoneNum
man displays the command help manual
If you don’t know how to use a command or you forget, you can man it:
$ man grep
The output may overwhelm you; you can use space to page down, press Enter to see the next line, and q to exit.
sudo executes with root privileges
The Raspberry Pi defaults to logging in as the “pi” user and does not enable the “root” user, which may corroborate the Linux geek saying, “only noobs log in as root.”
So when you need to perform some tasks as root, for example, if you need to hide the GirlsPhoneNum file in the /lib directory because your girlfriend will never think to look in the /lib directory where library files are stored, and writing files to the /lib directory generally requires root permissions, you can use sudo:
$ sudo cp GirlsPhoneNum /lib
Then the system will prompt you to enter the current user’s password, which is the password for the pi user. When entering the password, nothing will be displayed in the terminal, not even asterisks, so just enter it and press Enter.
chmod changes file read/write permissions
When introducing the ls command, you roughly learned that a file’s permissions can be represented in the following format:
-rwxrwxrwx
If you need to modify permissions, you will use the chmod command, which can modify permissions using both direct and binary methods.
Direct method
u represents the file owner
g represents users in the same group as the file owner
o represents all other users
For example, if a file’s permissions are:
-rwxrwxrwx
And you want other users to have only read permissions, you can use:
$ chmod o-wx filename
The file’s permissions will change to:
-rwxrwxr–
If you want to restore the original read, write, and execute permissions, use:
$ chmod o+wx filename
Numeric method
I prefer the numeric method, which assigns a number to each permission:
r = 4
w = 2
x = 1
Adding up the values of all permissions for each group will give you the number. For example, 744 means the file’s owner has rwx permissions, while users in the same group and all other users only have r permissions. If you want to set a file’s permissions to:
-rwxr-xr-x
You can use:
$ chmod 755 filename
Maybe you’re a bit confused; take it slow and you’ll get used to it.
./program runs the program
If you want to run a certain program (executable file), just enter its path in the terminal. If you want to execute the program in the current directory, you need to use:
$ ./program
apt-get installs/removes software packages
So how do you install software on the Raspberry Pi? Or uninstall software?
Generally, you do not need to download from a website first; you can directly use the package management tool apt-get. For example, to install the vim editor, you can:
$ sudo apt-get install vim
If you want to delete it, that’s simple too:
$ sudo apt-get remove vim
apt-get needs to run with root permissions, so add sudo in front of it.
If you want to upgrade the system and installed packages:
$ sudo apt-get update
$ sudo apt-get upgrade
Some Linux distributions use yum or dnf to manage software packages, but this is not very relevant to the Raspberry Pi’s Raspbian.
You may have also heard of compiling and installing with commands like make, but you can save that for later when you get tired of the current stuff.
exit exits
When you want to exit the terminal, simply enter exit and remember to press Enter afterward.
reboot restarts the system
If you want to restart the system:
$ sudo reboot
shutdown shuts down the system
If you want to shut down the system immediately:
$ sudo shutdown -h now
【Related Articles】
• Settings after the first startup of the Raspberry Pi
• How to install the operating system on the Raspberry Pi
• If you belong to these five types of people, you need to enjoy a Raspberry Pi
Li Bo’s Thinking Notes
Long press to follow and think together