shell 获取 目录名 当前目录名
Four ways to extract the current directory name
When you're programming a shell script, you often only need the current directory name, not the whole path that the pwd
command returns. Here are four ways you can extract only the current directory.
Using basename
Using the basename
command is the easiest and simplest way to extract the current directory:
basename /usr/local/bin
bin
However, it isn't useful in a shell script with changing directory variables. You can combine it with pwd
inside backticks to make it more dynamic:
cd /usr/local/bin
basename `pwd`
bin
Using parameter substitution with echo
The bash scripting language is full of nice tricks, including parameter substitution, which allows you to manipulate or expand variables. You can use parameter substitution with the${var##pattern}
syntax, which removes from $var
the longest part of $Pattern
that matches the front end of $var
. Take a look at an example:
cd /var/log/squid
echo ${PWD##*/}
squid
PWD
is the environment variable that holds the current path, and ## is the instruction that tells the script to remove everything it finds up to */. In other words, it removes everything until the last /, leaving only the last string, which here is the current directory, squid
. You can learn more about parameter substitution and other ways to manipulate variables in the Advanced Bash-Scripting Guide.
Using awk and rev
A more elaborate solution uses a combination of awk
(a pattern-scanning utility) and rev
(a utility that reverses lines from a file or from stdin):
cd /usr/share/cups/data
pwd | rev | awk –F \/ '{print $1}' | rev
data
It's a lot easier to understand this kind of script step by step:
pwd
/usr/share/cups/data
pwd | rev
atad/supc/erahs/rsu/
pwd | rev | awk –F \/ '{print $1}'
atad
pwd | rev | awk –F \/ '{print $1}' | rev
data
The -F
option indicates that you should separate by fields, where the field delimiter is /, and that you should print field 1.
Using sed
Finally, you can parse pwd
output in the stream editor sed
using an elaborate regular expression. This approach may be educational, but it's not practical:
cd /home/smith/music
pwd | sed 's,^\(.*/\)\?\([^/]*\),\2,'
music
For a better understanding of how this works, remove the escape character (\), which is required for special characters such as "(":
sed 's,^(.*/)?([^/]*),\2,'
s
substitutes one string for another. It looks for two patterns, which are indicated between the first comma and the second comma. The first pattern (^(.*/)?
) searches from the beginning of the line (^) until the last occurrence that it finds of / (in the example, it matches /home/smith/
). The second pattern (([^/]*)
) searches everything from the last pattern except the / character , which is indicated by [^/]*
, where ^ at the beginning of the square brackets means not. This results in both /home/smith/
and music
. The second part of this regular expression is the substitution, indicated by \2. In sed, this is called a back reference. As its name implies, it goes back and recalls a previously used reference. There may be nine of these, named \1, \2, \3, and so on. In the example, \2 refers to the second pattern found, which is music
-- the result expected.
As you can see, Linux gives you many ways to find a directory name. Having many choices for the same chore is one of its strengths.
Sergio Gonzalez Duran is a Linux administrator, systems developer, and network security counselor who also teaches Linux courses and publishes the Spanish-oriented Linux and open source Web site linuxtotal.com.mx.
shell 获取 目录名 当前目录名的更多相关文章
- shell 提取文件名和目录名
转自http://blog.csdn.net/universe_hao/article/details/52640321 shell 提取文件名和目录名 在写shell脚本中,经常会有需要对路径和文件 ...
- Bash Shell 获取进程 PID
转载地址:http://weyo.me/pages/techs/linux-get-pid/ 导读 Linux 的交互式 Shell 与 Shell 脚本存在一定的差异,主要是由于后者存在一个独立的运 ...
- 用C++和shell获取本机CPU、网卡IO、内存、磁盘等的基本信息
用C++和shell获取本机CPU.网卡.内存.磁盘等的基本信息: 由于对C++相关的函数没多少了解,但是觉得用shell反而相对简单一些: 一.shell脚本,用来辅助C++获取主机的资源使用信息 ...
- linux中用shell获取昨天、明天或多天前的日期
linux中用shell获取昨天.明天或多天前的日期 时间 -- :: BlogJava-专家区 原文 http://www.blogjava.net/xzclog/archive/2015/12/0 ...
- linux中用shell获取时间,日期
linux中用shell获取昨天.明天或多天前的日期:在Linux中对man date -d 参数说的比较模糊,以下举例进一步说明:# -d, --date=STRING display time d ...
- shell 获取网关 以及修改ip 启用网卡
shell 获取网关 以及修改ip 启用网卡 #!/bin/bash #autho freefei #script is a init computer eth #data 2014 10 09 19 ...
- python执行shell获取硬件参数写入mysql
最近要获取服务器各种参数,包括cpu.内存.磁盘.型号等信息.试用了Hyperic HQ.Nagios和Snmp,它们功能都挺强大的,但是于需求不是太符,亦或者太heavy. 于是乎想到用python ...
- Shell获取格式化日期
Shell获取格式化日期 shell date 获取昨天日期 使用date -d 选项: date +"%Y%m%d" -d "+n days" 今天的后n天日 ...
- Shell获取文件的文件名和扩展名的例子
这篇文章主要介绍了Shell获取文件的文件名和扩展名的例子,简明版的代码实例,看了就懂,需要的朋友可以参考下 basename example.tar.gz .tar.gz # => examp ...
- linux命令(26):Bash Shell 获取进程 PID
转载地址:http://weyo.me/pages/techs/linux-get-pid/ 根据pid,kill该进程:http://www.cnblogs.com/lovychen/p/54113 ...
随机推荐
- PC软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
前言 国庆假期各种活动比较多,直到上班才有时间来更新文章~ 不过这两天我还是做了个小玩意(Clipify),起因是想给之前开发来自己用的简单视频剪辑工具 QuickCutSharp 加个功能,不过这个 ...
- Oracle、MySQL等数据库故障处理优质文章分享 | 10月汇总
墨天轮社区于9月起持续举办[聊聊故障处理那些事儿]DBA专题征文活动,每月进行评优发奖,鼓励大家记录工作中遇到的数据库故障处理过程,不仅用于自我复盘与分析,同时也能帮助其他的同仁们避坑. 上月为大家梳 ...
- vue前端开发仿钉图系列(5)右侧编辑页面的开发详解
右侧编辑页面主要有两个入口,一是添加marker或者线面双击结束的时候,新建数据信息:二是点击底部数据的单元行或者查看编辑或者点击地图上的marker以及线面,编辑相关数据.整理总结不易,如需全部代码 ...
- k8s 中的 Gateway API 的背景和简介【k8s 系列之四】
〇.Gateway API 的背景 第一阶段:Service 初始的 Kubernetes 内部服务向外暴露,使用的是自身的 LoadBlancer 和 NodePort 类型的 Service. 在 ...
- css超出部分...显示
首先需要设置宽 white-space: nowrap;// 文字不换行 overflow: hidden;// 超出隐藏 text-overflow: ellipsis; 实现移入后正常显示的可 ...
- Java后端请求想接收多个对象入参的数据方法
在Java后端开发中,如果我们希望接收多个对象作为HTTP请求的入参,可以使用Spring Boot框架来简化这一过程.Spring Boot提供了强大的RESTful API支持,能够方便地处理各种 ...
- 2023NOIP A层联测25 T2 游戏
2023NOIP A层联测25 T2 游戏 优秀且新颖的期望题. 思路 分析问题,由于双方都是最优策略,所以可以说学生知道老师会选择那些教室设置概率(概率设置好就不能改变),老师也知道学生会怎样选择教 ...
- 基于Ubuntu搭建Pwn调试环境
Pwn环境配置 本文演示使用干净的Vmware下安装的的 Ubuntu 18.04 LTS镜像 配置以下Pwn环境: OS(系统)配置 VMware Tools net-tools open-vm-t ...
- Nuxt.js 应用中的 webpack:error 事件钩子
title: Nuxt.js 应用中的 webpack:error 事件钩子 date: 2024/11/25 updated: 2024/11/25 author: cmdragon excerpt ...
- (Redis基础教程之十三) 如何从命令行更改Redis的配置
介绍 Redis是一个开源的内存中键值数据存储.Redis有几个命令,可让您即时更改Redis服务器的配置设置.本教程将介绍其中一些命令,并说明如何使这些配置更改永久生效. 如何使用本指南 本指南以备 ...