欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

第四章:Linux文件及目录管理命令基础(三)

程序员文章站 2022-04-23 22:17:23
补充 /etc/hostname :CenOS7主机名配置文件 /etc/sysconfig/network C6主机名配置文件 修改主机名 永久生效 临时改一下 /etc/sysctl.confLinux内核参数信息文件※※※※※ 调整Linux系统、优化需要配置这个文件 sysctl p 让修 ......
  • 补充

    /etc/hostname :cenos7主机名配置文件

    [root@oldboyedu ~]# cat /etc/hostname 
    oldboyedu
    
    

    /etc/sysconfig/network c6主机名配置文件

    [root@oldboy ~]# cat /etc/sysconfig/network
    networking=yes
    hostname=oldboy
    
    • 修改主机名

      • 永久生效

        [root@oldboyedu ~]# hostnamectl set-hostname oldboy
        [root@oldboyedu ~]# hostname
        oldboy
        
      • 临时改一下

        [root@oldboyedu ~]# hostname sun
        [root@oldboyedu ~]# hostname
        sun
        

    /etc/sysctl.conf linux内核参数信息文件※※※※※

    调整linux系统、优化需要配置这个文件

    sysctl -p 让修改后的文件生效

一、/usr目录 用户相关

1、 /usr/local:源码编译安装软件默认的位置路径

  • 软件安装(了解):
    • rpm安装
      • rpm -ivh 软件包的包名.rpm(几乎不用)
    • yum安装,rpm包管理器,智能帮我们找一个软件包需要的依赖包(推荐)
      • 本质就是rpm安装。自动解决依赖,自动使用rpm命令实现安装。
    • 源代码安装。

2./usr/src:存放源码文件的目录

二、/var/目录 linux记录各种日志目录

1./var/log:记录系统及软件运行信息文件所在的目录

log日志信息====计算机运行行为

2./var/log/messages:linux系统级别日志文件*****

记录linux系统运行行为的文件,正常行为 非正常行为。

[root@oldboyedu ~]# tail /var/log/messages 
apr  7 19:22:12 oldboyedu networkmanager[1134]: <info>  [1586258532.0068] policy: set-hostname: set hostname to 'oldboy' (from system configuration)
apr  7 19:22:12 oldboyedu dbus[887]: [system] activating via systemd: service name='org.freedesktop.nm_dispatcher' unit='dbus-org.freedesktop.nm-dispatcher.service'
apr  7 19:22:12 oldboyedu systemd: starting network manager script dispatcher service...
apr  7 19:22:12 oldboyedu systemd-hostnamed: changed host name to 'oldboy'
apr  7 19:22:12 oldboyedu dbus[887]: [system] successfully activated service 'org.freedesktop.nm_dispatcher'
apr  7 19:22:12 oldboyedu systemd: started network manager script dispatcher service.
apr  7 19:22:12 oldboyedu nm-dispatcher: req:1 'hostname': new request (2 scripts)
apr  7 19:22:12 oldboyedu nm-dispatcher: req:1 'hostname': start running ordered scripts...
apr  7 19:22:12 oldboyedu nm-dispatcher: req:2 'hostname': new request (2 scripts)
apr  7 19:22:12 oldboyedu nm-dispatcher: req:2 'hostname': start running ordered scripts...

3./var/log/secure:用户登录信息日志文件(安全日志文件)

[root@oldboyedu ~]# tail /var/log/secure 
apr  7 19:19:55 oldboyedu polkitd[880]: loading rules from directory /usr/share/polkit-1/rules.d
apr  7 19:19:55 oldboyedu polkitd[880]: finished loading, compiling and executing 2 rules
apr  7 19:19:55 oldboyedu polkitd[880]: acquired the name org.freedesktop.policykit1 on the system bus
apr  7 19:19:58 oldboyedu sshd[1489]: server listening on 0.0.0.0 port 22.
apr  7 19:19:58 oldboyedu sshd[1489]: server listening on :: port 22.
apr  7 19:20:05 oldboyedu sshd[3398]: reverse mapping checking getaddrinfo for bogon [10.0.0.1] failed - possible break-in attempt!
apr  7 19:20:05 oldboyedu sshd[3398]: accepted password for root from 10.0.0.1 port 53921 ssh2
apr  7 19:20:05 oldboyedu sshd[3398]: pam_unix(sshd:session): session opened for user root by (uid=0)
apr  7 19:22:11 oldboyedu polkitd[880]: registered authentication agent for unix-process:9178:14338 (system bus name :1.23 [/usr/bin/pkttyagent --notify-fd 5 --fallback], object path /org/freedesktop/policykit1/authenticationagent, locale en_us.utf-8)
apr  7 19:22:12 oldboyedu polkitd[880]: unregistered authentication agent for unix-process:9178:14338 (system bus name :1.23, object path /org/freedesktop/policykit1/authenticationagent, locale en_us.utf-8) (disconnected from bus)

4./var/log/dmesg:记录硬件信息加载情况的日志文件

[root@oldboyedu ~]# tail /var/log/dmesg
[    5.706282] cryptd: max_cpu_qlen set to 1000
[    5.811659] avx2 version of gcm_enc/dec engaged.
[    5.811662] aes ctr mode by8 optimization enabled
[    5.850428] adding 786428k swap on /dev/sda2.  priority:-2 extents:1 across:786428k fs
[    5.869213] xfs (sda1): mounting v5 filesystem
[    6.104993] alg: no test for __gcm-aes-aesni (__driver-gcm-aes-aesni)
[    6.110197] alg: no test for __generic-gcm-aes-aesni (__driver-generic-gcm-aes-aesni)
[    6.122383] xfs (sda1): ending clean mount
[    6.336590] ppdev: user-space parallel port driver
[    6.642353] type=1305 audit(1586258394.437:4): audit_pid=792 old=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:auditd_t:s0 res=1
  • 对应命令dmesg

三、/proc下的重要路径知识

1./proc/meminfo 查系统内存

[root@oldboyedu ~]# cat /proc/meminfo 
memtotal:        3861320 kb
memfree:         3484452 kb
memavailable:    3449064 kb
buffers:            2076 kb
cached:           151372 kb
swapcached:            0 kb
active:           113984 kb
inactive:         108704 kb
active(anon):      69920 kb
inactive(anon):    11140 kb
active(file):      44064 kb
inactive(file):    97564 kb
unevictable:           0 kb
mlocked:               0 kb
swaptotal:        786428 kb
swapfree:         786428 kb
dirty:                 0 kb
writeback:             0 kb
anonpages:         69264 kb
mapped:            27244 kb
shmem:             11820 kb
slab:              68860 kb
sreclaimable:      27848 kb
sunreclaim:        41012 kb
kernelstack:        3776 kb
pagetables:         4372 kb
nfs_unstable:          0 kb
bounce:                0 kb
writebacktmp:          0 kb
commitlimit:     2717088 kb
committed_as:     269960 kb
vmalloctotal:   34359738367 kb
vmallocused:      185700 kb
vmallocchunk:   34359310332 kb
hardwarecorrupted:     0 kb
anonhugepages:      8192 kb
cmatotal:              0 kb
cmafree:               0 kb
hugepages_total:       0
hugepages_free:        0
hugepages_rsvd:        0
hugepages_surp:        0
hugepagesize:       2048 kb
directmap4k:       89920 kb
directmap2m:     3055616 kb
directmap1g:     3145728 kb
  • 对应命令:free

    [root@oldboyedu ~]# free
                  total        used        free      shared  buff/cache   available
    mem:        3861320      195676     3484348       11820      181296     3448960
    swap:        786428           0      786428
    [root@oldboyedu ~]# free -m   #以兆显示
                  total        used        free      shared  buff/cache   available
    mem:           3770         191        3402          11         177        3368
    swap:           767           0         767
    
    

2./proc/cpuinfo 查看cpu

[root@oldboyedu ~]# cat /proc/cpuinfo 
processor	: 0
vendor_id	: genuineintel
cpu family	: 6
model		: 60
model name	: intel(r) core(tm) i5-4570 cpu @ 3.20ghz
stepping	: 3
microcode	: 0x25
cpu mhz		: 3199.073
cache size	: 6144 kb
physical id	: 0
siblings	: 1
core id		: 0
cpu cores	: 1
apicid		: 0
initial apicid	: 0
fpu		: yes
fpu_exception	: yes
cpuid level	: 13
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm invpcid_single ssbd ibrs ibpb stibp fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid xsaveopt arat spec_ctrl intel_stibp flush_l1d arch_capabilities
bogomips	: 6398.14
clflush size	: 64
cache_alignment	: 64
address sizes	: 43 bits physical, 48 bits virtual
power management:

3./proc/loadavg 查看负载(系统繁忙程度的一个综合指标)

[root@oldboyedu ~]# cat /proc/loadavg 
0.00 0.02 0.05 2/110 9288
  • 对应命令:uptime

    [root@oldboyedu ~]# uptime 
     19:35:30 up       15 min,   1 user,  load average: 0.00,  0.02,     0.05
    系统时间   启动状态   开机时间   登入用户    平均负载    1分钟  5分钟   15分钟
    

4、/proc/mounts 当前设备挂载列表信息文件

[root@oldboyedu ~]# tail /proc/mounts 
cgroup /sys/fs/cgroup/cpuset cgroup rw,seclabel,nosuid,nodev,noexec,relatime,cpuset 0 0
configfs /sys/kernel/config configfs rw,relatime 0 0
/dev/sda3 / xfs rw,seclabel,relatime,attr2,inode64,noquota 0 0
selinuxfs /sys/fs/selinux selinuxfs rw,relatime 0 0
systemd-1 /proc/sys/fs/binfmt_misc autofs rw,relatime,fd=31,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=13527 0 0
debugfs /sys/kernel/debug debugfs rw,relatime 0 0
hugetlbfs /dev/hugepages hugetlbfs rw,seclabel,relatime 0 0
mqueue /dev/mqueue mqueue rw,seclabel,relatime 0 0
/dev/sda1 /boot xfs rw,seclabel,relatime,attr2,inode64,noquota 0 0
tmpfs /run/user/0 tmpfs rw,seclabel,nosuid,nodev,relatime,size=386132k,mode=700 0 0

四、centos6启动流程

第四章:Linux文件及目录管理命令基础(三)

五、centos7启动流程

第四章:Linux文件及目录管理命令基础(三)

六、vi/vim编辑器

普通模式:
:set nu 显示行号

普通模式:移动光标:
行尾 $
行首 0
文件尾部 g
文件开头 gg
到指定行 阿拉伯数字gg

普通模式:搜索与替换操作
向下搜索 /3306 继续搜 按n,反向搜n
向上搜索 ?80 继续搜 按n,反向搜n

普通模式:替换
:%s/a/b/g
:%s#world#oldgirl#g
/#还可以用@等特殊字符替换,实际就是分隔符。

s是替换 g表示全局(所有)
指定行号替换:
:4,6s/oldboy/oldgirl/g

普通模式:复制、粘贴、删除等操作
删除单行 dd(剪切)
删除多行 ndd n用任意数字替换
撤销 u
单行复制 yy
多行复制 nyy n用任意数字替换
粘贴 p

进入编辑模式命令
i 插入*****
a 追加 行尾插入
o 下一行开头插入
o 上一行开头插入
a 光标下一个字符插入

命令行模式
:wq 退出并保存
:wq! 退出并强制保存,“!”为强制的意思
:q! 强制退出,不保存
:set nu 显示行号
:set nonu 与set nu相反,取消行号

七、命令

1、cat 查看文件内容

[root@oldboyedu ~]# cat /proc/cpuinfo 
processor	: 0
vendor_id	: genuineintel
cpu family	: 6
model		: 60
model name	: intel(r) core(tm) i5-4570 cpu @ 3.20ghz
stepping	: 3
microcode	: 0x25
cpu mhz		: 3199.073
cache size	: 6144 kb
physical id	: 0
siblings	: 1
core id		: 0
cpu cores	: 1
apicid		: 0
initial apicid	: 0

2.head查看文件开头(默认前10行)

  • -n查看多少行
[root@oldboyedu ~]# seq 30 >test.txt
[root@oldboyedu ~]# cat test.txt 
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
[root@oldboyedu ~]# head -n 5 test.txt 
1
2
3
4
5

3.tail 查看文件尾部(默认最后10行)

  • -n查看多少行
[root@oldboyedu ~]# tail test.txt 
21
22
23
24
25
26
27
28
29
30
[root@oldboyedu ~]# tail -n 5 test.txt 
26
27
28
29
30
  • -f 动态查看(用于查看linux日志文件)
[root@oldboyedu ~]# tail -f  test.txt 
21
22
23
24
25
26
27
28
29
30

4.tac按行翻转文件内容(和cat相反)

5.rev左右按字符翻转行的内容

6、tr 替换或删除字符

  • translate or delete characters(替换或删除字符)

    • linux中严格区分字母的大小写
    • 替换的单位是字符
    [root@localhost ~]# cat > text.txt << end
    > welcome to oldboy training.
    > we are excellent.
    > end
    [root@localhost ~]# cat text.txt 
    welcome to oldboy training.
    we are excellent.
    #示例一,替换单个字符
    [root@localhost ~]# tr "w" "1" < text.txt 
    welcome to oldboy training.
    1e are excellent.
    #示例二,替换多个字符(被替换的字母个数与替换的字符个数相等)
    [root@localhost ~]# tr "a-b" "0-3" < text.txt 
    welcome to old1oy tr0ining.
    we 0re excellent.
    #示例三,替换成转义字符
    [root@localhost ~]# tr "a-b" "\t" < text.txt 
    welcome to old	oy tr	ining.
    we 	re excellent.
    [root@localhost ~]# tr "a-b" "\n" < text.txt 
    welcome to old
    oy tr
    ining.
    we 
    re excellent.
    

7、grep文本过滤

  • print lines matching a pattern(打印行匹配一个模式/样式)
[root@localhost ~]# grep "oldboy" text.txt 
welcome to oldboy training.
  • --color=auto 过滤内容加色(默认红色)

  • -v 取反

    [root@localhost ~]# grep -v "oldboy" text.txt 
    we are excellent.
    
  • -i 不区分大小写

    [root@localhost ~]# grep "w"  text.txt 
    we are excellent.
    [root@localhost ~]# grep -i "w"  text.txt 
    welcome to oldboy training.
    we are excellent.
    
    
  • -n 显示行号

    [root@localhost ~]# grep -n "w"  text.txt 
    2:we are excellent.
    [root@localhost ~]# grep -ni "w"  text.txt 
    1:welcome to oldboy training.
    2:we are excellent.
    
  • -o 只显示过滤的内容(默认按行过滤)

    [root@localhost ~]# cat text.txt 
    welcome to oldboy training.
    we are excellent.
    [root@localhost ~]# echo "oldboy1" >> text.txt 
    [root@localhost ~]# cat text.txt 
    welcome to oldboy training.
    we are excellent.
    oldboy1
    [root@localhost ~]# grep -oi "oldboy" text.txt 
    oldboy
    oldboy
    [root@localhost ~]# grep -o "oldboy" text.txt 
    oldboy
    oldboy
    
    
  • -e 扩展的grep,即egrep

    [root@localhost ~]# grep -e "to|are" text.txt 
    welcome to oldboy training.
    we are excellent.            
    [root@localhost ~]# egrep "to|are" text.txt 
    welcome to oldboy training.
    we are excellent.
    

8、重定向符号的核心知识

  • >或1>标准输出重定向

    • 箭头方向就是数据流向,把左边的数据流向到右边。
    • 会清空右边之前的数据。
    [root@localhost ~]# cp text.txt{,.ori}
    [root@localhost ~]# ls
    anaconda-ks.cfg  text.txt  text.txt.ori
    [root@localhost ~]# cat text.txt
    welcome to oldboy training.
    we are excellent.
    oldboy1
    [root@localhost ~]# > text.txt
    [root@localhost ~]# cat text.txt
    
  • >>或1>>追加输出重定向

    • 箭头方向就是数据流向,把左边的数据流向到右边。
    • 把内容追加到文件内容尾部
    [root@localhost ~]# cat text.txt
    welcome to oldboy training.
    we are excellent.
    oldboy1
    [root@localhost ~]# echo "sun">> text.txt
    [root@localhost ~]# cat text.txt
    welcome to oldboy training.
    we are excellent.
    oldboy1
    sun
    
  • <或0<标准输入重定向。

    • 箭头方向就是数据流向。
    [root@localhost ~]# tr "w" "0" < text.txt
    welcome to oldboy training.
    0e are excellent.
    oldboy1
    sun
    
  • <<或0<<追加输入重定向

    [root@localhost ~]# cat >> text.txt << end
    > 123
    > 234
    > end
    [root@localhost ~]# cat text.txt
    welcome to oldboy training.
    we are excellent.
    oldboy1
    sun
    123
    234
    [root@localhost ~]# cat text.txt
    welcome to oldboy training.
    we are excellent.
    oldboy1
    sun
    123
    234
    [root@localhost ~]# cat > text.txt << end
       
    > 123
    > 234
    > end
    > end
    [root@localhost ~]# cat text.txt
    
    123
    234
    end
    
    
  • 2>标准错误输出重定向

    • 会清空保存错误日志的文件(每次看只能看见最近一次的错误)
  • 2>>标准错误追加输出重定向

    [root@localhost ~]# ca text.txt 1>> text1.txt 2>> text2.txt
    [root@localhost ~]# ls
    anaconda-ks.cfg  text1.txt  text2.txt  text.txt  text.txt.ori
    [root@localhost ~]# cat text1.txt 
    [root@localhost ~]# cat text2.txt 
    -bash: ca: command not found
    #正确日志和错误日志写到一个文件里
    [root@localhost ~]# ca text.txt 1>> text1.txt 2>&1
    [root@localhost ~]# cat text1.txt 
    -bash: ca: command not found
    [root@localhost ~]# ca text.txt 1>> text1.txt 2>&1
    [root@localhost ~]# cat text1.txt 
    -bash: ca: command not found
    -bash: ca: command not found
    
    
  • 数字1:标准输出

  • 数字0:标准输入

  • 数字2:错误输出