@Linux之打包与压缩

1. 压缩的概念

什么是压缩包?


就是一个特殊的文件,将多个文件或者目录压缩成一个特殊的文件  


为什么要用压缩包?

	减少占用的空间  
	
	加快传输的速度


windows:

	.zip      .rar
	
linux:

	.zip	.gz    .tar.gz  	


如果要想Windows和linux中的压缩包互相使用,建议使用.zip种类的压缩包 


在Linux中压缩包的类型


.zip		#根据zip命令进行打包压缩的

.gz			#通过gzip命令进行压缩  只压缩文件,也会删除源文件 

.bz2		#通过bzip进行压缩, 只压缩文件,也会删除源文件

.tar.gz		#使用tar命令归档打包,然后使用gzip命令进行压缩 

.tar.bz2		#使用tar命令归档打包,然后使用bzip命令进行压缩  


2. zip命令


可以对文件和目录进行打包,需要指定压缩包的名称 

[root@qls ~]# yum install  -y  zip  unzip

zip		#压缩的命令

选项:

		-r		#压缩目录 
		
		-q		#静默输出,不显示压缩的过程 
		
		
unzip	#解压zip格式的压缩包

选项:

		-l		#查看压缩包中的列表信息 
		
		-q		#静默输出,不显示解压的过程
		
		-d		#指定解压的目录





[root@qls ~]# ll
total 656
-rw-r--r-- 1 root root 670293 Jul 29 08:45 services

#针对文件进行压缩打包 

[root@qls ~]# zip  services.zip   services 
  adding: services (deflated 80%)
[root@qls ~]# ll
total 792
-rw-r--r-- 1 root root 670293 Jul 29 08:45 services
-rw-r--r-- 1 root root 136227 Jul 29 09:06 services.zip


#压缩目录时不加选项,只压缩目录本身 

[root@qls ~]# zip  etc.zip  /etc
  adding: etc/ (stored 0%)
[root@qls ~]# ll
total 796
-rw-r--r-- 1 root root    158 Jul 29 09:07 etc.zip
-rw-r--r-- 1 root root 670293 Jul 29 08:45 services
-rw-r--r-- 1 root root 136227 Jul 29 09:06 services.zip
[root@qls ~]# unzip  -l  etc.zip 
Archive:  etc.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  07-29-2020 08:56   etc/
---------                     -------
        0                     1 file
[root@qls ~]# unzip  etc.zip 
Archive:  etc.zip
   creating: etc/
[root@qls ~]# ll
total 796
drwxr-xr-x 2 root root      6 Jul 29 08:56 etc
-rw-r--r-- 1 root root    158 Jul 29 09:07 etc.zip
-rw-r--r-- 1 root root 670293 Jul 29 08:45 services
-rw-r--r-- 1 root root 136227 Jul 29 09:06 services.zip
[root@qls ~]# ll etc
total 0

[root@qls ~]# zip  -r  etc.zip  /etc


[root@qls ~]# ll -h
total 13M
-rw-r--r-- 1 root root  12M Jul 29 09:10 etc.zip
-rw-r--r-- 1 root root 655K Jul 29 08:45 services
-rw-r--r-- 1 root root 134K Jul 29 09:06 services.zip
[root@qls ~]# du -sh  /etc
31M	/etc


#查看压缩包里面的文件

[root@qls ~]# unzip -l  etc.zip

#静默输出 

[root@qls ~]# zip  -rq    etc1.zip  /etc
[root@qls ~]# ll
total 24976
-rw-r--r-- 1 root root 12380952 Jul 29 09:13 etc1.zip
-rw-r--r-- 1 root root 12380952 Jul 29 09:10 etc.zip
-rw-r--r-- 1 root root   670293 Jul 29 08:45 services
-rw-r--r-- 1 root root   136227 Jul 29 09:06 services.zip


#解压
[root@qls ~]# unzip   etc.zip

#静默解压文件,将文件解压到指定的目录下面

[root@qls ~]# unzip  -q  etc.zip   -d  /opt/
[root@qls ~]# ll /opt/
total 12
drwxr-xr-x 85 root root 8192 Jul 29 08:56 etc
-rw-r--r--  1 root root    0 Jul 28 12:09 file9



3. gzip命令



gzip	#只能压缩文件,压缩文件的同时,会删除源文件,解压的时候,会删除压缩文件

选项:

	-d		#解压
	
	-r		#递归操作 
	
	
[root@qls ~]# ll
total 656
-rw-r--r-- 1 root root 670293 Jul 29 08:45 services

[root@qls ~]# gzip   services 
[root@qls ~]# ll
total 136
-rw-r--r-- 1 root root 136088 Jul 29 08:45 services.gz

#使用zcat命令查看gzip压缩的文件

[root@qls ~]# zcat  services.gz

#解压 

[root@qls ~]# gzip  -d  services.gz 
[root@qls ~]# ll
total 656
-rw-r--r-- 1 root root 670293 Jul 29 08:45 services

#将目录下的所有都进行压缩处理

[root@qls ~]# gzip  -r   /etc

#解压目录下的所有压缩包 

[root@qls ~]# gzip  -rd  /etc


[root@qls ~]# yum  install  -y  bzip2

#压缩
[root@qls ~]# bzip2  services 
[root@qls ~]# ll
total 124
-rw-r--r-- 1 root root 123932 Jul 29 08:45 services.bz2

#解压 
[root@qls ~]# bzip2  -d  services.bz2 
[root@qls ~]# ll
total 656
-rw-r--r-- 1 root root 670293 Jul 29 08:45 services

#查看bzip2压缩的文件

[root@qls ~]# bzcat   services.bz2 

4. tar命令

不负责压缩,只负责归档打包


选项:

	z		#使用gzip格式进行压缩
	
	j		# 使用bzip2格式进行压缩    了解
	
	J		#使用xz格式进行压缩       了解 
	
	c		#创建压缩包 
	
	v		#显示过程 
	
	f		#指定压缩包的名称 
	
	
	czf		#打包压缩  tar.gz格式的压缩包 
	
	cjf		#打包压缩 tar.bz2格式的压缩包 		了解
	
	cJf		#打包压缩 tar.xz格式的压缩包 			了解 
	
	t		#查看压缩中的内容 
	
	tf		#指定要查看的压缩包名称 
	
	x		#解压
	
	xf		#解压指定的压缩包 
	
	-C		#解压到指定的目录 
	
	h		#打包软连接的真实路径
	
	P		#使用绝对路径打包 
	
	X		#指定排除文件的列表文件
	
	--exclude=		#指定排除的文件
	
	--exclude-from=		#指定排除文件的列表
	
	
	
[root@qls ~]# ll
total 656
-rw-r--r-- 1 root root 670293 Jul 29 09:47 services
[root@qls ~]# tar  czf  services.tar.gz   services 
[root@qls ~]# ll
total 792
-rw-r--r-- 1 root root 670293 Jul 29 09:47 services
-rw-r--r-- 1 root root 136200 Jul 29 09:48 services.tar.gz

[root@qls ~]# tar  cjf  services.tar.bz2  services
[root@qls ~]# ll
total 916
-rw-r--r-- 1 root root 670293 Jul 29 09:47 services
-rw-r--r-- 1 root root 124166 Jul 29 09:48 services.tar.bz2
-rw-r--r-- 1 root root 136200 Jul 29 09:48 services.tar.gz
[root@qls ~]# tar  cJf  services.tar.xz  services
[root@qls ~]# ll
total 1016
-rw-r--r-- 1 root root 670293 Jul 29 09:47 services
-rw-r--r-- 1 root root 124166 Jul 29 09:48 services.tar.bz2
-rw-r--r-- 1 root root 136200 Jul 29 09:48 services.tar.gz
-rw-r--r-- 1 root root 100640 Jul 29 09:49 services.tar.xz

[root@qls ~]# tar  czf  etc.tar.gz   /etc
tar: Removing leading `/' from member names		#删根的操作是正常的
[root@qls ~]# ll
total 10952
-rw-r--r-- 1 root root 10171684 Jul 29 09:56 etc.tar.gz



[root@qls ~]# tar  tf  services.tar.gz 
services



[root@qls ~]# tar  xf  services.tar.gz 
[root@qls ~]# ll
total 10952
-rw-r--r-- 1 root root 10171684 Jul 29 09:56 etc.tar.gz
-rw-r--r-- 1 root root   670293 Jul 29 09:47 services
-rw-r--r-- 1 root root   124166 Jul 29 09:48 services.tar.bz2
-rw-r--r-- 1 root root   136200 Jul 29 09:48 services.tar.gz
-rw-r--r-- 1 root root   100640 Jul 29 09:49 services.tar.xz
[root@qls ~]# tar xf  etc.tar.gz 
[root@qls ~]# ll
total 10964
drwxr-xr-x 77 root root     8192 Jul 29 08:56 etc
-rw-r--r--  1 root root 10171684 Jul 29 09:56 etc.tar.gz
-rw-r--r--  1 root root   670293 Jul 29 09:47 services
-rw-r--r--  1 root root   124166 Jul 29 09:48 services.tar.bz2
-rw-r--r--  1 root root   136200 Jul 29 09:48 services.tar.gz
-rw-r--r--  1 root root   100640 Jul 29 09:49 services.tar.xz


[root@qls ~]# tar  cjf  etc.tar.bz2   /etc
tar: Removing leading `/' from member names
[root@qls ~]# tar  cJf  etc.tar.xz   /etc
tar: Removing leading `/' from member names
[root@qls ~]# ll
total 26808
-rw-r--r-- 1 root root  8994047 Jul 29 10:01 etc.tar.bz2
-rw-r--r-- 1 root root 10171684 Jul 29 09:56 etc.tar.gz
-rw-r--r-- 1 root root  7238084 Jul 29 10:01 etc.tar.xz
-rw-r--r-- 1 root root   670293 Jul 29 09:47 services
-rw-r--r-- 1 root root   124166 Jul 29 09:48 services.tar.bz2
-rw-r--r-- 1 root root   136200 Jul 29 09:48 services.tar.gz
-rw-r--r-- 1 root root   100640 Jul 29 09:49 services.tar.xz
[root@qls ~]# tar  xf  etc.tar.bz2 		#解压的时候,自动识别压缩包的格式 
[root@qls ~]# ll
total 26820
drwxr-xr-x 77 root root     8192 Jul 29 08:56 etc
-rw-r--r--  1 root root  8994047 Jul 29 10:01 etc.tar.bz2
-rw-r--r--  1 root root 10171684 Jul 29 09:56 etc.tar.gz
-rw-r--r--  1 root root  7238084 Jul 29 10:01 etc.tar.xz


[root@qls ~]# tar  xf  etc.tar.gz   -C  /opt/
[root@qls ~]# ll /opt/
total 668
drwxr-xr-x 77 root root   8192 Jul 29 08:56 etc
-rw-r--r--  1 root root 670293 Jul 29 09:47 services


[root@qls ~]# tar czvf  etc.tar.gz   /etc


[root@qls ~]# tar  xvf  etc.tar.gz 


[root@qls ~]# ll  /bin  -d
lrwxrwxrwx. 1 root root 7 Jul  6 02:13 /bin -> usr/bin
[root@qls ~]# tar  czf  bin.tar.gz  /bin
tar: Removing leading `/' from member names
[root@qls ~]# ll
total 9952
-rw-r--r--  1 root root      110 Jul 29 10:14 bin.tar.gz
drwxr-xr-x 77 root root     8192 Jul 29 08:56 etc
-rw-r--r--  1 root root 10171684 Jul 29 10:04 etc.tar.gz
[root@qls ~]# tar xf  bin.tar.gz 
[root@qls ~]# ll
total 9952
lrwxrwxrwx  1 root root        7 Jul  6 02:13 bin -> usr/bin
-rw-r--r--  1 root root      110 Jul 29 10:14 bin.tar.gz
drwxr-xr-x 77 root root     8192 Jul 29 08:56 etc
-rw-r--r--  1 root root 10171684 Jul 29 10:04 etc.tar.gz



[root@qls ~]# tar  czhf  bin1.tar.gz  /bin
tar: Removing leading `/' from member names
tar: Removing leading `/' from hard link targets
[root@qls ~]# ll
total 36880
lrwxrwxrwx  1 root root        7 Jul  6 02:13 bin -> usr/bin
-rw-r--r--  1 root root 27573768 Jul 29 10:16 bin1.tar.gz
-rw-r--r--  1 root root      110 Jul 29 10:14 bin.tar.gz
drwxr-xr-x 77 root root     8192 Jul 29 08:56 etc
-rw-r--r--  1 root root 10171684 Jul 29 10:04 etc.tar.gz



#打包的时候有个删根的操作   怎么避免这个删根的操作 

#第一种   使用相对路径打包 

[root@qls ~]# cd  /  
[root@qls /]# tar  czf  /root/etc1.tar.gz    etc
[root@qls /]# ll /root
total 46852
dr-xr-xr-x  2 root root    20480 Jul 29 08:58 bin
-rw-r--r--  1 root root 27573768 Jul 29 10:16 bin1.tar.gz
-rw-r--r--  1 root root      110 Jul 29 10:14 bin.tar.gz
drwxr-xr-x 77 root root     8192 Jul 29 08:56 etc
-rw-r--r--  1 root root 10171684 Jul 29 10:19 etc1.tar.gz
-rw-r--r--  1 root root 10171684 Jul 29 10:04 etc.tar.gz

#真实打包场景 

[root@qls ~]# cd  /  &&  tar  czf  /root/etc3.tar.gz   etc  &&  cd  -
/root
[root@qls ~]# ll
total 581012
dr-xr-xr-x  2 root root     20480 Jul 29 08:58 bin
-rw-r--r--  1 root root  27573768 Jul 29 10:16 bin1.tar.gz
-rw-r--r--  1 root root       110 Jul 29 10:14 bin.tar.gz
drwxr-xr-x 77 root root      8192 Jul 29 08:56 etc
-rw-r--r--  1 root root  10171684 Jul 29 10:19 etc1.tar.gz
-rw-r--r--  1 root root 347209728 Jul 29 10:22 etc2.tar.gz
-rw-r--r--  1 root root  10171684 Jul 29 10:22 etc3.tar.gz


#第二种方法   使用绝对路径打包    告诉系统不要删根 


#使用绝对路径打包 

[root@qls ~]# tar  czPf  etc4.tar.gz   /etc

#使用绝对路经解压 

[root@qls ~]# tar xPf  etc4.tar.gz


[root@qls ~]# cat  /etc/services  >> /opt/services 
[root@qls ~]# ll /opt/
total 1744
drwxr-xr-x 77 root root    8192 Jul 29 08:56 etc
-rw-r--r--  1 root root     158 Jul 29 10:28 hosts
-rw-r--r--  1 root root 1340586 Jul 29 10:29 services
[root@qls ~]# tar xPf  opt.tar.gz 
[root@qls ~]# ll /opt/
total 672
drwxr-xr-x 77 root root   8192 Jul 29 08:56 etc
-rw-r--r--  1 root root    158 Jul 29 10:28 hosts
-rw-r--r--  1 root root 670293 Jul 29 09:47 services


#排除不需要打包的文件

[root@qls ~]# tar czf  etc1.tar.gz  /etc
tar: Removing leading `/' from member names
[root@qls ~]# ll
total 9936
-rw-r--r-- 1 root root 10171949 Jul 29 10:51 etc1.tar.gz
[root@qls ~]# ll /etc/services 
-rw-r--r-- 1 root root 670293 Jun  7  2013 /etc/services
[root@qls ~]# tar  czf  etc2.tar.gz   --exclude=/etc/services   /etc
tar: Removing leading `/' from member names
[root@qls ~]# ll
total 19740
-rw-r--r-- 1 root root 10171949 Jul 29 10:51 etc1.tar.gz
-rw-r--r-- 1 root root 10036635 Jul 29 10:52 etc2.tar.gz
[root@qls ~]# tar tf  etc2.tar.gz  |  grep  services
etc/firewalld/services/


#排除多个文件

[root@qls ~]# tar czf  etc3.tar.gz  --exclude=/etc/services  --exclude=/etc/passwd  /etc
tar: Removing leading `/' from member names
[root@qls ~]# ll
total 29544
-rw-r--r-- 1 root root 10171949 Jul 29 10:51 etc1.tar.gz
-rw-r--r-- 1 root root 10036635 Jul 29 10:52 etc2.tar.gz
-rw-r--r-- 1 root root 10035297 Jul 29 10:54 etc3.tar.gz
[root@qls ~]# tar tf  etc3.tar.gz  | grep  -E  'services|passwd'
etc/security/opasswd
etc/passwd-
etc/pam.d/passwd
etc/firewalld/services/


[root@qls ~]# tar czf  etc4.tar.gz  --exclude=/etc/{services,passwd}   /etc
tar: Removing leading `/' from member names
[root@qls ~]# ll
total 39348
-rw-r--r-- 1 root root 10171949 Jul 29 10:51 etc1.tar.gz
-rw-r--r-- 1 root root 10036635 Jul 29 10:52 etc2.tar.gz
-rw-r--r-- 1 root root 10035297 Jul 29 10:54 etc3.tar.gz
-rw-r--r-- 1 root root 10035297 Jul 29 10:55 etc4.tar.gz


[root@qls ~]# cat paichu.list
/etc/fstab
/etc/group
/etc/hosts
/etc/passwd
/etc/services
[root@qls ~]# tar czfX   etc5.tar.gz   paichu.list    /etc
tar: Removing leading `/' from member names
[root@qls ~]# ll 
total 49152
-rw-r--r-- 1 root root 10171949 Jul 29 10:51 etc1.tar.gz
-rw-r--r-- 1 root root 10036635 Jul 29 10:52 etc2.tar.gz
-rw-r--r-- 1 root root 10035297 Jul 29 10:54 etc3.tar.gz
-rw-r--r-- 1 root root 10035297 Jul 29 10:55 etc4.tar.gz
-rw-r--r-- 1 root root 10033927 Jul 29 10:58 etc5.tar.gz
-rw-r--r-- 1 root root       59 Jul 29 10:57 paichu.list




[root@qls ~]# tar tf etc5.tar.gz  | grep  -Ew  'fstab|group|hosts|passwd|services'
etc/security/group.conf
etc/group-
etc/passwd-
etc/hosts.allow
etc/hosts.deny
etc/pam.d/passwd
etc/iproute2/group
etc/firewalld/services/



[root@qls ~]# tar  czf  etc6.tar.gz   --exclude-from=paichu.list   /etc
tar: Removing leading `/' from member names
[root@qls ~]# ll
total 58952
-rw-r--r-- 1 root root 10171949 Jul 29 10:51 etc1.tar.gz
-rw-r--r-- 1 root root 10036635 Jul 29 10:52 etc2.tar.gz
-rw-r--r-- 1 root root 10035297 Jul 29 10:54 etc3.tar.gz
-rw-r--r-- 1 root root 10035297 Jul 29 10:55 etc4.tar.gz
-rw-r--r-- 1 root root 10033927 Jul 29 10:58 etc5.tar.gz
-rw-r--r-- 1 root root 10033927 Jul 29 11:01 etc6.tar.gz
-rw-r--r-- 1 root root       59 Jul 29 10:57 paichu.list



5. tar命令与find结合


[root@qls ~]# tar  czf  log1.tar.gz   `find  /var/log/  -type f  -name "*.log"`
tar: Removing leading `/' from member names
[root@qls ~]# tar  czf  log2.tar.gz   $(find  /var/log/  -type f  -name "*.log")
tar: Removing leading `/' from member names
[root@qls ~]# find  /var/log/  -type f  -name "*.log"  -exec  tar czf  log3.tar.gz  {}  \;
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
[root@qls ~]# 
[root@qls ~]# find  /var/log/  -type f  -name "*.log"  | xargs  tar czf   log4.tar.gz  
tar: Removing leading `/' from member names



[root@qls ~]# ll
total 916
-rw-r--r-- 1 root root 309155 Jul 29 11:09 log1.tar.gz
-rw-r--r-- 1 root root 309155 Jul 29 11:09 log2.tar.gz
-rw-r--r-- 1 root root    356 Jul 29 11:10 log3.tar.gz
-rw-r--r-- 1 root root 309155 Jul 29 11:10 log4.tar.gz

[root@qls ~]# tar tf  log1.tar.gz   
var/log/tuned/tuned.log
var/log/audit/audit.log
var/log/anaconda/anaconda.log
var/log/anaconda/X.log
var/log/anaconda/program.log
var/log/anaconda/packaging.log
var/log/anaconda/storage.log
var/log/anaconda/ifcfg.log
var/log/anaconda/ks-script-3PCKae.log
var/log/anaconda/journal.log
var/log/boot.log
var/log/vmware-vmsvc.log
var/log/vmware-network.8.log
var/log/vmware-network.5.log
var/log/vmware-network.2.log
var/log/vmware-network.7.log
var/log/vmware-network.4.log
var/log/vmware-network.1.log
var/log/test.log
var/log/yum.log
var/log/vmware-network.9.log
var/log/nginx/error.log
var/log/nginx/access.log
var/log/vmware-network.6.log
var/log/vmware-network.3.log
var/log/vmware-network.log
[root@qls ~]# tar tf  log3.tar.gz   
var/log/vmware-network.log



当使用-exec时,  把find查找的文件是进行一个个赋值给{}xargs   把find命令查找出来的文件统一的赋值给了后面 


6. date命令


#显示系统时间 


[root@qls ~]# date
Wed Jul 29 11:19:19 CST 2020


[root@qls ~]# date  +%Y		#世纪
2020
[root@qls ~]# date  +%y		#年代
20
[root@qls ~]# date  +%m		#月份
07
[root@qls ~]# date  +%d		#日期 
29

[root@qls ~]# date  +%Y-%m-%d
2020-07-29


[root@qls ~]# date +%F		#显示日期 
2020-07-29


[root@qls ~]# date  +%y-%m-%d
20-07-29


[root@qls ~]# date +%H		#时
11
[root@qls ~]# date +%M		#分
23
[root@qls ~]# date +%S		#秒
24
[root@qls ~]# date
Wed Jul 29 11:23:28 CST 2020
[root@qls ~]# date +%H:%M:%S
11:23:43

[root@qls ~]# date +%T		#显示时间 
11:24:08

[root@qls ~]# date +%F-%T
2020-07-29-11:24:34

[root@qls ~]# date +%w		#周几
3
[root@qls ~]# date +%W		#今年第几周 
30


[root@qls ~]# date +%s		#距离1970年1月1日0分0秒过来多少秒 
1595993167


选项:

	-d		#自定义日期 
	
	
	-s		#修改日期   时间 
	
	
	
[root@qls ~]# date +%F
2020-07-29
[root@qls ~]# date  -d  "-1 day"  +%F
2020-07-28

[root@qls ~]# date  -d  "+1 day"  +%F
2020-07-30
[root@qls ~]# date  -d  "+1 month"  +%F
2020-08-29
[root@qls ~]# date  -d  "-1 month"  +%F
2020-06-29
[root@qls ~]# date  -d  "-1 year"  +%F
2019-07-29
[root@qls ~]# date  -d  "+1 year"  +%F
2021-07-29


[root@qls ~]# date
Wed Jul 29 11:31:42 CST 2020
[root@qls ~]# date  -s  20200730
Thu Jul 30 00:00:00 CST 2020
[root@qls ~]# date
Thu Jul 30 00:00:08 CST 2020
[root@qls ~]# date  -s  2020/07/29
Wed Jul 29 00:00:00 CST 2020
[root@qls ~]# date
Wed Jul 29 00:00:02 CST 2020
[root@qls ~]# date  -s  11:32:30
Wed Jul 29 11:32:30 CST 2020
[root@qls ~]# date
Wed Jul 29 11:32:31 CST 2020
[root@qls ~]# date  -s  "20200730  11:00:00"
Thu Jul 30 11:00:00 CST 2020


#时间同步

ntpdate

[root@qls ~]# yum install ntpdate  -y 


向时间服务器进行同步时间 

ntp.aliyun.com   

ntp1.aliyun.com   ....   ntp7.aliyun.com  

[root@qls ~]# ntpdate   ntp.aliyun.com
29 Jul 11:36:33 ntpdate[31370]: step time server 203.107.6.88 offset -84415.000442 sec
[root@qls ~]# date
Wed Jul 29 11:36:35 CST 2020
[root@qls ~]# date  -s  20200730
Thu Jul 30 00:00:00 CST 2020
[root@qls ~]# ntpdate   ntp.aliyun.com
29 Jul 11:36:56 ntpdate[31392]: step time server 203.107.6.88 offset -44591.413933 sec
[root@qls ~]# date
Wed Jul 29 11:37:02 CST 2020


7.基础总结-练习题

基础阶段-


1.  创建/root/user /root/logs  /root/conf  /root/html目录,请用一条命令实现。



2. Linux关机重启及注销的命令有哪些 (每种至少两个)?



3. Linux中系统网卡的配置文件路径什么?


4. 你知道哪些bash的快捷键呢?请把他写出来,并解释一下他的作用?(至少8个)


5. 网络类型都有哪几种?这几种模式,每个的含义你是怎么理解的?


6. 找出系统中文件名以oldboy开头的所有文件,要求只能查找到/目录的前三级目录。

find  / -type f  -maxdepth  3  -name  "oldboy*"

7. Linux的发行版本都有哪些?(至少6个)

8. 怎样清除你所执行过的命令记录?

history  -c

history  -w


9. 说明下面这几个文件的作用: 

/etc/resolv.conf、		#本地DNS配置文件

/etc/hosts、			#域名跟IP地址的映射关系

/var/log/messages、	#系统日志


/var/log/secure。		#用户登录日志



10. 如何快速返回上一次所在的目录?


11. 欲把当前目录下的file1.txt复制为file2.txt 命令是,如果已存在该名称的文件名,怎么执行不会提示是否覆盖?

\cp

绝对路径  

12. 假设超级用户root当前所在目录为:/usr/local,键入cd命令后,用户当前所在目录为?

/root

13. 创建/data/test目录,已知/data目录不存在,请给出命令?

-p


14. 在/root/目录下创建文件test.txt  test.log  test.sh  test.conf  请用一条命令创建。


15.将/etc/passwd文件中的第一列和第七列的位置进行调换,以:号为分隔符。
[root@web01 ~]# head -1 /etc/passwd
root:x:0:0:root:/root:/bin/bash


[root@qls ~]# awk  -F:  '{a=$1;$1=$NF;$NF=a;print}'  passwd  | tr  ' '  ":"
/bin/bash:x:0:0:root:/root:root
/sbin/nologin:x:1:1:bin:/bin:bin



[root@qls ~]# sed  -r  's#(.*)(:x.*:)(.*)#\3\2\1#g'  passwd 
/bin/bash:x:0:0:root:/root:root
/sbin/nologin:x:1:1:bin:/bin:bin
/sbin/nologin:x:2:2:daemon:/sbin:daemon
/sbin/nologin:x:3:4:adm:/var/adm:adm
/sbin/nologin:x:4:7:lp:/var/spool/lpd:lp



16. 如果我在当前在/目录下,之后执行了以下操作,请告诉我,最后我所在的目录位置?
cd /etc/sysconfig/   
cd ..
cd ..
cd -
cd ~
cd ..   /   /home
cd -

/home/xx   /root  当前用户的家目录  



17. 查看oldboy.txt文件中的内容,并显示行号。(至少两种方法)


18. 显示/etc/services文件的第11行到第20行的内容(至少两种方法)?


19. 已知文件123.txt内容如下,请过滤出不包含oldboy字符串的命令
test
OLDBOY
online
oldboy
online
oldboyoldboy



20. 接上题,要求过滤出文件123.txt中包含online字符串的行,并统计过滤出来的内容共有多少行。

grep   -c   'online'   123.txt


21. 调试系统服务时,希望能实时查看系统日志/var/log/messages的更新,如何做?


22. 如何删除一个非空目录/opt?


23. 统计/etc/passwd文件一共有多少行?(两种方法)


24. 已知软件包的链接地址为http://nginx.org/download/nginx-1.16.0.tar.gz,要求将其下载且下载之后的软件包名为nginx.tar.gz,如何执行命令。

wget -O  nginx.tar.gz  http://nginx.org/download/nginx-1.16.0.tar.gz

curl  -o  nginx.tar.gz  http://nginx.org/download/nginx-1.16.0.tar.gz


25. 查找ifconfig命令的绝对路径在哪里?(两种方法)


26. 统计文件/etc/services的字节数?(两种方法)


27. 执行下面的命令echo "Im qiuzengjia , is QQ 1176494252" >file.txt,要求取出该文件中的姓名和QQ号。(注意逗号前面有一个空格)(两种方法)。

cut  -d " "  -f2,6   file.txt


28.执行如下命令,要求去除重复的行,并统计出现的次数?
cat > file.txt <<EOF
abc
123
abc
123
def
EOF

sort  file.txt  | uniq  -c  


29. 过滤出/etc/passwd以nologin结尾的内容,并统计行数。

grep  -c  'nologin$'  /etc/passwd  

sed  -n  '/nologin$/p'  /etc/passwd | wc  -l



30. 使用cat和echo命令把oldboy oldgirl student三行内容(每个字符串是一行内容)写入到test.txt文件中

echo  -e  "oldboy\noldgirl\nstudent"  >>test.txt


31. 以“:”为分隔符,取出/etc/passwd第一行的最后一列的内容?(两种方法)

awk  -F:  'NR==1{print $NF}'  /etc/passwd

32.已知执行ifconfig  eth0 结果如下:要求取出IP地址
[root@qiudao ~]# ifconfig  eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.19.186.20  netmask 255.255.240.0  broadcast 172.19.191.255	#前面有8个空格
        ether 00:16:3e:1a:18:44  txqueuelen 1000  (Ethernet)
        
 ifconfig  eth0  | awk  'NR==2{print $2}'       
        
33. 删除test.txt文件中的第10行到20行的内容。(两种方法)

sed  '10,20d'		test.txt

awk  'NR < 10  ||  NR > 20 '  test.txt

34. 取出test.txt文件中的第9行和第11行的内容(两种方法)

sed  -n  '9p;11p'  test.txt

awk    'NR==9;NR=11'  test.txt

35.统计nginx访问日志access.log中每个访问ip出现次数,显示最多的10个。(已知ip在第一列)

awk  '{print $1}'  access.log | sort  | uniq  -c  | sort -rn  | head 


36.过滤出/etc/services 文件包含3306或1521两数字所在的行的内容。(两种方法)

grep  -E  '3306|1521'   /etc/services

sed  -nr  '/3306|1521/p'  /etc/services

awk  '/3306|1521/'  /etc/services

37. 排除/etc/ssh/sshd_config文件中的空行和注释行(以#开头的就是注释行)(两种方法)

grep  -Ev  '^$|^#'  /etc/ssh/sshd_config

sed  -r  '/^$|^#/d'   /etc/ssh/sshd_config

awk  '!/^$|^#/'   /etc/ssh/sshd_config

38. 将/etc/passwd文件中第一行到第五行的root替换为admin?

sed   -i  '1,5s#root#admin#g'   /etc/passwd

39. 在test.txt文件末尾插入test。(两种方法)

echo "test"  >>  test.txt

sed  '$atest'  test.txt


40. 写出redhat 中,配置网卡及dns 的配置文件是什么?并说明区别?



41. 已知sort.log文件内容如下,请根据文件内容的第二列进行倒序排序。?
cat >>sort.log<<'EOF'
218.65.30.25 68652
218.65.30.53 34326
218.87.109.154 21201
112.85.42.103 18065
112.85.42.99 17164
218.87.109.151 17163
218.87.109.150 17163
218.65.30.61 17163
218.65.30.126 17163
218.65.30.124 17163
EOF

sort  -rnk2  sort.log

42. 已知文件test.txt内容如下,请给出输出test.txt文件内容时,不包含oldboy字符串的命令。
test
qiudao
oldboy



43. 找到/backup目录下所有后缀名为.txt的文件?

find  /backup   -type f  -name "*.txt"

44. 把/etc/passwd文件中的:替换成# (两种方法)

tr  ':'  '#'   < /etc/passwd

sed  's/:/#/g'  /etc/passwd

45. rm是个危险的命令,要求使用命令rm删除文件时提示“rm command no bny”,怎么实现?

alias  rm='echo  rm command no bny'

46. 取出下列文件的权限如:0644 ?(两种方法)
[root@qls ~]# stat  123.txt 
  File: ‘123.txt’
  Size: 44        	Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d	Inode: 67160518    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-07-27 16:55:57.055131412 +0800
Modify: 2019-07-27 16:55:50.098131214 +0800
Change: 2019-07-27 16:55:50.101131214 +0800




47. 分别写出GNU和GPL是什么?


48. 把/etc/passwd文件中的oldboy替换成oldgirl。

sed  's#oldboy#oldgirl#g'   /etc/passwd
  
49.显示/proc/meminfo文件中以s开头的行(忽略大小写)(两种方法)

grep  -i  '^s'    /proc/meminfo

sed  -nr  '/^s|^S/p'   /proc/meminfo

awk  '/^s|^S/'  /proc/meminfo


50.翻译题
01).command not found  				#命令找不到			  
02).No such file or directory  		#没有这个文件或者目录			   
03).File exists  					#文件已经存在					
04).Is a directory					#这是个目录



  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值