rpm,yum命令,定時任務與sed常見用法

rpm yum sed at 定時任務crontab

1、簡述rpm與yum命令的常見選項,并舉例
rpm命令:
簡介:包管理器,rmp原本是Red Hat Linux發行版專門用來管理Linux各項套件的程序,由于它遵循GPL規則且功能強大方便,因而廣受歡迎。逐漸受到其他發行版的采用。RPM套件管理方式的出現,讓Linux易于安裝,升級,間接提升了Linux的適用度。
格式:
查詢:
rpm {-q|–query} [select-options] [query-options]
校驗
rpm {-V|–verify} [select-options] [verify-options]
安裝
rpm {-i|–install} [install-options] PACKAGE_FILE …
升級或安裝
rpm {-U|–upgrade} [install-options] PACKAGE_FILE …
升級
rpm {-F|–freshen} [install-options] PACKAGE_FILE …
卸載
rpm {-e|–erase} [–allmatches] [–justdb] [–nodeps] [–noscripts] [–notriggers] [–test] PACKAGE_NAME …
常見用法:
安裝:
格式: rpm {-i|–install} [install-options] PACKAGE_FILE …
[install-options]:
-h:hash marks輸出進度條;每個#表示2%的進度;
–test:測試安裝,檢查并報告依賴關系及沖突消息等;
–nodeps:忽略依賴關系;不建議;
–replacepkgs:重新安裝
–nosignature:不檢查包簽名信息,不檢查來源合法性;
–nodigest:不檢查包完整性信息;

舉例:安裝zip的rpm包

rpm -ivh zip-3.0-11.el7.x86_64.rpm

clipboard
升級:
格式:rpm {-U|–upgrade} [install-options] PACKAGE_FILE …
rpm {-F|–freshen} [install-options] PACKAGE_FILE …
-U:升級或安裝;
-F:升級
[install-option]:
–oldpackage:降級;
–force:強制升級;

舉例:使用-U時,如果程序未安裝則會安裝,-F時則只能用在升級

rpm -Uvh zip-3.0-11.el7.x86_64.rpm

clipboard
卸載:
格式:rpm {-e|–erase} [–allmatches] [–nodeps] [–noscripts] [–test] PACKAGE_NAME …
卸載選項:
–allmatches:卸載所有匹配指定名稱的程序包的各版本;
–nodeps:忽略依賴關系
–test:測試卸載,dry run模式

舉例:卸載zip程序

rpm -evh zip

clipboard

查詢:
格式:rpm {-q|–query} [select-options] [query-options]
[select-options]
PACKAGE_NAME:查詢指定的程序包是否已經安裝,及其版本;
-a, –all:查詢所有已經安裝過的包;
-f FILE:查詢指定的文件由哪個程序包安裝生成;
-p, –package PACKAGE_FILE:用于實現對未安裝的程序包執行查詢操作;
–whatprovides CAPABILITY:查詢指定的CAPABILITY由哪個程序包提供;
–whatrequires CAPABILITY:查詢指定的CAPABILITY被哪個包所依賴;
[query-options]
–changelog:查詢rpm包的changlog;
-l, –list:程序安裝生成的所有文件列表;
-i, –info:程序包相關的信息,版本號、大小、所屬的包組,等;
-c, –configfiles:查詢指定的程序包提供的配置文件;
-d, –docfiles:查詢指定的程序包提供的文檔;
–provides:列出指定的程序包提供的所有的CAPABILITY;
-R, –requires:查詢指定的程序包的依賴關系;
–scripts:查看程序包自帶的腳本片斷;
常見用法:
-qi PACKAGE 查詢包的簡要信息

clipboard
-qf FILE 查詢某個文件屬于哪個包生成的

clipboard
-qc PACKAGE 查詢某包完成安裝完成后生成的所有配置文件

clipboard
-ql PACKAGE 查詢某包安裝生成的文件列表

clipboard
-qd PACKAGE 查詢某包安裝完成后生成的幫助文件

clipboard
-qpi PACKAGE_FILE 查詢尚未安裝的包的簡要信息

clipboard
-qpl PACKAGE_FILE 查詢尚未安裝的某包安裝生成的文件列表

clipboard
-qpc PACKAGE_FILE 查詢尚未安裝的包安裝完成后生成的配置文件? ? #有些包生成后是沒有配置文件的。

clipboard

 

校驗:
格式:rpm {-V|–verify} [select-options] [verify-options]
安裝完軟件包后會生成相關的文件,如果文件發生了變動,rpm會說明變動的類型

有以下幾種
S file Size differs 文件大小發生變化
M Mode differs (includes permissions and file type) 模式發生變化(包括文件權限)
5 digest (formerly MD5 sum) differs md5校驗碼發生變化
D Device major/minor number mismatch 設備主/次設備號不匹配
L readLink(2) path mismatch
U User ownership differs 屬主發生變化
G Group ownership differs 屬組發生變化
T mTime differs mtime發生變化
P caPabilities differ caPabilities 發生變化

例如:先追加一個空行到 /etc/skel/.bashrc? 然后校驗發現S 5 T 發生變動,即文件大小改變,md5校驗和改變,mtime也發生變化

clipboard

yum命令:
簡介:YUM是一個交互式的基于RPM的包管理器。它可以自動執行系統更新,包括依賴性分析和基于“知識庫”元數據的過時處理。它還可以執行新的包的安裝,刪除舊的包,并在許多其他命令/服務中執行對已安裝/可用包的查詢
格式:
yum [options] [command] [package …]
常見用法
顯示倉庫列表:
repolist [all|enabled|disabled]

clipboard
顯示程序包:
yum list [all | glob_exp1] [glob_exp2] […]
yum list {available|installed|updates} [glob_exp1] […]

clipboard
安裝程序包:
install package1 [package2] […]
reinstall package1 [package2] […] (重新安裝)

舉例: 安裝gcc

clipboard
升級程序包:
update [package1] [package2] […]
downgrade package1 [package2] […] (降級)

舉例:升級vim

clipboard
檢查可用升級:
check-update

clipboard
卸載程序包:
remove | erase package1 [package2] […]

舉例:卸載unzip包

clipboard
查看程序包information:
info […]

舉例:查看unzip包相關信息

clipboard
查看指定的特性(可以是某文件)是由哪個程序包所提供:
provides | whatprovides feature1 [feature2] […]

clipboard
清理本地緩存:
clean [ packages | metadata | expire-cache | rpmdb | plugins | all ]
構建緩存:
makecache
搜索:
search string1 [string2] […] 以指定的關鍵字搜索程序包名及summary信息;

clipboard
查看指定包所依賴的capabilities:
deplist package1 [package2] […]

舉例:查看gcc包的依賴

clipboard
查看yum事務歷史:
history [info|list|packages-list|packages-info|summary|addon-info|redo|undo|rollback|new|sync|stats]

clipboard
安裝及升級本地程序包:
* localinstall rpmfile1 [rpmfile2] […]
(maintained for legacy reasons only – use install)
* localupdate rpmfile1 [rpmfile2] […]
(maintained for legacy reasons only – use update)

包組管理的相關命令:
* groupinstall group1 [group2] […]
* groupupdate group1 [group2] […]
* grouplist [hidden] [groupwildcard] […]
* groupremove group1 [group2] […]
* groupinfo group1 […]

2、自建yum倉庫,分別為網絡源和本地源
本地源
a.先將本地的光盤掛載到/mnt
mount -r /dev/cdrom /mnt

clipboard

本地鏡像已經創建好了yum倉庫數據

clipboard

b.編寫配置文件
[root@localhost /]# cat /etc/yum.repos.d/CentOS-Local.repo
[local]
name=local
baseurl=file:///mnt
gpgcheck=0
enabled=1

c.重新建立緩存
yum clean all
yum makecache

clipboard

網絡源
a.安裝ftp服務
yum install vsftpd
b.掛載光盤到/mnt目錄
mount -r /dev/cdrom /mnt

clipboard

c.復制鏡像光盤文件到/var/ftp/pub/Centos-yumrepo目錄下
cp -rv /mnt/* /var/ftp/pub/Centos-yumrepo/
d.編寫配置文件
[root@localhost ~]# cat /etc/yum.repos.d/CentOS-Ftp.repo
[ftp_repo]
name=ftp_repo
baseurl=ftp://192.168.253.157/pub/Centos-yumrepo/
gpgcheck=0
enabled=1

e.關閉防火墻與selinux
setenforce 0
systemctl stop firewalld
f.啟動ftp服務
systemctl start vsftpd
g.重建緩存
yum clean all
yum makecache
h.查看網絡源倉庫
yum repolist

clipboard

3、簡述at和crontab命令,制定 每周三凌晨三、五點10分執行某個腳本,輸出當前時間,時間格式為 2017-12-28 10:00:00

at命令:未來某個時間點執行一次某任務
格式:at [OPTION]… TIME
TIME:
HH:MM [YYYY-mm-dd]
noon,midnight, teatime
tomorrow
now+#
單位:minutes, hours, days, OR weeks
OPTION:
-l:查看作業隊列,相當于atq
-f /PATH/FROM/SOMEFILE:從指定文件中讀取作業任務,而不用再交互式輸入;
-d:刪除指定的作業,相當于atrm;
-c:查看指定作業的具體內容;
-q QUEUE:指明隊列;

注意:作業執行結果是以郵件發送給提交作業的用戶

crontab命令:周期執行任務
格式:crontab [-u user] [-l | -r | -e] [-i]
-e:編輯任務;
-l:列出所有任務;
-r:移除所有任務;即刪除/var/spool/cron/USERNAME文件;
-i:在使用-r選項移除所有任務時提示用戶確認;
-u user:root用戶可為指定用戶管理cron任務;

注意:運行結果以郵件通知給當前用戶;如果拒絕接收郵件:
(1) COMMAND > /dev/null
(2) COMMAND &> /dev/null
注意:定義COMMAND時,如果命令需要用到%,需要對其轉義;但放置于單引號中的%不用轉義亦可;

練習:
每周三凌晨三、五點10分執行某個腳本,輸出當前時間,時間格式為 2017-12-28 10:00:00
10 3,5 * * 3 /bin/bash /root/script/now_time.sh

[root@localhost ~]# cat /root/script/now_time.sh
#!/bin/bash
date “+%F %T ”

4、簡述sed常用操作命令,并舉例

sed [OPTION]...  'script'  [input-file] ...
    script:
        地址定界編輯命令
        
    常用選項:
        -n:不輸出模式空間中的內容至屏幕;
        -e script, --expression=script:多點編輯;
        -f  /PATH/TO/SED_SCRIPT_FILE
            每行一個編輯命令;
        -r, --regexp-extended:支持使用擴展正則表達式;
        -i[SUFFIX], --in-place[=SUFFIX]:直接編輯原文件 ;
    
        ~]# sed  -e  's@^#[[:space:]]*@@'   -e  '/^UUID/d'  /etc/fstab
    
    地址定界:
        (1) 空地址:對全文進行處理;
        (2) 單地址:
            #:指定行;
            /pattern/:被此模式所匹配到的每一行;
        (3) 地址范圍
            #,#:
            #,+#:
            #,/pat1/
            /pat1/,/pat2/
            $:最后一行;
        (4) 步進:~
            1~2:所有奇數行
            2~2:所有偶數行
            
    編輯命令:
        d:刪除;
        p:顯示模式空間中的內容;
        a  \text:在行后面追加文本“text”,支持使用\n實現多行追加; 
        i  \text:在行前面插入文本“text”,支持使用\n實現多行插入; 
        c  \text:把匹配到的行替換為此處指定的文本“text”;
        w /PATH/TO/SOMEFILE:保存模式空間匹配到的行至指定的文件中;
        r  /PATH/FROM/SOMEFILE:讀取指定文件的內容至當前文件被模式匹配到的行后面;文件合并;
        =:為模式匹配到的行打印行號;
        !:條件取反;
            地址定界!編輯命令;
        s///:查找替換,其分隔符可自行指定,常用的有s@@@, s###等;
            替換標記:
                g:全局替換;
                w /PATH/TO/SOMEFILE:將替換成功的結果保存至指定文件中;
                p:顯示替換成功的行;
            
            練習1:刪除/boot/grub/grub2.cfg文件中所有以空白字符開頭的行的行首的所有空白字符;
                ~]# sed  's@^[[:space:]]\+@@' /etc/grub2.cfg
            練習2:刪除/etc/fstab文件中所有以#開頭的行的行首的#號及#后面的所有空白字符;
                ~]# sed  's@^#[[:space:]]*@@'  /etc/fstab
            練習3:輸出一個絕對路徑給sed命令,取出其目錄,其行為類似于dirname;
                ~]# echo "/var/log/messages/" | sed 's@[^/]\+/\?$@@'
                ~]# echo "/var/log/messages" | sed -r 's@[^/]+/?$@@'
                
    高級編輯命令:
        h:把模式空間中的內容覆蓋至保持空間中;
        H:把模式空間中的內容追加至保持空間中;
        g:把保持空間中的內容覆蓋至模式空間中;
        G:把保持空間中的內容追加至模式空間中;
        x:把模式空間中的內容與保持空間中的內容互換;
        n:覆蓋讀取匹配到的行的下一行至模式空間中;
        N:追加讀取匹配到的行的下一行至模式空間中;
        d:刪除模式空間中的行;
        D:刪除多行模式空間中的所有行;
        
實例:
1.在命令行中定義編輯器命令 在命令行同時執行多個命令
[root@localhost sed]# cat data1
the lazy dog
the lazy fox
the lazy dog
the lazy dog
the lazy dog
[root@localhost sed]# sed -e ‘s/dog/cat/; s/lazy/e/’ data1
the e cat
the e fox
the e cat
the e cat
the e cat
2.從文件讀取編輯器命令
[root@localhost sed]# cat data1
the lazy dog
the lazy fox
the lazy dog
the lazy dog
the lazy dog
[root@localhost sed]# vim script1
[root@localhost sed]# sed -f script1 data1
the x xx
the x fox
the x xx
the x xx
the x xx
3.替換標記中的數字例子 這里只替換了第二次出現的test
[root@localhost sed]# sed ‘s.test.trial.2’ data2
this is a test of the trial script
this is the second test of the trial script
[root@localhost sed]# cat data2
this is a test of the test script
this is the second test of the test script
4.替換標記中的g實例 替換所有遇到的實例
[root@localhost sed]# sed ‘s/test/trial/g ‘ data2
this is a trial of the trial script
this is the second trial of the trial script
5.替換標記中的w實例 將完成的替換寫入文件中
[root@localhost sed]# sed ‘s/t/xxxxxx/gw test12’ data1
xxxxxxhis is a xxxxxxesxxxxxx of xxxxxxhe xxxxxxesxxxxxx scripxxxxxx
xxxxxxhis is xxxxxxhe second xxxxxxesxxxxxx of xxxxxxhe xxxxxxesxxxxxx scripxxxxxx
[root@localhost sed]# cat test12
xxxxxxhis is a xxxxxxesxxxxxx of xxxxxxhe xxxxxxesxxxxxx scripxxxxxx
xxxxxxhis is xxxxxxhe second xxxxxxesxxxxxx of xxxxxxhe xxxxxxesxxxxxx scripxxxxxx
[root@localhost sed]# cat data1
this is a test of the test script
this is the second test of the test script
6.定位地址 中 替換第一行的第二次出現test為xxxxxxx
[root@localhost sed]# sed ‘1s/test/xxxxxxx/2’ data1
this is a test of the xxxxxxx script
this is the second test of the test script
7.定位地址中的匹配定位 先找到hal用戶那一行再講home改為xxxxxx
[root@localhost sed]# sed -n ‘/hal/s/home/xxxxxx/p’ /etc/passwd
hal:x:500:500:hal:/xxxxxx/hal:/bin/bash
8.尋找到地址之后直接插入xxxxxxx
[root@localhost sed]# sed ‘2i\
xxxxxxxxx’ data1
this is a test of the test script
xxxxxxxxx
this is the second test of the test script
9.尋找到地址之后直接附加xxxxxxx
[root@localhost sed]# sed ‘1a\
> xxxxx’ data1
this is a test of the test script
xxxxx
this is the second test of the test script
10找到位置之后直接更改該行
[root@localhost sed]# sed ‘1c\
xxxxx’ data1
xxxxx
this is the second test of the test script
11.變換命令
[root@localhost sed]# echo “123456”| sed ‘y/123456/asdfgh/’
asdfgh
12打印命令中的p
[root@localhost sed]# sed -n ‘1p’ data1
this is a test of the test script
13.打印命令中的=
[root@localhost sed]# sed ‘=’ data1
1
this is a test of the test script
2
this is the second test of the test script
14.列出行命令小寫L
[root@localhost sed]# cat data2
this line contains tabs.
[root@localhost sed]# sed ‘l’ data2
this\tline\t contains\t\ttabs.$
this line contains tabs.
15在data1中插入data2數據
[root@localhost sed]# sed ‘1r data2’ data1
this is a test of the test script
this line contains tabs.
this is the second test of the test script
16、用輸入的文本數據替換地址標記
[root@localhost sed]# sed ‘1{
> r data2
> d
> }’ data1
this line contains tabs.
this is the second test of the test script
17.sed -n ‘n;p’ FILE:顯示偶數行;
18.sed ‘1!G;h;$!d’ FILE:逆序顯示文件的內容;
19.sed ’$!d’ FILE:取出最后一行;
20.sed ‘$!N;$!D’ FILE:取出文件后兩行;
21.sed ‘/^$/d;G’ FILE:刪除原有的所有空白行,而后為所有的非空白行后添加一個空白行;
22.sed ‘n;d’ FILE:顯示奇數行;
23.sed ‘G’ FILE:在原有的每行后方添加一個空白行;

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

(0)
halhal
上一篇 2018-07-16 15:36
下一篇 2018-07-16

相關推薦

欧美性久久久久