如何删除文件名preFIX与外壳的Posix [英] How to remove filename prefix with an Posix shell

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

问题描述

我如何可以删除的文件名preFIX Bash中如下面的例子:

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

XY TD-11212239.pdf

获得

11212239.pdf

即,删除 XY TD - ?

推荐答案

您提供的信息很少,但假设你是在bash这样做,并有其preFIX需要被删除的文件列表,你可以通过管道列表 SED

You have given very little information, but assuming you are doing this in bash, and have a list of files whose prefix needs to be removed, you can pipe the list through sed:

例如:

./generate_your_list_of_files | sed -e 's/^prefix//'

或者如果你的文件列表是分隔空间:

or if your list of files are space separated:

echo TD-file1 TD-file2.x.yt TD-file3-beep | sed -e 's/\<TD-//g'

第一个匹配的行的开头preFIX和删除它。第二个匹配 TD - (或者你想替换任何其他preFIX),只有当它发生在一个单词的开头,并在所有的比赛替换它的所有行。这的可能的危险拿到然而,例如像 TD-文件\\的文件\\ TD-包含\\ space.txt 变成文件\\ \\ space.txt

The first one matches prefix in the beginning of the line and removes it. The second one matches TD- (or any other prefix you want to substitute) only when it happens at the beginning of a word and replaces it in all the matches in all the lines. This could get dangerous though, for example a file like TD-file\ that\ TD-contains\ space.txt becomes file\ that\ contains\ space.txt

作为一个方面说明,不使用 LS 让你的文件列表。 这是一个可怕的错误。如果你需要得到的文件列表继续努力,指的是多单的地方,我建议将它们放在一个数组:

As a side note, don't get your list of files using ls. That's a horrible mistake. If you need to get a list of files to work with and refer to in more than a single place, I'd suggest putting them in an array:

files=(*)

,然后用这个数组工作。

and then work with this array.

由于大众请求(以下单注释),这里是如何命名的目录下,以 TD开头的文件 - 这样的preFIX是删除(感谢@tripleee):

Due to popular requests (the single comment below), here is how to rename all files in the directory that start with TD- so that the prefix is removed (Thanks to @tripleee):

for file in prefix*;
do
    mv "$file" "${file#XY TD-}"
done

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

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