【若泽大数据基础第三天】基础-Linux基础命令三

Linux基础命令三

  • 文本编辑器:vi

注:在生产环境/操作系统上直接编辑文本文件时,一定要先进行备份,再对源文件进行编辑修改操作。
例:需要修改用户环境变量时:

[root@hadoop614 ~]# cp .bash_profile .bash_profile_20190124
## 编辑器常用命令

跳转到第一行首字符:gg
跳转到左后一行首字符:G
跳转到行尾:shift $
跳转到行首:shift ^
删除当前行:dd
删除光标以下的所有行:dG
删除光标一下n行:ndd

场景一:清楚文件内所有内容
方法一:
[root@hadoop614 ~]# ll test.txt 
-rw-r--r-- 1 root root 42 Jan 26 15:08 test.txt	#查看文件大小 42 字节
[root@hadoop614 ~]# echo > test.txt 				# 像文件内重定向一个空值替换文件所有内容
[root@hadoop614 ~]# ll test.txt 
-rw-r--r-- 1 root root 1 Jan 26 15:09 test.txt		# 查看文件大小为 1字节
结论:文件大小还有1字节,判断时为非空文件!因echo输出本身自带换行符
方法二:
[root@hadoop614 ~]# ll test.txt 
-rw-r--r-- 1 root root 42 Jan 26 15:08 test.txt	#查看文件大小 42 字节
[root@hadoop614 ~]# cat /dev/null > test.txt		# 将/dev/null内容输出到3.txt文件内,/dev/null为纯空
[root@hadoop614 ~]# ll test.txt 
-rw-r--r-- 1 root root 0 Jan 26 15:19 test.txt
结论:文件大小为0字节,判断时为空文件!因cat /dev/null返回无任何内容,无换行符或者空格等
场景二:在文件最后追加内容
 [root@hadoop614 ~]# vi test.txt 
 光标切换到最后一行:`G`
 光标切换到最后一个字符:`$`
 方法一:
 进入编辑模式:`i`	#当前光标位置进行编辑
 光标像后移动一位:`→`
 回车进入下一行
 方法二:
 进入编辑模式:`a`	# 当前光标位置后一位进行编辑
 回车进入下一行
 方法三:
 进入编辑模式:`o`	# 当前光标位置下一行新建一行进行编辑
场景三:快速定位关键字 - error
 方法一:
 [root@hadoop614 ~]# vi test.txt 
 在尾行模式输入/error,回车即可向下查找
 或者光标在文件任意位置,可输入?error,回车即可向上查找
 注:确为当前光标位置:
 	尾行模式输入 `:set nu`  	可显示行号
 	尾行模式输入 `:set nonu`  可取消显示行号
 方法二:
# 可一次性输出文件内所有存在关键字的所有行
 [root@hadoop614 ~]# cat test.txt | grep error
 error 1 2 3
 error 4
 error
 方法三:
 将test.txt文件下载到本地,使用PadNotes++等文本编辑工具打开并查找
更多vi编辑器基础知识

【Linux基础】VIM编辑器常用命令详解(基础篇)

  • 权限控制

权限解读
[root@hadoop614 ~]# su - hadoop
[hadoop@hadoop614 ~]$ ll /root
ls: cannot open directory /root: Permission denied	# Permission denied  表示权限不足或者没有权限
# 切换回root用户,看看为什么hadoop用户无权访问/root
[root@hadoop614 ~]# ll -d /root
dr-xr-x---. 6 root root 4096 Jan 26 15:42 /root

# ll命令查询出的结果介绍:
/root		# 文件或目录名
Jan 26 15:42 # 表示文件/目录创建时间或文件更新时间
4096 		# 表示文件大小和目录本身的大小,一般情况下目录统一为4096,因为这个值不包含目录下的文件和子目录内文件的大小
root root	# 第一个root表示文件或目录所属用户--所属者
				# 第二个root表示文件或目录所属组--所属组
dr-xr-x---	# 第1位:d:表示目录	-:表示文件	l:表示软硬链接(软链接类似于快捷方式)
			# 第2-4位:r-x:表示目录或文件所属者的权限
			# 第5-7位:r-x:表示目录或文件所属组成员用户的权限
			# 第8-10位:---:表示其他用户对目录或文件的权限

权限说明:
-:表示无权限 – 0
x:表示执行权限 – 1
w:表示可写权限 – 2
r:表示刻度权限 --4
解读/root目录权限:550
dr-xr-x—. 6 root root 4096 Jan 26 15:42 /root
所属者:root 拥有权限:读+执行 = 4+1 = 5
所属组:root 拥有权限:读+执行 = 4+1 = 5
其他用户拥有权限:无 = 0

权限修改

修改文件或目录权限:chmod

[root@hadoop614 ~]# ll test.txt
-rw-r--r-- 1 root root 295 Jan 26 15:42 test.txt	# test.txt文件权限为644
[root@hadoop614 ~]# chmod 755 test.txt
[root@hadoop614 ~]# ll test.txt 
-rwxr-xr-x 1 root root 295 Jan 26 15:42 test.txt	# test.txt文件权限为755

若要一起修改目录及其子文件和子目录的权限,则使用chmod -R参数

修改文件或目录所属者和所属组:chown

[root@hadoop614 ~]# chown hadoop:hadoop001 test.txt 
[root@hadoop614 ~]# ll test.txt
-rwxr-xr-x 1 hadoop hadoop001 295 Jan 26 15:42 test.txt	# 此时文件所属以被修改为hadoop:hadoop001(所属者:所属组)

若要一起修改目录及其子文件和子目录的所属,则使用chown -R参数

  • 执行shell脚本

shell脚本也是文本文件,一般命名后缀为.sh

想要文件可执行,首先需要赋予文件可执行权限

[root@hadoop614 ~]# ll bigdata.sh 
-rw-r--r-- 1 root root 66 Jan 26 16:34 bigdata.sh	# 文件权限644 没有执行权限

执行脚本的两种方法
方法一:./bigdata.sh
方法二:sh bigdata.sh

[root@hadoop614 ~]# ./bigdata.sh
-bash: ./bigdata.sh: Permission denied	# 没有执行权限是会返回权限不足
[root@hadoop614 ~]# chmod u+x bigdata.sh	# 给所属者添加执行权限
[root@hadoop614 ~]# ll bigdata.sh 
-rwxr--r-- 1 root root 23 Jan 26 16:49 bigdata.sh
[root@hadoop614 ~]# ./bigdata.sh 
若泽大数据
  • 软链接

软连接:相当于Windows系统中的快捷方式,删除软连接而源文件删除,但通过软连接对文件进行编辑后,源文件也会修改

创建软连接:ln -s

[root@hadoop614 ~]# ln -s bigdata.sh bat
[root@hadoop614 ~]# ll
total 4
lrwxrwxrwx 1 root root 10 Jan 26 16:56 bat -> bigdata.sh
-rwxr--r-- 1 root root 36 Jan 26 16:51 bigdata.sh
[root@hadoop614 ~]# cat bat
#!/bin/bash 
echo "若泽大数据"
  • 远程上传/下载:rz/sz
# 安装传输工具:
[root@hadoop614 ~]# yum -y install lrzsz
上传文件:rz

选择需要上传的文件 --> 点击打开在这里插入图片描述
上传完成在这里插入图片描述

下载文件:sz

[root@hadoop614 ~]# sz bigdata.sh
在这里插入图片描述
bat文件下载完成
在这里插入图片描述

  • 系统命令
系统资源使用了监控:top

可监控系统资源使用率情况
通常情况下,生产环境load average: 0.00, 0.01, 0.05经验值不可超过10
<【Linux基础】top命令的用法详细详解>

top - 17:19:46 up 5 days, 10:10,  1 user,  load average: 0.00, 0.01, 0.05
Tasks:  83 total,   1 running,  82 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.2 sy,  0.0 ni, 99.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  3880412 total,  1854140 free,   116716 used,  1909556 buff/cache
KiB Swap:        0 total,        0 free,        0 used.  3456572 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                            
 1377 root       0 -20  126352  11548   8920 S   0.3  0.3  13:03.91 AliYunDun                                          
    1 root      20   0   43508   3744   2448 S   0.0  0.1   0:25.31 systemd                                            
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.01 kthreadd                                           
    3 root      20   0       0      0      0 S   0.0  0.0   0:00.11 ksoftirqd/0                                        
    5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0H                                       
    6 root      20   0       0      0      0 S   0.0  0.0   0:00.16 kworker/u4:0                                       
    7 root      rt   0       0      0      0 S   0.0  0.0   0:00.04 migration/0                                        
    8 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_bh                                             
    9 root      20   0       0      0      0 S   0.0  0.0   0:39.26 rcu_sched                                          
查询内存使用情况:free -m
[root@hadoop614 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3789         114        1810           0        1864        3375
Swap:             0           0           0
文件系统查询:df
[root@hadoop614 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        40G  4.8G   33G  13% /
devtmpfs        1.9G     0  1.9G   0% /dev
tmpfs           1.9G     0  1.9G   0% /dev/shm
tmpfs           1.9G  592K  1.9G   1% /run
tmpfs           1.9G     0  1.9G   0% /sys/fs/cgroup
overlay          40G  4.8G   33G  13% /var/lib/docker/overlay2/20cf413aad752a7988a0667d87698f5dcb6677329bc34184ad0c148950ada89a/merged
shm              64M     0   64M   0% /var/lib/docker/containers/0f9bcc7a9ebb896384bc444019fd4b45aca9a9e63d4b7057c6f1a4a6e1621a35/shm
tmpfs           379M     0  379M   0% /run/user/0
压缩与解压缩:zip/unzip gzip/gunzip
# 压缩需要安装压缩插件
[root@hadoop614 ~]# zip bigdata.sh 
-bash: zip: command not found
[root@hadoop614 ~]# yum -y install zip	
# 压缩为zip文件,源文件不变,重新生成zip压缩文件
[root@hadoop614 ~]# zip -r bigdata.zip bigdata.sh 
  adding: bigdata.sh (stored 0%)
[root@hadoop614 ~]# ll bigdata.zip bigdata.sh 
-rwxr--r-- 1 root root  36 Jan 26 16:51 bigdata.sh
-rw-r--r-- 1 root root 206 Jan 26 17:37 bigdata.zip
# 压缩为gz文件,源文件删除
[root@hadoop614 ~]# gzip bigdata.sh
[root@hadoop614 ~]# ll bigdata.*
-rwxr--r-- 1 root root  68 Jan 26 16:51 bigdata.sh.gz
-rw-r--r-- 1 root root 206 Jan 26 17:37 bigdata.zip
# 解压缩需要安装解压缩插件
[root@hadoop614 ~]# unzip bigdata.zip 
-bash: unzip: command not found
[root@hadoop614 ~]# yum -y install unzip
# 解压zip文件:解压缩后压缩文件和源文件并存
[root@hadoop614 ~]# unzip bigdata.zip 
Archive:  bigdata.zip
 extracting: bigdata.sh              
[root@hadoop614 ~]# ll bigdata.*
-rwxr--r-- 1 root root  36 Jan 26 16:51 bigdata.sh
-rwxr--r-- 1 root root  68 Jan 26 16:51 bigdata.sh.gz
-rw-r--r-- 1 root root 206 Jan 26 17:37 bigdata.zip
# 解压缩gz文件:解压缩后gz文件消耗四,仅保留解压后的文件
[root@hadoop614 ~]# gunzip bigdata.sh.gz 
gzip: bigdata.sh already exists; do you wish to overwrite (y or n)? y
[root@hadoop614 ~]# ll bigdata.*
-rwxr--r-- 1 root root  36 Jan 26 16:51 bigdata.sh
-rw-r--r-- 1 root root 206 Jan 26 17:37 bigdata.zip

小结:
zip/unzip 压缩和解压缩后,源文件和压缩文件并存
gzip/gunzip 压缩后只有压缩后的文件,解压缩后只有解压缩后的文件

打包压缩多个文件到同一个包内:tar

压缩:
[root@hadoop614 ~]# tar -czvf test.tar.gz test*
test1
test2
test3
[root@hadoop614 ~]# ll test*
-rw-r--r-- 1 root root  26 Jan 26 17:48 test1.gz
-rw-r--r-- 1 root root  26 Jan 26 17:48 test2.gz
-rw-r--r-- 1 root root  26 Jan 26 17:48 test3.gz
-rw-r--r-- 1 root root 129 Jan 26 17:49 test.tar.gz
解压缩:
[root@hadoop614 ~]# tar -xzvf test.tar.gz
[root@hadoop614 ~]# ll test*
-rw-r--r-- 1 root root  26 Jan 26 17:48 test1.gz
-rw-r--r-- 1 root root  26 Jan 26 17:48 test2.gz
-rw-r--r-- 1 root root  26 Jan 26 17:48 test3.gz
-rw-r--r-- 1 root root 129 Jan 26 17:49 test.tar.gz
下载URL内容:wget

用法:wget URL

[root@hadoop614 test]# wget https://mp.csdn.net/postlist
--2019-01-26 17:59:00--  https://mp.csdn.net/postlist
Resolving mp.csdn.net (mp.csdn.net)... 47.95.47.253
Connecting to mp.csdn.net (mp.csdn.net)|47.95.47.253|:443... connected.
HTTP request sent, awaiting response... 307 Temporary Redirect
Location: http://passport.csdn.net/account/login?from=https://mp.csdn.net/postlist [following]
--2019-01-26 17:59:00--  http://passport.csdn.net/account/login?from=https://mp.csdn.net/postlist
Resolving passport.csdn.net (passport.csdn.net)... 101.201.169.146
Connecting to passport.csdn.net (passport.csdn.net)|101.201.169.146|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://passport.csdn.net/account/login?from=https://mp.csdn.net/postlist [following]
--2019-01-26 17:59:00--  https://passport.csdn.net/account/login?from=https://mp.csdn.net/postlist
Connecting to passport.csdn.net (passport.csdn.net)|101.201.169.146|:443... connected.
HTTP request sent, awaiting response... 302 
Location: https://passport.csdn.net/login [following]
--2019-01-26 17:59:00--  https://passport.csdn.net/login
Reusing existing connection to passport.csdn.net:443.
HTTP request sent, awaiting response... 200 OK
Length: 1121 (1.1K) [text/html]
Saving to: ‘postlist’

100%[==============================================================================>] 1,121       --.-K/s   in 0s      

2019-01-26 17:59:00 (211 MB/s) - ‘postlist’ saved [1121/1121]

[root@hadoop614 test]# cat postlist
<!DOCTYPE html><html translate=no lang=zh-CN><head><meta http-equiv=X-UA-Compatible content="IE=Edge,chrome=1"><meta name=referrer content=always><meta name=renderer content=webkit><meta name=force-rendering content=webkit><meta charset=utf-8><meta name=description content=CSDN登录注册><meta name=google value=notranslate><link type=image/x-icon href=https://csdnimg.cn/public/favicon.ico rel="SHORTCUT ICON"><title>CSDN-专业IT技术社区</title><!--[if lt IE 9]>
       <script>window.location.href="https://g.csdnimg.cn/browser_upgrade/1.0.2/index.html";</script>
    <![endif]--><link href=https://csdnimg.cn/release/passport_fe/assets/css/login.abab6048cf6e272a47f28638f3aa52ec.css rel=stylesheet></head><body><div id=app></div><script type=text/javascript src=https://csdnimg.cn/release/passport_fe/assets/js/manifest.603a92bc4219ee818e42.js></script><script type=text/javascript src=https://csdnimg.cn/release/passport_fe/assets/js/vendor.020317dbffa47ab1eb8b.js></script><script type=text/javascript src=https://csdnimg.cn/release/passport_fe/assets/js/login.5853170f9532597487d3.js></script></body></html> 
  • 调度任务/计划任务:crontab

编辑创建任务:crontab -e # vi编辑模式
查询任务内容:crontab -l

编辑脚本内容为:

[root@hadoop614 ~]# cat bat
#!/bin/bash 
for((i=1;i<=6;i++));
do
        date
        sleep 10s
done
[root@hadoop614 ~]# crontab -l
* * * * * /root/bat >> /root/bat.log	# 书写规则: 分 时 日 月 周 执行的命令  * 表示’每‘
[root@hadoop614 ~]# tail -F bat.log 
Sat Jan 26 18:14:01 CST 2019
Sat Jan 26 18:14:11 CST 2019
Sat Jan 26 18:14:21 CST 2019
  • 后台执行命令/脚本:nohup xxxx &

前台执行结果:

[root@hadoop614 ~]# ./bat >> bat.log 

后台执行结果

[root@hadoop614 ~]# nohup ./bat >> bat.log 2>&1 &
[2] 10691
[root@hadoop614 ~]# ps -ef | grep 10691 | grep -v grep
root     10691 10335  0 18:18 pts/0    00:00:00 /bin/bash ./bat
root     10703 10691  0 18:18 pts/0    00:00:00 sleep 10s
[root@hadoop614 ~]# kill -9 10691
[root@hadoop614 ~]# 
[2]+  Killed                  nohup ./bat >> bat.log 2>&1

详细的输出重定向介绍:
【若泽大数据第一天】基础-Linux基础命令一

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值