linux第三周

總結

shell腳本如果前面的命令錯誤不影響后面的命令執行
shell腳本如果前面的語法錯誤(syntax error),后續的命令不會執行。不影響語法前的命令
bash -n file ?檢查語法錯誤,不檢查命令錯誤
bash -x file ?跟蹤腳本運行情況。
[root@CENTOS7 ~]#name=”mage”變量的賦值 ?, 當為name賦新的值時,新值存放在新的地址中
[root@CENTOS7 ~]#echo $name
mage
name=”wang renbing”可以運行
name=wang renbing ? renbing會被當做命令,執行報錯
[root@CENTOS7 ~]#name=`hostname` 賦值命令時需要加“,不然會被當成字符串
[root@CENTOS7 ~]#echo $name
CENTOS7.localdomain
[root@CENTOS7 ~]#name=`cat /etc/fstab` ?能賦值但是會使顯示格式改變。解決方法:ehco “$name” ,可以保留顯示格式
[root@CENTOS7 ~]#echo $name
# # /etc/fstab # Created by anaconda on Tue Mar 27 17:53:47 2018 # # Accessible filesystems, by reference, are maintained under ‘/dev/disk’ # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=0a1bc23e-06e5-4210-9b32-0edbff09ca1a / xfs defaults 0 0 UUID=4d8d9214-eeed-4758-8c34-f05492b9ea73 /boot xfs defaults 0 0 UUID=fb669d84-551f-4a70-a11e-f61deec0fd86 /data xfs defaults 0 0 UUID=906330a5-2af1-4bf4-8b2d-9337eaf92250 swap swap defaults 0 0
[root@CENTOS7 ~]#name=mage ?變量可以用變量賦值
[root@CENTOS7 ~]#name1=wang
[root@CENTOS7 ~]#name2=$name
[root@CENTOS7 ~]#echo $name2
mage
[root@CENTOS7 ~]#name=zhang ? 從新給name賦值,不會影響name2
[root@CENTOS7 ~]#echo $name2
mage
bash不支持浮點數(小數),賦值是不用考慮賦值類型,可以直接賦值。都當做字符串來賦值
[root@CENTOS7 ~]#type if ?在shell中關鍵字不可以當做變量名
if is a shell keyword ? ,命令最好不要用容易混淆,不可以數字開頭,最好變量名能見名知意
student_name
StudentName 大駝峰
studentName 小駝峰
bash中變量種類
普通變量 只能在自己的session(會話,窗口)中使用,不能再子進程中使用,子進程再次給name賦值不影響父進程
[root@CENTOS7 ~]#bash ? ?再開一個bash
[root@CENTOS7 ~]#echo $$ ?子shell
10064
[root@CENTOS7 ~]#echo $PPID 父shell
5021
[root@CENTOS7 ~]#pstree -p ? ?查看父子進程
bash(5021)───bash(10064)─
bash后臺執行,sleep 100前臺執行
全局變量 父進程可以傳給子進程,子進程不能傳給父進程
expotr name=”mage”
declare -x naem=”mage”
[root@CENTOS7 ~]#name=”mage”
[root@CENTOS7 ~]#export name ?定義全局變量
[root@CENTOS7 ~]#echo $name
mage
[root@CENTOS7 ~]#echo $$
5021
[root@CENTOS7 ~]#bash
[root@CENTOS7 ~]#echo $$
10385
[root@CENTOS7 ~]#
[root@CENTOS7 ~]#echo $name
mage
[root@CENTOS7 ~]#name=”wang”
[root@CENTOS7 ~]#echo $name
wang
[root@CENTOS7 ~]#exit
exit
[root@CENTOS7 ~]#echo $name
mage
export和declare -x和env 查看全局變量
exit可以刪除變量
unset name刪除變量
set 可以查看所有變量(局部全部都顯示)還可以顯示函數
hostname 主機名
ifconfig ens33 網址
cat /etc/centos-release centos版本
uname -r 內核版本
lscpu ? ?cpu型號
free -h或者 cat /proc/meminfo ?查看內存大小
lsblk ? 查看硬盤大小
ehco “start backup”
sleep 2
cp -av /etc/ /root/etc`date +%F`
echo “backup is finished”
echo $SHLVL shell的嵌套深度
ehco $_ 執行上一個命令最后一個參數
readonly name 只讀變量 是能聲明但是不能修改和刪除
declare – r
查看只讀變量 readonly -p
[root@CENTOS7 data]#echo $LANG
en_US.UTF-8
[root@CENTOS7 data]#readonly name=”mage” ?定義變量name只讀
[root@CENTOS7 data]#echo $name
mage
[root@CENTOS7 data]#name=”wang”
-bash: name: readonly variable
unset name 無法刪除 ?exit可以刪除
declare -r 或者readonly -p查看常量
()一次性的
[root@CENTOS7 ~]#(cd /data;rm -rf /data/*)
[root@CENTOS7 ~]#ls /data
[root@CENTOS7 ~]#(umask 666;touch /data/f1)
[root@CENTOS7 ~]#umask
0022
[root@CENTOS7 ~]#ll /data/f1
———-. 1 root root 0 Apr ?9 22:44 /data/f1
[root@CENTOS7 ~]#(name=”wang”;echo $name)
wang
[root@CENTOS7 ~]#echo $name
{}影響現在的shell
[root@CENTOS7 ~]#{ name=mage; echo $name; }
mage
[root@CENTOS7 ~]#echo $name
mage
()在當前進程下開辟出一個小空間,()外面會影響()里面,()里面不會影響()外面
[root@CENTOS7 ~]#x=1;echo “pid=$$”;(echo “subpid=$$”;echo “sunx=$x”;x=2;echo “subx2=$x”);echo x=$x
pid=2423
subpid=2423
sunx=1
subx2=2
x=1
#********************************************************************
echo “1st arg is $1”
echo “2st arg is $2”
echo “3st arg is $s”
echo “all arg is $*”
echo “all arg is $@”
echo “the args number is $#”
echo “the script name is `basename $0`”
[root@CENTOS7 ~]#bash /data/arg.sh 111 222 333
1st arg is 111
2st arg is 222
3st arg is 333
all arg is 111 222 333
all arg is 111 222 333
the args number is 3
the script name is arg.sh
[root@CENTOS7 ~]#bash /data/arg.sh 111 222 333 444
1st arg is 111
2st arg is 222
3st arg is 333
all arg is 111 222 333 444
all arg is 111 222 333 444
the args number is 4
the script name is arg.sh
echo “1st arg is $1”
echo “2st arg is $2”
echo “3st arg is $3”
echo “9st arg is $9”
echo “10st arg is ${10}” ?當數字達到2位數以上是需要{數字}
echo “all arg is $*”
echo “all arg is $@”
echo “the args number is $#”
echo “the script name is `basename $0`”
~
~
“/data/arg.sh” 20L, 562C written
[root@CENTOS7 ~]#bash /data/arg.sh a b c d e f i g k l m n
1st arg is a
2st arg is b
3st arg is c
9st arg is k
10st arg is l
all arg is a b c d e f i g k l m n
echo magedu | passwd –stdin wang ?修改王賬號密碼
vim scp.sh
echo “start coyp ….”
scp $* wang@172.20.102.77:/home/wang/bin
echo “copy is finished”
~
~
scp.sh scp.sh ?將scp.sh遠程復制到該IP的/home/wang/bin下
echo “all the args is $*”
./f2.sh “$*” ? ? 需要+””,不然沒有區別
echo “The 1st arg is $1”
[root@CENTOS7 data]#./f1.sh 11 22 33
all the args is 11 22 33
The 1st arg is 11 22 33
$* 和$@的不同之處,$*中的字符串被當做一個整體,$@識別每一個獨立字符串
echo “all the args is $*”
./f2.sh “$@” ? ? ?需要+””,不然沒有區別
echo “The 1st arg is $1”
[root@CENTOS7 data]#./f1.sh 11 22 33
all the args is 11 22 33
The 1st arg is 11
#********************************************************************
echo “all the args is $*”
set — ? ? ? ?清空所有位置參數
./f2.sh “$@”
[root@CENTOS7 data]#./f1.sh 1122 33 33 44
all the args is 1122 33 33 44
The 1st arg is
$0,當創建軟連接的時候會顯示軟連接的路徑,根據這種情況可以創建多個軟連接來實現不同的執行結果
[root@CENTOS7 data]#ln -s arg.sh link.sh
[root@CENTOS7 data]#ll
lrwxrwxrwx. 1 root root ? 6 Apr 10 09:27 link.sh -> arg.sh
[root@CENTOS7 data]#bash ./link.sh
1st arg is
2st arg is
3st arg is
9st arg is
10st arg is
all arg is
all arg is
the args number is 0
the script name is link.sh
pidof 是killall5的軟連接,但是兩個命令的執行結果并不一樣
shift ?用處 ,將位置參數依次向前執行
echo “The 1st arg is $1”
shift
echo “The 1st arg is $1”
shift
echo “The 1st arg is $1”
“f2.sh” 16L, 428C written
[root@CENTOS7 data]#./f2.sh 11 22 33
The 1st arg is 11
The 1st arg is 22
The 1st arg is 33
echo “The 1st arg is $1”
shift
echo “The 1st arg is $1”
shift 2 ? ? ? ? ? ? ? ? 一次移動多個
echo “The 1st arg is $1”
[root@CENTOS7 data]#./f2.sh 11 22 33 44
The 1st arg is 11
The 1st arg is 22
The 1st arg is 44
[root@CENTOS7 data]#grep -q root /etc/passwd ?靜默模式
[root@CENTOS7 data]#echo $? ? , $?如果是0找到不是0沒找到。$?的值時(0-255)之間。在腳本中之現實$?上一條命令的結果
0
ping -c1 172.20.0.1 ping一次
[root@CENTOS7 data]#ping -c1 127.20.0.1
PING 127.20.0.1 (127.20.0.1) 56(84) bytes of data.
64 bytes from 127.20.0.1: icmp_seq=1 ttl=64 time=0.034 ms
ls
exit 10 ? ?可以自己定義$?返回的值
[root@CENTOS7 data]#bash ./f3.sh
arg.sh f1 ?f1.sh ?f2.sh ?f3.sh ?link.sh
[root@CENTOS7 data]#echo $?
10
[root@CENTOS7 data]#x=10
[root@CENTOS7 data]#y=20
[root@CENTOS7 data]#let z=$x+$y ? 可以不加$
[root@CENTOS7 data]#echo $z
30
[root@CENTOS7 data]#let z=x*y ? 和z=$[x+y]或z=$((x+y))或declare -i 或 (expr x + y)。let和(expr)用*時需要轉義
[root@CENTOS7 data]#echo $z
200
[root@CENTOS7 data]#let z=x/y
[root@CENTOS7 data]#echo $z
0
[root@CENTOS7 data]#let z=y/x
[root@CENTOS7 data]#echo $z
2
[root@CENTOS7 data]#let x++ ? ? x++和++x一樣
[root@CENTOS7 data]#echo $x
11
[root@CENTOS7 data]#let x+=3
[root@CENTOS7 data]#echo $x
14
[root@CENTOS7 data]#let x–
[root@CENTOS7 data]#echo $x
13
[root@CENTOS7 data]#n=$[RANDOM%7+31];echo -e “\e[1;${n}mcolor\e[0m”
color ? ? ? ? ? ? ?隨機顏色的color
[root@CENTOS7 data]#expr 3 + 4
7
[root@CENTOS7 data]#expr 3 \* 4 ? 乘的時候需要轉義
12
[root@CENTOS7 data]#expr 6 / 3
2
0&0=0 ? &并且 ?and
0&1=0
1&0=0
1&1=1
0|0=0 ?| 或者 or
0|1=1
1|0=1
1|1=1
斷路與|| ?結果同上
短路或&& ?結果同shang
cmd1 && cmd2
如果cmd1為假cmd2不執行
若果cmd1為真cmd2執行
cmd1 || cmd2
如果cmd1為假cmd2執行
如果cmd1為真cmd2不執行
0^1=1 ? ? 異或
0^0=0
1^0=1
1^1=0
a=2,b=3,let c=a^b echo $c ?1 ? ?對位二進制異或(10)(11)
Last login: Tue Apr 10 09:07:13 CST 2018 on pts/0
[root@CENTOS7 ~]#a=6
[root@CENTOS7 ~]#b=4
[root@CENTOS7 ~]#let a=a^b;let b=a^b; let a=a^b; echo $a $b
4 6
1、編寫腳本/root/bin/sumid.sh,計算/etc/passwd文件中的第10個用戶和第
20用戶的ID之和
[root@CENTOS7 ~]#a=`cat /etc/passwd |head |tail -n1|cut -d “:” -f3`
[root@CENTOS7 ~]#echo $a
11
[root@CENTOS7 ~]#b=`cat /etc/passwd |head -n20|tail -n1|cut -d “:” -f3`
[root@CENTOS7 ~]#let c=a+b
[root@CENTOS7 ~]#echo $c
1008
test
[root@CENTOS7 ~]#test $str1 = $str2 ?,與[ $str1 = $str2 ]相同。 判斷兩個字符串是否一樣
[root@CENTOS7 ~]#echo $?
0
[root@CENTOS7 ~]#[ -z $nima ] ?-z 判斷字符串是否為空。-z (zero)
[root@CENTOS7 ~]#echo $?
0
[root@CENTOS7 ~]#[ $nima ] ? ?-n可以省略不寫,判斷字符串是否非空
[root@CENTOS7 ~]#echo $?
0
[root@CENTOS7 ~]#a=nnn;b=nnn;[ $a = $b ] && echo “equal” || echo “no equal” ?比較字符串
equal
[root@CENTOS7 ~]#
[root@CENTOS7 ~]#a=nnn;b=nnsn;[ $a = $b ] && echo “equal” || echo “no equal”
no equal
[root@CENTOS7 ~]#a=10;b=10; [ $a -eq $b ] && echo “equal” || echo “no equal” 數字相等用-eq
equal
[root@CENTOS7 ~]#a=10;b=15; [ $a -eq $b ] && echo “equal” || echo “no equal”
no equal
diskfree=`df | tr -s ” ” “%” | cut -d “%” -f5 |sort -n | head -n1`
[ $diskfree -ge 80 ]&&echo “wall disk will be full”||echo “wall disk will not full”
[root@CENTOS7 ~]#bash ./ff.sh
wall disk will not full
diskfree=`df | tr -s ” ” “%” | cut -d “%” -f5 |sort -n | head -n1`
diskfree=80
[ $diskfree -ge 80 ]&&echo “wall disk will be full”||echo “wall disk will not full”
[root@CENTOS7 ~]#bash ./ff.sh
wall disk will be full
#********************************************************************
[ $# -ne 2 ]&&echo “arg number is 2″&&exit
[[ ! “$1” =~ ^[0-9]+$ ]]&&echo “$1 not digit”&&exit
[[ ! “$2” =~ ^[0-9]+$ ]]&&echo “$2 not digit”&&exit
uid1=`head -n$1 /etc/passwd | tail -n1|cut -d: -f3`
uid2=`head -n$2 /etc/passwd | tail -n1|cut -d: -f3`
echo sumuid=$[ $uid1 + $uid2 ]
[root@CENTOS7 data]#bash ./sumuid.sh 10 20
sumuid=1008
[root@CENTOS7 data]#bash ./sumuid.sh 10
arg number is 2
[root@CENTOS7 data]#bash ./sumuid.sh 10 a
a not digit
[root@CENTOS7 data]#bash ./sumuid.sh a 10
a not digit
[root@CENTOS7 data]#bash ./sumuid.sh 5 15
sumuid=85
[root@CENTOS7 data]#file=a.ch
[root@CENTOS7 data]#[[ $file =~ ..*\.sh$ ]]&& echo “sh” ||echo “notsh” ? ?=~后面跟正則表達式,左側和右側匹配
notsh
[root@CENTOS7 data]#file=bahfsh.sh
[root@CENTOS7 data]#[[ $file =~ ..*\.sh$ ]]&& echo “sh” ||echo “notsh”
sh
[root@CENTOS7 data]#[ -d /etc ]&&echo ture ? ?-d判斷是不是文件夾,如果軟連接指向文件夾也會顯示為ture
ture
[root@CENTOS7 data]#[ -d /etc/passwd ]&&echo ture
[root@CENTOS7 data]#[ -d /etc/passwd ]&&echo ture||echo notture
notture
[root@CENTOS7 data]#[ -L /bin ]&&echo rure ||echo notture ?-L判斷是不是軟連接
rure
[root@CENTOS7 data]#[ -L /etc ]&&echo rure ||echo notture
notture
[root@CENTOS7 data]#[ -w /etc/passwd ]&& echo “ture” || echo “false” ?-w判斷的是用戶的實際寫權限
ture
[root@CENTOS7 data]#ll /etc/passwd
-rw-r–r–. 1 root root 2174 Apr ?5 14:07 /etc/passwd
[root@CENTOS7 data]#[ -x /etc/passwd ]&& echo “ture” || echo “false” ?-x執行權限
false
id $1 &> /dev/null
[ $? -eq 0 ] &&echo user is exist&&exit
useradd $1
echo “magedu” |passwd –stdin $1
[root@CENTOS7 data]#bash ./username.sh wang
user is exist
[root@CENTOS7 data]#bash ./username.sh xixx
Changing password for user xixx.
passwd: all authentication tokens updated successfully.
需要加()或者{}
[root@CENTOS7 data]#true || echo cmd1 ; echo cmd2
cmd2
[root@CENTOS7 data]#false || echo cmd1 ; echo cmd2
cmd1
cmd2
[root@CENTOS7 data]#false || (echo cmd1 ; echo cmd2)
cmd1
cmd2
[root@CENTOS7 data]#true || (echo cmd1 ; echo cmd2)
()和{}的區別
[root@CENTOS7 data]#false || { echo cmd1 ; exit; }
cmd1
logout
[root@CENTOS7 data]#false || (echo cmd1 ; exit)
cmd1
expotr f10=a.sh ?必須定義全局變量才能被shell程序調用
[[ ${f10} =~ .+\.sh$ ]] && echo sh || echo no
[wang@CENTOS7 ~]$[ -r /etc/passwd -a -w /home/wang/f1 ]&&echo can ||echo cannot ? -a 并且
can
[wang@CENTOS7 ~]$[ -r /etc/passwd -a -w /etc/passwd ]&&echo can ||echo cannot
cannot
[wang@CENTOS7 ~]$[ -r /etc/passwd -o -w /home/f1 ]&&echo can ||echo cannot ? ? ? ?-o 或者
can
[wang@CENTOS7 ~]$[ -v vat ] && echo set ? -v查看是否被賦值
[wang@CENTOS7 ~]$vat=11
[wang@CENTOS7 ~]$[ -v vat ] && echo set
set
[[]]支持擴展的正則表達式
判斷一個字符串是否為空
[root@CENTOS7 bin]#[ “x$n123” = “x” ] && echo “yes”
yes
[root@CENTOS7 bin]#[ “x$n123” = “x” ] && echo “yes” || echo “no”
no
touch /etc/nologin 就會禁止用戶登錄
read -p “please input your name:” name ?-p “”現實””里的字符串
read -s -p “please input your passwd:” passwd -s 靜默模式
echo your name is $name
echo your passwd is $passwd
“read.sh” 15L, 480C written
[root@CENTOS7 bin]#read.sh
please input your name:wang
please input your passwd:your name is wang
your passwd is magedu
[root@CENTOS7 bin]#read -n 3 name ?-n限制輸入的字符數
123[root@CENTOS7 bin]#echo $name
123
[root@CENTOS7 bin]#read -d a name ?-d a 看到a就退出輸入
4564789a[root@CENTOS7 bin]#echo $name
4564789
[root@CENTOS7 bin]#read -t 6 name ?-t限定輸入時間如果超時則$name為空
雞兔同籠 頭35 腳94
read -p “please input how many heads:” head
read -p “please input how many foots:” foot
let rabbit=($foot-2\*$head)/2
let chook=$head-$rabbit
echo “chook is $chook”
echo “rabbit is $rabbit”
“headfoot.sh” 17L, 537C written
[root@CENTOS7 bin]#headfoot.sh
please input how many heads:35
please input how many foots:94
chook is 23
rabbit is 12
腳本里不能放置別名
echo $$查看當前進程號
[root@CENTOS7 ~]#cmd=”hostname”
[root@CENTOS7 ~]#echo $cmd
hostname
[root@CENTOS7 ~]#$cmd
CENTOS7.localdomain
[[ == ]]后面加通配符*代表任意一個字符串
* 匹配零個或多個字符
?? 匹配任何單個字符
?~ 當前用戶家目錄
?~mage 用戶mage家目錄
?~+ 當前工作目錄
?~- 前一個工作目錄
?[0-9] 匹配數字范圍
?[a-z]:字母
?[A-Z]:字母
?[wang] 匹配列表中的任何的一個字符
?[^wang] 匹配列表中的所有字符以外的字符
read一次賦值多個變量的方法
[root@CENTOS7 ~]#echo a b c >f1
[root@CENTOS7 ~]#read x y z <f1
[root@CENTOS7 ~]#echo $x
a
[root@CENTOS7 ~]#echo $y
b
[root@CENTOS7 ~]#echo $z
c
[root@CENTOS7 ~]#read x y z <<< “1 2 3”
[root@CENTOS7 ~]#echo $x
1
[root@CENTOS7 ~]#echo $y
2
[root@CENTOS7 ~]#echo $z
3
yes or no
read -p “do you agree,yes or no:” ans
[[ $ans =~ ^([Yy]([Ee][Ss])?)$ ]]&&echo “your answer is yes”&&exit
[[ $ans =~ ^([Nn][Oo]?)$ ]]&&echo “your answer is no”&&exit
[[ ! $ans =~ (^([Yy]([Ee][Ss])?)$ | ^([Nn][Oo]?)$) ]]&&echo “please inpur yes or no”
按生效范圍劃分,存在兩類:
?全局配置:
/etc/profile
/etc/profile.d/*.sh
/etc/bashrc
?個人配置:
~/.bash_profile ?里面放的是變量和用戶特定環境和啟動程序
~/.bashrc(bash run command) ? 里面放的是alias
交互式登錄:
(1)直接通過終端輸入賬號密碼登錄
(2)使用“su – UserName” 切換的用戶
執行順序:/etc/profile –> /etc/profile.d/*.sh –> ~/.bash_profile –>
~/.bashrc –> /etc/bashrc
?非交互式登錄:
(1)su UserName
(2)圖形界面下打開的終端
(3)執行腳本
(4)任何其它的bash實例
執行順序: ~/.bashrc –> /etc/bashrc –> /etc/profile.d/*.sh
配置文件用. 或者source 是在當前shell運行而不是開啟子進程。腳本里的內容會影響當前shell環境
在普通的腳本中不支持別名,在腳本中定義別名也不好用.
.bash_logout ?退出時想要執行的命令。創建自動備份 清除臨時文件
$-
[root@CENTOS7 ~]#echo $-
himBH
himBH中,h代表hash set+h set-h開啟關閉
i代表是否是交互式的,在腳本中不顯示。
m代表監控
B代表大括號展開
H代表history
vim /etc/issue ?在登錄前顯示 ? 或者/etc/motd
hi,anggerous
bash如何展開命令行
把命令行分成單個命令詞
展開別名
展開大括號的聲明({})
展開波浪符聲明(~)
命令替換$() 和 “)
再次把命令行分成命令詞
展開文件通配(*、?、[abc]等等)
準備I/0重導向(<、>)
運行命令
.vimrc文件不是bash文件所以不需要. .vimrc,當運行vim時會自動讀取此文件
locate 搜索索引型搜索,需要更新數據庫才能找到
數據庫開機事會自動更新。
[root@CENTOS7 ~]#ll /var/lib/mlocate/mlocate.db
-rw-r—–. 1 root slocate 2567142 Apr 12 09:16 /var/lib/mlocate/mlocate.db
touch 11.sh
locate 11.sh 找不到文件是是因為數據庫沒有更新
updaredb ? ? 手工更新數據庫后就可以找到剛剛創建的文件了
[root@CENTOS7 ~]#locate 11.sh
/root/11.sh
locate
-i ?不區分大小寫
-n ?取匹配的前幾行
-r ?使用正則表達式
[root@CENTOS7 ~]#locate -r “^/etc/.*\.conf$” ?搜索/etc/下的所有.conf結尾的文件
find
[root@CENTOS7 ~]#find /etc/ -name passwd ?找到etc文件夾下的passwd
/etc/pam.d/passwd
/etc/passwd
[root@CENTOS7 ~]#find /etc/ -maxdepth 1 -name passwd ? 第一層
/etc/passwd
find ~/ -maxdepth 1 = ls -1 -a
[root@CENTOS7 ~]#find /etc/ -maxdepth 2 -name passwd
/etc/pam.d/passwd
/etc/passwd
[root@CENTOS7 ~]#find /data/ -maxdepth 1 -name f1
/data/f1
[root@CENTOS7 ~]#find /data/ -maxdepth 2 -name f1
/data/f1
/data/d1/f1
[root@CENTOS7 ~]#find /data/ -maxdepth 3 -name f1
/data/f1
/data/d1/d2/f1
/data/d1/f1
[root@CENTOS7 ~]#find /data/ -maxdepth 4 -name f1
/data/f1
/data/d1/d2/d3/f1
/data/d1/d2/f1
/data/d1/f1
[root@CENTOS7 ~]#find /data/ -mindepth 3 -maxdepth 4 -name f1
/data/d1/d2/d3/f1
/data/d1/d2/f1
[root@CENTOS7 ~]#find /data/ -mindepth 4 -maxdepth 4 -name f1 ? 只顯示第4層
/data/d1/d2/d3/f1
find -name f1 是精確搜索,模糊搜索需要”*f1*”
[root@CENTOS7 ~]#find -name “*f1*”
./bin/f10.sh
./f1
./f11
find / -inum 30 ?-inum,搜索節點編號。顯示節點編號命令 ,ll -i
/sys/bus/platform/drivers_autoprobe
[root@CENTOS7 data]#ln arg.sh arg
[root@CENTOS7 data]#find -samefile arg ? ?搜索節點編號相同的文件,就是搜索硬鏈接
./arg.sh
./arg
[root@CENTOS7 data]#find -links 2 ? 搜索鏈接數是2的文件
./arg.sh
./d1/d2/d3
./arg
[root@CENTOS7 data]#find /etc/ -regex “.*\.conf$” ?搜索etc下的所有已.conf結尾的文件
[root@CENTOS7 ~]#find /home/ -user wang ? 搜索home下的wang擁有的所有文件
[root@CENTOS7 ~]#find /home/ -user wang -ls ?搜索同上并顯示文件詳細信息
[root@CENTOS7 ~]#find /home -user wang -name “*.sh” ?home下wang用戶的所有已.sh后綴的文件
/home/wang/68_scp.sh
[root@CENTOS7 ~]#find /home -nouser -group lele -ls ?搜索沒有擁有者但是組時lele的文件
[root@CENTOS7 ~]#find /home/ -nouser -ls -o -nogroup -ls ?= find /home/ \( -nouser -o -nogroup \) -ls ?當用戶和所屬組都能找到時需要每個命令后都加-ls
[root@CENTOS7 ~]#find /home -maxdepth 1 -type d ? ? 只搜索home下第一層文件夾
/home
/home/wang
/home/xixi
/home/f1
/home/f2
/home/f3
/home/gentoo
/home/lele
/home/mage
/home/gir
/home/git
/home/xixx
/home/xxxxxxxx
/home/yyyyyyyyyyyyyy
[root@CENTOS7 ~]#find /data/ -empty ? 搜索data下的空文件
/data/f1
/data/d1/d2/d3/f1
/data/d1/d2/f1
/data/d1/f1
/data/d1f1
[root@CENTOS7 ~]#find /data/ -empty -type f -ls ? ? ?搜索data下的普通空文件
? ? 67 ? ?0 ———- ? 1 root ? ? root ? ? ? ? ? ?0 Apr 12 13:03 /data/f1
33592097 ? ?0 -rw-r–r– ? 1 root ? ? root ? ? ? ? ? ?0 Apr 12 13:03 /data/d1/d2/d3/f1
16781761 ? ?0 -rw-r–r– ? 1 root ? ? root ? ? ? ? ? ?0 Apr 12 13:03 /data/d1/d2/f1
? ? 78 ? ?0 -rw-r–r– ? 1 root ? ? root ? ? ? ? ? ?0 Apr 12 13:03 /data/d1/f1
? ? 77 ? ?0 -rw-r–r– ? 1 root ? ? root ? ? ? ? ? ?0 Apr 12 13:03 /data/d1f1
[root@CENTOS7 ~]#find /data/ ! -empty -type d -ls ? ?搜索data下的非空文件夾
? ? 64 ? ?0 drwxr-xr-x ? 3 root ? ? root ? ? ? ? ?168 Apr 12 13:29 /data/
? ? 72 ? ?0 drwxr-xr-x ? 3 root ? ? root ? ? ? ? ? 26 Apr 12 13:03 /data/d1
16781760 ? ?0 drwxr-xr-x ? 3 root ? ? root ? ? ? ? ? 26 Apr 12 13:03 /data/d1/d2
33592096 ? ?0 drwxr-xr-x ? 2 root ? ? root ? ? ? ? ? 16 Apr 12 13:03 /data/d1/d2/d3
[root@CENTOS7 ~]#find /data/ ! -empty ! -type d -ls ? 搜索data下的非空非文件夾的文件
? ? 71 ? 12 -rw-r–r– ? 1 root ? ? root ? ? ? ?12288 Apr ?7 19:07 /data/.f1.swp
? ? 70 ? ?4 -rw-r–r– ? 2 root ? ? root ? ? ? ? ?562 Apr ?9 23:36 /data/arg.sh
? ? 69 ? ?4 -rwxr-xr-x ? 1 root ? ? root ? ? ? ? ?387 Apr 10 09:23 /data/f1.sh
? ? 68 ? ?0 lrwxrwxrwx ? 1 root ? ? root ? ? ? ? ? ?6 Apr 10 09:27 /data/link.sh -> arg.sh
? ? 73 ? ?4 -rwxr-xr-x ? 1 root ? ? root ? ? ? ? ?430 Apr 10 09:44 /data/f2.sh
? ? 74 ? ?4 -rw-r–r– ? 1 root ? ? root ? ? ? ? ?353 Apr 10 09:58 /data/f3.sh
? ? 75 ? ?4 -rw-r–r– ? 1 root ? ? root ? ? ? ? ?628 Apr 10 13:54 /data/sumuid.sh
? ? 76 ? ?4 -rw-r–r– ? 1 root ? ? root ? ? ? ? ?450 Apr 10 14:51 /data/username.sh
? ? 70 ? ?4 -rw-r–r– ? 2 root ? ? root ? ? ? ? ?562 Apr ?9 23:36 /data/arg
德摩根定律
(非 A) 或 (非 B) = 非(A 且 B)
(非 A) 且 (非 B) = 非(A 或 B)
find /etc/ -path “/etc/sane.d” -a -prune -o -name “*.conf” ? 搜索排除/etc/sane.d剩下的.conf結尾的文件
find /etc/ \( -path “/etc/libreport” -o -path “/etc/sane.d” \) -a -prune -o -name “*.conf”
搜索既不是/etc/libreport也不是”/etc/sane.d”中的其他已.conf結尾的文件
[root@CENTOS7 data]#dd if=/dev/zero of=/data/f11 bs=1 count=1023 ? 創建大文件或者固定大小的文件
1023+0 records in
1023+0 records out
1023 bytes (1.0 kB) copied, 0.00218562 s, 468 kB/s
[root@CENTOS7 data]#dd if=/dev/zero of=/data/f12 bs=1 count=1024
1024+0 records in
1024+0 records out
1024 bytes (1.0 kB) copied, 0.00232464 s, 440 kB/s
./f12
[root@CENTOS7 data]#find -size 1k -ls
? ? 64 ? ?0 drwxr-xr-x ? 3 root ? ? root ? ? ? ? ?190 Apr 12 15:16 .
? ? 70 ? ?4 -rw-r–r– ? 2 root ? ? root ? ? ? ? ?562 Apr ?9 23:36 ./arg.sh
? ? 69 ? ?4 -rwxr-xr-x ? 1 root ? ? root ? ? ? ? ?387 Apr 10 09:23 ./f1.sh
? ? 68 ? ?0 lrwxrwxrwx ? 1 root ? ? root ? ? ? ? ? ?6 Apr 10 09:27 ./link.sh -> arg.sh
? ? 73 ? ?4 -rwxr-xr-x ? 1 root ? ? root ? ? ? ? ?430 Apr 10 09:44 ./f2.sh
? ? 74 ? ?4 -rw-r–r– ? 1 root ? ? root ? ? ? ? ?353 Apr 10 09:58 ./f3.sh
? ? 75 ? ?4 -rw-r–r– ? 1 root ? ? root ? ? ? ? ?628 Apr 10 13:54 ./sumuid.sh
? ? 76 ? ?4 -rw-r–r– ? 1 root ? ? root ? ? ? ? ?450 Apr 10 14:51 ./username.sh
? ? 72 ? ?0 drwxr-xr-x ? 3 root ? ? root ? ? ? ? ? 26 Apr 12 13:03 ./d1
16781760 ? ?0 drwxr-xr-x ? 3 root ? ? root ? ? ? ? ? 26 Apr 12 13:03 ./d1/d2
33592096 ? ?0 drwxr-xr-x ? 2 root ? ? root ? ? ? ? ? 16 Apr 12 13:03 ./d1/d2/d3
? ? 70 ? ?4 -rw-r–r– ? 2 root ? ? root ? ? ? ? ?562 Apr ?9 23:36 ./arg
? ? 79 ? ?4 -rw-r–r– ? 1 root ? ? root ? ? ? ? 1023 Apr 12 15:14 ./f11
? ? 80 ? ?4 -rw-r–r– ? 1 root ? ? root ? ? ? ? 1024 Apr 12 15:16 ./f12
[root@CENTOS7 data]#find -size 1024c -ls
? ? 80 ? ?4 -rw-r–r– ? 1 root ? ? root ? ? ? ? 1024 Apr 12 15:16 ./f12
[root@CENTOS7 data]#find -size -1024c ?代表[0-1023]
[root@CENTOS7 data]#find -size +1024c ?代表(1024-無窮大]
[root@CENTOS7 data]#useradd shagua
[root@CENTOS7 data]#find /etc/ -mmin -1 ?搜索1分鐘以內修改過的文件
/etc/
/etc/group
/etc/gshadow
/etc/passwd
/etc/shadow
[root@CENTOS7 data]#userdel -r shagua
[root@CENTOS7 data]#find /etc/ -mmin -1
/etc/
/etc/group
/etc/gshadow
/etc/passwd
/etc/shadow
[root@CENTOS7 data]#find -perm /666 -ls ? ?3個權限有一個是6就行
? ? 64 ? ?0 drwxr-xr-x ? 3 root ? ? root ? ? ? ? ?190 Apr 12 15:16 .
? ? 71 ? 12 -rw-r–r– ? 1 root ? ? root ? ? ? ?12288 Apr ?7 19:07 ./.f1.swp
? ? 70 ? ?4 -rw-r–r– ? 2 root ? ? root ? ? ? ? ?562 Apr ?9 23:36 ./arg.sh
? ? 69 ? ?4 -rwxr-xr-x ? 1 root ? ? root ? ? ? ? ?387 Apr 10 09:23 ./f1.sh
? ? 68 ? ?0 lrwxrwxrwx ? 1 root ? ? root ? ? ? ? ? ?6 Apr 10 09:27 ./link.sh -> arg.sh
? ? 73 ? ?4 -rwxr-xr-x ? 1 root ? ? root ? ? ? ? ?430 Apr 10 09:44 ./f2.sh
? ? 74 ? ?4 -rw-r–r– ? 1 root ? ? root ? ? ? ? ?353 Apr 10 09:58 ./f3.sh
? ? 75 ? ?4 -rw-r–r– ? 1 root ? ? root ? ? ? ? ?628 Apr 10 13:54 ./sumuid.sh
? ? 76 ? ?4 -rw-r–r– ? 1 root ? ? root ? ? ? ? ?450 Apr 10 14:51 ./username.sh
? ? 72 ? ?0 drwxr-xr-x ? 3 root ? ? root ? ? ? ? ? 26 Apr 12 13:03 ./d1
16781760 ? ?0 drwxr-xr-x ? 3 root ? ? root ? ? ? ? ? 26 Apr 12 13:03 ./d1/d2
33592096 ? ?0 drwxr-xr-x ? 2 root ? ? root ? ? ? ? ? 16 Apr 12 13:03 ./d1/d2/d3
33592097 ? ?0 -rw-r–r– ? 1 root ? ? root ? ? ? ? ? ?0 Apr 12 13:03 ./d1/d2/d3/f1
16781761 ? ?0 -rw-r–r– ? 1 root ? ? root ? ? ? ? ? ?0 Apr 12 13:03 ./d1/d2/f1
? ? 78 ? ?0 -rw-r–r– ? 1 root ? ? root ? ? ? ? ? ?0 Apr 12 13:03 ./d1/f1
? ? 77 ? ?0 -rw-r–r– ? 1 root ? ? root ? ? ? ? ? ?0 Apr 12 13:03 ./d1f1
? ? 70 ? ?4 -rw-r–r– ? 2 root ? ? root ? ? ? ? ?562 Apr ?9 23:36 ./arg
? ? 79 ? ?4 -rw-r–r– ? 1 root ? ? root ? ? ? ? 1023 Apr 12 15:14 ./f11
? ? 80 ? ?4 -rw-r–r– ? 1 root ? ? root ? ? ? ? 1024 Apr 12 15:16 ./f12
[root@CENTOS7 data]#find -perm -666 -ls ? ?大于等于666就行
? ? 68 ? ?0 lrwxrwxrwx ? 1 root ? ? root ? ? ? ? ? ?6 Apr 10 09:27 ./link.sh -> arg.sh
[root@CENTOS7 data]#find -perm 666 -ls ? 權限是666的文件
echo f{1..10000000} | xargs -n100 touch ?創建多個小文件或者刪除時使用。 ?-n100每100個一組
[root@CENTOS7 data]#find -perm 644 -delete ? 刪除找到的文件
find -perm 644 -fls file1 = find -perm 644 -ls > file1 ? 保存找到的文件的屬性到file1中
[root@CENTOS7 data]#find -empty -ok rm {} \; ?交互式刪除找到的文件
< rm … ./f1 > ? y
< rm … ./d1/d2/d3/f1 > ? y
< rm … ./d1/d2/f1 > ? y
< rm … ./d1/f1 > ? y
< rm … ./d1f1 > ? y
find -name “f*” -exec rm {} \; 直接刪除不在詢問
find -name “f*” -exec mv {} /root/bin/ \; ?移動文件到root/bin下
find -name “f*” -exec mv {} {}.bak \; ?修改文件名字
壓縮
壓縮的是文件,可以一次壓縮多個文件但是壓縮結果也是多個,不會打包成一個文件
[root@CENTOS7 f111]#compress m ? 壓縮文件m ? ?,會刪除原文件
[root@CENTOS7 f111]#ll
total 2244
-rw——-. 1 root root 1867600 Apr 12 16:47 messages
-rw——-. 1 root root ?428795 Apr 12 16:48 m.Z
(uncompress或者 compress -d) m.Z ? 解壓文件
[root@CENTOS7 f111]#compress -c m > m.Z ?不會刪除原文件
[root@CENTOS7 f111]#ll
total 4068
-rw——-. 1 root root 1867600 Apr 12 16:48 m
-rw——-. 1 root root 1867600 Apr 12 16:47 messages
-rw-r–r–. 1 root root ?428795 Apr 12 16:54 m.Z
[root@CENTOS7 f111]#zcat m.Z > mm ? 解壓縮且不刪除壓縮文件
[root@CENTOS7 f111]#ll
total 5892
-rw——-. 1 root root 1867600 Apr 12 16:48 m
-rw——-. 1 root root 1867600 Apr 12 16:47 messages
-rw-r–r–. 1 root root 1867600 Apr 12 16:55 mm
-rw-r–r–. 1 root root ?428795 Apr 12 16:54 m.Z
[root@CENTOS7 f111]#gzip m
[root@CENTOS7 f111]#ll
total 4268
-rw——-. 1 root root 1867600 Apr 12 16:47 messages
-rw——-. 1 root root ?204721 Apr 12 16:48 m.gz
-rw-r–r–. 1 root root 1867600 Apr 12 16:55 mm
-rw-r–r–. 1 root root ?428795 Apr 12 16:54 m.Z
gzip/gunzip
?gzip [OPTION]… FILE …
-d: 解壓縮,相當于gunzip
-c: 將壓縮或解壓縮的結果輸出至標準輸出
-#:1-9,指定壓縮比,值越大壓縮比越大
?zcat:不顯式解壓縮的前提下查看文本文件內容
?實例:
gzip -c messages >messages.gz
gzip -c -d messages.gz > messages
zcat messages.gz > messages
bzip2/bunzip2/bzcat ? ? ? ? ? ? ? ? ?解壓縮文件需要正確的后綴名
?bzip2 [OPTION]… FILE …
-k: keep, 保留原文件
-d:解壓縮
-#:1-9,壓縮比,默認為9
?bzcat:不顯式解壓縮的前提下查看文本文件內容
xz/unxz/xzcat
?xz [OPTION]… FILE …
-k: keep, 保留原文件
-d:解壓縮
-#:1-9,壓縮比,默認為6
?xzcat: 不顯式解壓縮的前提下查看文本文件內容
打包壓縮
zip –r /testdir/sysconfig /etc/sysconfig/
?解包解壓縮
unzip sysconfig.zip
cat /var/log/messages | zip messages – ?如果不帶-沒有東西可壓縮
unzip -p message > message
[root@CENTOS7 f111]#ll data.tar -h ? 查看文件daxiao
-rw-r–r–. 1 root root 30K Apr 12 17:47 data.tar
[root@CENTOS7 f111]#du -sh /data ? ?查看文件夾大小
76K /data
[root@CENTOS7 f111]#tar -tvf vp ? ?預覽壓縮的文件包
tar -cpvf dara.tar /data ? ? ?打包data
tar -xvf dara.tar -C/mnt ? ? ? 默認解壓到當前文件夾,-C/mnt ?指定解包到/mnt目錄下
tar -zcpvf data.tar.gz /data ? ? 打包并壓縮
tar -jcpvf data.tar.bgz2 /data
tar -Jcpvf data.tar.xz /data
tar -xvf dara.tar -C/mnt ? ? ? ? 解壓縮
tar -Jcvpf list.tar .xz -T list.txt -X exlist.txt ? ? ?-T中放的/root和/boot這是要打包的,-X中放的是/root/passwd和/boot/initramfs-3.10.0-693.el7.x86_64.img,這些是不打包的文件
split ?-b 10M -d list.tar.xz ?a.tar ? 切割后的文件是 ? a.tar.xz00…a.tar.xz09
cat a.tar.xz* > a.tar.xz ? ?將切割后的文件合并成切割前的文件
find /etc/sysconfig/ | cpio -ov > sysconfig.cpio ? ,打包成cpio文件,如果打包的文件帶絕對路徑,解包時會按照原來的路徑解壓
cpio -tv < sysconfig.cpio ? ? ?預覽文件的文件名
cpio -idv <sysconfig.cpio ? ? ? 解開文件默認解在當前文件夾下
sed
sed命令是一個行編輯器
當運行sed時在內存中開辟一個空間叫做模式空間,每次讀取一行,默認print。
[root@CENTOS7 data]#sed “2p” f1 ? ?自動打印 第二行再打印一遍
1
2
2
3
4
5
6
7
8
9
10
[root@CENTOS7 data]#ifconfig | sed -n “2p”
? ? ? ? inet 192.168.30.101 ?netmask 255.255.255.0 ?broadcast 192.168.30.255
[root@CENTOS7 data]#sed -n “/root/p” /etc/passwd ? ? ? ? ? ? 搜索有root的行
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@CENTOS7 data]#sed -n ‘$p’ /etc/passwd ? ? ? ? ? ? ? ?搜索最后一行,注意只能使用’$p’,不能使用”$p”。
yyyyyyyyyyyyyy:x:1006:1012::/home/yyyyyyyyyyyyyy:/bin/bash
[root@CENTOS7 data]#sed -n ‘/^root/p’ /etc/passwd ? ? ? ? ? 已root開頭的行
root:x:0:0:root:/root:/bin/bash
[root@CENTOS7 data]#sed -n ‘2,5p’ /etc/passwd ? ? ? ? ? ? ? 顯示2到5行
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
[root@CENTOS7 data]#sed -n ‘2,+3p’ /etc/passwd
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
[root@CENTOS7 data]#sed -n ‘/^bin/,/^ftp/p’ /etc/passwd ? ? ?顯示bin開頭到ftp
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
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
sed -n ‘/^bin/,/^disjofhwaohfosh/p’ /etc/passwd
從bin開頭如果后面都沒有匹配到這從bin一直顯示全文
[root@CENTOS7 data]#sed -n ‘1~2p’ f1 ?顯示奇數行
1
3
5
7
9
[root@CENTOS7 data]#sed -n ‘2~2p’ f1 ? 顯示偶數行
2
4
6
8
10
[root@CENTOS7 data]#sed -n -e ‘2p’ -e ‘4p’ -e ‘6p’ f1 ? 多點編輯
2
4
6
[root@CENTOS7 data]#cat > f
2~2p
2p
5p
[root@CENTOS7 data]#sed -n -f f f1 ? ? 處理文件內的多條命令
2
2
4
5
6
8
10
[root@CENTOS7 data]#sed ‘2d’ f1 ? ?刪除第二行
1
3
4
5
6
7
8
9
10
[root@CENTOS7 data]#sed ‘2!d’ f1 ? ? 刪除第二行取反
2
[root@CENTOS7 data]#sed -n ‘/root/=’ /etc/passwd ? ? = ?顯示匹配到的行號
1
10
[root@CENTOS7 data]#sed ‘2,5a=== aewwq’ f1 ? ? ? a 在指定行后追加內容
1
2
=== aewwq
3
=== aewwq
4
=== aewwq
5
=== aewwq
6
7
8
9
10
[root@CENTOS7 data]#sed ‘/aliase/aaliase p=”poweroff”‘ /root/.bashrc ? 添加alias p=poweeroff
# .bashrc
# User specific aliases and functions
aliase p=”poweroff”
alias rm=’rm -i’
alias cp=’cp -i –backup=numbered’
alias mv=’mv -i’
alias cdnet=”cd /etc/sysconfig/network-scripts”
alias editnet=”vim /etc/sysconfig/network-scripts/ifcfg-ens33″
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
[root@CENTOS7 data]#sed -i.bak ‘/aliases/aalias p=poweroff’ /root/.bashrc ? ? 修改原文件并備份文件
[root@CENTOS7 data]#sed ‘2,5a ? 123’ f1 ? ?需要加\才能顯示空格
1
2
123
3
123
4
123
5
123
6
7
8
9
10
[root@CENTOS7 data]#sed ‘2,5a\ ? 123’ f1 ? ?a\表示從\開始后面都是要追加的內容
1
2
? ?123
3
? ?123
4
? ?123
5
? ?123
6
7
8
9
10
[root@CENTOS7 data]#sed ‘2,5i\ ? 123’ f1 ? ? a在后面加,i在前面追加
1
? ?123
2
? ?123
3
? ?123
4
? ?123
5
6
7
8
9
10
[root@CENTOS7 data]#sed ‘2,5c\ ? 2345’ f1 ? c用2345代替2到5行
1
? ?2345
6
7
8
9
10
[root@CENTOS7 data]#sed ‘2~2w f2’ f1 ? ?w將符合條件的行存入到f2中
1
2
3
4
5
6
7
8
9
10
[root@CENTOS7 data]#cat f2
2
4
6
8
10
[root@CENTOS7 data]#sed ‘2~2r /etc/issue’ f1 ? r讀入文件追加到指定的行后
1
2
\S
Kernel \r on an \m
3
4
\S
Kernel \r on an \m
5
6
\S
Kernel \r on an \m
7
8
\S
Kernel \r on an \m
9
10
\S
Kernel \r on an \m
[root@CENTOS7 data]#sed -n s’/root/rooter/p’ /etc/passwd ? ?搜索root并替換成rooter
rooter:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/rooter:/sbin/nologin
[root@CENTOS7 data]#sed -n ‘s/root/rooter/gp’ /etc/passwd ? ? 全部替換
rooter:x:0:0:rooter:/rooter:/bin/bash
operator:x:11:0:operator:/rooter:/sbin/nologin
[root@CENTOS7 data]#sed -n -r ‘s/(root)/\1mei/gp’ /etc/passwd ? ?使用正則表達式
rootmei:x:0:0:rootmei:/rootmei:/bin/bash
operator:x:11:0:operator:/rootmei:/sbin/nologin
[root@CENTOS7 data]#sed -r ‘s/(.*)/\1magedu/’ /etc/passwd ? ? 每行后都加上magedu
root:x:0:0:root:/root:/bin/bashmagedu
bin:x:1:1:bin:/bin:/sbin/nologinmagedu
daemon:x:2:2:daemon:/sbin:/sbin/nologinmagedu
adm:x:3:4:adm:/var/adm:/sbin/nologinmagedu
[root@CENTOS7 data]#sed -r ‘/GRUB_CMDLINE_LINUX=/s/(.*)”$/\1 xyz”/’ /etc/default/grub
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR=”$(sed ‘s, release .*$,,g’ /etc/system-release)” ? ?/搜索條件/s/正則表達式/要替換的內容/
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT=”console”
GRUB_CMDLINE_LINUX=”crashkernel=auto rhgb quiet xyz”
GRUB_DISABLE_RECOVERY=”true”
[root@CENTOS7 data]#sed -r ‘/GRUB_CMDLINE_LINUX=/s/”$/ xyz”/’ /etc/default/grub
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR=”$(sed ‘s, release .*$,,g’ /etc/system-release)”
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT=”console”
GRUB_CMDLINE_LINUX=”crashkernel=auto rhgb quiet xyz”
GRUB_DISABLE_RECOVERY=”true”
[root@CENTOS7 data]#ifconfig ens33 | sed -n ‘2p’ | sed -r ‘s/(.*inet )(.*)( net.*)/\2/’ ?取ip地址
192.168.30.101
[root@CENTOS7 data]#ifconfig ens33 | sed -r ‘2!d;s/(.*inet )(.*)( net.*)/\2/’ ? ? 刪除除了第2行
192.168.30.101
[root@CENTOS7 data]#ifconfig ens33 | sed -n ‘2p’ | sed ‘s/.*net //’ | sed ‘s/ net.*//’ ? ?掐頭去尾
192.168.30.101
[root@CENTOS7 data]#ifconfig ens33 | sed -n ‘2p’ | sed -e ‘s/.*net //’ -e ‘s/ net.*//’
192.168.30.101
[root@centos ~]#cat -n /etc/httpd/conf/httpd.conf ?| sed -n -e ‘990p’ -e ‘1003,1009p’ | sed -r ‘s/.*#//g’
NameVirtualHost *:80
<VirtualHost *:80>
? ? ServerAdmin webmaster@dummy-host.example.com
? ? DocumentRoot /www/docs/dummy-host.example.com
? ? ServerName dummy-host.example.com
? ? ErrorLog logs/dummy-host.example.com-error_log
? ? CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
[root@centos ~]#echo “/etc/sysconfig/network/” |sed -r ‘s@(.*/)([^/]+/?)@\1@’ ? 取基名和路徑
/etc/sysconfig/
[root@centos ~]#cat -n /etc/httpd/conf/httpd.conf | sed ‘990,1009!d’ | sed ‘2,14d’ ? ? ?后面的第二次去行是前面取完文件的行數。
? ?990 #NameVirtualHost *:80
? 1004 # ? ?ServerAdmin webmaster@dummy-host.example.com
? 1005 # ? ?DocumentRoot /www/docs/dummy-host.example.com
? 1006 # ? ?ServerName dummy-host.example.com
? 1007 # ? ?ErrorLog logs/dummy-host.example.com-error_log
? 1008 # ? ?CustomLog logs/dummy-host.example.com-access_log common
? 1009 #</VirtualHost>

本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/96014

(0)
王仁兵王仁兵
上一篇 2018-04-15
下一篇 2018-04-15

相關推薦

  • 向CA申請證書

    ?實驗向CA申請證書 步驟: 建立Root CA 1)生成私鑰 2)自簽名證書 2.用戶或服務器 1)生成私鑰 2)生成證書申請文件 3)將申請文件發給CA 3.CA頒發證書 4.證書發送給客戶端 5.應用軟件使用證書 如:centos7當服務器,centos6客戶端 ,即centos6向centos7申請證書 第一:建立根CA 生成私鑰的文件放在哪,文件名…

    2018-05-19
  • Linux第四周作業

    1、復制/etc/skel目錄為/home/tuser1,要求/home/tuser1及其內部文件的屬組和其它用戶均沒有任何訪問權限。 ]# cp -r /etc/skel /home/tuser1 ]# chmod -R go= /home/tuser1 2、編輯/etc/group文件,添加組hadoop。 ]# echo “hadoop:x…

    2018-07-09
  • vim

    翻屏半Ctrl+ b uf d 屏幕 首 中 尾HML 段首尾 分割 整空行#{} 句首 分割 ._ 或者 整空行#() 選行1G(gg)#GG 光標# kh lj 行內0_^$ 單詞#bWe w 配置/etc/vimrc????? /* 全局 */~/.vimrc???????? /* 個人 */ 行號???????????????????????????…

    Linux筆記 2018-07-01
  • 初識Linux集群

    Linux cluster基礎與lvs簡單介紹

    2018-06-24
欧美性久久久久