Removing spaces from a batch of filenames - Questions

This is a discussion on Removing spaces from a batch of filenames - Questions ; I am using the Almquist Shell (sh). What is the most simple way to strip spaces from a batch of files in a directory. For example: "your file 1.txt" should be renamed "yourfile1.txt". There are a couple of thousand files ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: Removing spaces from a batch of filenames

  1. Removing spaces from a batch of filenames

    I am using the Almquist Shell (sh).

    What is the most simple way to strip spaces from a batch of files in a
    directory.

    For example:

    "your file 1.txt" should be renamed "yourfile1.txt".

    There are a couple of thousand files that need renaming.

    I have found a perl script, but I want to do this without invoking perl or
    bash.

    I also would like a modified version of the same script that removes underscores.

    --
    Mark Hobley
    393 Quinton Road West
    QUINTON
    Birmingham
    B32 1QE

    Telephone: (0121) 247 1596
    International: 0044 121 247 1596

    Email: markhobley at hotpop dot donottypethisbit com

    http://markhobley.yi.org/


  2. Re: Removing spaces from a batch of filenames

    On 2005-10-31, Mark Hobley wrote:
    > I am using the Almquist Shell (sh).


    I assume that is the shell usually called ash and that it is a
    POSIX shell.

    > What is the most simple way to strip spaces from a batch of files in a
    > directory.
    >
    > For example:
    >
    > "your file 1.txt" should be renamed "yourfile1.txt".
    >
    > There are a couple of thousand files that need renaming.
    >
    > I have found a perl script, but I want to do this without invoking perl or
    > bash.
    >
    > I also would like a modified version of the same script that removes underscores.


    From Chapter 3 of my book, "Shell Scripting Recipes", this
    function substitutes all instances of a pattern with a
    replacement string. If the replacement string is empty, it has
    the effect of deleting the pattern:

    _gsub()
    {
    ## clear variable in case of failure
    _GSUB=

    ## assign the string to sr2
    sr2=$1

    ## return an error if there is no pattern specified
    [ -z "$2" ] && return 1

    ## assign the search pattern to s_srch
    s_srch=${2}

    ## assign the replacement text, if any, to rep
    rep=$3

    ## sr1 will hold the portion of the string that has been processed
    sr1=

    ## loop until sr2 no longer contains the search pattern
    while :
    do
    case $sr2 in
    *$s_srch*)

    ## add the string up to the match,
    ## and the replacement text, to sr1
    sr1=$sr1${sr2%%$s_srch*}$rep

    ## remove up to the match from sr2
    sr2=${sr2#*$s_srch}
    ;;
    *) break ;;
    esac
    done
    _GSUB=$sr1$sr2
    }


    The bash/ksh93 version of that function is:

    _gsub() {
    _GSUB=${1//$2/$3}
    }


    To remove all the spaces and rename the file:

    file="your file 1.txt"
    _gsub "$file" " " ""
    mv "$file" "$_GSUB"


    If the shell ios a Bourne shell, and doesn't have POSIX parameter
    expansion, you would nornally use an external command such as tr or
    sed:

    newname=`echo "$file" | tr -d " "`
    mv "$file" "$newname"


    But is can be done in the shell itself if the name contains no
    wildcard characters:

    newname=`
    set -- $file
    for w
    do
    echo "$w\c" ## In some shells: echo -n "$w"
    done`
    mv "$file" "$newname"

    --
    Chris F.A. Johnson | Author:
    | Shell Scripting Recipes:
    Any code in this post is released | A Problem-Solution Approach,
    under the GNU General Public Licence | 2005, Apress

  3. Re: Removing spaces from a batch of filenames

    On comp.unix.shell, in
    , Mark 'The Jerk' Hobley wrote:

    > I am using the Almquist Shell (sh).
    >
    > What is the most simple way to strip spaces from a batch of
    > files in a directory.
    >
    > For example:
    >
    > "your file 1.txt" should be renamed "yourfile1.txt".
    >
    > There are a couple of thousand files that need renaming.
    >
    > I have found a perl script, but I want to do this without
    > invoking perl or bash.
    >
    > I also would like a modified version of the same script that
    > removes underscores.
    >


    That's easy. Fix your obnoxious signature and I'll be happy to
    show you how.

    What's this, the 4th time you have been asked to conform to the
    Netiquette guidelines?

    Just why do you imagine that you are above the standards that the
    rest of us conform to?

    Or is it that you don't even bother to look over the posts on
    whatever newsgroup you are posting on at present?

    Maybe that's why you are asking questions that have been
    answered a thousand times on this group and is basic in the
    _extreme_, and are a part of the FAQ.

    So you are both arrogant and lazy. No surprise there: These
    qualities often go hand-in-hand.

    All letters and numbers in the illegal sig below converted to Os:

    > --
    > OOOO OOOOOO
    > OOO OOOOOOO OOOO OOOO
    > OOOOOOO
    > OOOOOOOOOO
    > OOO OOO
    >
    > OOOOOOOOO: (OOOO) OOO OOOO
    > OOOOOOOOOOOOO: OOOO OOO OOO OOOO
    >
    > OOOOO: OOOOOOOOOO OO OOOOOO OOO OOOOOOOOOOOOOOOO OOO
    >
    > OOOO://OOOOOOOOOO.OO.OOO/
    >



    http://groups.google.com/advanced_group_search

    Mark Hobley
    Results 1 - 100 of 326 posts in the last year
    1 24hoursupport.helpdesk
    28 alt.comp.linux
    4 alt.comp.os.linux
    5 alt.linux
    20 alt.os.linux
    1 alt.os.linux.mandriva
    1 alt.pl.comp.os.linux.newbie
    3 comp.lang.tcl
    4 comp.mail.imap
    1 comp.mail.misc
    1 comp.mail.mutt
    1 comp.os.linux
    5 comp.os.linux.misc
    1 comp.os.linux.security
    2 comp.os.linux.setup
    4 comp.os.linux.x
    1 comp.periphs.printers
    1 rec.models.rc.helicopter
    8 uk.comp.os.linux
    1 uk.culture.nostalgia.1980s
    1 uk.forsale
    1 uk.rec.cars.misc
    5 uk.rec.models.radio-control.air


    AC

    --
    URLs in headers. See the slrn directory on my website
    for info on how to get a posting history report on any
    alias with a single keystroke. Not limited to slrn.

  4. Re: Removing spaces from a batch of filenames

    Chris F.A. Johnson wrote:
    [...]
    > The bash/ksh93 version of that function is:
    >
    > _gsub() {
    > _GSUB=${1//$2/$3}
    > }
    >


    add zsh to the list

    =arvi=


+ Reply to Thread