One liner to rename bunch of files

前端 未结 3 759
失恋的感觉
失恋的感觉 2021-01-02 04:32

I was looking for a linux command line one-liner to rename a bunch of files in one shot.

pattern1.a  pattern1.b pattern1.c ...

Once the com

3条回答
  •  耶瑟儿~
    2021-01-02 05:03

    Plenty of ways to skin this cat. If you'd prefer your pattern to be a regex rather than a fileglob, and you'd like to do the change recursively you could use something like this:

    find . -print | sed -ne '/^\.\/pattern1\(\..*\)/s//mv "&" "pattern2\1"/p'
    

    As Kerrek suggested with his answer, this one first shows you what it would do. Pipe the output through a shell (i.e. add | sh to the end) once you're comfortable with the commands.

    This works for me:

    [ghoti@pc ~]$ ls -l foo.*
    -rw-r--r--  1 ghoti  wheel  0 Mar 26 13:59 foo.php
    -rw-r--r--  1 ghoti  wheel  0 Mar 26 13:59 foo.txt
    [ghoti@pc ~]$ find . -print | sed -ne '/^\.\/foo\(\..*\)/s//mv "&" "bar\1"/p'
    mv "./foo.txt" "bar.txt"
    mv "./foo.php" "bar.php"
    [ghoti@pc ~]$ find . -print | sed -ne '/^\.\/foo\(\..*\)/s//mv "&" "bar\1"/p' | sh
    [ghoti@pc ~]$ ls -l foo.* bar.*
    ls: foo.*: No such file or directory
    -rw-r--r--  1 ghoti  wheel  0 Mar 26 13:59 bar.php
    -rw-r--r--  1 ghoti  wheel  0 Mar 26 13:59 bar.txt
    [ghoti@pc ~]$ 
    

提交回复
热议问题