Modifying replace string in xargs

前端 未结 8 1464
梦毁少年i
梦毁少年i 2021-01-30 01:43

When I am using xargs sometimes I do not need to explicitly use the replacing string:

find . -name \"*.txt\" | xargs rm -rf

In oth

8条回答
  •  误落风尘
    2021-01-30 02:00

    the files should be renamed/moved from .txt to /foo/.bar.txt

    You can use rename utility, e.g.:

    rename s/\.txt$/\.txt\.bar/g *.txt
    

    Hint: The subsitution syntax is similar to sed or vim.

    Then move the files to some target directory by using mv:

    mkdir /some/path
    mv *.bar /some/path
    

    To do rename files into subdirectories based on some part of their name, check for:

    -p/--mkpath/--make-dirs Create any non-existent directories in the target path.


    Testing:

    $ touch {1..5}.txt
    $ rename --dry-run "s/.txt$/.txt.bar/g" *.txt
    '1.txt' would be renamed to '1.txt.bar'
    '2.txt' would be renamed to '2.txt.bar'
    '3.txt' would be renamed to '3.txt.bar'
    '4.txt' would be renamed to '4.txt.bar'
    '5.txt' would be renamed to '5.txt.bar'
    

提交回复
热议问题