Chapter 5——Linux程序包管理和sed命令

1、简述rpm与yum命令的常见选项,并举例

rpm命令

安装:

rpm {-i|--install} [install-options] PACKAGE_FILE ...
常用选项:
rpm -ivh PACKAGE1 [PACKAGE2]...
-i,--install:安装
-v:显示详细过程
-h:显示进度条(用50个hash标识,每个表示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 ...
两者区别:-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:包相关文件
-p,--package PACKAGE_FILE:未安装的包的相关查询
--whatprovides CAPABILITY:提供哪些特性
--whatrequires CAPABILITY:依赖哪些特性
其中[query-options]可以指定的有:
--changelog:更新日志
-c,--configfiles:配置文件
-d,--docfiles:相关文档
-i,--info:相关信息
-l,--list:列出包提供的所有文件
示例:
1、查询当前系统已安装的包:

[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包的相关信息

[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关系:yum是rpm的包管理前端工具,yum并不是独立存在的,而是在rpm管理的基础上更为优化管理。

显示仓库列表:

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
构建缓存:

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] [...]
以指定关键字搜索程序包名及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事务历史:

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仓库,分别为网络源和本地源

示例:
本地源:本地光驱挂载的ISO镜像文件
网络源:阿里云的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分执行某个脚本,输出当前时间,时间格式为 2017-12-28 10:00:00

at命令

at命令用于未来某时间点执行一次性任务,执行的结果以邮件的形式发送给提交任务的用户
用法:
at [OPTION]... TIME
常用的[OPTION]:
-l:查看作业队列,相当于atq命令
-f /PATH/FROM/SOMEFILE:从指定文件中读取作业任务,而不用再交互式输入;
-d:删除指定的作业,相当于atrm;
-c:查看指定作业的具体内容;
-q QUEUE:指明队列;
TIME的格式:
HH:MM [YYYY-mm-dd]
noon,midnight,teatime
tomorrow
now+#——(#:minutes,hours,days,OR weeks)

crontab命令

crontab命令用于周期性执行计划任务
crontab命令:
crontab [-u user] [-l | -r | -e] [-i] [-s]
-e:编辑任务;
-l:列出所有任务;
-r:移除所有任务;即删除/var/spool/cron/USERNAME文件;
-i:在使用-r选项移除所有任务时提示用户确认;
-u user:root用户可为指定用户管理cron任务;
系统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、每一行定义一个周期性任务,共7个字段(普通用于6个);
* * * * *:定义周期性时间
user-name:运行任务的用户身份(普通用户无此项)
command to be executed:任务
2、此处的环境变量不同于用户登陆后获得的环境,因此建议命令使用绝对路径,或者自定义PATH环境变量;
3、执行结果邮件发送给MAILTO指定的用户
通常建议直接使用crontab -e进行编辑,不建议直接编辑配置文件;

练习:
每周三凌晨三、五点10分执行某个脚本,输出当前时间,时间格式为 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命令使用的主要构成:sed [选项] '[地址定界][编辑命令]' [参数]
例:
sed -e '/^UUID/d' -e '1d' /etc/fstab
上述命令中:
-e为选项,用于多条件编辑;
'/^UUID/d'和'1d'中,/^UUID/和1为地址定界,意思分别为非UUID开头的行和第一行;而d为编辑命令,意为删除;
故以上命令的意思是:将/etc/fstab文件中非UUID开头的行或第一行删除后,输出至标准输出。

常用选项:

-n:不输出模式空间中的内容至屏幕;
-e script,--expression=script:多点编辑;(类似grep -e ... -e ...)
-f /PATH/TO/SED_SCRIPT_FILE
    每行一个编辑命令
-r, --regexp-extended:支持使用扩展正则表达式;
-i[SUFFIX], --in-place[=SUFFIX]:直接编辑原文件;

地址定界:

1、空地址:对全文进行处理;
2、单地址:
    #:指定行;
    /pattern/:被此模式所匹配到的每一行;
3、地址范围:
    #,#:
    #,+#:
    #,/pat1/
    /pat1/,/pat2/
    $:最后一行
4、步进:~
    1~2:所有奇数行
    2~2:所有偶数行

编辑命令:

d:删除
    例:sed '1,5d' /etc/fstab
        sed '/^UUID/d' /etc/fstab
p:显示模式空间中的内容
    例:sed '1~2p' /etc/fstab(奇数行会显示两遍,因默认会显示模式空间中的内容,被匹配的行又会显示一次)
        sed -n '1~2p' /etc/fstab(显示奇数行)
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、sed -r 's@^[[:space:]]+@@' /etc/fstab
-r:使用扩展正则表达式
s@^[[:space:]]+@@:s@...@@部分为查找...替换为空(即删除),^[[:space:]]+表示查找的内容为以空白字符开头的行
即,以上命令表示:删除/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/
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,636评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,890评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,680评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,766评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,665评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,045评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,515评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,182评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,334评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,274评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,319评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,002评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,599评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,675评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,917评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,309评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,885评论 2 341

推荐阅读更多精彩内容