Linux 基本操作指令
Linux 是一种开源的操作系统,是一种类 Unix 操作系统。它的内核由林纳斯·托瓦兹(Linus Torvalds)在 1991 年首次发布。Linux 是自由软件和开源软件的代表,它的源代码可以自由获取、自由传播、自由修改。Linux 的内核是 Linux Torvalds 开发的,但是 Linux 系统中包含了大量的 GNU 软件,因此 Linux 系统通常被称为 GNU/Linux 系统。
本章课程我们将按照一个初学者循序渐进的学习步骤步,展开我们的学习内容,具体安排如下:
- Linux 系统的基本命令行操作。
- Linux 文件系统结构 (File System Hierarchy, FHS); 系统初始化配置。
- 综合课程设计:在 U 盘中安装 Linux 系统,以供随时随地使用。
在本节,我们将学习 Linux 系统的基本命令行操作。这些操作是使用 Linux 系统的基础,掌握这些命令行操作可以帮助我们更好地理解和使用 Linux 系统。
Fisrtly, prepare for your consequent practice. Please copy the following code to your Linux terminal and then run it :
echo "This is an example file.
It contains several lines of text for practicing a series of Linux shell commands.
1. The first line of text.
2. The second line of text.
3. The third line of text.
End of the file.
" > example.txt
01. Linux 命令的用途
Linux 命令 是一种 Unix 命令或shell过程。它们是用于与 Linux 进行交互的基本工具。Linux 命令用于执行各种任务,包括显示有关文件和目录的信息。
Linux 操作系统用于服务器、桌面,甚至可能是您的智能手机。它有许多可以在系统上几乎做任何事情的命令行工具。
所有用户都应该熟悉大多数这些命令,因为它们是大多数操作系统任务和计算机编程所必需的。
02. 24 个最常用的 Linux 命令
1. ls 命令
ls常用于识别工作目录中的文件和目录。这个命令是经常使用的 Linux 命令之一,你应该知道。。
Exercise:
cd ~ && ls. Check the output. Have you found the file example.txt ?
2. pwd 命令
pwd 主要用于在终端打印当前工作目录。这也是最常用的命令之一。
Exercise:
pwd. What is your current working dir ?
3. mkdir 命令
此mkdir 命令允许您在终端本身创建新的目录。默认语法是 mkdir <dirname> 并将创建新目录。
Exercise:
mkdir my_new_dirto create a new folder, andlsto check if it exists.
4. cd 命令
cd 用于在目录之间导航。它需要完整路径或目录名称,具体取决于您当前的工作目录。如果运行此命令时不带任何选项,它将带您到主文件夹。请记住,只有具有sudo 权限的用户才能执行此操作。
Exercise:
cd my_new_dir. and thenpwdto check your current working dir. What is it ?
Exercise: create a subdir named sub_dir within my_new_dir.
5. rmdir 命令
rmdir 用于永久删除空目录。执行此命令的用户必须在父目录中拥有sudo权限。
Exercise: remove the newly cretated dir subdir.
6. cp 命令
Linux 的cp 相当于 Windows 中的复制粘贴和剪切粘贴。
Exercise: copy the file example.txt under
~/to~/my_new_dir, with the same file name.cp ~/example.txt ~/my_new_dir/example.txt
7. mv 命令
mv 通常用于重命名 Linux 中的文件。
Exercise: rename example.txt under
~/my_new_dir/as example1.txt.cd ~/my_new_dir/ && mv example.txt example1.txt. Uselsto check if you success.
8. rm 命令
Linux 中的rm 通常用于删除目录中创建的文件。
Exercise: remove the newly created example1.txt.
rm example1.txt
Exercise:
rmcan also be used for delete directory asrmdir. And the syntax should berm -r, i.e. with-roption. Try it.
9. uname 命令
uname 用于检查系统的完整 OS 信息。查看下面的命令和输出。
Exercise: Try
uname,uname -aanduname --helprespectively. Compare the ouput of them.
10. locate 命令
locate 通常用于在数据库中定位文件。
Note: Maybe you should install plocate firstly.
sudo apt install plocate
Exercise:
locate example.txt. Watch the output and guess the usage of this command.
Exercise: You could use
findto search the example.txt precisely.find / -type f -name example.txt 2>/dev/null
练习:
locate hosts.conf, 并查看此文件的内容。
11. touch 命令
touch 创建一个空文件。
Exercise: create an empty plain text file hello.txt.
touch hello.txt.
12. cat 命令
cat 是查看特定文件内容时使用的最简单命令。唯一的问题是它只是将整个文件卸载到您的终端。如果要在大文件中导航,应使用**less**命令替代。
Exercise: print the content of hello.txt.
cat hello.txt
13. echo 命令
Linux 中的echo 特别用于在终端中打印某些内容。
Exercise: Print "hello world" in the terminal.
echo "hello world"
Exercise: We could write the output of
echoto the empty file hello.txt.echo "hello world" > hello.txt. Check the content of "hello.txt" withcat.
14. ln 命令
ln 用于创建指向另一个文件的快捷方式链接。如果您想作为 Linux 管理员操作,这是最重要的 Linux 命令之一。
Exercise: create a soft link of hello.txt, the destiny file is world.txt.
ln -s ./hello.txt ./world.txt. Check the result withls -l. Can you find anything unusual ?
15. clear 命令
clear 是清除终端屏幕的标准命令。
Exercise:
clear. What happened ?
16. ps 命令
Linux 中的ps 用于检查终端中的活动进程。
Exercise:
ps. What is the output ? It print all log in shell.
17. man 命令
man 显示终端中可用的任何命令或实用程序的用户手册,包括它们的名称、描述和选项。
查看完整手册的命令:
man <命令名>
Exercise:
man ps. Read the 1st paragraph of DESCRIPTION section. Tell your parter what the commandpsis used for ?
18. grep 命令
grep 用于在一系列输出中查找特定字符串。例如,如果您想在文件中查找字符串,可以使用语法: grep 'string' filename.
Exercise:
cd ~to return home- Then check if example.txt still there.
ls- Print the content of example.txt.
cat example.txt- grep. Type
grep second example.txt. What happened ?
19. wget 命令
Linux 命令行中的wget 允许您从互联网下载文件。它在后台运行,不会干扰其他进程。
Exercise:
wget https://orz.tips/assets/ideal-img/girl-water.676f094.200.png. Check the output withls. Did you succeed in downloading the image?
20. whoami 命令
whoami 提供基本信息,在使用多系统时非常有用。一般来说,如果您只使用一台电脑,则不需要像网络管理员那样频繁使用它。
Exercise:
whoami. Compare this command withid. Try the latter.
21. sort 命令
sort 命令通常用于对文件的输出进行排序。让我们使用该命令并查看输出。
Exercise: Run
cat example.txtandcat example.txt | sortrespectively. What makes the difference ?
22. whereis 命令
Linux 中的whereis 通常用于查看在此之后键入的任何命令的确切位置。让我们看看它的表现如何。
Exercise: You have used too much
ls. Let's see where is it:whereis ls. What if we runwhereis l, without letter 's' ?
23. df 命令
Linux 中的df 获取文件系统的详细信息。
Exercise:
df. Another similar one isdu. Compare the both.
24. wc 命令
Linux 中的wc 使用一组选项指示单词、字符、行等的数量。
wc -w # 显示单词数
wc -l # 显示行数
wc -m # 显示文件中存在的字符数
Exercise:
cat example.txt | wc,cat example.txt | wc -w, and the other two.
03. Linux 命令速查表
Linux 命令行是一个强大的工具,它让你在开发环境中拥有灵活性和控制力,从文件和目录操作到复杂的系统管理任务。这份Linux 命令速查表涵盖了开发者需要了解的最常用的 Linux 命令,并附有代码示例和易于学习的快捷方式。
本快速参考帮助你熟悉以下基本 Linux 命令:
1. 应用程序与进程管理
which- 获取命令可执行文件的完整路径。apt/apt-get- 搜索、安装、删除和更新 Debian 和 Ubuntu 的软件包及其依赖项。
2. 控制台与输出管理
cat- 在终端中显示文件内容。clear- 清除终端显示。echo- 在终端中打印文本或变量。top- 获取正在运行的进程信息。
3. 创建和导出环境变量
env- 显示系统上运行的所有环境变量。export- 导出环境变量。printenv- 将特定的环境变量打印到控制台。source- 从当前 shell 中执行存储在文件中的命令,或者刷新环境变量。
4. 文件和目录操作
cd- 切换到另一个目录。cp- 将源目录或文件的内容复制到目标目录或文件。find- 按名称查找文件或目录。grep- 在输出中搜索字符串。ls- 列出目录内容。mkdir- 创建目录。more- 查看和遍历文件或标准输出的内容。mv- 移动或重命名文件。pwd- 获取当前工作目录的名称。rm- 删除文件或目录。tar- 解压和压缩文件。
5. 访问命令行帮助文档
man- 访问所有 Linux 命令的手册页。
6. 在 Linux 计算机上使用网络
curl- 根据 URL 从互联网获取或发送文件。ip- 获取物理机或虚拟机的 IP 信息。netstat- 获取网络连接等信息。ssh- 在不安全的网络中建立两台主机之间的安全加密连接。wget- 直接从互联网下载文件。
7. 进程管理
&&- 按顺序执行命令。kill- 从内存中移除正在运行的进程。ps- 显示活动进程。
8. 系统控制
poweroff- 关闭计算机。restart- 重启计算机。
9. 用户管理
whoami- 显示用户 ID。
Homework
Learn by yourself:
- What is environment variable(环境变量) in Linux.
- Write up about Environment Variables.
- Prove with an example that you have mastered environment variables.
- You should NOT write a too long text. Please refer to 网络协同报告写作及质量标准 (password: 2023).