删除部分文件名

问题描述 投票:2回答:3

我有一堆像这样的文件:

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

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

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

rename 's/txt.*/txt/' *
regex linux bash redhat file-rename
3个回答
1
投票

就像是:

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

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.

0
投票

试试像这样的正则表达式:

string.replace("(.+\\.txt)(.+)", "$1")
© www.soinside.com 2019 - 2024. All rights reserved.