1、簡述rpm與yum命令的常見選項,并舉例
rpm命令
安裝:
rpm {-i|--install} [install-options] PACKAGE_FILE ...
常用選項:
rpm -ivh PACKAGE1 [PACKAGE2]...
-i,--install:安裝
-v:顯示詳細(xì)過程
-h:顯示進度條(用50個hash標(biāo)識,每個表示2%)
示例:
[root@lnmp ~]# rpm -ivh zsh-4.3.11-8.el6.centos.x86_64.rpm
warning: zsh-4.3.11-8.el6.centos.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:zsh-4.3.11-8.el6.centos ################################# [100%]
更新:
rpm {-U|--upgrade} [install-options] PACKAGE_FILE ...
rpm {-F|--freshen} [install-options] PACKAGE_FILE ...
兩者區(qū)別:-U安裝并更新,-F僅更新;即若包未安裝,則-F無法進行更新;
常用選項:
rpm -Uvh PACKAGE1 [PACKAGE2]...
示例:
[root@lnmp ~]# rpm -Uvh zsh-5.0.2-28.el7.x86_64.rpm
warning: zsh-5.0.2-28.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:zsh-5.0.2-28.el7 ################################# [ 50%]
Cleaning up / removing...
2:zsh-4.3.11-8.el6.centos ################################# [100%]
卸載:
rpm {-e|--erase} [--allmatches] [--justdb] [--nodeps] [--noscripts]
[--notriggers] [--test] PACKAGE_NAME ...
常用選項:
rpm -evh PACKAGE1 [PACKAGE2]...
示例:
[root@lnmp ~]# rpm -evh zsh wget
Preparing... ################################# [100%]
Cleaning up / removing...
1:wget-1.14-15.el7 ################################# [ 50%]
2:zsh-5.0.2-28.el7 ################################# [100%]
查詢:
rpm {-q|--query} [select-options] [query-options]
其中[select-options]可以指定的有:
[PACKAGE_NAME]:具體某個包
-a,--all:所有包
-f,--file:包相關(guān)文件
-p,--package PACKAGE_FILE:未安裝的包的相關(guān)查詢
--whatprovides CAPABILITY:提供哪些特性
--whatrequires CAPABILITY:依賴哪些特性
其中[query-options]可以指定的有:
--changelog:更新日志
-c,--configfiles:配置文件
-d,--docfiles:相關(guān)文檔
-i,--info:相關(guān)信息
-l,--list:列出包提供的所有文件
示例:
1、查詢當(dāng)前系統(tǒng)已安裝的包:
[root@lnmp ~]# rpm -qa
tuned-2.8.0-5.el7.noarch
setup-2.8.71-7.el7.noarch
qemu-guest-agent-2.8.0-2.el7.x86_64
bind-license-9.9.4-50.el7.noarch
biosdevname-0.7.2-2.el7.x86_64
glibc-common-2.17-196.el7.x86_64
man-db-2.6.3-9.el7.x86_64
...
2、查詢已安裝的rpm包提供了哪些配置文件:
[root@lnmp ~]# rpm -qc zsh
/etc/skel/.zshrc
/etc/zlogin
/etc/zlogout
/etc/zprofile
/etc/zshenv
/etc/zshrc
3、查詢某個未安裝的rpm包的相關(guān)信息
[root@lnmp ~]# rpm -qpi zsh-5.0.2-28.el7.x86_64.rpm
warning: zsh-5.0.2-28.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Name : zsh
Version : 5.0.2
Release : 28.el7
Architecture: x86_64
Install Date: (not installed)
Group : System Environment/Shells
Size : 5855982
License : MIT
Signature : RSA/SHA256, Thu 10 Aug 2017 04:28:17 PM EDT, Key ID 24c6a8a7f4a80eb5
Source RPM : zsh-5.0.2-28.el7.src.rpm
Build Date : Wed 02 Aug 2017 06:52:37 AM EDT
Build Host : c1bm.rdu2.centos.org
Relocations : (not relocatable)
Packager : CentOS BuildSystem <http://bugs.centos.org>
Vendor : CentOS
URL : http://zsh.sourceforge.net/
Summary : Powerful interactive shell
Description :
The zsh shell is a command interpreter usable as an interactive login
shell and as a shell script command processor. Zsh resembles the ksh
shell (the Korn shell), but includes many enhancements. Zsh supports
command line editing, built-in spelling correction, programmable
command completion, shell functions (with autoloading), a history
mechanism, and more.
yum命令
yum與rpm關(guān)系:yum是rpm的包管理前端工具,yum并不是獨立存在的,而是在rpm管理的基礎(chǔ)上更為優(yōu)化管理。
顯示倉庫列表:
repolist [ all | enabled | disabled ]
示例:
[root@lnmp ~]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id repo name status
development local 3,894
repolist: 3,894
顯示軟件包:
list
# yum list [all | glob_exp1] [glob_exp2] [...]
# yum list {available|updates|installed} [glob_exp1] [...]
示例:
[root@lnmp ~]# yum list http*
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Installed Packages
httpd.x86_64 2.4.6-67.el7.centos @development
httpd-tools.x86_64 2.4.6-67.el7.centos @development
Available Packages
http-parser.x86_64 2.7.1-1.el7 development
httpcomponents-client.noarch 4.2.5-5.el7_0 development
httpcomponents-core.noarch 4.2.4-6.el7 development
httpd-devel.x86_64 2.4.6-67.el7.centos development
httpd-manual.noarch 2.4.6-67.el7.centos development
安裝程序包:
install package1 [package2] [...]
reinstall package1 [package2] [...] (重新安裝)
示例:
[root@lnmp ~]# yum install -y httpd
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-67.el7.centos will be installed
--> Finished Dependency Resolution
Dependencies Resolved
======================================================================================================
Package Arch Version Repository Size
======================================================================================================
Installing:
httpd x86_64 2.4.6-67.el7.centos development 2.7 M
Transaction Summary
======================================================================================================
Install 1 Package
Total download size: 2.7 M
Installed size: 9.4 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : httpd-2.4.6-67.el7.centos.x86_64 1/1
Verifying : httpd-2.4.6-67.el7.centos.x86_64 1/1
Installed:
httpd.x86_64 0:2.4.6-67.el7.centos
Complete!
升級程序包:
update package1 [package2] [...]
downgrade package1 [package2] [...] (降級)
示例:
[root@localhost ~]# yum update zsh
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package zsh.x86_64 0:4.3.11-8.el6.centos will be updated
---> Package zsh.x86_64 0:5.0.2-14.el7 will be an update
--> Finished Dependency Resolution
Dependencies Resolved
======================================================================================================
Package Arch Version Repository Size
======================================================================================================
Updating:
zsh x86_64 5.0.2-14.el7 development 2.4 M
Transaction Summary
======================================================================================================
Upgrade 1 Package
Total download size: 2.4 M
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
Updating : zsh-5.0.2-14.el7.x86_64 1/2
Cleanup : zsh-4.3.11-8.el6.centos.x86_64 2/2
Verifying : zsh-5.0.2-14.el7.x86_64 1/2
Verifying : zsh-4.3.11-8.el6.centos.x86_64 2/2
Updated:
zsh.x86_64 0:5.0.2-14.el7
Complete!
檢查可用升級:
check-update
卸載程序包:
remove | erase package1 [package2] [...]
示例:
[root@localhost ~]# yum remove -y zsh
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package zsh.x86_64 0:5.0.2-14.el7 will be erased
--> Finished Dependency Resolution
Dependencies Resolved
======================================================================================================
Package Arch Version Repository Size
======================================================================================================
Removing:
zsh x86_64 5.0.2-14.el7 @development 5.6 M
Transaction Summary
======================================================================================================
Remove 1 Package
Installed size: 5.6 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Erasing : zsh-5.0.2-14.el7.x86_64 1/1
Verifying : zsh-5.0.2-14.el7.x86_64 1/1
Removed:
zsh.x86_64 0:5.0.2-14.el7
Complete!
查看程序包information:
info package1 [package2] [...]
示例:
[root@localhost ~]# yum info httpd
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Installed Packages
Name : httpd
Arch : x86_64
Version : 2.4.6
Release : 40.el7.centos
Size : 9.4 M
Repo : installed
From repo : development
Summary : Apache HTTP Server
URL : http://httpd.apache.org/
License : ASL 2.0
Description : The Apache HTTP Server is a powerful, efficient, and extensible
: web server.
查看指定的特性(可以是某文件)是由哪個程序包提供的:
provides | whatprovides features1 [features2] [...]
示例:
[root@localhost ~]# yum provides mysql
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
1:mariadb-5.5.44-2.el7.centos.x86_64 : A community developed branch of MySQL
Repo : development
Matched from:
Provides : mysql = 1:5.5.44-2.el7.centos
清理本地緩存:
clean [ packages | metadata | expire-cache | rpmdb | plugins | all ]
示例:
[root@localhost ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: development docker-ce-stable
Cleaning up everything
Cleaning up list of fastest mirrors
構(gòu)建緩存:
makecache
示例:
[root@localhost ~]# yum makecache
Loaded plugins: fastestmirror
development | 3.6 kB 00:00:00
docker-ce-stable | 2.9 kB 00:00:00
(1/7): development/group_gz | 155 kB 00:00:00
(2/7): development/filelists_db | 2.9 MB 00:00:00
(3/7): development/primary_db | 2.8 MB 00:00:00
(4/7): development/other_db | 1.2 MB 00:00:00
(5/7): docker-ce-stable/x86_64/primary_db | 16 kB 00:00:00
(6/7): docker-ce-stable/x86_64/other_db | 70 kB 00:00:00
(7/7): docker-ce-stable/x86_64/filelists_db | 9.1 kB 00:00:00
Determining fastest mirrors
Metadata Cache Created
搜索:
search string1 [string2] [...]
以指定關(guān)鍵字搜索程序包名及summary信息
示例:
[root@localhost ~]# yum search nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
========================================= N/S matched: nginx =========================================
collectd-nginx.x86_64 : Nginx plugin for collectd
munin-nginx.noarch : NGINX support for Munin resource monitoring
nextcloud-nginx.noarch : Nginx integration for NextCloud
nginx-all-modules.noarch : A meta package that installs all available Nginx modules
nginx-filesystem.noarch : The basic directory layout for the Nginx server
nginx-mod-http-geoip.x86_64 : Nginx HTTP geoip module
nginx-mod-http-image-filter.x86_64 : Nginx HTTP image filter module
nginx-mod-http-perl.x86_64 : Nginx HTTP perl module
nginx-mod-http-xslt-filter.x86_64 : Nginx XSLT module
nginx-mod-mail.x86_64 : Nginx mail modules
nginx-mod-stream.x86_64 : Nginx stream modules
owncloud-nginx.noarch : Nginx integration for ownCloud
pcp-pmda-nginx.x86_64 : Performance Co-Pilot (PCP) metrics for the Nginx Webserver
python2-certbot-nginx.noarch : The nginx plugin for certbot
nginx.x86_64 : A high performance web server and reverse proxy server
Name and summary matches only, use "search all" for everything.
查看指定包所依賴的capabilities
deplist package1 [package2] [...]
示例:
[root@localhost ~]# yum deplist bc
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
package: bc.x86_64 1.06.95-13.el7
dependency: /bin/sh
provider: bash.x86_64 4.2.46-19.el7
dependency: /sbin/install-info
provider: info.x86_64 5.1-4.el7
dependency: libc.so.6(GLIBC_2.14)(64bit)
provider: glibc.x86_64 2.17-105.el7
dependency: libncurses.so.5()(64bit)
provider: ncurses-libs.x86_64 5.9-13.20130511.el7
dependency: libreadline.so.6()(64bit)
provider: readline.x86_64 6.2-9.el7
dependency: libtinfo.so.5()(64bit)
provider: ncurses-libs.x86_64 5.9-13.20130511.el7
dependency: rtld(GNU_HASH)
provider: glibc.x86_64 2.17-105.el7
查看yum事務(wù)歷史:
history [info|list|packages-list|packages-info|summary|addon-info|redo|undo|roll‐back|new|sync|stats]
示例:
[root@localhost ~]# yum history
Loaded plugins: fastestmirror
ID | Login user | Date and time | Action(s) | Altered
-------------------------------------------------------------------------------
9 | root <root> | 2018-09-19 09:28 | Erase | 1
8 | root <root> | 2018-09-19 09:26 | Update | 1 <
7 | root <root> | 2018-09-18 08:40 | Install | 93 >
6 | root <root> | 2018-09-16 09:30 | Install | 1
5 | root <root> | 2018-09-16 08:35 | Install | 1
4 | root <root> | 2018-08-27 21:28 | Install | 2 <
3 | root <root> | 2018-08-27 21:27 | Install | 7 ><
2 | root <root> | 2018-08-26 21:49 | Install | 5 ><
1 | System <unset> | 2018-04-24 05:37 | Install | 297 >
history list
安裝及升級本地rpm包:
localinstall rpmfile1 [rpmfile2] [...]
localupdate rpmfile1 [rpmfile2] [...]
示例:
[root@localhost ~]# yum update zsh-5.0.2-28.el7.x86_64.rpm
Loaded plugins: fastestmirror
Examining zsh-5.0.2-28.el7.x86_64.rpm: zsh-5.0.2-28.el7.x86_64
Marking zsh-5.0.2-28.el7.x86_64.rpm as an update to zsh-4.3.11-8.el6.centos.x86_64
Resolving Dependencies
--> Running transaction check
---> Package zsh.x86_64 0:4.3.11-8.el6.centos will be updated
---> Package zsh.x86_64 0:5.0.2-28.el7 will be an update
--> Finished Dependency Resolution
Dependencies Resolved
======================================================================================================
Package Arch Version Repository Size
======================================================================================================
Updating:
zsh x86_64 5.0.2-28.el7 /zsh-5.0.2-28.el7.x86_64 5.6 M
Transaction Summary
======================================================================================================
Upgrade 1 Package
Total size: 5.6 M
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
Updating : zsh-5.0.2-28.el7.x86_64 1/2
Cleanup : zsh-4.3.11-8.el6.centos.x86_64 2/2
Verifying : zsh-5.0.2-28.el7.x86_64 1/2
Verifying : zsh-4.3.11-8.el6.centos.x86_64 2/2
Updated:
zsh.x86_64 0:5.0.2-28.el7
2、自建yum倉庫,分別為網(wǎng)絡(luò)源和本地源
示例:
本地源:本地光驅(qū)掛載的ISO鏡像文件
網(wǎng)絡(luò)源:阿里云的epel源:https://mirrors.aliyun.com/epel/7Server/x86_64/
[root@localhost ~]# mount -o loop -t iso9660 /dev/cdrom /mnt/iso
[root@localhost ~]# cat << EOF > /etc/yum.repos.d/DIY.repo
> [local]
> name=local iso repo
> baseurl=file:///mnt/iso
> enabled=1
> gpgcheck=0
> [epel]
> name=epel repo from internet
> baseurl=https://mirrors.aliyun.com/epel/7Server/x86_64/
> enabled=1
> gpgcheck=1
> gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-7Server
> EOF
[root@localhost ~]# yum repolist
Loaded plugins: fastestmirror
epel | 3.2 kB 00:00:00
local | 3.6 kB 00:00:00
(1/2): local/group_gz | 155 kB 00:00:00
(2/2): local/primary_db | 2.8 MB 00:00:00
Loading mirror speeds from cached hostfile
repo id repo name status
epel epel repo from internet 12,685
local local iso repo 3,723
repolist: 16,408
3、簡述at和crontab命令,制定 每周三凌晨三、五點10分執(zhí)行某個腳本,輸出當(dāng)前時間,時間格式為 2017-12-28 10:00:00
at命令
at命令用于未來某時間點執(zhí)行一次性任務(wù),執(zhí)行的結(jié)果以郵件的形式發(fā)送給提交任務(wù)的用戶
用法:
at [OPTION]... TIME
常用的[OPTION]:
-l:查看作業(yè)隊列,相當(dāng)于atq命令
-f /PATH/FROM/SOMEFILE:從指定文件中讀取作業(yè)任務(wù),而不用再交互式輸入;
-d:刪除指定的作業(yè),相當(dāng)于atrm;
-c:查看指定作業(yè)的具體內(nèi)容;
-q QUEUE:指明隊列;
TIME的格式:
HH:MM [YYYY-mm-dd]
noon,midnight,teatime
tomorrow
now+#——(#:minutes,hours,days,OR weeks)
crontab命令
crontab命令用于周期性執(zhí)行計劃任務(wù)
crontab命令:
crontab [-u user] [-l | -r | -e] [-i] [-s]
-e:編輯任務(wù);
-l:列出所有任務(wù);
-r:移除所有任務(wù);即刪除/var/spool/cron/USERNAME文件;
-i:在使用-r選項移除所有任務(wù)時提示用戶確認(rèn);
-u user:root用戶可為指定用戶管理cron任務(wù);
系統(tǒng)cron的配置格式:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
注意:
1、每一行定義一個周期性任務(wù),共7個字段(普通用于6個);
* * * * *:定義周期性時間
user-name:運行任務(wù)的用戶身份(普通用戶無此項)
command to be executed:任務(wù)
2、此處的環(huán)境變量不同于用戶登陸后獲得的環(huán)境,因此建議命令使用絕對路徑,或者自定義PATH環(huán)境變量;
3、執(zhí)行結(jié)果郵件發(fā)送給MAILTO指定的用戶
通常建議直接使用crontab -e進行編輯,不建議直接編輯配置文件;
練習(xí):
每周三凌晨三、五點10分執(zhí)行某個腳本,輸出當(dāng)前時間,時間格式為 2017-12-28 10:00:00
[root@localhost ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@localhost ~]# crontab -l
10 3,5 * * 3 root /usr/bin/date "+%Y-%m-%d %H:%M:%S"
4、簡述sed常用操作命令,并舉例
sed命令
sed:Steam EDitor,流編輯器,主要針對行
用法:
sed [OPTION]... 'script' [input-file]...
解析:
sed命令使用的主要構(gòu)成:sed [選項] '[地址定界][編輯命令]' [參數(shù)]
例:
sed -e '/^UUID/d' -e '1d' /etc/fstab
上述命令中:
-e為選項,用于多條件編輯;
'/^UUID/d'和'1d'中,/^UUID/和1為地址定界,意思分別為非UUID開頭的行和第一行;而d為編輯命令,意為刪除;
故以上命令的意思是:將/etc/fstab文件中非UUID開頭的行或第一行刪除后,輸出至標(biāo)準(zhǔn)輸出。
常用選項:
-n:不輸出模式空間中的內(nèi)容至屏幕;
-e script,--expression=script:多點編輯;(類似grep -e ... -e ...)
-f /PATH/TO/SED_SCRIPT_FILE
每行一個編輯命令
-r, --regexp-extended:支持使用擴展正則表達(dá)式;
-i[SUFFIX], --in-place[=SUFFIX]:直接編輯原文件;
地址定界:
1、空地址:對全文進行處理;
2、單地址:
#:指定行;
/pattern/:被此模式所匹配到的每一行;
3、地址范圍:
#,#:
#,+#:
#,/pat1/
/pat1/,/pat2/
$:最后一行
4、步進:~
1~2:所有奇數(shù)行
2~2:所有偶數(shù)行
編輯命令:
d:刪除
例:sed '1,5d' /etc/fstab
sed '/^UUID/d' /etc/fstab
p:顯示模式空間中的內(nèi)容
例:sed '1~2p' /etc/fstab(奇數(shù)行會顯示兩遍,因默認(rèn)會顯示模式空間中的內(nèi)容,被匹配的行又會顯示一次)
sed -n '1~2p' /etc/fstab(顯示奇數(shù)行)
a \text:在行后面追加文本“text”,支持使用\n實現(xiàn)多行追加;
i \text:在行前面插入文本“text”,支持使用\n實現(xiàn)多行插入;
c \text:把匹配到的行替換為此處指定的文本“text”;
w /PATH/TO/SOMEFILE:保存模式空間匹配到的行至指定的文件中;
r /PATH/FROM/SOMEFILE:讀取指定文件中的內(nèi)容至當(dāng)前文件被模式匹配到的行后面;(常用于實現(xiàn)文件合并)
=:為模式匹配到的行打印行號;
!:條件取反;使用位置如下:
地址定界!編輯命令;
s///:查找替換,其分隔符可自行指定,常用的有s@@@,s###等;
替換標(biāo)記:
g:全局替換;
w /PATH/TO/SOMEFILE:將替換成功的結(jié)果保存至指定文件中;
p:顯示替換成功的行;
常用示例:
1、sed -r 's@^[[:space:]]+@@' /etc/fstab
-r:使用擴展正則表達(dá)式
s@^[[:space:]]+@@:s@...@@部分為查找...替換為空(即刪除),^[[:space:]]+表示查找的內(nèi)容為以空白字符開頭的行
即,以上命令表示:刪除/etc/fstab中以空白字符開頭的行的行首空白字符
[root@localhost ~]# sed -r 's@^[[:space:]]+@@' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue Apr 24 05:37:33 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
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=d9a55bc5-6b72-4ddd-bb69-e00fc1eb909d /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
/dev/sr0 /mnt/iso iso9660 defaults,loop 0 0
2、sed 's/^#[[:space:]]*//' /etc/fstab
刪除/etc/fstab/文件中所有以#開頭的行的行首的#號及#后面的所有空白字符
[root@localhost ~]# sed 's/^#[[:space:]]*//' /etc/fstab
/etc/fstab
Created by anaconda on Tue Apr 24 05:37:33 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
/dev/mapper/centos-root / xfs defaults 0 0
UUID=d9a55bc5-6b72-4ddd-bb69-e00fc1eb909d /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
/dev/sr0 /mnt/iso iso9660 defaults,loop 0 0
3、輸出一個絕對路徑給sed命令,取出其目錄,其行為類似于dirname
兩種方式:
echo "/var/log/messages/" | sed 's@[^/]+/?$@@'
echo "/var/log/messages" | sed -r 's@[^/]+/?$@@'
[root@localhost ~]# echo "/var/log/messages/" | sed 's@[^/]\+/\?$@@'
/var/log/
[root@localhost ~]# echo "/var/log/messages" | sed -r 's@[^/]+/?$@@'
/var/log/