Linux运维实训day3

文件管理命令

touch
	创建或刷新时间戳
	
案例:批量创建相同扩展名的文件
# touch {1..10}.txt

[root@hd ~]# touch {1..10}.txt
[root@hd ~]# ls
10.txt  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt  anaconda-ks.cfg  done  useradd

# touch {a..f}.txt

[root@hd ~]# touch {a..f}.txt
[root@hd ~]# ls
10.txt  2.txt  4.txt  6.txt  8.txt  anaconda-ks.cfg  b.txt  done   e.txt  useradd
1.txt   3.txt  5.txt  7.txt  9.txt  a.txt            c.txt  d.txt  f.txt

cp
	copy 复制或另存为
	cp [option] source destination
		-r 递归复制目录树:递归式复制指的是当要复制的目录下存在子目录,且子目录中存在子目录或文件的时候,将逐一复制它们
#复制文件
[root@hd ~]# cp 1.txt 11.txt
[root@hd ~]# ls
10.txt  1.txt  3.txt  5.txt  7.txt  9.txt            a.txt  c.txt  d.txt  f.txt
11.txt  2.txt  4.txt  6.txt  8.txt  anaconda-ks.cfg  b.txt  done   e.txt  useradd
#无法直接复制文件夹
[root@hd ~]# cp /etc/ /eee/
cp: 略过目录"/etc/"
[root@hd ~]# ls /eee/
ls: 无法访问/eee/: 没有那个文件或目录
#递归复制文件夹
[root@hd ~]# cp -r /etc/ /ew/
[root@hd ~]# ls /ew/
adjtime                  ethertypes          ld.so.cache               plymouth        shadow
aliases                  exports             ld.so.conf                pm              shadow-

注意:系统定义别名cp='cp -i',如果不想执行这种别名,可以使用转移符'\'

案例1:在/etc/sysconfig/network-scripts/里,将ifcfg-ens33复制成ifcfg-ens34
方法一:
# cd /etc/sysconfig/network-scripts/
# cp ifcfg-ens33 ifcfg-ens34

[root@hd ~]# cd /etc/sysconfig/network-scripts/
[root@hd network-scripts]# cp ifcfg-ens33 ifcfg-ens34
[root@hd network-scripts]# ls
ifcfg-ens33  ifdown-ippp    ifdown-sit       ifup-bnep  ifup-plusb   ifup-TeamPort
ifcfg-ens34  ifdown-ipv6    ifdown-Team      ifup-eth   ifup-post    ifup-tunnel
ifcfg-lo     ifdown-isdn    ifdown-TeamPort  ifup-ippp  ifup-ppp     ifup-wireless
ifdown       ifdown-post    ifdown-tunnel    ifup-ipv6  ifup-routes  init.ipv6-global
ifdown-bnep  ifdown-ppp     ifup             ifup-isdn  ifup-sit     network-functions
ifdown-eth   ifdown-routes  ifup-aliases     ifup-plip  ifup-Team    network-functions-ipv6

方法二:
# cp /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-ens34

[root@hd ~]# cp /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-ens34
[root@hd ~]# ls /etc/sysconfig/network-scripts/
ifcfg-ens33  ifdown-ippp    ifdown-sit       ifup-bnep  ifup-plusb   ifup-TeamPort
ifcfg-ens34  ifdown-ipv6    ifdown-Team      ifup-eth   ifup-post    ifup-tunnel
ifcfg-lo     ifdown-isdn    ifdown-TeamPort  ifup-ippp  ifup-ppp     ifup-wireless
ifdown       ifdown-post    ifdown-tunnel    ifup-ipv6  ifup-routes  init.ipv6-global
ifdown-bnep  ifdown-ppp     ifup             ifup-isdn  ifup-sit     network-functions
ifdown-eth   ifdown-routes  ifup-aliases     ifup-plip  ifup-Team    network-functions-ipv6
方法三:
# cp /etc/sysconfig/network-scripts/ifcfg-ens3{3,4}

[root@hd ~]# cp /etc/sysconfig/network-scripts/ifcfg-ens3{3,4}
[root@hd ~]# ls /etc/sysconfig/network-scripts/
ifcfg-ens33  ifdown-ippp    ifdown-sit       ifup-bnep  ifup-plusb   ifup-TeamPort
ifcfg-ens34  ifdown-ipv6    ifdown-Team      ifup-eth   ifup-post    ifup-tunnel
ifcfg-lo     ifdown-isdn    ifdown-TeamPort  ifup-ippp  ifup-ppp     ifup-wireless
ifdown       ifdown-post    ifdown-tunnel    ifup-ipv6  ifup-routes  init.ipv6-global
ifdown-bnep  ifdown-ppp     ifup             ifup-isdn  ifup-sit     network-functions
ifdown-eth   ifdown-routes  ifup-aliases     ifup-plip  ifup-Team    network-functions-ipv6

mv
	move 剪切或重命名
	mv [option] source destination

[root@hd ~]# mv /ew/ /eee/
[root@hd ~]# ls /eee/
adjtime                  ethertypes          ld.so.cache               plymouth        shadow
aliases                  exports             ld.so.conf                pm              shadow-

rename
	批量重命名
案例1:将CentOS7的官方默认yum仓库文件都重命名
# cd /etc/yum.repos.d/
# rename .repo .repo.bak *

[root@hd ~]# cd /etc/yum.repos.d/
[root@hd yum.repos.d]# rename .repo .repo.bak *
[root@hd yum.repos.d]# ls
bak  cdrom.repo.bak  CentOS-Base.repo.bak  epel.repo.bak
rm
	remove
	rm [option] destination
		-f force 强制删除或者使用\rm
		-r 递归删除目录树
			-rf 强制删除目录下所有文件
  # rm -rf /* 

#普通删除		
[root@hd ~]# rm /eee/adjtime 
rm:是否删除普通文件 "/eee/adjtime"?y

#强制删除
[root@hd ~]# rm -f /eee/audi

#无法删除
[root@hd ~]# rm /eee/
rm: 无法删除"/eee/": 是一个目录
#递归删除目录树
[root@hd ~]# \rm -r /eee/
[root@hd ~]# ls /eee/
ls: 无法访问/eee/: 没有那个文件或目录

文件查看命令

cat
	适合查看短文件

[root@hd ~]# cat /etc/networks 
default 0.0.0.0
loopback 127.0.0.0
link-local 169.254.0.0

head
	从文件头部看起,默认前10行

[root@hd ~]# head /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin	
	
#显示filename前面N行
head -n N filename

[root@hd ~]# head -n 5 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

tail
	从文件尾部看起,默认后10行
	-f 监视文件尾部内容,刷新在屏幕上
	
[root@hd ~]# tail /etc/passwd
test91:x:1090:1090::/home/test91:/bin/bash
test92:x:1091:1091::/home/test92:/bin/bash
test93:x:1092:1092::/home/test93:/bin/bash
test94:x:1093:1093::/home/test94:/bin/bash
test95:x:1094:1094::/home/test95:/bin/bash
test96:x:1095:1095::/home/test96:/bin/bash
test97:x:1096:1096::/home/test97:/bin/bash
test98:x:1097:1097::/home/test98:/bin/bash
test99:x:1098:1098::/home/test99:/bin/bash
test100:x:1099:1099::/home/test100:/bin/bash

#显示filename最后N行
tail -n N filename

#显示/etc/passwd最后5行
[root@hd ~]# tail -n 5 /etc/passwd
test96:x:1095:1095::/home/test96:/bin/bash
test97:x:1096:1096::/home/test97:/bin/bash
test98:x:1097:1097::/home/test98:/bin/bash
test99:x:1098:1098::/home/test99:/bin/bash
test100:x:1099:1099::/home/test100:/bin/bash


more
	分页浏览,看完自动退出
more filename	

less
	分页反复浏览,按'q'退出
less filename

命令帮助

有存储位置的命令为外部命令,没有存储位置的命令为内部命令
#type判断内外部命令

[root@hd ~]# type help
help 是 shell 内嵌      #内部命令
[root@hd ~]# type passwd 
passwd 是 /usr/bin/passwd   #外部命令

--help
	帮助外部命令
	
命令字 --help

[[root@hd ~]# passwd --help
用法: passwd [选项...] <帐号名称>
  -k, --keep-tokens       保持身份验证令牌不过期
  -d, --delete            删除已命名帐号的密码(只有根用户才能进行此操作)
  -l, --lock              锁定指名帐户的密码(仅限 root 用户)
  -u, --unlock            解锁指名账户的密码(仅限 root 用户)
  -e, --expire            终止指名帐户的密码(仅限 root 用户)
  -f, --force             强制执行操作
  -x, --maximum=DAYS      密码的最长有效时限(只有根用户才能进行此操作)
  -n, --minimum=DAYS      密码的最短有效时限(只有根用户才能进行此操作)
  -w, --warning=DAYS      在密码过期前多少天开始提醒用户(只有根用户才能进行此操作)
  -i, --inactive=DAYS     当密码过期后经过多少天该帐号会被禁用(只有根用户才能进行此操作)
  -S, --status            报告已命名帐号的密码状态(只有根用户才能进行此操作)
  --stdin                 从标准输入读取令牌(只有根用户才能进行此操作)

Help options:
  -?, --help              Show this help message
  --usage                 Display brief usage message
help
	帮助内部命令
	
help 命令字

[root@hd ~]# help cd
cd: cd [-L|[-P [-e]]] [dir]
    Change the shell working directory.
    
    Change the current directory to DIR.  The default DIR is the value of the
    HOME shell variable.
    
    The variable CDPATH defines the search path for the directory containing
    DIR.  Alternative directory names in CDPATH are separated by a colon (:).
    A null directory name is the same as the current directory.  If DIR begins
    with a slash (/), then CDPATH is not used.
    
    If the directory is not found, and the shell option `cdable_vars' is set,
    the word is assumed to be  a variable name.  If that variable has a value,
    its value is used for DIR.
    
    Options:
        -L	force symbolic links to be followed
        -P	use the physical directory structure without following symbolic
    	links
        -e	if the -P option is supplied, and the current working directory
    	cannot be determined successfully, exit with a non-zero status
    
    The default is to follow symbolic links, as if `-L' were specified.
    
    Exit Status:
    Returns 0 if the directory is changed, and if $PWD is set successfully when
    -P is used; non-zero otherwise.

man
	手册页
	以全屏显示在线帮助,按q退出,上,下键移动 
man 命令字

[root@hd ~]# man rm

info
	信息页
info 命令字

[root@hd ~]# info rm

文件下载命令

wget    //文件下载
	-O   //指定下载地址,更改名称  
	-T   //超时时间
	-q   //安静下载(关闭wget输出)
	--spider //网络爬虫 
	
#下载并以新文件名保存
wget -O 新文件名 URL

# yum install -y wget

[root@hd ~]# yum install -y wget
已加载插件:fastestmirror
Determining fastest mirrors
 * base: mirrors.cqu.edu.cn
 * extras: mirrors.cqu.edu.cn
 * updates: mirrors.njupt.edu.cn
.......
已安装:
  wget.x86_64 0:1.14-18.el7_6.1                                                             

完毕!

# yum install -y lrzsz

[root@hd ~]# yum install -y lrzsz
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.cqu.edu.cn
 * extras: mirrors.cqu.edu.cn
 * updates: mirrors.njupt.edu.cn
......

已安装:
  lrzsz.x86_64 0:0.12.20-36.el7                                                             

完毕!

lrzsz提供xshell远程软件实现服务器和windows主机之间文件传输
rz
命令本地上传文件到服务器:
# rz
执行该命令后,在弹出框中选择要上传的文件即可。

[root@hd ~]# rz -E
rz waiting to receive.
sz
命令发送文件到本地:
# sz filename

[root@hd ~]# wget https://www.baidu.com/img/bd_logo1.png
--2019-06-23 11:07:39--  https://www.baidu.com/img/bd_logo1.png
正在解析主机 www.baidu.com (www.baidu.com)... 61.135.169.125, 61.135.169.121
正在连接 www.baidu.com (www.baidu.com)|61.135.169.125|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:7877 (7.7K) [image/png]
正在保存至: “bd_logo1.png”

100%[==================================================>] 7,877       --.-K/s 用时 0s      

2019-06-23 11:07:40 (49.7 MB/s) - 已保存 “bd_logo1.png” [7877/7877])

[root@hd ~]# sz bd_logo1.png
[root@hd ~]# 00000

管道

cmd1 | cmd2 | cmd3

案例1:获取当前登录用户的数量
分析:
查看用户登录信息的命令
who

[root@hd ~]# who
root     pts/0        2019-06-23 10:58 (10.0.0.1)

统计命令
wc
	word count
	-l 统计行数

答案:
# who | wc -l

[root@hd ~]# who | wc -l
1

案例2:获取当前系统根分区使用情况
分析:
查看分区使用情况的命令
df

[root@hd ~]# df
文件系统                   1K-块    已用     可用 已用% 挂载点
/dev/mapper/centos-root 17811456 1143116 16668340    7% /
devtmpfs                 1001916       0  1001916    0% /dev
tmpfs                    1014044       0  1014044    0% /dev/shm
tmpfs                    1014044    9736  1004308    1% /run
tmpfs                    1014044       0  1014044    0% /sys/fs/cgroup
/dev/sda1                1038336  135296   903040   14% /boot
tmpfs                     202812       0   202812    0% /run/user/0

分隔字段
awk -F'' '{print $n}'

答案:
[root@hd ~]# df /
文件系统                   1K-块    已用     可用 已用% 挂载点
/dev/mapper/centos-root 17811456 1143152 16668304    7% /
[root@hd ~]#  df / | tail -1 
/dev/mapper/centos-root 17811456 1143192 16668264    7% /
[root@hd ~]#  df / | tail -1 | awk '{print $5}'
7%
[root@hd ~]# df / | tail -1 | awk '{print $5}' | awk -F'%' '{print $1}'
7

# df / | tail -1 | awk '{print $5}' | awk -F'%' '{print $1}'


案例3:获取当前系统登录成功的IP地址top10
分析:
1.需要查看日志/var/log/secure
# grep 'Accept' /var/log/secure 

2.过滤出ip
# grep 'Accept' secure.log | awk '{print $11}' 

3.排序,去重
排序命令
	sort
		-r 降序
		-n 以数字的根式
去重命令
	uniq
		-c 去重,并统计重复出现的次数
		
# grep 'Accept' secure.log | awk '{print $11}' | sort | uniq -c | sort -r | head

[root@hd ~]#  grep 'Accept' secure.log | awk '{print $11}' | sort | uniq -c | sort -r | head
      5 59.172.62.234
      4 121.60.119.85


案例4:获取当前系统登录失败的IP地址top10
# grep 'Fail' secure.log  | awk '{print $(NF-3)}' | sort | uniq -c | sort -rn | head

[root@hd ~]# grep 'Fail' secure.log  | awk '{print $(NF-3)}' | sort | uniq -c | sort -rn | head
    370 218.2.0.180
     40 175.6.79.194
     35 101.227.249.121
      6 221.216.205.6
      6 154.126.227.244
      5 27.150.169.54
      5 111.207.30.136
      3 54.223.168.233
      3 112.175.29.58
      2 123.207.54.49

案例5:获取访问网站客户ip top5
# awk '{print $1}' nginx.log | sort | uniq -c | sort -rn | head -5

[root@hd ~]#  awk '{print $1}' nginx.log | sort | uniq -c | sort -rn | head -5
   4156 47.92.239.154
   1305 47.101.50.252
   1096 120.244.106.104
    827 125.106.191.210
    767 192.227.86.114

输入输出重定向



标准输入	stdin	0
标准输出	stdout
				正确1
				错误2

	输入重定向 <
	输入重定向:是指不使用系统提供的标准输入端口,而进行重新的指定。换言之,输入重定向就是不使用标准输入端口输入文件,而是使用指定的文件作为标准输入设备。
	正确输出重定向 > >>
	错误输出重定向 2> 2>>
	混合输出重定向 &>

应用1:创建文件hello.txt,并写入内容hello
# echo hello > hello.txt

[root@hd ~]#  echo hello > hello.txt
[root@hd ~]# cat hello.txt 
hello

应用2:创建文件hello.txt,并写入三行hello
解法一:
# echo 'hello
> hello
> hello' > hello.txt

[root@hd ~]# echo 'hello
> hello
> hello' > hello.txt
[root@hd ~]# cat hello.txt 
hello
hello
hello

解法二:
# cat > bbb.txt <<end
bbb
bbb
bbb
end

[root@hd ~]# cat > bbb.txt <<end
> bbb
> bbb
> bbb
> end
[root@hd ~]# cat bbb.txt 
bbb
bbb
bbb

脚本案例:

1.批量创建用户,并配置默认密码
分析:
创建用户
useradd 
修改密码
echo 123456 |passwd --stdin user


[root@hd ~]# useradd tls
[root@hd ~]# echo 123456 |passwd --stdin tls
更改用户 tls 的密码 。
passwd:所有的身份验证令牌已经成功更新。


# yum install -y vim

案例(1)批量创建相同前缀的用户
vim user.sh
#!/bin/bash

i=1
while [ $i -le 5 ]
do
        useradd user$i
        echo 123456 | passwd --stdin user$i &> /dev/null
        echo "create user$i success"
        let i++
done

案例(2)批量创建没管理的用户
zhangsan,lisi,wangwu
分析:
1)先创建用户列表文件
# echo 'zhangsan
lisi
wangwu' > user.list'

2)使用for循环实现
vim user.sh
#!/bin/bash

for user in $(cat user.list)
do
        useradd $user
        echo 123456 | passwd --stdin $user &> /dev/null
        echo "create $user success"
done

猜你喜欢

转载自blog.csdn.net/qq_37324376/article/details/93708665