Results 1 to 9 of 9

Thread: Mass search and replace file renaming

  1. #1
    Join Date
    Dec 2009
    Beans
    Hidden!

    Mass search and replace file renaming

    I've used fetchmail to download every message on my gMail account to ~/mail. When I run rsync on ~, I run into errors because a lot of the mail downloaded is has a '.' in its name.

    ~/mail contains [Gmail].All Mail, [Gmail].Bin, [Gmail].Drafts,[Gmail].Sent Mail, [Gmail].Spam, [Gmail].Starred and INBOX. In each of these directories are 3 folders: cur, new and tmp which each store text file version of every email.

    I want to replace the '.' in file names with a '!' - how do I do this?

    In vim, I understand
    Code:
    :% s/:/\?/g
    and I can do for loops in bash.

    TIA,
    xtheunknown0

  2. #2
    Join Date
    Jul 2005
    Location
    England
    Beans
    Hidden!

    Re: Mass search and replace file renaming

    Code:
    find ~/mail -name '*' -exec rename -n 's/\./!/g' "{}" +
    should do it for all files in the ~/mail folder irrespective of any file suffix they may or may not have, but back up everything first, just in case I've got it wrong.

    The one thing I'm not sure about is the need for the backslash after the s/ as the command I have used this for is to replace a space with an underscore. I think, therefore that the \ is merely an escape for the space, and may not be needed if you are replacing a .
    DISTRO: Xubuntu 14.04-64bit --- Code-tags --- Boot-Repair --- Grub2 wiki & Grub2 Basics --- RootSudo

  3. #3
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Mass search and replace file renaming

    usually in regular expressions '.' is a symbol for 'any character', therefore you need to escape it to treat it literally as a plain dot.

  4. #4
    Join Date
    Jul 2005
    Location
    England
    Beans
    Hidden!

    Re: Mass search and replace file renaming

    Thanks Vaphell, I wasn't sure.

    How about the rest of the command?
    DISTRO: Xubuntu 14.04-64bit --- Code-tags --- Boot-Repair --- Grub2 wiki & Grub2 Basics --- RootSudo

  5. #5
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Mass search and replace file renaming

    i have no experience with rename, but that expression is identical to these used in sed and it looks legit (replaces dots with !).
    -name * doesn't make too much sense (it doesn't narrow down anything), find matches everything without it either way, also i'd narrow down to files only (-type f)
    Last edited by Vaphell; January 24th, 2011 at 01:28 PM.

  6. #6
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Mass search and replace file renaming

    If you want to rename directories too, then you need -depth (process each directory's contents before the directory itself).

  7. #7
    Join Date
    Dec 2009
    Beans
    Hidden!

    Re: Mass search and replace file renaming

    Quote Originally Posted by ajgreeny View Post
    Code:
    find ~/mail -name '*' -exec rename -n 's/\./!/g' "{}" +
    <snip>
    I had a read of the man page for find (and found parts of the discussion too technical). What are the last two bits

    Code:
    "{}" +
    for?

    I'm surprised that I was inconsistent in the OP. The vim regex searched for colons but I requested to replace '.'s. Thanks for a working command (and -name '*' is *not* needed).

    TIA,
    xtheunknown0

  8. #8
    Join Date
    Dec 2010
    Beans
    572
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Mass search and replace file renaming

    The "{}" in the -exec portion of a find command substitutes the path of the file that found.

    If you are looking at only renaming the files with a '.' in name I would change the
    Code:
    -name '*'
    to be
    Code:
    -name '*.*'

  9. #9
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Mass search and replace file renaming

    -exec command {} ; runs the command separately on each file found:
    comannd file1
    comamnd file2
    command file3
    ...

    -exec command {} + collects the filenames into groups or sets, and runs the command once per set:

    command file1 file2 file3 file4 ... file999
    command file1000 file1001 ...

Tags for this Thread

Bookmarks

Posting Permissions

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