Ubuntu基本命令的熟悉和使用
宣传一下自己的博客Ubuntu基本命令的熟悉和使用
为区分命令输入和返回,采用将终端的输出完全复制的策略
六个常用的终端命令
本篇仅介绍最常用的几个文件操作指令,对于其他的指令在使用时会补充介绍
| 序号 | 命令 | 对应英文 | 作用 |
|---|---|---|---|
| 01 | ls | list | 查看当前文件夹下的内容 |
| 02 | pwd | print work directory | 查看当前所在文件夹 |
| 03 | cd [目录名] | change directory | 切换文件夹 |
| 04 | touch [目录名] | touch | 如果文件不存在,新建文件 |
| 05 | mkdir [目录名] | make directory | 创建目录 |
| 06 | rm [文件名] | remove | 删除指定的文件名 |
-
首先使用
ls命令,查找默认目录下的文件ubuntu@VM-8-11-ubuntu:~$ ls ubuntu@VM-8-11-ubuntu:~$根据结果可得,此处没有任何文件
-
使用
pwd命令查找此处的文件路径ubuntu@VM-8-11-ubuntu:~$ pwd /home/ubuntu ubuntu@VM-8-11-ubuntu:~$由此可见,在上面还有一级home
-
使用
cd ..命令返回到上一级文件目录ubuntu@VM-8-11-ubuntu:~$ cd .. ubuntu@VM-8-11-ubuntu:/home$ ubuntu@VM-8-11-ubuntu:/home$ pwd /home ubuntu@VM-8-11-ubuntu:/home$
根据标识可知,现在已经来到了home文件
-
再次使用
cd ..命令,返回到根目录ubuntu@VM-8-11-ubuntu:/home$ cd .. ubuntu@VM-8-11-ubuntu:/$ ls bin boot cdrom data dev etc home lib lib32 lib64 libx32 lost+found media mnt opt proc root run sbin srv sys tmp usr var ubuntu@VM-8-11-ubuntu:/$由此可见,根目录下有很多种文件
-
然后尝试返回home/ubuntu文件夹
ubuntu@VM-8-11-ubuntu:/$ cd home/ubuntu ubuntu@VM-8-11-ubuntu:~$ ls -
使用
mkdir命令尝试建立新的文件夹ubuntu@VM-8-11-ubuntu:~$ mkdir test ubuntu@VM-8-11-ubuntu:~$ ls test ubuntu@VM-8-11-ubuntu:~$由此可见,
mkdir命令成功建立了一个名为test的新文件夹 -
使用
touch命令创建hello.cubuntu@VM-8-11-ubuntu:~$ cd test ubuntu@VM-8-11-ubuntu:~/test$ touch hello.c ubuntu@VM-8-11-ubuntu:~/test$ ls hello.c ubuntu@VM-8-11-ubuntu:~/test$可见,
touch命令成功创建了hello.c文件 -
使用
vim编辑hello.c终端界面:
ubuntu@VM-8-11-ubuntu:~/test$ vim hello.c ubuntu@VM-8-11-ubuntu:~/test$vim界面:
hello,world! ~ ~ "hello.c" 1L, 13C 1,12 All使用
ESC+:wq+ENTER退出vim编辑器 -
再次使用
touch命令新建hello.c`文件终端界面
ubuntu@VM-8-11-ubuntu:~/test$ touch hello.c ubuntu@VM-8-11-ubuntu:~/test$ vim hello.c ubuntu@VM-8-11-ubuntu:~/test$vim界面:
hello,world! ~ ~ "hello.c" 1L, 13C 1,12 All可以看到,对于已经建好的文件,touch命令不会有效果
-
使用
rm命令删除hello.cubuntu@VM-8-11-ubuntu:~/test$ ls hello.c ubuntu@VM-8-11-ubuntu:~/test$ rm hello.c ubuntu@VM-8-11-ubuntu:~/test$ ls ubuntu@VM-8-11-ubuntu:~/test$可以看到
hello.c已经被成功删除 -
使用
rm -r强制删除test目录ubuntu@VM-8-11-ubuntu:~/test$ cd .. ubuntu@VM-8-11-ubuntu:~$ rm test rm: cannot remove 'test': Is a directory ubuntu@VM-8-11-ubuntu:~$ rm -r test ubuntu@VM-8-11-ubuntu:~$ ls ubuntu@VM-8-11-ubuntu:~$由此可见,
rm -r可以强制删除目录
其他常用命令
whereis 命令
基本语法
whereis [options] [string...]
- [options]:可以是一些特定的选项,如
-bmsu。 - [string…]:是你想要查找的程序或文件的名称。
选项说明
-b:只查找二进制文件。-m:只查找手册页。-s:只查找源代码。
示例
-
查找特定程序的路径:
ubuntu@VM-8-11-ubuntu:~$ whereis java java: /usr/share/java ubuntu@VM-8-11-ubuntu:~$ -
仅查找二进制文件:
ubuntu@VM-8-11-ubuntu:~$ whereis -b python python: /usr/bin/python3.8 /usr/bin/python2.7 /usr/bin/python /usr/lib/python3.8 /usr/lib/python2.7 /usr/lib/python3.9 /etc/python3.8 /etc/python2.7 /etc/python /usr/local/lib/python3.8 /usr/local/lib/python2.7 /usr/share/python ubuntu@VM-8-11-ubuntu:~$ whereis python python: /usr/bin/python3.8 /usr/bin/python2.7 /usr/bin/python /usr/lib/python3.8 /usr/lib/python2.7 /usr/lib/python3.9 /etc/python3.8 /etc/python2.7 /etc/python /usr/local/lib/python3.8 /usr/local/lib/python2.7 /usr/share/python /usr/share/man/man1/python.1.gz可以看到仅显示二进制文件查找到的比查找所有文件要少
-
查找手册页:
ubuntu@VM-8-11-ubuntu:~$ whereis -m gcc gcc: /usr/share/man/man1/gcc.1.gz这将显示
gcc的手册页位置。
ps命令
ps 命令用于显示当前系统中的进程状态。ps 是 “process status” 的缩写。这个命令用于监控系统性能、调试程序或查找占用资源的进程
基本语法
ps [options]
选项说明
-a:显示所有与终端会话关联的进程-u:以用户易读的格式显示进程信息x:显示没有控制终端的进程e:显示所有进程f:全格式,显示完整的信息
组合选项
-aux:显示所有进程的详细信息,包括环境变量-ef:显示所有进程的完整信息-aef:用于显示所有与终端会话关联的进程的完整信息
输出字段解释
PID:进程ID。TTY:进程所在的终端类型。TIME:CPU时间,单位通常是分钟。CMD:启动进程的命令。
示例
-
显示所有进程:
ubuntu@VM-8-11-ubuntu:~$ ps -ePID TTY TIME CMD1 ? 00:00:09 systemd2 ? 00:00:00 kthreadd3 ? 00:00:00 rcu_gp4 ? 00:00:00 rcu_par_gp6 ? 00:00:00 kworker/0:0H-kblockd -
显示所有进程的详细信息:
ubuntu@VM-8-11-ubuntu:~$ ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 Sep04 ? 00:00:09 /sbin/init root 2 0 0 Sep04 ? 00:00:00 [kthreadd] root 3 2 0 Sep04 ? 00:00:00 [rcu_gp] root 4 2 0 Sep04 ? 00:00:00 [rcu_par_gp] root 6 2 0 Sep04 ? 00:00:00 [kworker/0:0H-kblockd] root 8 2 0 Sep04 ? 00:00:00 [mm_percpu_wq] -
以用户易读的格式显示所有进程:
ubuntu@VM-8-11-ubuntu:~$ ps -aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.6 168520 12256 ? Ss Sep04 0:09 /sbin/init root 2 0.0 0.0 0 0 ? S Sep04 0:00 [kthreadd] root 3 0.0 0.0 0 0 ? I< Sep04 0:00 [rcu_gp] root 4 0.0 0.0 0 0 ? I< Sep04 0:00 [rcu_par_gp] root 6 0.0 0.0 0 0 ? I< Sep04 0:00 [kworker/0:0H-kblockd] -
显示特定用户的进程:
root用户ubuntu@VM-8-11-ubuntu:~$ ps -u rootPID TTY TIME CMD1 ? 00:00:09 systemd2 ? 00:00:00 kthreadd3 ? 00:00:00 rcu_gp4 ? 00:00:00 rcu_par_gp6 ? 00:00:00 kworker/0:0H-kblockdubuntu用户ubuntu@VM-8-11-ubuntu:~$ ps -u ubuntuPID TTY TIME CMD6222 ? 00:00:00 systemd6226 ? 00:00:00 (sd-pam)455485 ? 00:00:00 sshd455486 ? 00:00:00 bash455505 ? 00:00:00 code-fee1edb8d6 -
显示特定进程的详细信息:
ubuntu@VM-8-11-ubuntu:~$ ps -f --pid 6222 UID PID PPID C STIME TTY TIME CMD ubuntu 6222 1 0 Sep04 ? 00:00:00 /lib/systemd/systemd --user
grep命令
grep 命令用于搜索文件中的文本模式。它的名字来源于 “global regular expression print”,意味着全局正则表达式打印。
基本语法
grep 命令的基本语法如下:
grep [options] pattern [file...]
- [options]:可以是一些特定的选项,用于控制
grep的行为。 - pattern:要搜索的文本模式或正则表达式。
- [file…]:要搜索的文件列表,如果不指定文件,则
grep会从标准输入读取数据。
选项说明
-i:忽略大小写。-v:显示不匹配模式的行。-c:只输出匹配模式的行数。-l:只输出包含匹配模式的文件名。-n:显示匹配行及行号。-r或-R:递归搜索指定目录下的所有文件。
示例
首先现在/home/ubuntu目录下新建一个test.txt
ubuntu@VM-8-11-ubuntu:~$ touch test.txt
ubuntu@VM-8-11-ubuntu:~$ ls
test.txt
打开文本文件,向其中写入以下内容
abc
abcba
abccba
Abcba
abcCba
-
搜索包含特定文本的行:
ubuntu@VM-8-11-ubuntu:~$ grep "abc" test.txt abc abcba abccba abcCba ubuntu@VM-8-11-ubuntu:~$ -
忽略大小写搜索:
ubuntu@VM-8-11-ubuntu:~$ grep -i "abc" test.txt abc abcba abccba Abcba abcCba ubuntu@VM-8-11-ubuntu:~$ -
显示不匹配的行:
ubuntu@VM-8-11-ubuntu:~$ grep -v "abc" test.txt Abcba ubuntu@VM-8-11-ubuntu:~$ -
输出匹配行的行号:
ubuntu@VM-8-11-ubuntu:~$ grep -n "abc" test.txt 1:abc 2:abcba 3:abccba 5:abcCba ubuntu@VM-8-11-ubuntu:~$
管道|的使用
使用管道|和 grep 搜索特定进程
ubuntu@VM-8-11-ubuntu:~$ ps -aux | grep 'nginx'
root 20128 0.0 0.1 9852 4012 ? Ss Sep04 0:00 nginx: master process nginx
www-data 40165 0.0 0.1 10536 3484 ? S Sep04 0:00 nginx: worker process
www-data 40166 0.0 0.1 10536 3484 ? S Sep04 0:00 nginx: worker process
ubuntu 477580 0.0 0.0 6300 724 pts/3 S+ 18:14 0:00 grep --color=auto nginx
ubuntu@VM-8-11-ubuntu:~$
可以看到,如此可以返回所有包括nginx字样的进程信息
systemctl 命令
使用systemctl列出所有的linux服务
ubuntu@VM-8-11-ubuntu:~$ sudo systemctl --type=serviceUNIT LOAD ACTIVE SUB DESCRIPTION accounts-daemon.service loaded active running Accounts Service acpid.service loaded active running ACPI event daemon apparmor.service loaded active exited Load AppArmor profiles apport.service loaded active exited LSB: automatic crash report generation atd.service loaded active running Deferred execution scheduler blk-availability.service loaded active exited Availability of block devices
按ctrl+c返回终端
使用 systemctl 命令列出活动服务
该命令将过滤掉那些不活动(已停止)或失败的服务
ubuntu@VM-8-11-ubuntu:~$ sudo systemctl --type=service --state=activeUNIT LOAD ACTIVE SUB DESCRIPTION accounts-daemon.service loaded active running Accounts Service acpid.service loaded active running ACPI event daemon apparmor.service loaded active exited Load AppArmor profiles apport.service loaded active exited LSB: automatic crash report generation atd.service loaded active running Deferred execution scheduler blk-availability.service loaded active exited Availability of block devices
使用 systemctl 命令查看服务详细信息
ubuntu@VM-8-11-ubuntu:~$ sudo systemctl status nginx
● nginx.service - nginx - high performance web serverLoaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)Active: inactive (dead)Docs: https://nginx.org/en/docs/
ubuntu@VM-8-11-ubuntu:~$
可以看到,nginx未处于活跃状态
使用systemctl设置开机自启
ubuntu@VM-8-11-ubuntu:~$ sudo systemctl enable mysql
Synchronizing state of mysql.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable mysql
ubuntu@VM-8-11-ubuntu:~$
apt命令
apt 命令提供了查找、安装、升级、删除某一个、一组甚至全部软件包的命令
基本语法
apt [options] [command] [package ...]
常用命令
update:更新软件包索引。upgrade:升级所有可升级的软件包。install:安装一个新的软件包。remove:移除一个已安装的软件包,但保留配置文件。purge:移除一个已安装的软件包,并删除其配置文件。autoremove:自动移除不再需要的软件包。search:搜索软件包。show:显示软件包的详细信息。
常用选项
-y:自动回答 “yes” 以确认安装。-qq:静默模式,不显示额外信息。-v:详细模式,显示更多的信息。-s:模拟模式,不实际执行命令,只显示将要执行的操作。
由于写这里的时候没有安装软件包的需要,因此无法提供示例
