Shell技能树---字符串目录操作命令

  • 功能介绍

    目录是字符串的特殊应用。这里主要介绍目录相关的字符串处理命令。

  • 命令介绍

1. dirname

Usage: dirname [OPTION] NAME...
Output each NAME with its last non-slash component and trailing slashes
removed
; if NAME contains no /'s, output '.' (meaning the current directory).
标红的部分不是很好理解,从例子看大概是将路径最后一部分删除和其前面的/删除,输出剩余部分。
如果路径中没有/,输出. 
注意目录只有一个开始的/,/不被删除。例如/usr
  -z, --zero     end each output line with NUL, not newline
Examples:
  dirname /usr/bin/          -> "/usr"
  dirname dir1/str dir2/str  -> "dir1" followed by "dir2"
  dirname stdio.h            -> "."

renhl@verygood:~/usb$ dirname /usr/bin/ #去除左后的/bin/
/usr
renhl@verygood:~/usb$ dirname dir1/str dir2/str #支持多个目录输入,分别去除左后的/str
dir1
dir2
renhl@verygood:~/usb$ dirname stdio.h # 不包含/时,返回. 表示当前目录
.
renhl@verygood:~/usb$ dirname /usr # 一般都会去除最后一部分的和其前面的/, 例如 /usr/bin/,去除/bin/。如果只有根目录,不去除前面的/。
# 或者理解为绝对路径,全部去除,返回/ 表示根目录
/
renhl@verygood:~/usb$

     

2. basename

Usage: basename NAME [SUFFIX]
  or:  basename OPTION... NAME...
Print NAME with any leading directory components removed.
If specified, also remove a trailing SUFFIX.
打印名字,同时将前面的目录组件全部删除。
如果指定后缀,删除尾部的后缀

Mandatory arguments to long options are mandatory for short options too.
  -a, --multiple       support multiple arguments and treat each as a NAME
  -s, --suffix=SUFFIX  remove a trailing SUFFIX; implies -a
  -z, --zero           end each output line with NUL, not newline
      --help     display this help and exit
      --version  output version information and exit

Examples:
  basename /usr/bin/sort          -> "sort"
  basename include/stdio.h .h     -> "stdio"
  basename -s .h include/stdio.h  -> "stdio"
  basename -a any/str1 any/str2   -> "str1" followed by "str2"

renhl@verygood:~/usb$ basename /usr/bin/sort  
sort
renhl@verygood:~/usb$ basename include/stdio.h .h #指定后缀.h 同时删除后缀和前面的目录部分
stdio
renhl@verygood:~/usb$ basename -s .h include/stdio.h  #-s指定后缀.h 同时删除后缀和前面的目录部分
stdio 
renhl@verygood:~/usb$ basename -a any/str1 any/str2 #-a 多个NAME依次处理
str1
str2
renhl@verygood:~/usb$

3. realpath

Usage: realpath [OPTION]... FILE...
Print the resolved absolute file name;
all but the last component must exist
打印文件名的绝对路径
除了最后的文件名,其余的部分需要存在的。

  -e, --canonicalize-existing  all components of the path must exist
  -m, --canonicalize-missing   no path components need exist or be a directory
  -L, --logical                resolve '..' components before symlinks
  -P, --physical               resolve symlinks as encountered (default)
  -q, --quiet                  suppress most error messages
      --relative-to=DIR        print the resolved path relative to DIR
      --relative-base=DIR      print absolute paths unless paths below DIR

  -s, --strip, --no-symlinks   don't expand symlinks
  -z, --zero                   end each output line with NUL, not newline 

renhl@verygood:~$ ls -l
total 0
lrwxrwxrwx 1 renhl renhl 13 Mar 30 09:27 usb -> /mnt/f/share/
renhl@verygood:~$ realpath usb #解析symlinks
/mnt/f/share
renhl@verygood:~$ pwd
/home/renhl
renhl@verygood:~$ realpath -s usb #-s 不扩展symlinks,不解析symlinks
/home/renhl/usb
renhl@verygood:~$ realpath 1.txt #1.txt文件是不存在,realpath只是将当前路径和1.txt进行连接输出
/home/renhl/1.txt
renhl@verygood:~$

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值