【笔记】字符集&系统提示符&vim进阶& echo & cat & less & more & grep &‘ ‘和“ “的区别& tr

字符集

字符集作用: 文字在Linux系统中一种表达方式
我们在工作中我们是使用远程连接工具连接操作系统
注意: xshell和操作系统的字符集不统一 则产生乱码 默认相同
两种常用的字符集:
第一种字符集: UTF-8 万国码 xshell默认的 操作系统默认的
第二种字符集: GBK 国标

查看字符集

使用变量 LANG

[root@ahui ~]# echo $LANG
en_US.UTF-8

临时修改系统的语言 (字符集)

重启 退出xshell失效

[root@ahui ~]# LANG="zh_CN.UTF-8"
[root@ahui ~]# echo $LANG
zh_CN.UTF-8

查看语言是否修改成功

	[root@ahui ~]# useradd --help
	用法:useradd [选项] 登录
		  useradd -D
		  useradd -D [选项]

    选项:
	  -b, --base-dir BASE_DIR	新账户的主目录的基目录
	  -c, --comment COMMENT         新账户的 GECOS 字段
	  -d, --home-dir HOME_DIR       新账户的主目录
	  -D, --defaults		显示或更改默认的 useradd 配置
	  -e, --expiredate EXPIRE_DATE  新账户的过期日期


[root@ahui ~]# useradd www
useradd:用户“www”已存在

永久修改系统语言(字符集)

写入配置文件

[root@ahui ~]# cat /etc/locale.conf 
LANG="en_US.UTF-8"

[root@ahui ~]# vim /etc/locale.conf
[root@ahui ~]# cat /etc/locale.conf
LANG="zh_CN.UTF-8"

想让变量的配置文件直接生效 可以使用 . 或者source让变量直接生效 不需要重启

[root@ahui ~]# vim /etc/locale.conf 
en_US.UTF-8
[root@ahui ~]# echo $LANG
zh_CN.UTF-8
[root@ahui ~]# source /etc/locale.conf 
[root@ahui ~]# echo $LANG
en_US.UTF-8

修改字符集上面两种方式
01.在命令行使用 LANG=“zh_CN.UTF-8”
02.修改vim /etc/locale.conf文件

使用命令永久+临时的方式配置字符集

[root@ahui ~]# localectl set-locale LANG="zh_CN.UTF-8"
[root@ahui ~]# cat /etc/locale.conf 
LANG=zh_CN.UTF-8
[root@ahui ~]# echo $LANG
en_US.UTF-8
[root@ahui ~]# source /etc/locale.conf 

centos6.x 修改字符集
1.临时修改和centos7.x相同 LANG=“zh_CN.UTF-8”
2.永久修改配置文件 /etc/sysconfig/i18n

系统提示符

命令提示符: 我们在命令提示符的后面可以输入命令
命令提示符通过PS1变量设置的
[root@ahui ~]#

PS1变量的参数以及含义:

  • /a ASCII 响铃字符(也能够键入 /007)
  • /d “Wed Sep 06” 格式的日期
  • /e ASCII 转义字符(也能够键入 /033)
  • /h 主机名的第一部分(如 “mybox”)
  • /H 主机的全称(如 “mybox.mydomain.com”)
  • /j 在此shell中通过按 ^Z 挂起的进程数
  • /l 此 shell 的终端设备名(如 “ttyp4”)
  • /n 换行符
  • /r 回车符
  • /s shell 的名称(如 “bash”)
  • /t 24 小时制时间(如 “23:01:01”)
  • /T 12 小时制时间(如 “11:01:01”)
  • /@ 带有 am/pm 的 12 小时制时间
  • /u username
  • /v bash 的版本号(如 2.04)
  • /V Bash 版本号(包含补丁级别)
  • /w 显示全路径
  • /W 当前工作文件夹的“基名 (basename)”(如 “drobbins”)
    查看PS1变量
[root@ahui ~]# echo $PS1
[\u@\h \W]\$

案例1: 修改PS1变量

[root@ahui ~]# PS1='[\u@\h \W \t]\$'
[root@ahui ~ 09:39:14]#
[root@ahui ~ 09:39:14]#
[root@ahui ~ 09:39:15]#

案例2:将W修改为w全路径

[root@ahui ~ 09:39:15]#PS1='[\u@\h \w \t]\$'
[root@ahui ~ 09:41:15]#
[root@ahui ~ 09:41:15]#
[root@ahui ~ 09:41:16]#cd /etc/sysconfig/network-scripts/
[root@ahui /etc/sysconfig/network-scripts 09:41:22]#

案例3: 修改PS1的颜色 临时 退出 重启 失效

`[root@ahui ~]# PS1="[\[\e[34;1m\]\u@\[\e[0m\]\[\e[32;1m\]\H\[\e[0m\]\[\e[31;1m\] \w\[\e[0m\]]\\$` " 

永久生效

写入/etc/profile

[root@ahui ~]# tail -1 /etc/profile
PS1="[\[\e[34;1m\]\u@\[\e[0m\]\[\e[32;1m\]\H\[\e[0m\]\[\e[31;1m\] \w\[\e[0m\]]\\$" 

vim进阶

复制剪切

底行模式中:

:1copy3 # 底行模式复制 第一行到第三行 相当于 yy 复制 p粘贴
:1co3
:1move3 # 移动第一行到第三行 类似 dd剪切 p粘贴剪切的内容

文件内容替换:

语法格式:
底行模式中:

  • s/要替换的内容/替换成什么内容/ # 替换光标所在行的第一个单词
  • s/要替换的内容/替换成什么内容/g # 替换光标所在行的所有单词
  • %s/要替换的内容/替换成什么内容/g # 替换文本内所有的单词
    %s///g
  • 如果遇到替换的内容有/ #可以使用三个相同的任意符号
    %s#/root#/ahui#g
    %s@@@g-
  • 可以使用\转移字符 必须有%
    %s//root/ahui/g
  • 指定行替换内容 替换第2行的内容
    2s///g
  • 区间范围替换 替换2-4行的内容
    2,4s///g

echo

输出内容到屏幕或者输入到文件中
使用> 输出重定向 先清空文件内容在写入新的内容
使用>> 追加输出重定向 不清空文件内容 新的内容追加到文件的底部
-e 激活特殊符号的含义
\t tab键
\n 回车

[root@ahui ~]# echo ahui
ahui
[root@ahui ~]# echo ahui > 1.txt
[root@ahui ~]# cat 1.txt 
ahui
[root@ahui ~]# echo hehe > 1.txt
[root@ahui ~]# cat 1.txt 
hehe
[root@ahui ~]# echo ahui >> 1.txt 
[root@ahui ~]# cat 1.txt 
hehe
ahui

#\n 表示回车
[root@ahui ~]# echo "test1\ntest2"   #没有加-e 激活特殊字符的含义
test1\ntest2

[root@ahui ~]# echo -e "test1\ntest2"
test1
test2
[root@ahui ~]# echo -e "test1\ntest2" > 1.txt
[root@ahui ~]# cat 1.txt 
test1
test2

#\t tab建 制表符
[root@ahui ~]# echo -e "test1\t\t\t\ttest2"
test1				test2
[root@ahui ~]# 
[root@ahui ~]# echo -e "test1\t\t\t\ttest2" >> 1.txt
[root@ahui ~]# cat 1.txt 
test1
test2
test1				test2

序列{}

序列输出规范的数字

[root@ahui ~]# echo {1..3}
	1 2 3
	[root@ahui ~]# 
	[root@ahui ~]# echo {1..10}
	1 2 3 4 5 6 7 8 9 10
	[root@ahui ~]# echo {01..10}
	01 02 03 04 05 06 07 08 09 10
	[root@ahui ~]# echo {1..3..1}
	1 2 3
	[root@ahui ~]# 
	[root@ahui ~]# echo {1..10}
	1 2 3 4 5 6 7 8 9 10
	[root@ahui ~]# echo {1..10..2}
	1 3 5 7 9

使用输出的序列可以创建多个目录
注意: 在Linux中的{}可被大部分命令直接调用

[root@ahui ~]# mkdir {1..3}
[root@ahui ~]# 
[root@ahui ~]# ll
总用量 8
drwxr-xr-x 2 root root   6 5月  10 10:24 1
-rw-r--r-- 1 root root  27 5月  10 10:21 1.txt
drwxr-xr-x 2 root root   6 5月  10 10:24 2
drwxr-xr-x 2 root root   6 5月  10 10:24 3
-rw-r--r-- 1 root root 105 5月  10 09:56 ahui.txt
[root@ahui ~]# ll 1
总用量 0
[root@ahui ~]# ll 2
总用量 0
[root@ahui ~]# ll 3
总用量 0

mkdir {1…3} 等同于 mkdir 1 2 3

输出字符串序列

[root@ahui ~]# echo {a..c}
a b c
[root@ahui ~]# echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z
[root@ahui ~]# echo {a..z..2}
a c e g i k m o q s u w y

[root@ahui ~]# echo {A..Z}
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

输出不连续的数字或者字符使用逗号分隔

[root@ahui ~]# echo {3,5,15}
3 5 15
[root@ahui ~]# 
[root@ahui ~]# mkdir {3,5,15}
[root@ahui ~]# echo {a,y,p}
a y p

使用字符串创建目录

[root@ahui ~]# mkdir {a,y,p}
[root@ahui ~]# ll
drwxr-xr-x 2 root root   6 5月  10 10:29 a
drwxr-xr-x 2 root root   6 5月  10 10:29 p
drwxr-xr-x 2 root root   6 5月  10 10:29 y

使用数字+字符串的方式

组合方式: 将相同的部分拿出来 将连续的数字作为序列输出
拼接方式: 序列可以在字符串的前面 也可以在字符串的后面
拼接: test和序列中的每个数字进行拼接

[root@ahui ~]# echo test{1..3}
test1 test2 test3

拼接: 序列在前字符在后面 序列中的每个数字和test分别拼接

[root@ahui ~]# echo {1..3}test
1test 2test 3test

拼接: 可以加符号

[root@ahui ~]# echo {1..3}.test
1.test 2.test 3.test

拼接: 如果不连续的拼接

[root@ahui ~]# echo {1,4,7}
1 4 7
[root@ahui ~]# echo ahui-{1,4,7}
ahui-1 ahui-4 ahui-7

[root@ahui ~]# echo {bbs,blog,www}
bbs blog www

案例1: 在当前位置创建test1 test2 test3目录

[root@ahui ~]# echo test{1..3}
test1 test2 test3
[root@ahui ~]# 
[root@ahui ~]# mkdir test{1..3}
[root@ahui ~]# ll
总用量 0
drwxr-xr-x 2 root root 6 5月  10 10:37 test1
drwxr-xr-x 2 root root 6 5月  10 10:37 test2
drwxr-xr-x 2 root root 6 5月  10 10:37 test3

案例2: 在当前位置创建 1.txt 2.txt 3.txt 普通文件

[root@ahui ~]# echo {1..3}.txt
1.txt 2.txt 3.txt
[root@ahui ~]# 
[root@ahui ~]# touch {1..3}.txt
[root@ahui ~]# ll
总用量 0
-rw-r--r-- 1 root root 0 5月  10 10:38 1.txt
-rw-r--r-- 1 root root 0 5月  10 10:38 2.txt
-rw-r--r-- 1 root root 0 5月  10 10:38 3.txt

案例3:通过序列查看文件

[root@ahui ~]# ll {1..3}.txt
-rw-r--r-- 1 root root 0 5月  10 10:38 1.txt
-rw-r--r-- 1 root root 0 5月  10 10:38 2.txt
-rw-r--r-- 1 root root 0 5月  10 10:38 3.txt

案例4:在test目录下创建ahui01 ahui02 ahui03目录

[root@ahui ~]# mkdir -p test/ahui{01..03}
[root@ahui ~]# ll
总用量 0
-rw-r--r-- 1 root root  0 5月  10 10:38 1.txt
-rw-r--r-- 1 root root  0 5月  10 10:38 2.txt
-rw-r--r-- 1 root root  0 5月  10 10:38 3.txt
drwxr-xr-x 5 root root 54 5月  10 10:41 test
drwxr-xr-x 2 root root  6 5月  10 10:37 test1
drwxr-xr-x 2 root root  6 5月  10 10:37 test2
drwxr-xr-x 2 root root  6 5月  10 10:37 test3
[root@ahui ~]# ll test/
总用量 0
drwxr-xr-x 2 root root 6 5月  10 10:41 ahui01
drwxr-xr-x 2 root root 6 5月  10 10:41 ahui02
drwxr-xr-x 2 root root 6 5月  10 10:41 ahui03

如果用到空格需要加双引号 双引号中的内容是一个整体分别和序列进行拼接

[root@ahui ~]# echo "Linux test"{1..2}
Linux test1 Linux test2
[root@ahui ~]# 
[root@ahui ~]# 
[root@ahui ~]# echo Linux test{1..2}
Linux test1 test2

案例5: 在boy下创建多个文件

[root@ahui ~]# mkdir boy
[root@ahui ~]# echo {1..3}.txt
1.txt 2.txt 3.txt
[root@ahui ~]# echo boy/{1..3}.txt
boy/1.txt boy/2.txt boy/3.txt
[root@ahui ~]# 
[root@ahui ~]# touch boy/{1..3}.txt
[root@ahui ~]# ll boy/
总用量 0
-rw-r--r-- 1 root root 0 5月  10 11:20 1.txt
-rw-r--r-- 1 root root 0 5月  10 11:20 2.txt
-rw-r--r-- 1 root root 0 5月  10 11:20 3.txt

在根目录下创建

[root@ahui ~]# mkdir /boy
[root@ahui ~]# touch /boy/{1..3}.txt
[root@ahui ~]# ll /boy/
总用量 0
-rw-r--r-- 1 root root 0 5月  10 11:21 1.txt
-rw-r--r-- 1 root root 0 5月  10 11:21 2.txt
-rw-r--r-- 1 root root 0 5月  10 11:21 3.txt

seq

输出序列
-w 规范输出 seq -w 05
-s 指定分割符 seq -s + 100

[root@ahui ~]# seq 3
1
2
3
[root@ahui ~]# seq -w 05
01
02
03
04
05

[root@ahui ~]# seq -s + 100
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+31+32+33+34+35+36+37+38+39+40+41+42+43+44+45+46+47+48+49+50+51+52+53+54+55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70+71+72+73+74+75+76+77+78+79+80+81+82+83+84+85+86+87+88+89+90+91+92+93+94+95+96+97+98+99+100

cat

查看文件中的内容
作用: 支持将内容输出到屏幕(菜单)
支持合并多个文件的内容
参数选项:
-n 显示行号

案例1: 输出内容到屏幕

[root@ahui ~]# cat<<EOF
> aaa
> bbb
> ccc
> EOF
aaa
bbb
ccc

案例2: 将内容输入到文件

[root@ahui ~]# cat>1.txt<<EOF	#注意:> 清空  >>追加
> aaa
> bbb
> ccc
> EOF
[root@ahui ~]# cat 1.txt 
aaa
bbb
ccc

案例3: cat合并文件

第一步: cat查看多个文件内容

[root@ahui ~]# cat 1.txt 
111
222
[root@ahui ~]# cat 2.txt 
aaaa
bbbb
[root@ahui ~]# cat 1.txt 2.txt 
111
222
aaaa
bbbb

第二步: 所有输出到屏幕上的内容都可以使用>,>>输入到文件中 不影响源文件

[root@ahui ~]# cat 1.txt 2.txt > all.txt
[root@ahui ~]# cat all.txt 
111
222
aaaa
bbbb
[root@ahui ~]# cat 1.txt 
111
222
[root@ahui ~]# cat 2.txt 
aaaa
bbbb

案例3: cat -n 显示文件内容的行号

[root@ahui ~]# cat -n 2.txt 
     1	aaaa
     2	bbbb
[root@ahui ~]# cat -n all.txt 
     1	111
     2	222
     3	aaaa
     4	bbbb

less

一页一页的查看文件内容 重点 主要查看大文件使用
参数选项:
less查看到文件的底部不会退出
-N 显示行号
空格 往下翻页
f 往下翻页
b 往上翻页
ng 快速到n行
g 快速到首行
G 快速到文件的末尾
/ 搜索内容
n 往下查找
N 往上查找
q 退出less模式
v 进入到编辑模式 按:q 退出编辑模式 然后在按q退出less模式

more

命令 一页一页的查看 了解 翻到文件底部 直接退出
f 往下翻页
b 往上翻页
/ 搜索
不支持快捷键

grep

过滤文件内容 三剑客老三 awk sed grep find
过滤包含xx内容的行
-v 对过滤到的内容进行取反
-n 显示过滤到内容的行号 了解
-r 递归过滤目录及目录下所有文件中的内容
语法结构:
grep ‘过滤的内容’ file
cat file|grep ‘过滤的内容’ # 将其他命令的输出结果 作为后面命令的输入

’ '和" "的区别

特点:
1.对于普通字符串和数字没有任何区别
2.对于变量单引号所见即所得
3.对于变量双引号可以解析变量的内容
4.不加引号和双引号相同

[root@ahui ~]# echo '$PS1'
$PS1
[root@ahui ~]# 
[root@ahui ~]# echo "$PS1"
[\[\e[34;1m\]\u@\[\e[0m\]\[\e[32;1m\]\H\[\e[0m\]\[\e[31;1m\] \w\[\e[0m\]]\$ 
[root@ahui ~]# echo $PS1
[\[\e[34;1m\]\u@\[\e[0m\]\[\e[32;1m\]\H\[\e[0m\]\[\e[31;1m\] \w\[\e[0m\]]\$
[root@ahui ~]# cat 1.txt
aaaaaaaa
bbbbbbbb
test
ahui
lidao
$name
[root@ahui ~]# name=ahui
[root@ahui ~]# grep 'lidao' 1.txt 
lidao
[root@ahui ~]# echo $name
ahui
[root@ahui ~]# grep $name 1.txt
ahui
[root@ahui ~]# grep '$name' 1.txt
[root@ahui ~]# vim 1.txt 
[root@ahui ~]# grep '$name' 1.txt
$name
[root@ahui ~]# grep "$name" 1.txt
ahui

案例1:

[root@ahui ~]# ls
1.txt  2.txt  3.txt  all.txt  boy  services  test  test1  test2  test3
[root@ahui ~]# ls|grep all
all.txt

案例2: 直接过滤搜索

[root@ahui ~]# cat /etc/passwd|grep root
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

如果使用cat命令则建议直接grep 后跟文件过滤

[root@ahui ~]# grep 'root' /etc/passwd

[root@ahui ~]# ip add|grep inet|grep eth0
inet 10.0.0.200/24 brd 10.0.0.255 scope global noprefixroute eth0

案例: -n 显示到过滤内容的行号

[root@ahui ~]# head -5 /etc/passwd > 1.txt
[root@ahui ~]# grep -n 'adm' 1.txt 
4:adm:x:3:4:adm:/var/adm:/sbin/nologin

案例: -v 对过滤到的内容进行取反(不包含)

[root@ahui ~]# cat 2.txt
zhangsan
lisi
ahui
test
[root@ahui ~]# grep 'zhangsan' 2.txt
zhangsan
[root@ahui ~]# grep -v 'zhangsan' 2.txt
lisi
ahui
test

案例: 将/etc/selinux/config中的 空行和# 过滤出来

在系统中# 为注释 说明的意思

[root@ahui ~]# grep '#' /etc/selinux/config

对获取到#的行进行取反 剩下的就是真实有意义的配置文件

[root@ahui ~]# grep -v '#' /etc/selinux/config
SELINUX=disabled
SELINUXTYPE=targeted 

过滤空行

[root@ahui ~]# grep '^$' /etc/selinux/config

案例: 排除/etc/selinux/config 中的带#的行和空行

拼接方式:

[root@ahui ~]# grep -v '#' /etc/selinux/config|grep -v '^$'
SELINUX=disabled
SELINUXTYPE=targeted 

使用正则:

[root@ahui ~]# egrep -v '^$|#' /etc/selinux/config 
SELINUX=disabled
SELINUXTYPE=targeted

案例:搜索目录及目录下所有文件包含ahui的

[root@ahui ~]# grep -r ahui *	#*表示所有
1.txt:ahui
test/2.txt:ahui

tr

一对一的替换 主要用于替换特殊字符 不影响源文件

[root@ahui ~]# cat 2.txt |tr ":" "-"
root-x-0-0-root-/root-/bin/bash

[root@ahui ~]# cat 2.txt |tr "/" "?"
root:x:0:0:root:?root:?bin?bash

[root@ahui ~]# cat 2.txt |tr ":" " "
root x 0 0 root /root /bin/bash

将替换后的内容输入到文件中

[root@ahui ~]# cat 2.txt |tr ":" " " > test.txt
[root@ahui ~]# cat test.txt 
root x 0 0 root /root /bin/bash

小结:

  1. 字符集 echo $LANG 字符集配置文件/etc/locale.conf

  2. 系统提示符 PS1变量

  3. vim进阶
    底行模式中复制、剪切、替换

  4. echo
    -e 激活特殊符号的含义

  5. 序列{} 序列输出数字、字符串,拼接 批量创建目录和文件

  6. cat
    -n 显示行号
    合并文件
    输出内容到屏幕和文件中

  7. less 一页一页的查看文件内容 主要查看大文件使用
    快捷键 :
    上下翻页
    查找内容
    移动到指定行
    编辑模式

  8. more 一页一页的查看文件内容 翻到文件底部 直接退出

  9. grep 过滤字符串内容
    过滤文件中的内容 而不是查找文件的
    grep ‘过滤内容’ file
    cat file|grep ‘过滤内容’
    cat file|grep ‘’|grep ‘’|grep -v ‘’
    grep -v 取反
    grep -r 递归过滤

  10. ’ '和" "的区别
    特点:

    1. 对于普通字符串和数字没有任何区别
    2. 对于变量单引号所见即所得
    3. 对于变量双引号可以解析变量的内容
    4. 不加引号和双引号相同
  11. tr 一对一的替换 主要用于替换特殊字符 不影响源文件

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值