Linux基础(二):Linux常用命令 3

目录

 1. 环境变量

2. shell脚本

 3. find, locate, grep

 find 以表达式在目录中查找文件

locate 以名称查找文件

grep 文件中搜索满足匹配条件的行

 典型用法:结合使用 find 和 grep 命令

4. file, date

file 打印文件类型

date 显示修改日期时间

5. gzip, gunzip, bzip2, bunzip2, tar

gzip, gunzip 压缩/解压缩

bzip2, bunzip2 压缩/解压缩

tar 打包,压缩/解压缩

6. wheris, which

wheris 显示命令的源文件、目标文件、帮助文档

which 显示命令的路径

 who, whoami, w 显示系统用户

7. time, ps, top, kill

time 显示进程的花费时间

ps 进程状态

top 动态查看进程状态

kill 向进程发送信号

8. mount, umount 挂载,卸载


1. 环境变量

三种变量:环境变量,自定义变量,系统变量

   环境变量可用于 shell 及子 shell,相当于 C 语言中的全局变量
   自定义变量只用于当前 shell,相当 C 语言中的自动变量
   系统变量是由系统自动生成的,以 $ 打头的变量,$0 表示当前命令,$1 第一个参数,...,另外 $*, $@

farsight@ubuntu:~$ env   # 查询当前 SHELL 的环境变量
...
PWD=/home/farsight      # 当前工作目录
HOME=/home/farsight     # 当前用户的主目录
SHELL=/bin/bash         # 当前的 shell
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin  # 命令的路径
TERM=xterm-256color     # 终端类型
USER=farsight           # 用户
...

2. shell脚本

脚本:命令的集合,形成的一个文件。
   
   写一个脚本 1.sh:


echo "======================================"
echo 

ls -l

echo
echo "======================================"

   授权执行:

  
farsight@ubuntu:~/test$ chmod a+x 1.sh    # 所有人都有执行权限
farsight@ubuntu:~/test$ ls -l 1.sh 
-rwxrwxr-x 1 farsight farsight 155 7月  14 09:42 1.sh
farsight@ubuntu:~/test$ 1.sh     # 不带路径执行,报错,因为找不到这个文件
1.sh: command not found

如何将 1.sh 变成系统命令?
   
   方法1,复制 1.sh 到 PATH 环境变量的路径下,如 /usr/local/bin 下

farsight@ubuntu:~/test$ ls /usr/local/bin/1.sh     # 验证一下原来是否已存在同名的文件
ls: cannot access '/usr/local/bin/1.sh': No such file or directory
farsight@ubuntu:~/test$ sudo cp 1.sh /usr/local/bin   # 复制 1.sh 到 /usr/local/bin 下面
[sudo] password for farsight: 
farsight@ubuntu:~/test$ ls /usr/local/bin/1.sh     # 验证复制成功
/usr/local/bin/1.sh
farsight@ubuntu:~/test$ 1.sh    # 不带路径执行命令
======================================

total 92
-rwxrwxr-x 1 farsight farsight   115 7月  14 10:02 1.sh
-rw-rw-r-- 1 farsight farsight    24 7月  16 16:29 1.txt
-rw-rw-r-- 1 farsight farsight     4 7月  16 16:30 2.txt
-rwxrwxr-x 1 farsight farsight  8304 7月  14 14:57 a.out
-rw-rw-r-- 1 farsight farsight   182 7月  14 14:57 kill.c
-rw-r-xr-x 2 farsight farsight 29665 7月  16 15:09 stdio.h
-rw-r-xr-x 2 farsight farsight 29665 7月  16 15:09 stdio_ln.h

======================================

 方法2,将命令的路径放入 PATH 环境变量中。使用 export 命令导出变量为环境变量:

farsight@ubuntu:~/test$ export PATH=$PATH:/home/farsight/test

        此方法只在当前终端有效,其他终端无效。为了打开终端即生效,可以将这个命令保存到启动文件中。
   启动文件:~/.bashrc,~/.profile。.profile 调用了 .bashrc

farsight@ubuntu:~/test$ vim ~/.bashrc       # 加入 export 命令
farsight@ubuntu:~/test$ source ~/.bashrc    # 让 .bashrc 中的环境变量生效
farsight@ubuntu:~/test$ . ~/.bashrc         # . 是 source 的同义词

 3. find, locate, grep

 find 以表达式在目录中查找文件

farsight@ubuntu:~$ find . -name "1.sh"      # 在当前目录下按名称查找 1.sh 文件
./test/1.sh

locate 以名称查找文件

   locate 不是要磁盘中查找,它在 /var/lib/mlocate/mlocate.db 数据库中查找,此文件会在系统启动时更新一次,之后除非执行 updatedb 命令,
   否则不会更新。因此有时间效性,即在这个文件更新后新增的文件找不到,删除的文件却能找到。如果要查找最新文件情况,就先执行 updatedb。

farsight@ubuntu:~/test$ locate 3.txt    # 查找 3.txt 文件

   总体来说,以名称查找 locate 比 find 更快,因为不需要在磁盘中逐个目录查找。
   

grep 文件中搜索满足匹配条件的行

farsight@ubuntu:~/test$ grep -nH "printf" /usr/include/*.h    
# 在 /usr/include/ 下的头文件中查找包含 printf 的行,并打印文件名和行号 

 典型用法:结合使用 find 和 grep 命令

farsight@ubuntu:~/test$ find /usr/include/ -name "*.h" | xargs grep -nH "printf"   # 典型用法

   xargs 命令将 | 管道传过来的文件名作为参数传递给 grep 命令。

4. file, date

file 打印文件类型

farsight@ubuntu:~/test$ file 1.sh
1.sh: ASCII text                          # 文本文件
farsight@ubuntu:~/test$ file /dev/pts/0 
/dev/pts/0: character special (136/0)     # 字符设备文件
farsight@ubuntu:~/test$ file d2-ln
d2-ln: symbolic link to d2                # 符号链接
farsight@ubuntu:~/test$ file /lib/x86_64-linux-gnu/libc-2.27.so    # 动态库文件
/lib/x86_64-linux-gnu/libc-2.27.so: ELF 64-bit LSB shared object, x86-64, version 1 (GNU/Linux), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=f7307432a8b162377e77a182b6cc2e53d771ec4b, for GNU/Linux 3.2.0, stripped

date 显示修改日期时间

farsight@ubuntu:~/test$ date    # 显示日期时间
2023年 07月 14日 星期五 11:09:56 CST
farsight@ubuntu:~/test$ sudo date -s "2023-07-15 12:00:00"; date    # -s 设置日期时间,需要超级权限
[sudo] password for farsight: 
2023年 07月 15日 星期六 12:00:00 CST
2023年 07月 15日 星期六 12:00:00 CST

5. gzip, gunzip, bzip2, bunzip2, tar

gzip, gunzip 压缩/解压缩

farsight@ubuntu:~/test$ gzip stdio*.h    # 压缩多个文件,每个文件后缀自动加上 .gz,并删除原文件

-rw-rw-r-- 1 farsight farsight 6645 7月  14 11:17  stdio2.h.gz
-rw-rw-r-- 1 farsight farsight 8030 7月  14 11:17  stdio3.h.gz
-rw-r--r-- 1 farsight farsight 6248 7月  14 11:17  stdio.h.gz

farsight@ubuntu:~/test$ gunzip stdio3.h.gz     # 解压
farsight@ubuntu:~/test$ gunzip stdio.h.gz stdio2.h.gz

bzip2, bunzip2 压缩/解压缩

farsight@ubuntu:~/test$ bzip2 stdio3.h   # 文件后缀自动加上 .bz2,并删除原文件
farsight@ubuntu:~/test$ bzip2 stdio*.h   # 压缩多个文件
farsight@ubuntu:~/test$ bunzip2 stdio*.bz2   # 解压缩

tar 打包,压缩/解压缩

   只打包,或先打包再压缩   c 打包
   x 解包
   j 以 bzip2 工具压缩/解压缩
   z 以 gzip 工具压缩/解压缩
   J 以 xz 工具压缩/解压缩
   f 打包/解包为文件
   v 显示打包/解包的信息
   

farsight@ubuntu:~/test$ tar czfv stdio.tar.gz stdio*.h   # gzip 压缩
farsight@ubuntu:~/test$ tar cjfv stio.tar.bz2 stdio*.h   # bzip2 压缩
farsight@ubuntu:~/test$ tar cJfv stio.tar.xz stdio*.h    # xz 压缩

farsight@ubuntu:~/test$ tar xzf stdio.tar.gz -C d11      # gzip 解压
farsight@ubuntu:~/test$ tar xjf stio.tar.bz2 -C d12      # bzip2 解压
farsight@ubuntu:~/test$ tar xJf stio.tar.xz -C d13       # xz 解压

6. wheris, which

wheris 显示命令的源文件、目标文件、帮助文档

farsight@ubuntu:~/test$ whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz

which 显示命令的路径

farsight@ubuntu:~/test$ which ls
/bin/ls

 who, whoami, w 显示系统用户

farsight@ubuntu:~/test$ who
farsight :0           2023-07-14 09:28 (:0)
farsight@ubuntu:~/test$ whoami   # 当前用户
farsight
farsight@ubuntu:~/test$ w        # 查看系统中登录的用户情况
 11:43:01 up  2:33,  1 user,  load average: 0.00, 0.00, 0.00
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
farsight :0       :0               09:28   ?xdm?  58.72s  0.00s /usr/lib/gdm3/g

farsight@ubuntu:~/test$ who
farsight :0           2023-07-14 09:28 (:0)
farsight@ubuntu:~/test$ whoami   # 当前用户
farsight
farsight@ubuntu:~/test$ w        # 查看系统中登录的用户情况
 11:43:01 up  2:33,  1 user,  load average: 0.00, 0.00, 0.00
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
farsight :0       :0               09:28   ?xdm?  58.72s  0.00s /usr/lib/gdm3/g

7. time, ps, top, kill

time 显示进程的花费时间

farsight@ubuntu:~/test$ time ls    # 执行 ls 命令,统计它的进程花费的时间
 1.sh    2.txt     d11   d13   d2-ln      stdio3.h   stdio.tar.gz   stio.tar.xz
 1.txt  '3.txt!'   d12   d2    stdio2.h   stdio.h    stio.tar.bz2

real    0m0.001s    # 实际时间,即进程开始到结束共花费的时间
user    0m0.001s    # 系统在用户空间运行花费的时间
sys    0m0.000s        # 系统运行这个进程在内核空间花费的时间,即系统调用花费的时间

ps 进程状态

farsight@ubuntu:~/test$ ps aux    # 显示所有进程状态
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.5 225500  5120 ?        Ss   09:09   0:02 /sbin/init spl
root          2  0.0  0.0      0     0 ?        S    09:09   0:00 [kthreadd]
root          3  0.0  0.0      0     0 ?        I<   09:09   0:00 [rcu_gp]
farsight   2680  0.0  0.5  24988  5164 pts/1    Ss+  10:17   0:00 bash
root       3089  0.0  0.0      0     0 ?        I    11:15   0:00 [kworker/u256:
root       3272  0.0  0.0      0     0 ?        I    11:22   0:00 [kworker/u256:
farsight   3398  0.0  0.4  41420  3764 pts/0    R+   11:51   0:00 ps aux

USER 用户
PID  进程号
%CPU 占用 CPU 的百分比
%MEM 占用内存的百分比
TTY  终端,脱离了控制终端的用 ? 号表示
STAT 状态,状态码如下:
               D    uninterruptible sleep (usually IO)          # 不可中断的休眠
               R    running or runnable (on run queue)          # 运行中,或就绪队列中等待运行
               S    interruptible sleep (waiting for an event to complete)   # 可中断的休眠(等待事件完成)
               T    stopped by job control signal               # 被 job 控制信号暂停
               t    stopped by debugger during the tracing      # 在跟踪调试期间被调试器暂停
               W    paging (not valid since the 2.6.xx kernel)  # 换页中(Linux 2.6 之后不支持)
               X    dead (should never be seen)                 # 已死(应该永不可见)
               Z    defunct ("zombie") process, terminated but not reaped by  # 僵尸进程
                    its parent

       BSD 扩展的状态码:

               <    high-priority (not nice to other users)     # 高优先级进程
               N    low-priority (nice to other users)          # 低优先级进程
               L    has pages locked into memory (for real-time and custom IO)   # 有页被锁在内存中
               s    is a session leader                         # 会话组长
               l    is multi-threaded (using CLONE_THREAD, like NPTL pthreads    # 多线程
                    do)
               +    is in the foreground process group          # 在前台进程组中

   一般关注占用 CPU 和 内存 情况,以及当前的状态

farsight@ubuntu:~/test$ ps axjf    # 查看进程树

top 动态查看进程状态

    默认每隔 3 秒刷新一次。可以使用 -d 选项更改。

farsight@ubuntu:~/test$ top
top - 14:05:11 up  4:55,  1 user,  load average: 0.00, 0.00, 0.00   # load 是 1分钟,5分钟,15分钟内系统负载
Tasks: 312 total,   2 running, 246 sleeping,   0 stopped,   0 zombie
%Cpu(s):  3.0 us,  1.0 sy,  0.0 ni, 96.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :   902772 total,    76448 free,   677192 used,   149132 buff/cache
KiB Swap:   969960 total,   395496 free,   574464 used.    84972 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                        
  1988 farsight  20   0 3041748 107164  28628 S  3.0 11.9   0:45.01 gnome-shell                                    
  1859 farsight  20   0  432652  14800   3460 S  0.7  1.6   0:21.11 Xorg 

    占用 CPU 比例说明:
    
           us, user    : time running un-niced user processes   高优先级用户进程
           sy, system  : time running kernel processes          内核进程
           ni, nice    : time running niced user processes      低优先级用户进程
           id, idle    : time spent in the kernel idle handler  内核空闲进程
           wa, IO-wait : time waiting for I/O completion        等待 IO 完成
           hi : time spent servicing hardware interrupts        服务硬件中断
           si : time spent servicing software interrupts        服务软件中断
           st : time stolen from this vm by the hypervisor      系统 VM "偷走"

    PageUp 向上翻页,PageDown 向下翻页。q 退出。

farsight@ubuntu:~/test$ top -d 5 -u farsight   # 间隔 5 秒刷新,查看 farsight 用户的进程
farsight@ubuntu:~/test$ top -d 5 -u 1000       # 间隔 5 秒刷新,查看 ID 为 1000 的用户的进程
farsight@ubuntu:~/test$ top -d 5 -p 1          # 间隔 5 秒刷新,查看进程号为 1 的进程

kill 向进程发送信号

farsight@ubuntu:~/test$ kill -l    # 显示系统支持的信号
 1) SIGHUP     2) SIGINT     3) SIGQUIT     4) SIGILL     5) SIGTRAP
 6) SIGABRT     7) SIGBUS     8) SIGFPE     9) SIGKILL    10) SIGUSR1
11) SIGSEGV    12) SIGUSR2    13) SIGPIPE    14) SIGALRM    15) SIGTERM
16) SIGSTKFLT    17) SIGCHLD    18) SIGCONT    19) SIGSTOP    20) SIGTSTP
21) SIGTTIN    22) SIGTTOU    23) SIGURG    24) SIGXCPU    25) SIGXFSZ
26) SIGVTALRM    27) SIGPROF    28) SIGWINCH    29) SIGIO    30) SIGPWR
31) SIGSYS    34) SIGRTMIN    35) SIGRTMIN+1    36) SIGRTMIN+2    37) SIGRTMIN+3
38) SIGRTMIN+4    39) SIGRTMIN+5    40) SIGRTMIN+6    41) SIGRTMIN+7    42) SIGRTMIN+8
43) SIGRTMIN+9    44) SIGRTMIN+10    45) SIGRTMIN+11    46) SIGRTMIN+12    47) SIGRTMIN+13
48) SIGRTMIN+14    49) SIGRTMIN+15    50) SIGRTMAX-14    51) SIGRTMAX-13    52) SIGRTMAX-12
53) SIGRTMAX-11    54) SIGRTMAX-10    55) SIGRTMAX-9    56) SIGRTMAX-8    57) SIGRTMAX-7
58) SIGRTMAX-6    59) SIGRTMAX-5    60) SIGRTMAX-4    61) SIGRTMAX-3    62) SIGRTMAX-2
63) SIGRTMAX-1    64) SIGRTMAX    

    32 以下的信号是不可靠信号,33 以上的信号是可靠信号。

farsight@ubuntu:~/test$ kill -9 -p 3720         # 向 3720 进程发送 9 号信号,-9 可以替代为 -KILL,或 -SIGKILL
farsight@ubuntu:~/test$ kill -STOP -p 3724      # 向 3724 进程发送 STOP 暂停信号
farsight@ubuntu:~/test$ kill -SIGCONT -p 3724   # 向 3724 进程发送 SIGCONT 恢复运行信号

获取 a.out 程序的进程号:

farsight@ubuntu:~/test$ ps aux | grep "a.out"

8. mount, umount 挂载,卸载

farsight@ubuntu:~/test$ df   # 查看文件系统
Filesystem     1K-blocks     Used Available Use% Mounted on
udev              425320        0    425320   0% /dev
tmpfs              90280     1812     88468   3% /run
/dev/sda1       20464208  8268076  11131276  43% /
tmpfs             451384        0    451384   0% /dev/shm
tmpfs               5120        4      5116   1% /run/lock
tmpfs             451384        0    451384   0% /sys/fs/cgroup
...
tmpfs              90276       16     90260   1% /run/user/121
tmpfs              90276       32     90244   1% /run/user/1000
/dev/sdb1       15101488 14416272    685216  96% /media/farsight/ŷ    # U 盘挂载点
farsight@ubuntu:~/test$ umount /media/farsight/ŷ   # 卸载 U 盘

farsight@ubuntu:~/test$ sudo fdisk -l     # 磁盘格式化、分区命令,-l 列出磁盘及分区
...
Disk /dev/sdb: 14.4 GiB, 15472047104 bytes, 30218842 sectors   # 磁盘 - U 盘
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x1b366af1

Device     Boot Start      End  Sectors  Size Id Type
/dev/sdb1  *       63 30217823 30217761 14.4G  b W95 FAT32     # U 盘的分区所在的设备文件

farsight@ubuntu:~/test$ sudo mount -o iocharset=utf8 /dev/sdb1 upan   # 挂载 U 盘到 ~/test/upan 目录
farsight@ubuntu:~/test$ sudo mount -o iocharset=utf8 /dev/sdb1 upan2  # 还可以同时挂载到另一个目录

    -o iocharset=utf8 解决查看 U 盘内容时乱码问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值