开始使用Raspberry Pi可能会让人望而生畏。即使您在第一次设置 Pi 时找到了一个很好的指南(good guide),也有很多东西要学。Raspberry Pis在Linux上运行,如果您以前从未使用过Linux操作系统,它可能看起来很奇怪和复杂。
虽然您已经知道如何在 PC 或Mac上执行基本任务(例如查看文件夹和文件) ,但在 Pi 上执行这些操作的方式有所不同,尤其是在您运行的版本没有图形用户界面 ( GUI ) 时。下面,我们将带您了解使用 Pi 时需要知道的常见Raspberry Pi Linux终端命令。(Raspberry Pi Linux)
1.列出当前(Current)目录(Contents)的内容
命令ls代表“列表”。这是您将在 Pi 上使用的最基本的Linux命令。(Linux)在终端输入ls ,回车(Enter),会返回当前目录下所有文件和文件夹的列表。
2. 更改你的 Pi 密码
passwd命令可能应该是您在Raspberry Pi上使用的第一个Linux命令之一。您没有使用默认密码运行您的 Pi,是吗?这不好。要更改 Pi 的密码,请在终端中输入passwd 。
它将提示您输入当前密码,因此输入并按Enter。接下来,输入您的新密码并按Enter。接下来,它会要求您确认新密码。再次输入,按Enter,您已成功更改 Pi 的密码。
3. 重启或关闭你的 Pi
重新启动或关闭您的 Pi 需要 root 访问权限,因此您必须使用sudo命令。Sudo是一个Linux命令,代表SuperuserDo。它允许您以提升的权限执行Raspberry Pi Linux命令——安装程序或重新启动机器等操作需要这些权限。要使用 sudo ,请输入sudo后跟要执行的命令。
要关闭您的 Pi,请输入sudo shutdown。当你点击Enter时,它会询问你 Pi 的 root 密码。此命令将在一分钟内关闭您的 Pi。使用sudo shutdown 0立即关闭。
要重新启动您的 pi,请使用sudo shutdown -r。默认情况下,您的 Pi 将在一分钟内重新启动。如果您希望它立即重新启动,您可以使用sudo shutdown -r 0,其中0代表零分钟或现在(right now)。
4.更改目录
cd命令代表——你猜对了——改变目录。它会更改当前工作目录,即您当前所在的任何目录。键入cd /[您要转到的目录的路径(path of the directory you want to go to)] 。这是一个示例:cd /usr/lib。在终端中键入该命令会将您带到 Pi 上的 user/lib 文件夹。
或者,您可以键入cd ..,它将在文件夹层次结构中向上移动一个目录。或者你可以使用cd ~。这会将您移动到登录用户的主目录,而cd /会将您移动到根文件夹。最后,cd –将您带到您所在的上一个文件夹。将该(Think)命令视为撤消上一个cd命令。
5. 在你的 Pi 上复制文件
cp命令复制文件和目录。一般来说,Raspberry Pi Linux命令将如下所示:cp [源文件位置] [目标文件位置](cp [source file location] [destination file location])。
复制文件时,您可以同时重命名它们。如果要复制当前目录中名为test.txt的文件并将其重命名为(test.txt)test2.txt,则命令为cp test.txt test2.txt。原始文件和文件的重命名副本都将位于当前目录中。使用ls命令查看新文件。
6. 重命名 Pi 上的文件
要重命名文件,请使用mv命令。例如,如果您使用mv test.txt test2.txt命令,重命名的文件将位于当前目录中。
7. 移动文件或文件夹
将文件从一个文件夹移动到另一个文件夹的工作方式类似于重命名文件。输入mv [文件名] [目标文件夹](mv [filename] [destination folder])。这假定您要移动的文件位于当前目录中。这是一个示例:mv test.txt ~/。该命令会将 test.txt 文件从当前目录移动到用户的主(home)文件夹。像往常一样,如果您收到“权限被拒绝”消息,请将sudo添加到命令的开头。
如果要移动的文件不在(not)当前目录中,可以使用如下命令:mv /usr/lib/test.txt ~/。该命令会将test.txt文件从usr/lib目录移动到用户的主(home)目录。
顺便说一句,您还可以在移动文件时(while )重命名文件。输入mv ~/test.txt /usr/lib/test2.txt。在此示例中,我们将 text.txt 文件重命名为 test2.txt 并将其从主(home)目录移至usr/lib 文件夹。
8. 编辑文本文件
Linux命令行文本编辑器称为nano。要运行 nano,请键入nano [要打开或创建的文本文件的路径](nano [path to the text file you want to open or create])。某些文件夹需要创建或编辑文件的权限。如果是这种情况,请使用sudo nano [filepath]。(如果您需要许可,编辑器会告诉您,您可以将其关闭并使用sudo(sudo)重新运行命令。)
如果您使用 nano 打开现有文件,它将打开文件进行编辑。如果您正在创建一个新文件,Linux将打开一个没有文本的空编辑器。您可以使用箭头键和键盘输入您想要的任何内容。请注意,终端窗口底部有一个命令菜单。它们都以^开头。在Linux中,这意味着您应该在使用该命令时 按住ctrl 。
要保存文件,请按ctrl+o。如果需要,您可以更改文件名。按Enter将保存文件。如果要退出,请按ctrl+x。如果您退出并且有未保存的更改,它会询问您是否要保存它们。通过输入y(y)并按Enter选择保存。或者您可以输入n并按Enter放弃更改。
9. 查找已安装程序的位置
要在您的 Pi 上查找已安装程序的位置,您将使用whereis命令。此命令查找任何已安装的软件包。输入whereis [包名](whereis [package name])。
例如,如果您正在寻找名为gcc的 C++ 编译器,您将输入whereis gcc,终端将显示可执行文件的完整路径,无论它存在于您的机器上的任何位置。在下面的屏幕截图中,已在两个地方找到该包。如果它在任何地方都找不到包,它将显示gcc:。
10. Apt-Get
这是最有趣的Raspberry Pi Linux命令之一。apt-get命令将在一个命令中找到您想要的包,下载并安装它。甜的!安装文件时,您需要提升权限,因此请输入sudo apt-get install [name of the package you want to install]。
如果要安装htop(一个交互式进程监视器,将显示您的 Pi 的CPU利用率、内存使用情况等),请输入以下命令 sudo apt-get install htop(sudo apt-get install htop)。
奖励:如何复制文本(Text)并将其粘贴(Paste)到 Pi 的终端窗口中(Terminal Window)
Windows复制/粘贴快捷方式在(Windows)Linux中不起作用。假设您从 PC 远程连接到您的 Pi,并且您想从Windows上的密码管理器中复制您的 Pi 密码。您不能只选择密码,使用CTRL + C 复制它,然后CTRL + V 将其粘贴到 Pi 的终端中。 (. )
但是,您可以使用(can)CTRL + C从Windows复制密码,然后在终端窗口中单击鼠标右键。(single right-click )右键单击将剪贴板中的文本粘贴到终端中。然后,按Enter。
请注意:您不会看到任何证据表明您已将任何内容粘贴到终端中,但它肯定存在!
Top 10 Raspberry Pi Linux Commands You Should Know
Getting stаrted 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:.
10. Apt-Get
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!