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 ...
随机推荐
- Java日期时间API系列23-----Jdk8中java.time包中的新的日期时间API类,获取准确开始时间00:00:00,获取准确结束时间23:59:59等
有时候,往往需要统计某个时间区间的销量等问题,这就需要准确的起始时间,获取准确开始时间00:00:00,获取准确结束时间23:59:59.下面增加了一一些方法,获取当天起始时间,昨天起始时间,当前月第 ...
- Oracle官方自动推荐大内存页脚本hugepages.sh
#!/bin/bash # # hugepages_settings.sh # # Linux bash script to compute values for the # recommended ...
- 自学PHP笔记(二) PHP数据类型
本文转发来自:https://blog.csdn.net/KH_FC/article/details/115415323 PHP数据类型可支持以下8种类型: 类型 说明 string 字符串 bool ...
- for-each循环陷阱
for-each删除元素报错 public static void main(String[] args) { List<String> list = new ArrayList<& ...
- Spark Web UI 监控详解
Spark集群环境配置 我们有2个节点,每个节点是一个worker,每个worker上启动一个Executor,其中Driver也跑在master上.每个Executor可使用的核数为2,可用的内存为 ...
- Node.js 构建命令行工具:实现 ls 命令的 -a 和 -l 选项
在日常的前端开发中,我们常常借助各种基于 Node.js 的脚手架工具来加速项目搭建和维护,比如 create-react-app 可以一键初始化一个 React 项目,eslint 则帮助我们保持代 ...
- git rebase -i的时候用的不是 vi 编辑器是 nano编辑器不会用
今天给同事 rebase 代码 发现 git fetch && git rebase -i origin/develop 的时候 出现了 那个 nano 编辑器的界面 不会用,和vim ...
- Linux中tar文件压缩与解压
文件压缩与解压缩 一般什么情况下使用文件压缩? 备份数据,数据传输 节省磁盘空间 减少带宽使用 减少负载 减少IO操作 什么情况下进行压缩比较合适? 错过业务高峰期,由于文件的压缩会瞬间加大cpu的负 ...
- bootstrap table 搜索只从当前页开始搜
项目中出现的情况,使用bootstrap table框架,使用搜索功能的是后查询的结果不是从第一也开始,有时候点击搜索第一次查不出来结果,点击第二次结果才出现. 解决方法: $("#btn_ ...
- 从PipedInputStream/PipedOutputStream谈起
本篇主要从分析PipeInputStrem和PipedOutputStream谈起.谈及软件设计的变化,以及如何将软件拆分.组合,适配-- 1 源代码分析 下面将详细分析PipedInputStrea ...