我在子文件夹中有很多文件。它们看起来都像in.*.radiate,其中*只是一个数字。我需要将不同子文件夹中的这些相似文件名重命名为常量名称,但将它们保留在各自的文件夹中。
有没有办法用一条命令将它们全部重命名为in.radiate?
在linux或MATLAB中?
Perl脚本
#!/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");
}
}发布于 2015-04-26 23:57:15
bash解决方案:
for file in $(find dir -iname "*.radiate"); do
mv $file ${file/.*././}
done要查找所有.radiate文件:
$ 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循环:
$ 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发布于 2015-04-24 06:02:32
这在MATLAB中是可能的。movefile函数允许您将文件移动到它所在的同一文件夹中,并为其指定一个新名称。您需要生成包括路径在内的所有文件的列表,然后遍历每个文件。类似于下面的内容..
%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”。
https://stackoverflow.com/questions/29834864
复制相似问题