首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将具有相似文件名的文件重命名为相同文件名但不同的文件夹

将具有相似文件名的文件重命名为相同文件名但不同的文件夹
EN

Stack Overflow用户
提问于 2015-04-24 05:43:22
回答 2查看 540关注 0票数 0

我在子文件夹中有很多文件。它们看起来都像in.*.radiate,其中*只是一个数字。我需要将不同子文件夹中的这些相似文件名重命名为常量名称,但将它们保留在各自的文件夹中。

有没有办法用一条命令将它们全部重命名为in.radiate?

在linux或MATLAB中?

Perl脚本

代码语言:javascript
复制
#!/usr/bin/perl
use warnings;
use strict;

my $newname = 'in.radiate'; # If you want a different filename, edit this line

foreach my $folder (glob("*"))
{
    # Ensure it's a folder
    if (-d $folder)
    {
        print "Processing $folder\n";
        system("mv $folder/*.radiate $folder/$newname");
    }
}
EN

回答 2

Stack Overflow用户

发布于 2015-04-26 23:57:15

bash解决方案:

代码语言:javascript
复制
for file in $(find dir -iname "*.radiate"); do
mv $file ${file/.*././}
done

要查找所有.radiate文件:

代码语言:javascript
复制
$ find dir -iname "*.radiate"
dir-root/1/1/in.1.radiate
dir-root/1/10/in.10.radiate
dir-root/1/100/in.100.radiate
dir-root/1/101/in.101.radiate
dir-root/1/102/in.102.radiate
...

在更换所有.*之后。有了。使用上面的for循环:

代码语言:javascript
复制
$ find dir -iname "*.radiate"
dir-root/1/1/in.radiate
dir-root/1/10/in.radiate
dir-root/1/100/in.radiate
dir-root/1/101/in.radiate
dir-root/1/102/in.radiate
票数 1
EN

Stack Overflow用户

发布于 2015-04-24 06:02:32

这在MATLAB中是可能的。movefile函数允许您将文件移动到它所在的同一文件夹中,并为其指定一个新名称。您需要生成包括路径在内的所有文件的列表,然后遍历每个文件。类似于下面的内容..

代码语言:javascript
复制
    %found_files represents the results of the search function i will 
    %mention below.
    new_name= 'in.radiate';

    for pp = 1 : length(found_files)

        %create string of path with new file name
        renamed_file = strcat(path, new_name); 

        %move file command. 
        movefile(found_files(pp).name, renamed_file);

    end

为了获得所有文件的完整列表,我将在matlab exchange上使用类似下面的函数。它将根据一个特定的过滤器生成一个包含所有文件的结构,对你来说,这个过滤器可以是“radiate”。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29834864

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档