系统相关
首页 > 系统相关> > Linux基础 -03

Linux基础 -03

作者:互联网

2.2.3 head-tail 命令

#------head
#head pass   #查看头部内容,默认前10行
#head -n5 pass	#查看头部前5行,使用-n指定

#-------tail
#tail pass
# tail -20 /var/log/secure
#tail -f /var/log/messages	#-f查看文件尾部的变化
# tailf /var/log/messages	# 查看文件尾部的变化

2.2.4 grep 过滤数据

#----------grep过滤文件内容
# grep "^root" pass		#匹配以root开头的行
# grep "bash$" pass 	#匹配以bash结尾的行
# grep -v "ftp" pass 	#匹配除了包含ftp的内容,其他全部打印
# grep -i "ftp" pass 	#忽略大小写匹配
# grep -Ei "sync$|ftp" pass 	#匹配文件中包含sync结尾或ftp字符串   有-E 可以写多个选项
# grep -n -A 2 "Failed" /var/log/secure #匹配/var/log/secure文件中Failed字符串,并打印它的下2行
# grep -n -B 2 "Failed" /var/log/secure #匹
配/var/log/secure文件中Failed字符串,并打印它的上2行
# grep -n -C 2 "Failed" /var/log/secure #匹
配/var/log/secure文件中Failed字符串,并打印它的上下2
行

2.3 文件下载类命令

2.3.1 wget 命令

#CentOS7 系统最小化安装默认没有wget命令,需要进行安装
# yum install wget -y

#下载互联网上的文件至本地
#wget http://mirrors.aliyun.com/repo/Centos-7.repo

#将阿里云的centos-7.repo下载到/etc/yum.repos.d/并改名为centos-base.repo -O参数指定
#wget -O /etc/yum.repos.d/Centos-Base.repo
http://mirrors.aliyun.com/repo/Centos-7.repo

2.3.2 curl命令

#仅查看这个url地址的文件内容
#curl http://mirrors.aliyun.com/repo/Centos-
7.repo

#将阿里云的centos-7.repo下载到/etc/yum.repos.d/并改
名为CentOS-Base.repo -o参数指定
# curl -o /etc/yum.repos.d/CentOS-Base.repo
http://mirrors.aliyun.com/repo/Centos-7.repo

#练习:请下载一个图片至于/opt目录下(不要修改名称),最少使
用2中方式, URl地址为:
http://fj.xuliangwei.com/public/ks.jpeg

#1.wget
[root@www ~]# cd /opt
[root@www opt]# wget
http://fj.xuliangwei.com/public/ks.jpeg
[root@www ~]# wget -O /opt/ks.jpeg
http://fj.xuliangwei.com/public/ks.jpeg

#2.curl
[root@www ~]# curl -o /opt/ks2.jpeg
http://fj.xuliangwei.com/public/ks.jpeg

2.3.3 rz-sz命令

# yum install lrzsz -y #不安装软件则无法执行该命令

# rz	#只能上传文件,不支持上传文件夹,不支持大于4个G上传,也不支持断电续传
#sz /path/file #只能下载文件,不支持下载文件夹

2.4 文件查找类命令

2.4.1 which命令

# which ls		#查找ls命令的绝对路径
# type -a ls	#查看命令的绝对路径(包括别名)

2.4.2 whereis 命令

# whereis ls		#查找命令的路径,帮助手册,等
# whereis -b ls		#仅显示命令虽在的路径

2.5 字符处理类命令

2.5.1 sort 命令

在有些情况下,需要对一个无序的文本进行数据的排序,这时就需要使用 sort 进行排序了

sort [OPTION]...[FILE]...
# -r :倒序 -n:按数字排序 -t :指定分隔符(默认空格)
# -k :指定第几列,指定几列几字符(指定1,1 3.1,3.3)

#1.首先创建一个文件,写入一写无序的内容
[root@www ~]# cat >> file.txt <<EOF
b:3
c:2
a:4
e:5
d:1
f:11
EOF

#2.使用sort下面对输出的内容进行排序
[root@www ~]# sort file.txt
a:4
b:3
c:2
d:1
e:5
f:11

#结果并不是按照数字排序,而是按字母排序。
#可以使用-t指定分隔符, 使用-k指定需要排序的列。
[root@www ~]# sort -t ":" -k2 sort.txt
d:1
f:11 #第二行为什么是11?不应该按照顺序排列?
c:2
b:3
a:4
e:5

#按照排序的方式, 只会看到第一个字符,11的第一个字符是1,
按照字符来排序确实比2小。
#如果想要按照数字的方式进行排序, 需要使用 -n参数。
[root@www ~]# sort -t ":" -n -k2 p.txt
d:1
c:2
b:3
a:4
e:5
f:11

2.5.2 uniq命令

如果文件中有多行完全相同的内容,当前是希望能删除重复的行,同时还可以统计出完全相同的行的出现总次数,那么就可以使用uniq命令解决(但是必须配合sort使用,先排序再去重)

uniq [OPTION]... [INPUT [OUTPUT]]
# 选项 : -c 计算重复的行

#1.创建一个file.txt文件:
[root@www ~]# cat file.txt
abc
123
abc
123
#2.uniq需要和sort一起使用, 先使用sort排序, 让重复内容
连续在一起
[root@www ~]# sort file.txt
123
123
abc
abc
#3.使用uniq去除相邻重复的行
[root@www ~]# sort file.txt |uniq
123
abc
#4.-c参数能统计出文件中每行内容重复的次数
[root@www ~]# sort file.txt |uniq -c
   2 123
   2 abc
#面试题: 请统计分析如下日志,打印出访问最高前10的IP

2.5.3 cut命令

cut OPTION... [FILE]...
#选项: -d 指定分隔符 -f 数字,取第几列 -f3,6三列和6列
# -c 按字符取(空格也算)
#echo "Im xlw, is QQ 552408925" >file.txt  #过
滤出文件里 xlw以及552408925
#实现上述题目几种思路
# cut -d " " -f2,5 file.txt
# cut -d " " -f2,5 file.txt |sed 's#,##g'
# sed 's#,# #g' file.txt | awk -F " " '{print
$2 " " $5}'
# awk '{print $2,$5}' file.txt |awk -F ',' '{print $1,$2}'
# awk -F "[, ]" '{print $2,$6}' file.txt
# awk -F '[, ]+' '{print $2,$5}' file.txt

.2.5.4 wc命令

wc [OPTION]... [FILE]...
# 选项: -l显示文件行数 -c 显示文件字节 -w显示文件单词
# wc -l /etc/fstab   #统计/etc/fstab文件有多少行
# wc -l /etc/services  #统计/etc/services 文件行号


#练习题: 过滤出/etc/passwd以nologin结尾的内容,并统计有多少行

# 扩展统计文件行号的方法
# grep -n ".*" /etc/services | tail -1
# cat -n /etc/services | tail -1
# awk '{print NR $0}' /etc/services | tail -1

2.6 课堂练习题

2.6.1 练习1

[root@student tmp]# cat web.log
http://www.example.com/index.html
http://www.example.com/1.html
http://post.example.com/index.html
http://mp3.example.com/index.html
http://www.example.com/3.html
http://post.example.com/2.html
# awk -F '/' '{print $3}' web.log|sort -rn|uniq –c
# cut -d / -f3 web.log|sort -rn|uniq –c

2.6.2 练习2

2.6.4练习3

2.6.3练习4

2.6.4练习5

03.文件扩展知识

3.1 文件属性

[root@oldxu ~]# ls -l ks.cfg
-rw-------. 1 root root 4434 May 30 13:58
ks.cfg


-rw-------. ①:第一个字符是文件类型,其他则是权限
1      ②:硬链接次数
root    ③:文件属于哪个用户
root    ④:文件属于哪个组
4434    ⑤:文件大小
May30 13:58 ⑥⑦⑧:最新修改的时间与日期
ks.cfg   ⑨:文件或目录名称

3.2 文件类型

[root@oldxu ~]# ll -d /etc/hosts /tmp /bin/ls
/dev/sda /dev/tty1 /etc/grub2.cfg /dev/log
/run/dmeventd-client
-rwxr-xr-x.  1 root root 117656 Jun 30  2016
/bin/ls
srw-rw-rw-.  1 root root    0 Jan 20 10:35
/dev/log
brw-rw----.  1 root disk  8, 0 Jan 20 10:36
/dev/sda
crw--w----.  1 root tty   4, 1 Jan 20 10:36
/dev/tty1
lrwxrwxrwx.  1 root root   22 Jan 13 11:31
/etc/grub2.cfg -> ../boot/grub2/grub.cfg
-rw-r--r--.  1 root root   199 Jan 20 11:03
/etc/hosts
drwxrwxrwt. 61 root root  8192 Jan 21 13:01
/tmp
[root@oldxu ~]# file /etc/hosts
/etc/hosts: ASCII text
[root@oldxu ~]# file /bin/ls
/bin/ls: ELF 64-bit LSB executable, x86-64,
version 1 (SYSV), dynamically linked (uses
shared libs), for GNU/Linux 2.6.32,
BuildID[sha1]=aa7ff68f13de25936a098016243ce57c3
c982e06, stripped
[root@oldxu ~]# file /dev/sda
/dev/sda: block special
[root@oldxu ~]# file /dev/tty1
/dev/tty1: character special
[root@oldxu ~]# file /etc/grub2.cfg
/etc/grub2.cfg: broken symbolic link to
`../boot/grub2/grub.cfg
[root@oldxu ~]# file /home
/home: directory

3.3 链接文件

3.3.1 Inode与Block

3.1.2 软连接

#1.准备网站1.1版本代码
[root@oldxu ~]# mkdir /data/rainbow-v1.1 -p
[root@oldxu ~]# echo "123" > /data/rainbow-
v1.1/index.html

#2.创建软链接
[root@oldxu ~]# ln -s /data/rainbow-v1.1/
/data/rainbow
[root@oldxu ~]# ll /data/
drwxr-xr-x. 2 root root  6 3月  5 12:23 dir
lrwxrwxrwx. 1 root root 19 3月  10 12:09 rainbow
-> /data/rainbow-v1.1/
drwxr-xr-x. 2 root root 24 3月  10 12:09
rainbow-v1.1

#3.检查网站程序
[root@oldxu ~]# cat /data/rainbow/index.html
123

#4.新更新一个网站的程序代码
[root@oldxu ~]# mkdir /data/rainbow-v1.2
[root@oldxu ~]# echo "456" > /data/rainbow-
v1.2/index.html

#5.升级
[root@oldxu ~]# rm -f /data/rainbow && ln -s
/data/rainbow-v1.2/ /data/rainbow
[root@oldxu ~]# cat /data/rainbow/index.html
456

#6.回退
[root@oldxu ~]# rm -f /data/rainbow && ln -s
/data/rainbow-v1.1/ /data/rainbow
[root@oldxu ~]# cat /data/rainbow/index.html
123

3.1.3 硬链接

3.4.1 环境准备

[root@oldxu ~]# mount -o remount,strictatime /
[root@oldxu ~]# echo "hello boy" >> new_file
[root@oldxu ~]# stat new_file
File: 'new_file'
Size: 10    Blocks: 8     IO
Block: 4096  regular file
Device: fd00h/64768d Inode: 34724341 
Links: 1
Access: (0644/-rw-r--r--) Uid: (   0/ 
root)  Gid: (   0/  root)
Access: 2021-07-07 16:24:02.320233640 +0800
Modify: 2021-07-07 16:24:02.320233640 +0800
Change: 2021-07-07 16:24:02.320233640 +0800
Birth: -

3.4.2 atime

[root@oldxu ~]# cat new_file
hello boy
[root@oldxu ~]# stat new_file
File: 'new_file'
Size: 10    Blocks: 8     IO
Block: 4096  regular file
Device: fd00h/64768d Inode: 34724341 
Links: 1
Access: (0644/-rw-r--r--) Uid: (   0/ 
root)  Gid: (   0/  root)
Access: 2021-07-07 16:24:56.362408139 +0800  
# 访问时间发生变化
Modify: 2021-07-07 16:24:02.320233640 +0800
Change: 2021-07-07 16:24:02.320233640 +0800
Birth: -

3.4.2 mtime

# 写入数据
[root@oldxu ~]# echo "Hello" >> new_file
[root@oldxu ~]# stat new_file
File: 'new_file'
Size: 16    Blocks: 8     IO
Block: 4096  regular file
Device: fd00h/64768d Inode: 34724341 
Links: 1
Access: (0644/-rw-r--r--) Uid: (   0/ 
root)  Gid: (   0/  root)
Access: 2021-07-07 16:28:35.520036828 +0800
Modify: 2021-07-07 16:32:36.806358744 +0800 # 内
容被修改后,mtime会变化
Change: 2021-07-07 16:32:36.806358744 +0800 # 变
化
Birth: -
# ctime时间发生变化的原因是,内容变化了,innode所记录的
大小也需要变化,所以时间发生了变化

3.4.3 ctime

# 修改文件属性
[root@oldxu ~]# chown adm new_file
[root@oldxu ~]# stat new_file
File: 'new_file'
Size: 16    Blocks: 8     IO
Block: 4096  regular file
Device: fd00h/64768d Inode: 34724341 
Links: 1
Access: (0644/-rw-r--r--) Uid: (   3/  
adm)  Gid: (   0/  root)
Access: 2021-07-07 16:28:35.520036828 +0800
Modify: 2021-07-07 16:32:36.806358744 +0800
Change: 2021-07-07 16:36:03.441728857 +0800 # 只
有ctime变化

标签:03,07,文件,oldxu,基础,file,Linux,root,链接
来源: https://www.cnblogs.com/gao0722/p/14992432.html