How to rename with prefix/suffix?

前端 未结 9 1517
独厮守ぢ
独厮守ぢ 2020-11-27 10:52

How do I do mv original.filename new.original.filename without retyping the original filename?

I would imagine being able to do something like mv

9条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 11:28

    In my case I have a group of files which needs to be renamed before I can work with them. Each file has its own role in group and has its own pattern.

    As result I have a list of rename commands like this:

    f=`ls *canctn[0-9]*`   ;  mv $f  CNLC.$f
    f=`ls *acustb[0-9]*`   ;  mv $f  CATB.$f
    f=`ls *accusgtb[0-9]*` ;  mv $f  CATB.$f
    f=`ls *acus[0-9]*`     ;  mv $f  CAUS.$f
    

    Try this also :

    f=MyFileName; mv $f {pref1,pref2}$f{suf1,suf2}
    

    This will produce all combinations with prefixes and suffixes:

    pref1.MyFileName.suf1
    ...
    pref2.MyFileName.suf2
    

    Another way to solve same problem is to create mapping array and add corespondent prefix for each file type as shown below:

    #!/bin/bash
    unset masks
    typeset -A masks
    masks[ip[0-9]]=ip
    masks[iaf_usg[0-9]]=ip_usg
    masks[ipusg[0-9]]=ip_usg
    ...
    for fileMask in ${!masks[*]}; 
    do  
    registryEntry="${masks[$fileMask]}";
    fileName=*${fileMask}*
    [ -e ${fileName} ] &&  mv ${fileName} ${registryEntry}.${fileName}  
    done
    

提交回复
热议问题