删除部分文件名

时间:2015-03-06 19:51:02

标签: regex linux bash redhat file-rename

我有一堆像这样的文件:

file123.txt.452
file456.txt.098
file789.txt.078

如何从文件名中删除第二个点和末尾的数字?

我尝试使用rename,但我不认为我的正则表达式是正确的:

rename 's/txt.*/txt/' *

3 个答案:

答案 0 :(得分:5)

尝试rename 's/txt\..+/txt/' *

\.用于字面点,.+用于后面的任何内容。

命令depends on your system的实际名称。它可以是renamefile-renameperl-rename或其他内容。上面代码使用的rename命令在man条目中包含此内容:

NAME
       rename - renames multiple files

SYNOPSIS
       rename [-bfilnv] [-B prefix] [-S suffix] [-V method] [-Y prefix] [-z suffix]
       [--backup] [--basename-prefix=prefix] [--dry-run] [--force] [--help] [--no-stdin]
       [--interactive] [--just-print] [--link-only] [--prefix=prefix] [--suffix=suffix]
       [--verbose] [--version-control=method] [--version] perlexpr [files]...

DESCRIPTION
       rename renames the filenames supplied according to the rule specified as the first
       argument.  The argument is a Perl expression which is expected to modify the $_ string
       for at least some of the filenames specified.  If a given filename is not modified by
       the expression, it will not be renamed.  If no filenames are given on the command
       line, filenames will be read via standard input.

答案 1 :(得分:1)

类似的东西:

for f in *.txt.*; do
    g=${f%.txt.*}.txt
    mv $f $g
done

答案 2 :(得分:0)

试试这样的正则表达式:

string.replace("(.+\\.txt)(.+)", "$1")