Linux cp with a regexp

后端 未结 3 889
孤街浪徒
孤街浪徒 2021-02-10 12:11

I would like to copy some files in a directory, renaming the files but conserving extension. Is this possible with a simple cp, using regex ?

For example :

3条回答
  •  遥遥无期
    2021-02-10 12:48

    The shell doesn't understand general regexes; you'll have to outsource to auxiliary programs for that. The classical scripty way to solve your task would be something like

    for a in myfile.* ; do
      b=`echo $a | sed 's!^myfile!mydir/newname!'`
      cp $a $b
    done
    

    Or have a perl script generate a list of commands that you then source into the shell.

提交回复
热议问题