Ubuntu基本命令的熟悉和使用

Ubuntu基本命令的熟悉和使用

宣传一下自己的博客Ubuntu基本命令的熟悉和使用

为区分命令输入和返回,采用将终端的输出完全复制的策略

六个常用的终端命令

本篇仅介绍最常用的几个文件操作指令,对于其他的指令在使用时会补充介绍

序号 命令 对应英文 作用
01 ls list 查看当前文件夹下的内容
02 pwd print work directory 查看当前所在文件夹
03 cd 目录名 change directory 切换文件夹
04 touch 目录名 touch 如果文件不存在,新建文件
05 mkdir 目录名 make directory 创建目录
06 rm 文件名 remove 删除指定的文件名
  1. 首先使用ls命令,查找默认目录下的文件

    powershell 复制代码
    ubuntu@VM-8-11-ubuntu:~$ ls
    ubuntu@VM-8-11-ubuntu:~$ 

    根据结果可得,此处没有任何文件

  2. 使用pwd命令查找此处的文件路径

    powershell 复制代码
    ubuntu@VM-8-11-ubuntu:~$ pwd
    /home/ubuntu
    ubuntu@VM-8-11-ubuntu:~$ 

    由此可见,在上面还有一级home

  3. 使用cd ..命令返回到上一级文件目录

    powershell 复制代码
    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文件

  1. 再次使用cd ..命令,返回到根目录

    powershell 复制代码
    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:/$ 

    由此可见,根目录下有很多种文件

  2. 然后尝试返回home/ubuntu文件夹

    powershell 复制代码
    ubuntu@VM-8-11-ubuntu:/$ cd home/ubuntu
    ubuntu@VM-8-11-ubuntu:~$ ls
  3. 使用mkdir命令尝试建立新的文件夹

    powershell 复制代码
    ubuntu@VM-8-11-ubuntu:~$ mkdir test
    ubuntu@VM-8-11-ubuntu:~$ ls
    test
    ubuntu@VM-8-11-ubuntu:~$ 

    由此可见,mkdir命令成功建立了一个名为test的新文件夹

  4. 使用touch命令创建hello.c

    powershell 复制代码
    ubuntu@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文件

  5. 使用vim编辑hello.c

    终端界面:

    powershell 复制代码
    ubuntu@VM-8-11-ubuntu:~/test$ vim hello.c
    ubuntu@VM-8-11-ubuntu:~/test$ 

    vim界面:

    powershell 复制代码
    hello,world!
    ~                                  
    ~
    "hello.c" 1L, 13C          1,12          All

    使用ESC+:wq+ENTER退出vim编辑器

  6. 再次使用touch命令新建hello.c`文件

    终端界面

    powershell 复制代码
    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界面:

    powershell 复制代码
    hello,world!
    ~                                                                                           
    ~
    "hello.c" 1L, 13C          1,12          All

    可以看到,对于已经建好的文件,touch命令不会有效果

  7. 使用rm命令删除hello.c

    powershell 复制代码
    ubuntu@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已经被成功删除

  8. 使用rm -r强制删除test目录

    powershell 复制代码
    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 命令

基本语法
bash 复制代码
whereis [options] [string...]
  • options :可以是一些特定的选项,如 -bmsu
  • string...:是你想要查找的程序或文件的名称。
选项说明
  • -b:只查找二进制文件。
  • -m:只查找手册页。
  • -s:只查找源代码。
示例
  1. 查找特定程序的路径

    powershell 复制代码
    ubuntu@VM-8-11-ubuntu:~$ whereis java
    java: /usr/share/java
    ubuntu@VM-8-11-ubuntu:~$ 
  2. 仅查找二进制文件

    powershell 复制代码
    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

    可以看到仅显示二进制文件查找到的比查找所有文件要少

  3. 查找手册页

    powershell 复制代码
    ubuntu@VM-8-11-ubuntu:~$ whereis -m gcc
    gcc: /usr/share/man/man1/gcc.1.gz

    这将显示 gcc 的手册页位置。

ps命令

ps 命令用于显示当前系统中的进程状态。ps 是 "process status" 的缩写。这个命令用于监控系统性能、调试程序或查找占用资源的进程

基本语法
bash 复制代码
ps [options]
选项说明
  • -a:显示所有与终端会话关联的进程
  • -u:以用户易读的格式显示进程信息
  • x:显示没有控制终端的进程
  • e:显示所有进程
  • f:全格式,显示完整的信息
组合选项
  • -aux:显示所有进程的详细信息,包括环境变量
  • -ef:显示所有进程的完整信息
  • -aef:用于显示所有与终端会话关联的进程的完整信息
输出字段解释
  • PID:进程ID。
  • TTY:进程所在的终端类型。
  • TIME:CPU时间,单位通常是分钟。
  • CMD:启动进程的命令。
示例
  1. 显示所有进程

    powershell 复制代码
    ubuntu@VM-8-11-ubuntu:~$ ps -e
        PID TTY          TIME CMD
          1 ?        00:00:09 systemd
          2 ?        00:00:00 kthreadd
          3 ?        00:00:00 rcu_gp
          4 ?        00:00:00 rcu_par_gp
          6 ?        00:00:00 kworker/0:0H-kblockd
  2. 显示所有进程的详细信息

    powershell 复制代码
    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]
  3. 以用户易读的格式显示所有进程

    powershell 复制代码
    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]
  4. 显示特定用户的进程

    root用户

    bash 复制代码
    ubuntu@VM-8-11-ubuntu:~$ ps -u root
        PID TTY          TIME CMD
          1 ?        00:00:09 systemd
          2 ?        00:00:00 kthreadd
          3 ?        00:00:00 rcu_gp
          4 ?        00:00:00 rcu_par_gp
          6 ?        00:00:00 kworker/0:0H-kblockd

    ubuntu用户

    powershell 复制代码
    ubuntu@VM-8-11-ubuntu:~$ ps -u ubuntu
        PID TTY          TIME CMD
       6222 ?        00:00:00 systemd
       6226 ?        00:00:00 (sd-pam)
     455485 ?        00:00:00 sshd
     455486 ?        00:00:00 bash
     455505 ?        00:00:00 code-fee1edb8d6
  5. 显示特定进程的详细信息

    powershell 复制代码
    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 命令的基本语法如下:

bash 复制代码
grep [options] pattern [file...]
  • options :可以是一些特定的选项,用于控制 grep 的行为。
  • pattern:要搜索的文本模式或正则表达式。
  • file... :要搜索的文件列表,如果不指定文件,则 grep 会从标准输入读取数据。
选项说明
  • -i:忽略大小写。
  • -v:显示不匹配模式的行。
  • -c:只输出匹配模式的行数。
  • -l:只输出包含匹配模式的文件名。
  • -n:显示匹配行及行号。
  • -r-R:递归搜索指定目录下的所有文件。
示例

首先现在/home/ubuntu目录下新建一个test.txt

powershell 复制代码
ubuntu@VM-8-11-ubuntu:~$ touch test.txt
ubuntu@VM-8-11-ubuntu:~$ ls
test.txt

打开文本文件,向其中写入以下内容

txt 复制代码
abc
abcba
abccba
Abcba
abcCba
  1. 搜索包含特定文本的行

    bash 复制代码
    ubuntu@VM-8-11-ubuntu:~$ grep "abc" test.txt
    abc
    abcba
    abccba
    abcCba
    ubuntu@VM-8-11-ubuntu:~$ 
  2. 忽略大小写搜索

    bash 复制代码
    ubuntu@VM-8-11-ubuntu:~$ grep -i "abc" test.txt
    abc
    abcba
    abccba
    Abcba
    abcCba
    ubuntu@VM-8-11-ubuntu:~$ 
  3. 显示不匹配的行

    bash 复制代码
    ubuntu@VM-8-11-ubuntu:~$ grep -v "abc" test.txt
    Abcba
    ubuntu@VM-8-11-ubuntu:~$ 
  4. 输出匹配行的行号

    bash 复制代码
    ubuntu@VM-8-11-ubuntu:~$ grep -n "abc" test.txt
    1:abc
    2:abcba
    3:abccba
    5:abcCba
    ubuntu@VM-8-11-ubuntu:~$ 

管道|的使用

使用管道|grep 搜索特定进程

powershell 复制代码
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服务

powershell 复制代码
ubuntu@VM-8-11-ubuntu:~$ sudo systemctl --type=service
  UNIT                                 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 命令列出活动服务

该命令将过滤掉那些不活动(已停止)或失败的服务

powershell 复制代码
ubuntu@VM-8-11-ubuntu:~$ sudo systemctl --type=service --state=active
  UNIT                                 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 命令查看服务详细信息

powershell 复制代码
ubuntu@VM-8-11-ubuntu:~$ sudo systemctl status nginx
● nginx.service - nginx - high performance web server
     Loaded: 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设置开机自启

powershell 复制代码
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 命令提供了查找、安装、升级、删除某一个、一组甚至全部软件包的命令

基本语法

bash 复制代码
  apt [options] [command] [package ...]

常用命令

  • update:更新软件包索引。
  • upgrade:升级所有可升级的软件包。
  • install:安装一个新的软件包。
  • remove:移除一个已安装的软件包,但保留配置文件。
  • purge:移除一个已安装的软件包,并删除其配置文件。
  • autoremove:自动移除不再需要的软件包。
  • search:搜索软件包。
  • show:显示软件包的详细信息。

常用选项

  • -y:自动回答 "yes" 以确认安装。
  • -qq:静默模式,不显示额外信息。
  • -v:详细模式,显示更多的信息。
  • -s:模拟模式,不实际执行命令,只显示将要执行的操作。

由于写这里的时候没有安装软件包的需要,因此无法提供示例

相关推荐
dualven_in_csdn12 小时前
mqtt消息及日志查看
linux·运维·服务器
都在酒里12 小时前
Linux字符设备驱动开发(四):进入硬件世界——GPIO子系统与LED设备驱动
linux·运维·驱动开发
无足鸟ICT12 小时前
【RHCA+】fortune命令(输出一句话)
linux
Coin_learning12 小时前
Linux 基础命令完全教程:从入门到实战
linux
Yupureki12 小时前
《MySQL数据库基础》9.索引原理
linux·运维·服务器·网络·数据库·mysql
睡不醒男孩03082313 小时前
StarRocks导入数据:从本地文件导入数据(Stream Load)
linux·数据库
2301_8090511413 小时前
Linux TCP 和 UDP 通信
linux·运维·tcp/ip
Qt程序员13 小时前
从上电到系统就绪:ARM+U-Boot 嵌入式 Linux 启动流程
linux·运维·c++·内核·设备树·嵌入式·ram
雨中来客13 小时前
在开启了FBDEV 模拟层的设备上屏蔽/dev/fb0的显示功能
linux
Yupureki13 小时前
《MySQL数据库基础》8.复合查询
linux·运维·服务器·网络·数据库·mysql