【问题标题】:how to change names of all directories / files containing a specific string on linux如何在linux上更改包含特定字符串的所有目录/文件的名称
【发布时间】:2011-06-02 18:01:15
【问题描述】:

我有一个目录,我想将其下的所有目录名称和文件更改为不同的名称。例如我的目录结构是

./mydir_ABC/
./mydir_ABC/myfile_ABC.txt
./mydir_ABC/otherdir_ABC/

例如,我想做到这一点

./mydir_DEF/
./mydir_DEF/myfile_DEF.txt
./mydir_DEF/otherdir_DEF/

我正在使用

find . -type 'f' -name '*ABC*'
find . -type 'd' -name '*ABC*'

获取名称中包含我的字符串的所有相关目录和文件的列表。如何将它传递给另一个实际更改目录名和文件名的命令? (如果文件或目录已经存在,我希望它能够覆盖。)

【问题讨论】:

    标签: linux shell unix pipe


    【解决方案1】:

    查找rename(1) 命令。它有一些不同的变体,但它们都支持基于某种正则表达式的某种重命名。我使用的版本(基于 Perl 'Camel Book' 第一版的代码)将用作:

    rename 's%ABC%DEF%g' ...
    

    或者,例如:

    find . -print0 | xargs -0 rename s/ABC/DEF/g
    

    或者,为了避免在遍历目录结构时路径名发生变化的问题,请先输入目录名,然后再输入文件(如问题所示):

    find . -type d -print0 | xargs -0 rename s/ABC/DEF/g
    find . -type f -print0 | xargs -0 rename s/ABC/DEF/g
    

    在进行时重命名目录可能仍然存在问题(因为一旦将 ./ABC 重命名为 ./DEF,您就不能再将 ./ABC/subABC/ 重命名为 ./DEF/subDEF,因为该目录现在是 ./DEF/subABC) .

    rename 的其他版本具有不同的语法 - 请查看您的手册页。


    这是基于 Perl 的 rename 版本,源自 Perl 'Camel Book' 的第一版;我在版本控制下的第一个版本的日期为 1992-01-05。今天的更改更新了 shebang 行(使用 env,删除了-w,因为它不可靠,并添加了use warnings; 以弥补没有-w)。之前的变化是 2008 年、1998 年、1996 年和 1992 年;在过去的 19 年里,它并没有太大变化。

    #!/usr/bin/env perl
    #
    # @(#)$Id: rename.pl,v 1.8 2011/06/03 22:30:22 jleffler Exp $
    #
    # Rename files using a Perl substitute or transliterate command
    
    use strict;
    use warnings;
    use Getopt::Std;
    
    my(%opts);
    my($usage) = "Usage: $0 [-fnxV] perlexpr [filenames]\n";
    my($force) = 0;
    my($noexc) = 0;
    my($trace) = 0;
    
    die $usage unless getopts('fnxV', \%opts);
    
    if ($opts{V})
    {
        printf "%s\n", q'RENAME Version $Revision: 1.8 $ ($Date: 2011/06/03 22:30:22 $)';
        exit 0;
    }
    $force = 1 if ($opts{f});
    $noexc = 1 if ($opts{n});
    $trace = 1 if ($opts{x});
    
    my($op) = shift;
    die $usage unless defined $op;
    
    if (!@ARGV) {
        @ARGV = <STDIN>;
        chop(@ARGV);
    }
    
    for (@ARGV)
    {
        if (-e $_ || -l $_)
        {
            my($was) = $_;
            eval $op;
            die $@ if $@;
            next if ($was eq $_);
            if ($force == 0 && -f $_)
            {
                print STDERR "rename failed: $was - $_ exists\n";
            }
            else
            {
                print "+ $was --> $_\n" if $trace;
                print STDERR "rename failed: $was - $!\n"
                    unless ($noexc || rename($was, $_));
            }
        }
        else
        {
            print STDERR "$_ - $!\n";
        }
    }
    

    【讨论】:

    • 这特别没有用,但我会进一步研究它,谢谢。它还可以重命名目录吗?
    • @Michael - 我忘了在 'find' 版本上加上 'g' 后缀 - 抱歉。是的,它可以重命名目录 - 但您最好先重命名目录,然后再重命名文件。
    • 我不确定我们使用的是相同的重命名命令。我的重命名手册页就像linux.die.net/man/1/rename 上的那个。语法看起来不同,我猜它的功能不那么强大。我认为您的 rename(1) 没有随我的发行版 (RHEL5) 一起提供。我查看了 rename(1) 的 ubuntu 手册页及其不同之处。更像你的建议。
    • @michael - 我确定了我的版本来自哪里;您可能是对的,“标准”版本功能较弱。我会将代码添加到此答案中。 (我更改它以修复 shebang 行;当文件“编译”时,我的旧编码标准强制将路径名更改为 Perl。)
    【解决方案2】:
    mkdir z; cd z; touch fooabcbar; touch fooABCbar
    find . -type 'f' \( -name '*ABC*' -o -name '*abc*' \) | while read f; do g=`echo "$f" | sed -e 's/abc/def/g' -e 's/ABC/DEF/g'`; echo mv -- "$f" "$g"; done
    

    只要路径名不包含换行符(非常罕见)。

    【讨论】:

    • bash 有文本替换,所以不需要 sed:g=${f//ABC/DEF}
    • @Seth 它给了我while: Expression Syntax.
    • @michael:你一定有一个复制粘贴错误。我刚刚测试了这个并且它有效。我会用测试用例更新答案。
    • @seth 对不起,我不能让它工作,我已经检查过了,没有复制粘贴问题。你用的是什么发行版/外壳?
    • @michael:我在 linux 系统上使用 bash 4.2.10(2),但这是我自 SunOS 3 以来所做的纯 POSIX shell 东西。也许你应该在你输入的会话中粘贴这个例子放到你的 shell 中,这样我们就可以看到发生了什么。您还应该包括的另一个测试是 echo foo | while read f; do echo zoo$f; done
    【解决方案3】:

    如果它总是最后 3 个字符,我认为这应该足够好:

    mv *abc.* *def.*
    

    【讨论】:

    • 不限于最后3个字符,可以是文件/目录名中的任何位置。
    猜你喜欢
    • 2014-12-23
    • 2012-12-05
    • 2020-04-14
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 2022-07-19
    • 1970-01-01
    • 2021-11-28
    相关资源
    最近更新 更多