Find the answer to your Linux question:
Results 1 to 5 of 5
Hi I need to write a bash script that will go through all subdirectories and replace the filename index.fld with index.bad I have an email server that requires you to ...
  1. #1
    Just Joined!
    Join Date
    Oct 2007
    Posts
    1

    Recursive BASH Rename script

    Hi

    I need to write a bash script that will go through all subdirectories and replace the filename index.fld with index.bad

    I have an email server that requires you to do this if I need to rebuild a mailbox. There is an index.fld file every folder that a user creates in their mail account, so it becoming a pain to rename them all.


    I have looked around google and have found bits of script that when i put them together dont work.

    I think i can search the files using the find commands but I cant seem to actually rename the file properly.

    Any ideas would be greatly appreciated.

  2. #2
    Linux Engineer
    Join Date
    Nov 2004
    Location
    Ft. Polk, LA
    Posts
    796

  3. #3
    Linux Enthusiast flipjargendy's Avatar
    Join Date
    Aug 2004
    Location
    Xanadu, Titan, Saturn
    Posts
    610

    Just a suggestion.

    I know how to change multiple files within one directory but not subdirectories. In the following case the script would be renaming the files named index.fld to index.fld.bad. It might help you.

    You can type the following in the command line or put it in an executable text file:

    Code:
    for k in $(ls directory/index.fld); do mv $k $k.bad; done;
    If you could add a for statement that would ad a /* each time the script was completed you could make it start from directory/index.fld and go to directory/*/*/index.fld... as far as you needed.
    Running Linux Since 2001®
    Registered Linux User #430868 - Since 9•12•06

  4. #4
    Just Joined!
    Join Date
    Dec 2007
    Posts
    1
    Quote Originally Posted by MrTubz View Post
    Hi

    I need to write a bash script that will go through all subdirectories and replace the filename index.fld with index.bad
    You can use the find command for this:

    find dir -name index.fld -exec mv {} `basename {}`/index.bad \;

  5. #5
    Just Joined!
    Join Date
    Jun 2010
    Posts
    4

    Lightbulb

    for complex renaming schemes or large trees, you may like Fixnames. it is a GPL bash script that does recursive renaming of directories and files ; it includes a progress inidcator, full logging, simple detection of duplicate files, alternate filename, and a --pretend option.

    it also includes a sed line to make all names in a tree be valid for windows (the script was developped for a osx->win migration that involved renaming over 12k files)

    you can get the script here: david . lutolf . net / dt /ulb / #fixnames

Posting Permissions

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