linux - Renaming multiple files at once with a pattern on Ubuntu

06
2014-04
  • Xseba360

    I have around 300 files named

    some_name_123456789.zip
    another-name2_987654321.zip
    something(1)_123454321.zip
    [2]something_987656789.zip
    

    I need to rename them all to

    ds_123456789.zip
    ds_987654321.zip
    ds_123454321.zip
    ds_987656789.zip
    

    How can i do this?

  • Answers
  • Scott

    You can do this with the rename command line utility. To do what you want you need a simple regular expression:

    rename "s/.+_/ds/g" files

    .+ represents everything up to (in this context) the last underscore (_) character (so this works with multiple underscores, as mentioned in your first example). This requires that there be at least one character before the underscore; if you might have file names like _20131012.zip, use .* instead. So this three-character string (.+_ or .*_) will match everything up to and including the last underscore in the filename. s/old/new/ means substitute the new string (ds) for the old string. The g means global and might not be necessary in this case.

  • Lloyd

    or, using the cross-platform renamer:

    $ renamer --regex --find '.+_' --replace 'ds' *
    

  • Related Question

    windows xp - Renaming long file name with illegal characters
  • Quintin Par

    Possible Duplicate:
    How to force Windows XP to rename a file with a special character?

    I have a 2GB file in windows which has a long file name FileTransfer.dll?Cmd=1&MN=1619353607&Dir=1&Mode=0&Off=0&TS=FA596160-1BFB-4113-9E10-B196243A73F3&CVN=5,0,0,32
    I am not sure how it got the filename that way(perhaps from the download manager)

    Now when I try to rename it, it says

    ---------------------------
    Error Renaming File or Folder
    ---------------------------
    Cannot rename file: Select only one file to rename, or use MS-DOS wildcards (for example, *.txt) to rename a group of files with similar names.
    ---------------------------
    OK   
    ---------------------------
    

    How do I rename this file? I cannot make use of this file in any other way(like loading).
    I am on windows XP home(NTFS) and I haven't used linux till now

    Edit :
    dir /X gives me

    E:\Downloads>dir /X
     Volume in drive E is x
     Volume Serial Number is c
    
     Directory of E:\Downloads
    
    02/04/2010  05:44 PM    <DIR>                       .
    02/04/2010  05:44 PM    <DIR>                       ..
    01/27/2010  09:12 PM            49,745              1.l
    01/28/2010  12:09 AM     2,501,894,144              FileTransfer.dll?Cmd=1&MN=16
    19353607&Dir=1&Mode=0&Off=0&TS=FA596160-1BFB-4113-9E10-B196243A73F3&CVN=5,0,0,32
    
    01/28/2010  12:09 AM         3,138,664              wget-log.1
    10/19/2009  02:46 AM        43,137,416              zapSetup_91_008_000_en.exe
                   4 File(s)  2,548,219,969 bytes
                   2 Dir(s)  80,069,509,120 bytes free
    

  • Related Answers
  • boot13

    I recently discovered that invalid filenames in Windows can be manipulated using this syntax:

    \\.\C:\somedir\filename
    

    This can be used to delete or rename the offending file, as in these examples:

    DEL \\.\C:\test\LPT1
    
    REN \\.\C:\test\LPT1 file.txt
    

    Testing shows that this syntax also works if the first part of the path is "\\?\" instead of "\\.\".

  • jtreser

    Find the short name of the file by using DIR /X, you should then be use REN to rename the short name to a new name.

  • quack quixote

    In many cases, the simplest way to do this is to quote the filename. [*]

    rename "long-file-name-here" new-short-file-name
    

    The quotes protect the wonky characters (? and & and others) from being interpreted by the shell as special characters. (In the Windows CMD shell, ? is a single-character wildcard. The quotes tell CMD not to interpret it that way.)

    Sometimes this doesn't work for other reasons, and you'll need to resort to one of the other techniques mentioned. But this one is the one to try first.

    [*] Unix shells would prefer single-quotes ('foo') instead of double-quotes ("foo") for complete protection from shell interpretation. The CMD shell seems to prefer double-quotes.

  • David Spillett

    You could try moving all the other files out of the directory then from a console window do

    e:
    cd e:\Downloads
    rename * <newname>
    

    Hopefully the rename command's wildcard expansion will not baulk at the unexpected characters. You may even be able to try this without moving the other files out with a slightly more refined rename command:

    rename FileTran* <newname>
    

    (if you are unfamiliar with command line console windows: one can be started by running cmd via the run command on the XP's start menu, and when you are done give then exit command)