Rename batch of file in UNIX

后端 未结 2 448
无人及你
无人及你 2021-01-14 08:36

I have a set of file as follow :

  • oldname_1.txt
  • oldname_1.pdf
  • oldname_1.bak

etc.. with a different extension for each, I need

2条回答
  •  一生所求
    2021-01-14 09:41

    First, you should really specify what "doesn't work" actually means. Does it do nothing? Does it rename them incorrectly? Does it deliver electrical pulses to certain private parts of your anatomy? :-)

    In any case, I'd start with:

    rename -v 's/^oldname/newname/' oldname_*.*
    

    There's no need to include the wildcard stuff in the actual expression, especially since it means something different to what you think (xyz_* means xyz followed by zero or more _ characters, it does not mean xyz_ followed by anything, and .* is just asking for trouble since that means zero or more of any character).

    The filtering of the filenames is done by the final argument. Since you know that only files matching that argument will be renamed, you can just tailor your regular expression to change the first bit.


    In addition, make sure you have the right rename. There's a different rename available to Linux that has a different syntax:

    rename oldname newname oldname_*.*
    

    In those systems, the regex variant is often called prename.


    Lastly, and forgive what may be a silly question, are you sure that the files are of the name you expect them to be? A simple ls -al oldname* should show this.


    As an aside, this works fine under my Debian box:

    pax> touch oldname_1.txt oldname_1.pdf oldname_1.bak
    
    pax> ll *name*
    -rw-r--r-- 1 pax pax 0 Sep  6 10:56 oldname_1.bak
    -rw-r--r-- 1 pax pax 0 Sep  6 10:56 oldname_1.pdf
    -rw-r--r-- 1 pax pax 0 Sep  6 10:56 oldname_1.txt
    
    pax> rename -v 's/^oldname/newname/' oldname_*.*
    oldname_1.bak renamed as newname_1.bak
    oldname_1.pdf renamed as newname_1.pdf
    oldname_1.txt renamed as newname_1.txt
    
    pax> ll *name*
    -rw-r--r-- 1 pax pax 0 Sep  6 10:56 newname_1.bak
    -rw-r--r-- 1 pax pax 0 Sep  6 10:56 newname_1.pdf
    -rw-r--r-- 1 pax pax 0 Sep  6 10:56 newname_1.txt
    

提交回复
热议问题