基本 Linux 命令
最后修改于 2023 年 10 月 18 日
本文将介绍 Shell 中的基本 Linux 命令。本教程将介绍各种基本 Linux 命令。
开始时,我们假设有一个空目录。
pwd 命令
我们从主目录开始。pwd
命令打印当前工作目录。
$ pwd /home/jano/Documents/prog/linux/basic-commands
clear 命令
clear
命令可以清除终端屏幕。除了此命令,我们还可以使用 Ctrl + L 快捷键。
$ clear
whoami 命令
whoami
命令打印用户名。当管理员登录到多台机器时,此命令很有用。
$ whoami jano
hostname 命令
hostname
显示主机名。主机名是网络上机器的名称。
$ hostname andromeda
date 命令
date
命令打印当前的本地日期。
$ date So 7. január 2023, 12:46:30 CET
该命令打印当前的本地日期和时间。
$ date -u So 7. január 2023, 11:48:20 UTC
此命令以 UTC 格式打印当前的日期和时间。
$ date +%Y 2023 $ date +%F 2023-01-07
我们以指定格式打印日期。
$ date +%s 1673092327
这是 Unix 时间戳——自 1970 年 1 月 1 日 00:00:00 UTC 以来经过的秒数。
uptime 命令
$ uptime 04:39:18 up 45 min, 1 user, load average: 0.00, 0.01, 0.00
uptime
命令显示系统运行了多长时间。
man 命令
man
命令提供命令的手册。在那里我们可以找到命令的所有选项。
$ man date
cal 命令
cal
命令显示日历。
$ cal Január 2023 Ne Po Ut St Št Pi So 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
$ cal -3 December 2022 Január 2023 Február 2023 Ne Po Ut St Št Pi So Ne Po Ut St Št Pi So Ne Po Ut St Št Pi So 1 2 3 1 2 3 4 5 6 7 1 2 3 4 4 5 6 7 8 9 10 8 9 10 11 12 13 14 5 6 7 8 9 10 11 11 12 13 14 15 16 17 15 16 17 18 19 20 21 12 13 14 15 16 17 18 18 19 20 21 22 23 24 22 23 24 25 26 27 28 19 20 21 22 23 24 25 25 26 27 28 29 30 31 29 30 31 26 27 28
使用 -3
选项,我们可以看到三个月:当前月、上个月和下个月。
arch 命令
arch
命令显示机器架构。
$ arch x86_64
$ arch --help Usage: arch [OPTION]... Print machine architecture. --help display this help and exit --version output version information and exit ...
大多数命令都有 --help
选项,它会提供关于该命令的简短帮助信息。
uname 命令
uname
命令提供系统信息。-a
选项列出所有信息。
$ uname Linux $ uname -a Linux andromeda 5.15.0-56-generic #62-Ubuntu SMP Tue Nov 22 19:54:14 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
who 命令
who
命令列出当前登录的用户。
$ who jano tty7 2023-01-07 10:04 (:0)
我们有一个登录用户。
w 命令
$ w 12:56:24 up 2:55, 1 user, load average: 0,54, 0,50, 0,46 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT jano tty7 :0 10:04 2:55m 4:41 0.18s xfce4-session
w
命令显示谁登录了以及用户正在做什么。
echo 命令
echo
命令将一行文本打印到终端。
$ echo an old falcon an old falcon
我们打印一条小消息。
$ echo "an old falcon" | sed 's/falcon/wolf/' an old wolf
sed
命令用于转换文本。我们将文本字符串中的“falcon”一词更改为“wolf”。
$ echo $LANG $PWD en_US.UTF-8 /home/jano/Documents/prog/linux/basic-commands $ echo $SHELL $RANDOM $HOSTNAME /bin/bash 15604 andromeda
这里我们打印几个环境变量。
printf 命令
使用 printf
命令,我们可以输出格式化的字符串。
$ printf "%s is %d years old\n" Jane 17 Jane is 17 years old
我们使用 printf
构建一条消息。
ping 命令
ping
命令用于检查网络连接。
$ ping webcode.me PING webcode.me (46.101.248.126) 56(84) bytes of data. 64 bytes from 46.101.248.126 (46.101.248.126): icmp_seq=1 ttl=54 time=29.2 ms 64 bytes from 46.101.248.126 (46.101.248.126): icmp_seq=2 ttl=54 time=27.9 ms ...
ping
命令会持续发送请求,直到我们用 Ctrl + C 终止它。
$ ping -c 4 webcode.me PING webcode.me (46.101.248.126) 56(84) bytes of data. 64 bytes from 46.101.248.126 (46.101.248.126): icmp_seq=1 ttl=54 time=32.1 ms 64 bytes from 46.101.248.126 (46.101.248.126): icmp_seq=2 ttl=54 time=34.6 ms 64 bytes from 46.101.248.126 (46.101.248.126): icmp_seq=3 ttl=54 time=33.4 ms 64 bytes from 46.101.248.126 (46.101.248.126): icmp_seq=4 ttl=54 time=37.9 ms --- webcode.me ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3004ms rtt min/avg/max/mdev = 32.100/34.497/37.876/2.145 ms
使用 -c
选项,我们可以指定发送请求的数量。
df 命令
df
命令报告文件系统的磁盘空间使用情况。
$ df -h Filesystem Size Used Avail Use% Mounted on tmpfs 1,6G 3,5M 1,6G 1% /run /dev/nvme0n1p2 468G 194G 251G 44% / tmpfs 7,8G 14M 7,7G 1% /dev/shm tmpfs 5,0M 4,0K 5,0M 1% /run/lock tmpfs 7,8G 0 7,8G 0% /run/qemu /dev/nvme0n1p1 511M 5,3M 506M 2% /boot/efi tmpfs 1,6G 140K 1,6G 1% /run/user/1000
-h
选项以人类可读的格式显示大小,即以 1024 的幂次方显示。
type 命令
命令有两种类型:shell 内建命令和外部命令。type
命令可用于给出命令类型。
$ type echo who echo is a shell builtin who is hashed (/usr/bin/who)
我们检查 echo
和 who
命令。
history 命令
history
显示用户输入命令的历史记录。
$ history ... 2017 man date 2018 date %F 2019 man date 2020 date -u 2021 man date 2022 date +%Y 2023 date +%F 2024 date +%s 2025 cal 2026 cal -3 2027 uname -a 2028 who 2029 w 2030 ping webcode.me 2031 df -h 2032 ping 2033 man ping 2034 ping -c 4 webcode.me 2035 man ping 2036 history ...
我们显示已执行命令的部分列表。
curl 命令
curl
命令可用于生成 HTTP 请求。
$ curl webcode.me <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>My html page</title> </head> <body> <p> Today is a beautiful day. We go swimming and fishing. </p> <p> Hello there. How are you? </p> </body> </html>
我们向网页生成一个 GET 请求;我们得到一个打印到控制台的 HTML 文档。
$ curl -I webcode.me HTTP/1.1 200 OK Server: nginx/1.6.2 Date: Sat, 07 Jan 2023 12:05:49 GMT Content-Type: text/html Content-Length: 394 Last-Modified: Sun, 23 Jan 2022 10:39:25 GMT Connection: keep-alive ETag: "61ed305d-18a" Accept-Ranges: bytes
使用 -I
选项,我们生成一个 HEAD 请求,该请求返回包含一些元数据的文档的标头。
wget 命令
wget
是一个非交互式网络检索器。
$ wget -q webcode.me/words.txt $ wget -q webcode.me/thermopylae.txt
使用 wget
命令,我们下载两个小的文本文件。-q
(quiet)选项会抑制命令的输出。
$ ls thermopylae.txt words.txt
ls 命令
ls
命令列出当前目录的内容;我们有两个文件。
$ ls -l total 8 -rw-rw-r-- 1 jano jano 226 júl 26 2021 thermopylae.txt -rw-rw-r-- 1 jano jano 140 júl 26 2021 words.txt
使用 -l
,我们获得长列表格式。我们获得额外信息,包括文件权限、所有权、大小和最后修改日期。
wc 命令
wc
命令计算文件中的行数、单词数和字节数。
$ wc thermopylae.txt 4 38 226 thermopylae.txt
thermopylae.txt
文件有 4 行,38 个单词和 226 个字节。
$ wc -l thermopylae.txt 4 thermopylae.txt $ wc -w thermopylae.txt 38 thermopylae.txt $ wc -c thermopylae.txt 226 thermopylae.txt
我们可以使用相应的选项单独获取信息。
cat 命令
cat
命令将文件连接起来并在控制台上打印它们。该命令通常用于显示小文件的内容。
$ cat thermopylae.txt The Battle of Thermopylae was fought between an alliance of Greek city-states, led by King Leonidas of Sparta, and the Persian Empire of Xerxes I over the course of three days, during the second Persian invasion of Greece.
我们显示 thermopylae.txt
文件的内容。
cp 命令
cp
命令用于复制文件和目录。
$ cp thermopylae.txt thermopylae2.txt
使用 cp
命令,我们创建了 thermopylae.txt 文件的副本。新文件名为 thermopylae2.txt。
$ echo "The battle took place simultaneously with the naval battle at Artemisium." >> thermopylae2.txt
使用 echo
命令和 >> 运算符,我们将新行追加到 thermopylae2.txt
文件。
$ cat thermopylae2.txt The Battle of Thermopylae was fought between an alliance of Greek city-states, led by King Leonidas of Sparta, and the Persian Empire of Xerxes I over the course of three days, during the second Persian invasion of Greece. The battle took place simultaneously with the naval battle at Artemisium.
我们用 cat
检查文件内容;它包含新行。
diff 命令
diff
命令逐行比较两个文本文件。
$ diff thermopylae.txt thermopylae2.txt 4a5 > The battle took place simultaneously with the naval battle at Artemisium.
输出表明,必须将第二个文件的第 5 行附加到第 4 行才能使文件相同。以 < 行开头的行是第一个文件的行。以 > 行开头的行是第二个文件的行。特殊字符是 a(添加)、d(删除)和 c(更改)。
head 命令
head
命令将每个文件的前 n 行(默认为十行)打印到标准输出。
$ head words.txt sky blue falcon rock wood forest book small tension war
head
命令打印文件的前十行。
$ head -n 3 words.txt sky blue falcon
使用 -n
选项,我们可以指定要显示的行数。
$ head -n 3 words.txt thermopylae.txt ==> words.txt <== sky blue falcon ==> thermopylae.txt <== The Battle of Thermopylae was fought between an alliance of Greek city-states, led by King Leonidas of Sparta, and the Persian Empire of Xerxes I over the course of three days, during the second Persian invasion of Greece.
我们可以指定多个文件。
tail 命令
tail
命令将每个文件的最后 n 行(默认为十行)打印到标准输出。
$ tail words.txt pen purple bow rock falcon owl bear wolf fox storm
tail
命令打印最后十行。
$ head -4 words.txt sky blue falcon rock $ tail -3 words.txt wolf fox storm
我们可以指定要输出的前/后多少行。
mkdir 命令
mkdir
命令创建一个新目录。
$ mkdir docs
我们创建一个名为 docs
的新目录。
$ ls -F docs/ thermopylae2.txt thermopylae.txt words.txt
-F
选项为文件附加指示符;/
表示 docs
是一个目录。
mv 命令
mv
命令移动或重命名文件和目录。
$ mv thermopylae2.txt docs/
mv
命令将给定文件移动到该目录。
apt
命令显式安装 tree
命令。$ ls docs/ thermopylae2.txt
我们列出 docs
目录的内容;它包含一个文件。
某些命令可能默认未安装在我们的机器上。例如,在 Debian Linux 上,我们需要通过 apt
命令显式安装 tree
命令。
apt 命令
apt
为包管理系统提供了一个高级命令行接口。
$ sudo apt install tree
apt
是一个 Debian 特定的包管理器。这里我们还假设 sudo
命令已安装在系统上,并且用户可以运行 sudo
命令。
tree 命令
tree
命令以树状格式列出当前目录的内容。
$ tree . ├── docs │ └── thermopylae2.txt ├── thermopylae.txt └── words.txt 1 directory, 3 files
我们可以看到我们到目前为止创建的文件和目录。
touch 命令
touch
命令将每个文件的访问和修改时间更新为当前时间。如果文件不存在,则会创建它。
$ touch vals.txt $ ls -s vals.txt 0 vals.txt
使用 touch
,我们创建了一个名为 vals.txt 的空文件。ls-s
命令打印文件大小。我们可以看到它是空的。
$ echo -e "8\n9\n11\n12\n7\n6\n3\n2\n4\n1\n5" >> vals.txt $ cat vals.txt 8 9 11 12 7 6 3 2 4 1 5
我们使用 echo
将 11 个数字插入到 vals.txt
文件中。echo
的 -e
选项会评估反斜杠转义。
$ ls -l vals.txt -rw-rw-r-- 1 jano jano 24 jan 7 13:24 vals.txt
现在文件有 24 个字节。
$ cat -E vals.txt 8$ 9$ 11$ 12$ 7$ 6$ 3$ 2$ 4$ 1$ 5$
文件中包含 13 个字符和 11 个换行符;13 加 11 等于 24。cat
的 -E
选项会打印换行符处的 $ 符号。
$ ls -l vals.txt | awk '{print $5}' 24
我们可以使用 awk
命令仅打印大小信息;我们输出行中的第五列。
sort 命令
sort
命令对文本文件的行进行排序。
$ sort words.txt | head bear blue book bow cloud cup falcon falcon forest fox
sort 命令对文本文件的行进行排序。在我们的示例中,我们对单词进行排序并打印前十行。
$ sort -r words.txt | head wood wolf water warm war tension storm snow small sky
使用 -r
选项,我们可以按反向顺序排序。
$ sort -n vals.txt 1 2 3 4 5 6 7 8 9 11 12
使用 -n
选项,我们可以按数值顺序排序。
$ awk '{ print $0, "has", length($0), "chars"}' words.txt sky has 3 chars blue has 4 chars falcon has 6 chars rock has 4 chars wood has 4 chars forest has 6 chars book has 4 chars small has 5 chars tension has 7 chars war has 3 chars water has 5 chars warm has 4 chars cup has 3 chars ...
awk
是一个非常复杂的命令。在这里,我们打印 words.txt
文件中每个单词的长度。$0
是整个输入记录,即文件中的一行。length
函数给出给定记录的大小。
$ ls docs thermopylae.txt vals.txt words.txt
此时,我们有这些文件。
rm 命令
rm
命令删除文件或目录。
$ rm *.txt
我们使用 rm
命令删除所有文本文件。
$ rm docs/thermopylae2.txt $ rmdir docs/
我们使用 rmdir
命令删除一个目录。在删除目录之前,该目录必须是空的。我们也可以使用 rm -rf
命令一次性删除一个目录。请注意,此命令很危险,应谨慎使用。
$ ls
目录是空的。
在本文中,我们介绍了基本的 Linux shell 命令。