重命名和移动Bash或Perl中的文件 [英] Renaming and Moving Files in Bash or Perl

查看:54
本文介绍了重命名和移动Bash或Perl中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Bash和StackOverflow的新手.

I'm completely new to Bash and StackOverflow.

我需要将一组文件(所有文件都包含在同一文件夹中)移动到目标文件夹中,该文件夹中可能已经存在相同名称的文件.

I need to move a set of files (all contained in the same folder) to a target folder where files with the same name could already exist.

如果存在特定文件,我需要在移动文件之前重命名该文件,例如,在文件名后附加一个增量整数.

In case a specific file exists, I need to rename the file before moving it, by appending for example an incremental integer to the file name.

应保留扩展名(换句话说,附加的增量整数应在扩展名之前).文件名中间可能包含点.

The extensions should be preserved (in other words, that appended incremental integer should go before the extension). The file names could contain dots in the middle.

最初,我正在考虑比较两个文件夹以获取现有文件的列表(我使用"comm"进行了此操作),但是后来我有点卡住了.我想我只是想以最复杂的方式做事.

Originally, I was thinking about comparing the two folders to have a list of the existing files (I did this with "comm"), but then I got a bit stuck. I think I'm just trying to do things in the most complicated possible way.

有人暗示要以打击方式"做到这一点吗?如果用bash脚本以外的脚本来完成也可以.

Any hint to do this in the "bash way"? It's OK if it is done in a script other than bash script.

推荐答案

根据OP,这可以是Perl,而不仅仅是bash.我们走了

As per OP, this can be Perl, not just bash. Here we go

新解决方案:(注意扩展性)

~/junk/a1$ ls
f1.txt   f2.txt   f3.txt   z1       z2


~/junk/a1$ ls ../a2
f1.txt     f2.1.txt   f2.2.txt   f2.3.txt   f2.txt     z1

# I split the one-liner into multiple lines for readability
$ perl5.8 -e 
     '{use strict; use warnings; use File::Copy; use File::Basename; 
       my @files = glob("*"); # assume current directory
       foreach my $file (@files) {
           my $file_base2 = basename($file); 
           my ($file_base, $ext) = ($file_base2 =~ /(.+?)([.][^.]+$)?$/);
           my $new_file_base = "../a2/$file_base";
           my $new_file = $new_file_base . $ext; 
           my $counter = 1;
           while (-e $new_file) { 
               $new_file = "$new_file_base." . $counter++ . $ext;
           }
           copy($file, $new_file)
               || die "could not copy $file to $new_file: $!\n";
        } }'

~/junk/a1> ls ../a2
f1.1.txt f1.txt  f2.1.txt  f2.2.txt  f2.3.txt  f2.4.txt  f2.txt  f3.txt
z1         z1.1       z2

旧解决方案:(不关注扩展名)

~/junk/a1$ ls
f1   f2   f3

~/junk/a1$ ls ../a2
f1     f2     f2.1   f2.2   f2.3

# I split the one-liner into multiple lines for readability
$ perl5.8 -e 
     '{use strict; use warnings; use File::Copy; use File::Basename; 
       my @files = glob("*"); # assume current directory
       foreach my $file (@files) {
           my $file_base = basename($file); 
           my $new_file_base = "../a2/$file_base"; 
           my $new_file = $new_file_base; 
           my $counter = 1;
           while (-e $new_file) { $new_file = "$new_file_base." . $counter++; }
           copy($file,$new_file)
               || die "could not copy $file to $new_file: $!\n";
        } }'

~/junk/a1> ls ../a2
f1     f1.1   f2     f2.1   f2.2   f2.3   f2.4   f3

这篇关于重命名和移动Bash或Perl中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆