Linux指令

基础指令

1.执行查看帮助命令

        man命令

weixin@weixin:/$ man man

查看man手册(manual:n.手册; 说明书; 指南;)

        help命令

weixin@weixin:/$ help

help命令只能显示shell内部的命令帮助信息。

而对于外部命令的帮助信息只能使用man或者info命令查看。

2.常用系统工作命令

        echo命令

#设置变量var
weixin@weixin:/$ var=fuck
#输出var
weixin@weixin:/$ echo var
var
#输出变量var
weixin@weixin:/$ echo $var
fuck

将结果输出在终端

        date命令

#查看当前时间
weixin@weixin:/$ date
2023年 01月 11日 星期三 16:07:21 CST

#按照特定格式查看当期时间
weixin@weixin:/$ date "+%Y-%m-%d %H:%M:%S"
2023-01-11 16:07:55

#查看今天是这今年的第几天
weixin@weixin:/$ date "+%j"
011

#设置当前系统时间
weixin@weixin:/$ date -s "20170901 8:30:00"
Fri Sep 1 08:30:00 CST 2017

        reboot命令

#切换到root用户(或者sudo命令)
weixin@weixin:/$ su -
密码: 
:'重启'
root@weixin:~# reboot

重新启动       

        poweroff命令

#切换到root用户(或者sudo命令)
weixin@weixin:/$ su -
密码: 
:'关机'
root@weixin:~# poweroff

关闭系统

        wget命令

参数作用
-b后台下载模式
-P下载到指定目录
-t最大尝试次数
-c断点续传
-p下载页面所有资源,包括图片,视频等
-r递归下载
:'下载 电子书pdf'
weixin@weixin:~$ wget http://www.linuxprobe.com/docs/LinuxProbe.pdf

--2023-01-11 16:27:41--  http://www.linuxprobe.com/docs/LinuxProbe.pdf
正在解析主机 www.linuxprobe.com (www.linuxprobe.com)... 117.169.12.168
正在连接 www.linuxprobe.com (www.linuxprobe.com)|117.169.12.168|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 301 Moved Permanently
位置:https://www.linuxprobe.com/docs/LinuxProbe.pdf [跟随至新的 URL]
--2023-01-11 16:27:41--  https://www.linuxprobe.com/docs/LinuxProbe.pdf
正在连接 www.linuxprobe.com (www.linuxprobe.com)|117.169.12.168|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度: 23335661 (22M) [application/pdf]
正在保存至: “LinuxProbe.pdf”

LinuxProbe.pdf                                 100%[===================================================================================================>]  22.25M  8.05MB/s    用时 2.8s  

2023-01-11 16:27:44 (8.05 MB/s) - 已保存 “LinuxProbe.pdf” [23335661/23335661])

终端中下载网络文件(World Wide Web get)

        ps命令

参数作用
-a显示所有进程(包括其他用户进程)
-u用户以及其他详细信息
-x显示没有控制终端的进程
:'
MEM:Memory Device内存
VSZ:Virtual Memory Size虚拟内存使用量
RSS:Resident Set Size占用固定内存量
TTY:终端(termimal)= tty(Teletypewriter, 电传打印机)
STAT:status进程状态
START:启动时间
TIME:实际使用的时间
COMMAND:命令名称
'

weixin@weixin:~$ ps aux    
USER        PID     %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
...............................省略...........................................
root       3238      0.0   0.0      0     0  ?        I    17:03   0:00 [kworker/u256:2]
root       3239      0.0   0.0      0     0  ?        I    17:03   0:00 [kworker/0:2]
root       3266      0.0   0.0      0     0  ?        I    17:11   0:00 [kworker/u256:0]
weixin     3297      0.0   0.0   9228   828  ?        S    17:22   0:00 sleep 180
weixin     3298      0.0   0.0   9228   832  ?        S    17:22   0:00 sleep 180
weixin     3314      0.0   0.0   9228   888  ?        S    17:24   0:00 sleep 180
weixin     3315      0.0   0.1  41432  3660  pts/0    R+   17:25   0:00 ps aux

查看系统中的进程状态(process status

Linux系统常见5种进程状态
 R(运行)进程正在运行或在运行队列中等待

S(中断)

进程处于休眠中。当某个条件形成后或者接收到信号时,则脱离该状态
D(不可中断)进程不响应系统异步信号,即便用kill命令也不能将其中断
Z(僵死)进程已经终止,但进程描述符依然存在,直到父进程调用wait4()系统函数后将进程释放
T(停止)进程收到停止信号后停止运行

        top命令

:'动态查看系统运维状态'
weixin@weixin:~$ top
top - 17:41:32 up  1:24,  2 users,  load average: 0.12, 0.03, 0.01
任务: 271 total,   1 running, 202 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.4 us,  0.4 sy,  0.0 ni, 99.1 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  2014888 total,   296620 free,  1208556 used,   509712 buff/cache
KiB Swap:   969960 total,   969180 free,      780 used.   633424 avail Mem 

进程 USER      PR  NI    VIRT    RES    SHR 䞠%CPU %MEM     TIME+ COMMAND                                                                                                               
  3350 weixin    20   0   46008   4092   3308 R   0.9  0.2   0:00.45 top                                                                                                                  
  3146 weixin    20   0  838568  61744  13288 S   0.4  3.1   0:09.29 cpptools                                                                                                             
     1 root      20   0  159912   9004   6524 S   0.0  0.4   0:05.33 systemd                                                                                                              
     2 root      20   0       0      0      0 S   0.0  0.0   0:00.03 kthreadd                                                                                                             
     3 root      20   0       0      0      0 I   0.0  0.0   0:00.57 kworker/0:0                                                                                                          
     4 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kworker/0:0H                                                                                                         
     6 root       0 -20       0      0      0 I   0.0  0.0   0:01.25 kworker/0:1H                                                                                                         
     7 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 mm_percpu_wq                                                                                                         
     8 root      20   0       0      0      0 S   0.0  0.0   0:00.45 ksoftirqd/0                                                                                                          
     9 root      20   0       0      0      0 I   0.0  0.0   0:01.92 rcu_sched                                                                                                            
    10 root      20   0       0      0      0 I   0.0  0.0   0:00.00 rcu_bh                                                                                                               
    11 root      rt   0       0      0      0 S   0.0  0.0   0:00.01 migration/0                                                                                                          
    12 root      rt   0       0      0      0 S   0.0  0.0   0:00.01 watchdog/0                                                                                                           
    13 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/0                                                                                                              
    14 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/1                                                                                                              
    15 root      rt   0       0      0      0 S   0.0  0.0   0:00.01 watchdog/1                                                                                                           
    16 root      rt   0       0      0      0 S   0.0  0.0   0:00.09 migration/1                                                                                                          
    17 root      20   0       0      0      0 S   0.0  0.0   0:01.86 ksoftirqd/1                                                                                                          
    19 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kworker/1:0H                                                                                                         
    20 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kdevtmpfs                                                                                                            
    21 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 netns                                                                                                                
    22 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_tasks_kthre                                                                                                      
    23 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kauditd                                                                                                              
    24 root      20   0       0      0      0 I   0.0  0.0   0:03.78 kworker/1:1                                                                                                          
    25 root      20   0       0      0      0 S   0.0  0.0   0:00.01 khungtaskd                                                                                                           
    26 root      20   0       0      0      0 S   0.0  0.0   0:00.00 oom_reaper                                                                                                           
    27 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 writeback                                                                                                            
    28 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kcompactd0                                                                                                           
    29 root      25   5       0      0      0 S   0.0  0.0   0:00.00 ksmd                                                                                                                 
    30 root      39  19       0      0      0 S   0.0  0.0   0:00.00 khugepaged                                                                                                           
    31 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 crypto                                                                                                               
    32 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kintegrityd                                                                                                          
    33 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kblockd                                                                                                              
    34 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 ata_sff                                                                                                              
    35 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 md                                                                                                                   
    36 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 edac-poller                                                                                                          
    37 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 devfreq_wq                                                                                                           
    38 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 watchdogd  

类似Windows的任务管理器        

        pidof命令

:'查询某个指定服务进程的PID值'
weixin@weixin:~$ pidof sshd
3043 2998 2916 2871 2130 2085 2072 2004 1157

 sshd(Secure Shell daemon守护进程)

        kill命令

:'知道进程号'
weixin@weixin:~$ kill 2156

杀死 pid为2156的 进程 

        killall命令

:'知道服务名称'
weixin@weixin:~$ killall sshd

杀死sshd服务的全部进程

2.系统状态检测命令

        ifconfig

:'获取网卡配置和网络状态等信息 ens33为虚拟机 ip地址,如没有,用dhclient指令动态配置'
weixin@weixin:~$ ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.213.128  netmask 255.255.255.0  broadcast 192.168.213.255
        inet6 fe80::20c:29ff:fe59:e0af  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:59:e0:af  txqueuelen 1000  (以太网)
        RX packets 1175921  bytes 1651766901 (1.6 GB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 189823  bytes 21823374 (21.8 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (本地环回)
        RX packets 10101  bytes 5106267 (5.1 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 10101  bytes 5106267 (5.1 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ifconfig(network interfaces configuring) 

        uname

:'查看系统内核与系统版本等信息'
weixin@weixin:~$ uname -a
Linux weixin 4.15.0-200-generic #211-Ubuntu SMP Thu Nov 24 18:16:04 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

#查看当前系统详细信息
weixin@weixin:~$ cat /etc/os-release
NAME="Ubuntu"
VERSION="18.04.6 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.6 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic

 uname(Unix name)

        uptime

:'用于查看系统运行了多久'

weixin@weixin:~$ uptime
18:02:31 up  1:45,  2 users,  load average: 0.00, 0.00, 0.00

        free

:'显示当前内存使用量'

root@weixin:~# free -h
              总计         已用        空闲      共享    缓冲/缓存    可用
内存:        1.9G        929M        309M         11M        729M        876M
交换:        947M          0B        947M

root@weixin:~# free
              总计         已用        空闲      共享    缓冲/缓存    可用
内存:     2014888      951716      315340       11288      747832      897572
交换:      969960           0      969960

        who

:'用于查看当前登入主机的用户终端信息'

root@weixin:~# who
weixin   :0           2023-01-12 09:58 (:0)
weixin   pts/0        2023-01-12 10:09 (192.168.213.1)

        last 

:'用于查看所有系统的登录记录'

root@weixin:~# last
weixin   pts/0        192.168.213.1    Thu Jan 12 10:09   still logged in
weixin   :0           :0               Thu Jan 12 09:58   still logged in
reboot   system boot  4.15.0-200-gener Thu Jan 12 09:57   still running
weixin   :0           :0               Thu Jan 12 09:08 - down   (00:48)
reboot   system boot  4.15.0-200-gener Thu Jan 12 09:07 - 09:57  (00:49)
weixin   pts/1        192.168.213.1    Wed Jan 11 16:49 - 17:15  (00:26)
weixin   pts/0        192.168.213.1    Wed Jan 11 16:21 - 18:05  (01:44)
weixin   :0           :0               Wed Jan 11 16:18 - crash  (16:49)
reboot   system boot  4.15.0-200-gener Wed Jan 11 16:17 - 09:57  (17:39)
weixin   pts/1        192.168.213.1    Wed Jan 11 09:43 - 16:15  (06:32)

        history

:'查看历史执行过的命令'
weixin@weixin:~$ history

#历史执行命令记录保存在bash_history文件里
weixin@weixin:~$ cat ~/.bash_history

#清空当前用户在本机上执行的Linux命令历史记录信息
weixin@weixin:~$ history -c

        sosreport

:'收集系统配置及架构信息并输出诊断文档'

root@weixin:~# sos report

sosreport (version 4.4)

This command will collect system configuration and diagnostic
information from this Ubuntu system.

For more information on Canonical visit:

        Community Website  : https://www.ubuntu.com/
        Commercial Support : https://www.canonical.com

The generated archive may contain data considered sensitive and its
content should be reviewed by the originating organization before being
passed to any third party.

No changes will be made to system configuration.


按 ENTER 键继续,或者 CTRL-C 组合键退出。

3.工作切换目录       

        pwd

:'显示用户当前所处的工作目录'
weixin@weixin:~$ pwd
/home/weixin

 pwd(Print Working Directory

        cd

#切换到projections目录下
weixin@weixin:~/桌面/Linux$ cd projections

#返回上一级目录
weixin@weixin:~/桌面/Linux/projections$ cd -
/home/weixin/桌面/Linux

#返回到上一级目录
weixin@weixin:~/桌面/Linux$ cd ..

#返回系统根目录
weixin@weixin:~/桌面/Linux$ cd /
weixin@weixin:/$ ls
bin   cdrom  etc   initrd.img      lib    lost+found  mnt  proc  run   snap  swapfile  tmp  var      vmlinuz.old
boot  dev    home  initrd.img.old  lib64  media       opt  root  sbin  srv   sys       usr  vmlinuz

#返回home目录,用户主目录
weixin@weixin:/$ cd ~
weixin@weixin:~$ ls
examples.desktop  公共的  模板  视频  图片  文档  下载  音乐  桌面

 cd(Change Directory

        ls

:'不加任何参数,表示查询当前目录下的文件/文件夹'
weixin@weixin:~$ ls
examples.desktop  公共的  模板  视频  图片  文档  下载  音乐  桌面

#后面加上路径,表示查询该路径下的文件/文件夹
weixin@weixin:~$ ls 桌面
Linux  myFile

# -a参数,表示查询所有的文件/文件夹,也包括以.开头的隐藏文件 
weixin@weixin:~$ ls -a 桌面
.  ..  Linux  myFile

# -l参数,表示查询文件的详细信息(ls -l 可简写为 ll)
weixin@weixin:~$ ls -l
总用量 44
-rw-r--r-- 1 weixin weixin 8980 1月   3 18:07 examples.desktop
drwxr-xr-x 2 weixin weixin 4096 1月   4 09:18 公共的
drwxr-xr-x 2 weixin weixin 4096 1月   4 09:18 模板
drwxr-xr-x 2 weixin weixin 4096 1月   4 09:18 视频
drwxr-xr-x 2 weixin weixin 4096 1月   4 09:18 图片
drwxr-xr-x 2 weixin weixin 4096 1月   4 09:18 文档
drwxr-xr-x 2 weixin weixin 4096 1月   4 09:18 下载
drwxr-xr-x 2 weixin weixin 4096 1月   4 09:18 音乐
drwxr-xr-x 4 weixin weixin 4096 1月  11 16:54 桌面

# -d只显示当前目录自身,通常和-l搭配使用来显示当前目录自身的权限和属性信息
weixin@weixin:~$ ls -d
.
weixin@weixin:~$ ls -ld
drwxr-xr-x 17 weixin weixin 4096 1月  12 10:50 .

#-lh        列出文件大小
weixin@weixin:~$ ls -lh
总用量 44K
-rw-r--r-- 1 weixin weixin 8.8K 1月   3 18:07 examples.desktop
drwxr-xr-x 2 weixin weixin 4.0K 1月   4 09:18 公共的
drwxr-xr-x 2 weixin weixin 4.0K 1月   4 09:18 模板
drwxr-xr-x 2 weixin weixin 4.0K 1月   4 09:18 视频
drwxr-xr-x 2 weixin weixin 4.0K 1月   4 09:18 图片
drwxr-xr-x 2 weixin weixin 4.0K 1月   4 09:18 文档
drwxr-xr-x 2 weixin weixin 4.0K 1月   4 09:18 下载
drwxr-xr-x 2 weixin weixin 4.0K 1月   4 09:18 音乐
drwxr-xr-x 4 weixin weixin 4.0K 1月  11 16:54 桌面


d-rwx-rwx-rwx
d/l/-/prwxrwxrwx
  • d是目录文件
  • l是链接文件
  • -是普通文件
  • p是管道 
可读可写可执行可读可写可执行可读可写可执行
文件类型owner权限user group权限other user权限

4.文本文件编辑命令

        cat

:'查看内容较少的纯文本文件 -n显示行数'
weixin@weixin:~$ cat -n /etc/profile
     1	# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
     2	
     3	# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
     4	
     5	if [ "${PS1-}" ]; then
     6	  if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
     7	    # The file bash.bashrc already sets the default PS1.
     8	    # PS1='\h:\w\$ '
     9	    if [ -f /etc/bash.bashrc ]; then
    10	      . /etc/bash.bashrc
    11	    fi
    12	  else
    13	    if [ "`id -u`" -eq 0 ]; then
    14	      PS1='# '
    15	    else
    16	      PS1='$ '
    17	    fi
    18	  fi
    19	fi
    20	
    21	if [ -d /etc/profile.d ]; then
    22	  for i in /etc/profile.d/*.sh; do
    23	    if [ -r $i ]; then
    24	      . $i
    25	    fi
    26	  done
    27	  unset i
    28	fi
    29	
    30	export LC_ALL="zh_CN.UTF-8"  export LANG="zh_CN.UTF-8"

cat(concatenate

        more

:'查看纯文本文件(内容较多,长篇小说,非常长的配置文件)'
weixin@weixin:~$ more /etc/profile
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))

# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "${PS1-}" ]; then
  if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
    # The file bash.bashrc already sets the default PS1.
    # PS1='\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
    fi
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
  fi
fi

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi

export LC_ALL="zh_CN.UTF-8"  export LANG="zh_CN.UTF-8"

        head

:'查看纯文本文档的前N行 -n显示行数'
weixin@weixin:~$ head -n 20 /etc/profile
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))

# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "${PS1-}" ]; then
  if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
    # The file bash.bashrc already sets the default PS1.
    # PS1='\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
    fi
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
  fi
fi

        tail

:'查看纯文本文档的后N行或持续刷新内容(-f)    Ctrl+c 退出'

weixin@weixin:~$ tail -f *.desktop
Comment[vec]=Contenuti de esempio de Ubuntu
Comment[vi]=Mẫu ví dụ cho Ubuntu
Comment[zh_CN]=Ubuntu 示例内容
Comment[zh_HK]=Ubuntu 的範例內容
Comment[zh_TW]=Ubuntu 的範例內容
URL=file:///usr/share/example-content/
Icon=folder
X-Ubuntu-Gettext-Domain=example-content

        tr

:'用于替换文本文件中的字符 (小写换大写)'

weixin@weixin:~$ cat article |tr [a-z] [A-Z]

I GOT SUMMER ON MY MIND

NOWHERE ELSE THOUGH

LORD TAKE ME DOWNTOWN OR TO YOU

tr(transform)

        wc

:'统计指定文本的行数、字数、字节数、格式为“wc [参数] 文本”
 -l只显示行数  -w只显示单词数 -c只显示字节数
'
weixin@weixin:~$ wc -l article
77 article
weixin@weixin:~$ wc -w article
211 article
weixin@weixin:~$ wc -c article
1014 article

 wc(Word Count

        stat

:'用于查看文件的具体存储信息和时间等信息'
weixin@weixin:~$ stat article
  文件:article
  大小:1014      	块:8          IO 块:4096   普通文件
设备:801h/2049d	Inode:931201      硬链接:1
权限:(0664/-rw-rw-r--)  Uid:( 1000/  weixin)   Gid:( 1000/  weixin)
最近访问:2023-01-12 15:54:17.741372231 +0800
最近更改:2023-01-12 15:53:23.700285269 +0800
最近改动:2023-01-12 15:53:23.700285269 +0800
创建时间:-

        cut

:'按列提取文本字符'
weixin@weixin:~$ head -n 2 /etc/passwd
root:x:0:0:root:/root:/bin/bash
# -d [参数] 设置间隔符号
# -f [参数] 设置需要看的列数
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
weixin@weixin:~$ cut -d: -f1 /etc/passwd
root
daemon
bin
sys
games
man
lp
mail
news
uucp
proxy
www-data
backup
list
irc
gnats
nobody
systemd-network
systemd-resolve
syslog
messagebus
_apt
uuidd
avahi-autoipd
usbmux
dnsmasq
rtkit
cups-pk-helper
speech-dispatcher
whoopsie
kernoops
saned
pulse
avahi
colord
hplip
geoclue
gnome-initial-setup
gdm
weixin
sshd

        diff

:'比较多个文本文件的差异'

# -brief判断两文件是否相等
weixin@weixin:~$ diff --brief A.txt B.txt
文件 A.txt 和 B.txt 不同

# -c描述文件内容具体的不同
weixin@weixin:~$ diff -c A.txt B.txt
*** A.txt	2023-01-12 16:12:08.069091106 +0800
--- B.txt	2023-01-12 16:12:22.832263439 +0800
***************
*** 1,2 ****
! AAAAAAAAAAAAA
! BBBBBBBBBBBBB
--- 1,2 ----
! BBBBBBBBBBBBBBBBB
! CCCCCCCCCCCCCCCCC

5.文件目录管理命令

        touch

:'用于创建空白文件或设置文件的时间
  -a 仅修改 “读取时间”
  -m 仅修改 “修改时间”
  -d 同时修改atime与mtime
'

weixin@weixin:~$ ll word
-rw-rw-r-- 1 weixin weixin 12 1月  12 16:50 word

weixin@weixin:~$ touch -d "2017-02-12 12:12" word
weixin@weixin:~$ ll word
-rw-rw-r-- 1 weixin weixin 12 2月  12  2017 word

        mkdir

:'用于创建空白的目录 -p 创建嵌套层关系的文件目录'

#创建文件目录
weixin@weixin:~$ mkdir newfold
weixin@weixin:~$ ls
examples.desktop  newfold  公共的  模板  视频  图片  文档  下载  音乐  桌面


#创建嵌套文件目录
weixin@weixin:~/newfold$ mkdir -p a/b/c/d/e/f/g
weixin@weixin:~/newfold$ tree
.
└── a
    └── b
        └── c
            └── d
                └── e
                    └── f
                        └── g

7 directories, 0 files

        cp

:'
用于复制文件或目录 cp[选项] 目录

三种情况:
    目标文件是目录,则会把源文件复制到该目录
    如果目标文件也是普通文件,则会询问是否要覆盖它
    如果目标文件不存在,则会执行正常的复制操作

参数:
-p 保留原始文件的属性
-d 若对象为“链接文件”,则保留该“链接文件”的属性
-r 递归持续复制(用于目录)
-i 若目标文件存在则询问是否覆盖
-a 相当于 -pdr(p、d、r为上述参数)
'

weixin@weixin:~/newfold$ tree
.
├── a
│   └── b
│       └── c
│           └── d
│               └── e
│                   └── f
│                       └── g
└── file

7 directories, 1 file

#复制文件
weixin@weixin:~/newfold$ cp file ./a/b/c/d/e/f/g
weixin@weixin:~/newfold$ tree
.
├── a
│   └── b
│       └── c
│           └── d
│               └── e
│                   └── f
│                       └── g
│                           └── file
└── file

7 directories, 2 files

        mv

:'用于剪辑文件或重命名(会把源文件删除)'
weixin@weixin:~/newfold$ ls
a  file
weixin@weixin:~/newfold$ mv file newfile
weixin@weixin:~/newfold$ ls
a  newfile

        rm

:'删除文件或目录(-r递归删除)'

weixin@weixin:~/newfold$ ls
a  newfile
weixin@weixin:~/newfold$ rm newfile
weixin@weixin:~/newfold$ ls
a

weixin@weixin:~/newfold$ rm a
rm: 无法删除'a': 是一个目录
weixin@weixin:~/newfold$ rm -r a

        dd

:'按照指定大小和个数的数据块来复制文件或转换文件 dd[参数] 
-if 输入的文件名称
-of 输出的文件名称
-bs 设置每个“块”的大小
-count 设置要复制“块”的个数
'

weixin@weixin:~$ dd if=/dev/zero of=mother_fuck count=1 bs=520
记录了1+0 的读入
记录了1+0 的写出
520 bytes copied, 0.000160884 s, 3.2 MB/s
weixin@weixin:~$ ls
examples.desktop  mother_fuck  公共的  模板  视频  图片  文档  下载  音乐  桌面
weixin@weixin:~$ 


        file

:'查看文件的类型'

weixin@weixin:~$ file mother_fuck
mother_fuck: data
weixin@weixin:~$ file /dev/sda
/dev/sda: block special (8/0)

6.打包压缩与搜索命令

        tar

:'对文件进行打包压缩或解压
-c 创建压缩文件
-x 解开压缩文件
-t 查看压缩包内有哪些文件
-z 用Gzip压缩或解压
-j 用bzip2压缩或解压
-v 显示压缩或解压的过程
-f 目标文件名
-P 保留原始的权限和属性
-p 使用绝对路径来压缩
-C 指定解压到的目录
'

#压缩
weixin@weixin:~$ tar -czvf etc.tar.gz /etc
......

#解压
weixin@weixin:~$ mkdir /root/etc
weixin@weixin:~$ tar xzvf etc.tar.gz -C /root/etc
......

        grep

:'在文本中执行关键词搜索
-b 将可执行文件(binary)当做文本文件(text)来搜索
-c 仅显示找到的行数
-i 忽略大小写
-n 显示行号
-v 反向选择-仅列出没有“关键词”的行
'

#检索当前系统不允许登录系统的所有用户信息
weixin@weixin:~$ grep /sbin/nologin /etc/passwd
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
systemd-network:x:100:102:systemd Network Management,,,:/run/systemd/netif:/usr/sbin/nologin
systemd-resolve:x:101:103:systemd Resolver,,,:/run/systemd/resolve:/usr/sbin/nologin
syslog:x:102:106::/home/syslog:/usr/sbin/nologin
messagebus:x:103:107::/nonexistent:/usr/sbin/nologin
_apt:x:104:65534::/nonexistent:/usr/sbin/nologin
uuidd:x:105:111::/run/uuidd:/usr/sbin/nologin
avahi-autoipd:x:106:112:Avahi autoip daemon,,,:/var/lib/avahi-autoipd:/usr/sbin/nologin
usbmux:x:107:46:usbmux daemon,,,:/var/lib/usbmux:/usr/sbin/nologin
dnsmasq:x:108:65534:dnsmasq,,,:/var/lib/misc:/usr/sbin/nologin
rtkit:x:109:114:RealtimeKit,,,:/proc:/usr/sbin/nologin
cups-pk-helper:x:110:116:user for cups-pk-helper service,,,:/home/cups-pk-helper:/usr/sbin/nologin
kernoops:x:113:65534:Kernel Oops Tracking Daemon,,,:/:/usr/sbin/nologin
saned:x:114:119::/var/lib/saned:/usr/sbin/nologin
pulse:x:115:120:PulseAudio daemon,,,:/var/run/pulse:/usr/sbin/nologin
avahi:x:116:122:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/usr/sbin/nologin
colord:x:117:123:colord colour management daemon,,,:/var/lib/colord:/usr/sbin/nologin
geoclue:x:119:124::/var/lib/geoclue:/usr/sbin/nologin
sshd:x:122:65534::/run/sshd:/usr/sbin/nologin

        find

:'按照指定条件来查找文件, find [查找路径] 寻找条件 操作
-name:匹配名称
-perm:匹配权限
-user:匹配所有者
-group:匹配所有组
-mtime -n +n :匹配修改内容的时间(-n指n天以内,+n指n天以前)
-atime -n +n :匹配访问文件的时间
-ctime -n +n :匹配修改文件权限的时间
-nouser :匹配无所有者的文件
-nogroup:匹配无所有组的文件
-newer f1 !f2: 匹配比文件f1新但比f2旧的文件
--type  b/d/c/p/l/f: 匹配文件类型(块设备、目录、字符设备、管道、;链接文件、文本大小)
-size:匹配文件的大小(+50KB为查找超过50KB的文件,而-50KB为查找小于50KB的文件)
-prune:忽略某个目录
-exec ······{}\; :跟用于进一步处理搜索结果的命令
'

#获取etc目录下所有以host开头的文件列表
root@weixin:~# find /etc -name "host" -print

#整个系统中搜索权限中包括SUID权限的所有文件,用-4000
root@weixin:~# find / -perm -4000 -print

文件读写操作相关

1.输入输出重定向

标准输入重定向STDIN,文件描述符为0
标准输出重定向STDOUT,文件描述符为1
错误输出重定向STDERR,文件描述符为2
输入重定向用到的符号及其作用
符号作用
命令 < 文件将文件作为命令的标准输入
命令 << 分界符从标准输入中读入,直到遇见分界符才停止
命令 < 文件1 > 文件2将文件1作为命令的标准输入并将标准输出到文件2
输出重定向中用到的符号及其作用
符号作用
命令 > 文件将标准输出重定向到一个文件中(清空原有文件的数据)
命令 2> 文件将错误输出重定向到一个文件中(清空原有文件的数据)
命令 >> 文件将标准输出重定向到一个文件中(追加到原有内容的后面)
命令 2>> 文件将错误输出重定向到一个文件中(追加到原有内容的后面)

命令 >> 文件 2>&1

命令 &>> 文件

将标准输出与错误输出共同写入到文件中(追加到原有内容的后面)
# 输出的内容    >   写入的文件
weixin@weixin:~/桌面/Linux$ a=motherfuck
weixin@weixin:~/桌面/Linux$ echo $a > read.txt
weixin@weixin:~/桌面/Linux$ cat read.txt
motherfuck

#统计内容行数
weixin@weixin:~/桌面/Linux$ wc -l <read.txt
1

2.管道命令符

:'管道符 |  的应用'

#翻页的形式查看/etc目录中的文件列表及属性信息
weixin@weixin:~/桌面/Linux$ ls -l /etc/ |more
总用量 1100
drwxr-xr-x  3 root root    4096 7月  25  2018 acpi
-rw-r--r--  1 root root    3028 7月  25  2018 adduser.conf
drwxr-xr-x  2 root root    4096 1月   4 13:21 alternatives
-rw-r--r--  1 root root     401 5月  30  2017 anacrontab
-rw-r--r--  1 root root     433 10月  2  2017 apg.conf

#将内容和标题打包,实现邮件的发送
root@weixin:~# echo "content"|mail -s "Subject" weixin

#查看邮件
weixin@weixin:~$ mail
"/var/mail/weixin": 1 message 1 new
>N   1 root               五 1月 13 15:4  13/406   Subject
? 
Return-Path: <root@weixin>
X-Original-To: weixin@weixin
Delivered-To: weixin@weixin
Received: by weixin.localdomain (Postfix, from userid 0)
	id AFBEE12629B; Fri, 13 Jan 2023 15:43:21 +0800 (CST)
Subject: Subject
To: <weixin@weixin>
X-Mailer: mail (GNU Mailutils 3.4)
Message-Id: <20230113074321.AFBEE12629B@weixin.localdomain>
Date: Fri, 13 Jan 2023 15:43:21 +0800 (CST)
From: root <root@weixin>
content
? 

 3.命令行通配符

:'匹配所有以sda开头的文件'
weixin@weixin:~$ ls -l /dev/sda
brw-rw---- 1 root disk 8, 0 1月  13 09:09 /dev/sda

4.常用的转义字符

反斜杠(\)后面的一个变量为单纯的字符串
单引号(' ')转义其中所有的变量为单纯的字符串
双引号(" ")保留其中的变量属性,不进行转移处理
反引号(` `)把其中的,命令执行后返回结果
#输出字符串
weixin@weixin:~$ price=5
weixin@weixin:~$ echo "Price is $price"
Price is 5

#输出转义字符
weixin@weixin:~$ echo "Price is \$$price"
Price is $5

#需要某个命令的输出值
weixin@weixin:~$ echo `uname -a`
Linux weixin 4.15.0-200-generic #211-Ubuntu SMP Thu Nov 24 18:16:04 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

5.环境变量

:'查看环境变量'
weixin@weixin:~$ env
LC_ALL=zh_CN.UTF-8
SSH_CONNECTION=192.168.213.1 1997 192.168.213.128 22
LESSCLOSE=/usr/bin/lesspipe %s %s
LANG=zh_CN.UTF-8
DISPLAY=localhost:10.0
XDG_SESSION_ID=3
USER=weixin
PWD=/home/weixin
HOME=/home/weixin
SSH_CLIENT=192.168.213.1 1997 22
XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktop
SSH_TTY=/dev/pts/0
MAIL=/var/mail/weixin
TERM=xterm
SHELL=/bin/bash
SHLVL=1
LANGUAGE=zh_CN:zh
LOGNAME=weixin
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
XDG_RUNTIME_DIR=/run/user/1000
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
LESSOPEN=| /usr/bin/lesspipe %s
_=/usr/bin/env
OLDPWD=/home/weixin/桌面/Linux

#设置环境变量
root@weixin:~# mkdir /home/work
root@weixin:~# WORK=/home/work
root@weixin:~# cd $WORK
root@weixin:/home/work# pwd
/home/work

#设置全局变量
root@weixin:/home/work# export WORK
root@weixin:/home/work# su weixin
weixin@weixin:~$ cd $WORK
weixin@weixin:~$ pwd
/home/weixin

#删除WORK环境变量
root@weixin:/home/work# unset WORK
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CZDXWX

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值