Find the answer to your Linux question:
Results 1 to 9 of 9
Good day. I am attempting to use the tool 'rename "regex-rule" files ' I have a group of files zzzzfpa.00nnnnnnnnnnn where n is any number. So I would like to ...
  1. #1
    Just Joined!
    Join Date
    May 2008
    Posts
    10

    file rename

    Good day. I am attempting to use the tool 'rename "regex-rule" files '

    I have a group of files zzzzfpa.00nnnnnnnnnnn where n is any number. So I would like to get rid of the zzzz and leave the rest of the file name. Here is what I have tried rename -n 's/zzzz//' zzzzfpa*
    it returns nothing but if I do an ls zzzzfpa* it returns the thousands of files I have to rename.

    Thanks
    Allen

  2. #2
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    955
    Hint: you need to use the for command:
    Code:
    for name in zzzzfpa.nn* do
    (code to do what you want to each $name)
    done
    I leave it to you to work out the actual code. We don't solve homework problems here.
    "I'm just a little old lady; don't try to dazzle me with jargon!"

  3. #3
    Just Joined!
    Join Date
    May 2008
    Posts
    10
    Thanks actually the for loop is how I did solve the problem. But I am attempting to learn how to use the rename function. I thought that maybe the fpa. with a dot being a special character is what is causing me headaches but I have done what I know with regex to work around it and so far have not figured it out. GRR

    for f in zzzz*; do mv $f ${f/zzzz/}; done

    But I would think it would be a simple thing to do with rename such does not seem to be the case.

    The examples I have seen only refer to replacing the last portion of the file. IE rename "s/ *//g" *.mp3
    woould remove all of the spaces out of a .mp3 file name. So, I must have some learning to do here on regex's

    Thanks
    Allen

  4. #4
    Just Joined!
    Join Date
    May 2008
    Posts
    10
    hmm this could be a redhat bug, or it could be that rename does not really take a regex except from perl?

    rename zzzzfpa. fpa. zzzzfpa*

    works fine bit if I try to use the -n -v or -f switches or a regex it does not work at all

    Thanks
    Allen

  5. #5
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    955
    I don't think you can use regular expressions directly on the command line. The rules for filename "globbing" are different. You would need to use a utility that understands regexps. You might be able to do it by echoing the filenames through sed and back-quoting the result. Have a look at the man page for sed. It's a very powerful utility.
    "I'm just a little old lady; don't try to dazzle me with jargon!"

  6. #6
    Linux Guru
    Join Date
    Oct 2007
    Location
    Tucson AZ
    Posts
    1,939
    But I would think it would be a simple thing to do with rename such does not seem to be the case.
    This worked for me in a test directory and seems like a pretty simple use of rename:

    rename zzzz "" zzzzfpa*

  7. #7
    drl
    drl is online now
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    My take on this is that there is more than one rename command. If you are on:
    Code:
    OS, ker|rel, machine: Linux, 2.6.18-164.9.1.el5, i686
    Distribution        : CentOS release 5.4 (Final)
    then you would have access to:
    Code:
    SYNOPSIS
           rename from to file...
    
    DESCRIPTION
           rename  will  rename  the specified files by replacing the first occur-
           rence of from in their name by to.
    (CentOS is essentially RedHat without the support contract.)

    However, on a distribution of Linux such as:
    Code:
    OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
    Distribution        : Debian GNU/Linux 5.0
    you would have access to:
    Code:
    mv (1)               - move (rename) files
    prename (1)          - renames multiple files
    rename (1)           - renames multiple files
    rename.ul (1)        - Rename files
    mmv (1)              - move/copy/append/link multiple files by wildcard patterns
    
    mostly from man -k rename
    The rename and prename there seem to be the same, whereas rename.ul is:
    Code:
    SYNOPSIS
           rename from to file...
           rename -V
    
    DESCRIPTION
           rename  will  rename  the specified files by replacing the first occur-
           rence of from in their name by to.
    which corresponds to the CentOS rename.

    The utility that understands perl regexes on Debian is:
    Code:
    SYNOPSIS
           rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]
    
    DESCRIPTION
           "rename" renames the filenames supplied according to the rule specified
           as the first argument.  The perlexpr argument is a Perl expression ...
    I think Hazel was trying to guide you to use mv in a loop, which is basic method of renaming files. After that, the discussion seemed to be diverge into different utilities.

    Most Linux distributions are the same, but differences occur, so it is best to specify what you are using, and to look at man pages on that distribution.

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  8. #8
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    955
    Well, thank you drl! On this forum you start off by trying to help someone and end up getting helped yourself. I'd never heard of rename before this; I just looked at the man page for it and it's a wonderful way of doing global edits on file names.
    "I'm just a little old lady; don't try to dazzle me with jargon!"

  9. #9
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Code:
    for file in zzzzfpa.*
    do
      echo mv "$file" "${file#zzzz}"
    done

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...