如何使用Posix Shell删除文件名前缀 [英] How to remove filename prefix with a Posix shell

查看:148
本文介绍了如何使用Posix Shell删除文件名前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Bash中删除文件名前缀,如以下示例所示:

How can I remove the filename prefix in Bash as in the following example:

XY TD-11212239.pdf

获得

11212239.pdf

即删除XY TD-?

推荐答案

您说过 POSIX shell,其中包括BASH,Kornshell,Ash,Zsh和Dash.幸运的是,所有这些shell都对变量值进行了模式过滤.

You said POSIX shells which would include BASH, Kornshell, Ash, Zsh, and Dash. Fortunately, all of these shells do pattern filtering on variable values.

在Unix/Linux命令行上使用*之类的文件指定文件时使用的模式是:

Patterns are what you use when you specify files with things like * on the Unix/Linux command line:

$ ls *.sh  # Lists all files with a `.sh` suffix

这些POSIX外壳使用四种不同的模式过滤:

These POSIX shells use four different pattern filtering:

  • ${var#pattern}-从左侧删除与模式匹配的最小字符串.
  • ${var##pattern}-从左侧删除与模式匹配的最大字符串.
  • ${var%pattern}-从右侧删除与模式匹配的最小字符串.
  • ${var%%pattern}-从右侧删除与模式匹配的最大字符串.
  • ${var#pattern} - Removes smallest string from the left side that matches the pattern.
  • ${var##pattern} - Removes the largest string from the left side that matches the pattern.
  • ${var%pattern} - Removes the smallest string from the right side that matches the pattern.
  • ${var%%pattern} - Removes the largest string from the right side that matches the pattern.

以下是一些示例:

foo="foo-bar-foobar"
echo ${foo#*-}   # echoes 'bar-foobar'  (Removes 'foo-' because that matches '*-')
echo ${foo##*-}  # echoes 'foobar' (Removes 'foo-bar-')
echo ${foo%-*}   # echoes 'foo-bar'
echo ${foo%%-*}  # echoes 'foo'

您并没有真正解释您想要的东西,也没有提供任何代码示例,因此很难提出可以满足您需求的东西.但是,使用模式过滤,您可能可以准确地弄清楚要使用的文件名.

You didn't really explain what you want, and you didn't include any code example, so it's hard to come up with something that will do what you want. However, using pattern filtering, you can probably figure out exactly what you want to do with your file names.

file_name="XY TD-11212239.pdf"
mv "$file_name" "${file_name#*-}" # Removes everything from up to the first dash

这篇关于如何使用Posix Shell删除文件名前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆