移除文件名的一部分

3

我有一堆像这样的文件:

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

如何从文件名中删除第二个点和末尾的数字?
我尝试使用“rename”命令,但是我的正则表达式可能不正确:
rename 's/txt.*/txt/' *
3个回答

6

尝试使用rename 's/txt\..+/txt/' *命令。

\.代表字面上的点,.+代表其后的任何字符。

实际命令名称取决于您的系统。它可能是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
顺便说一句,多亏了这个问题,我今天学会了rename命令的存在和用法。这对我也可能很有用... - Sebastian Simon
我尝试了那个正则表达式,但它也没有起作用。也许我的语法有误。 - user2189312
很奇怪,我按照你的示例使用了完全相同的文件名(“file123.txt.456”),然后运行了相应的命令,结果完美地运行成功了... - Sebastian Simon
也许我的机器上没有正确安装Perl。Rename是Perl包的一部分。 - user2189312

1

类似于:

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

-1
尝试使用这样的正则表达式:
string.replace("(.+\\.txt)(.+)", "$1")

1
这是哪种编程语言?请确保它也可以从终端运行。 - actual_kangaroo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接