Find the answer to your Linux question:
Results 1 to 10 of 10
Not the greatest shell scripter. This script does not work: Code: #! /bin/bash # Remove spaces from filenames and replace with underscores. echo "$1 is " $1 fileName=`echo "$1"|sed 's/ ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie lugoteehalt's Avatar
    Join Date
    Jan 2004
    Posts
    232

    Shell script confusion.


    Not the greatest shell scripter.

    This script does not work:
    Code:
    #! /bin/bash
    # Remove spaces from filenames and replace with underscores.
    
    echo "$1 is " $1
    fileName=`echo "$1"|sed 's/ /\\ /g'`
    echo "fileName = " $fileName
    rename -v 's/ /_/g' $fileName
    
    unset fileName
    exit=0
    
    ~                                                                               
    ~                                                                               
    ~                                                                               
    ~                                                                               
    ~                                                                               
    ~                                                                               
    ~                                                                               
    ~                                                                               
    "rmspace.sh" 11L, 212C                                        11,0-1        All
    The output is:
    Code:
    $ rmspace.sh ~/Videos/Medicine/Vaccine\ Nation\ -\ FULL\ LENGTH.flv 
    /home/lugo/Videos/Medicine/Vaccine Nation - FULL LENGTH.flv is  /home/lugo/Videos/Medicine/Vaccine Nation - FULL LENGTH.flv
    fileName =  /home/lugo/Videos/Medicine/Vaccine Nation - FULL LENGTH.flv
    $
    rename is not working.

    Code:
    $ echo "/home/lugo/Videos/Medicine/Vaccine Nation - FULL LENGTH.flv" | sed 's/ /\\ /g'
    /home/lugo/Videos/Medicine/Vaccine\ Nation\ -\ FULL\ LENGTH.flv
    $
    
    $ rename -v 's/ /_/g' ~/Videos/Medicine/Vaccine\ Nation\ -\ FULL\ LENGTH.flv 
    /home/lugo/Videos/Medicine/Vaccine Nation - FULL LENGTH.flv renamed as /home/lugo/Videos/Medicine/Vaccine_Nation_-_FULL_LENGTH.flv
    $
    Thanks any help.
    All power is violence; all power is evil.
    Money As Debt

  2. #2
    Linux Newbie
    Join Date
    Oct 2008
    Posts
    151
    Does this work for you?

    for i in *\ *
    do
    mv "$i" "${i// /_}"
    done

    If you want to do one at a time you could just use the third line and replace the two letters i with a 1.
    Last edited by kurtdriver; 09-17-2011 at 02:02 PM.

  3. #3
    Just Joined!
    Join Date
    Jan 2011
    Location
    Cambridge, Ontario, Canada
    Posts
    24
    Replace: fileName=`echo "$1"|sed 's/ /\\ /g'`
    With: fileName=`echo "$1"|sed 's/ /_/g'`

    and

    Replace: rename -v 's/ /_/g' $fileName
    With: rename $1 $fileName $1
    Last edited by PairOfBlanks2; 09-17-2011 at 02:22 AM. Reason: Completeness

  4. #4
    Linux Newbie
    Join Date
    Oct 2008
    Posts
    151
    Why not just run sed on $1 directly, without piping it through echo?

  5. #5
    Just Joined!
    Join Date
    Aug 2006
    Posts
    2
    I have a script posted in the opensuse forums that will do what you want with little or no editing I think.
    Post # 11 has the last version I left off with, it appears to work correctly but has not been extensively tested. I suggest you make a test run first.

    It wont let me post a url yet so:
    h-t-t-p-://forums.opensuse.org/english/get-technical-help-here/applications/457475-can-you-recommend-me-script-app.html#post2324828

  6. #6
    Linux Newbie lugoteehalt's Avatar
    Join Date
    Jan 2004
    Posts
    232
    Thanks all. Just got it to work:
    Code:
    #! /bin/bash
    # Remove spaces from filenames and replace with underscores.
    
    echo "$1 is " $1
    #fileName=$(echo $1|sed 's/ /\\ /g')
    #echo "fileName = " $fileName
    #rename -v 's/ /_/g' $(echo $1|sed 's/ /\\ /g')
    echo $1|rename -v 's/ /_/g'
    
    #unset fileName
    exit=0
    
    ~                                                                               
    ~                                                                               
    ~                                                                               
    ~                                                                               
    ~                                                                               
    ~                                                                               
    ~                                                                               
    "rmspace.sh" 12L, 260C                                        12,0-1        All
    The problem was very similar to http://www.linuxforums.org/forum/pro...-variable.htmljust below.

    Must say I don't understand what was wrong before. I'll try sugestions and see what happens.
    All power is violence; all power is evil.
    Money As Debt

  7. #7
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,283
    Hi.
    Quote Originally Posted by lugoteehalt View Post
    ... Must say I don't understand what was wrong before. I'll try sugestions and see what happens.
    Quoting issue. See the differences between this script in file "user1" and your original:
    Code:
    #! /bin/bash
    # Remove spaces from filenames and replace with underscores.
    
    # Remove debris from previous runs and set up file as parameter 1.
    echo
    rm -f "f  1" f__1
    touch "f  1"
    ls -lgG f*
    set -- "f  1"
    
    echo
    echo "arg 1 is \"$1\""
    # fileName=`echo "$1"|sed 's/ /\\ /g'`
    fileName=$1
    echo "fileName = " $fileName
    echo "fileName = " "$fileName"
    
    echo
    # rename -v 's/ /_/g' $fileName
    rename -v 's/ /_/g' "$fileName"
    
    unset fileName
    ls -lgG f*
    
    # exit=0
    exit 0
    producing:
    Code:
    % ./user1 
    
    -rw-r--r-- 1 0 Sep 17 06:17 f  1
    
    arg 1 is "f  1"
    fileName =  f 1
    fileName =  f  1
    
    f  1 renamed as f__1
    -rw-r--r-- 1 0 Sep 17 06:17 f__1
    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
    Just Joined!
    Join Date
    Jan 2011
    Location
    Cambridge, Ontario, Canada
    Posts
    24
    Quote Originally Posted by PairOfBlanks2 View Post
    Replace: fileName=`echo "$1"|sed 's/ /\\ /g'`
    With: fileName=`echo "$1"|sed 's/ /_/g'`

    and

    Replace: rename -v 's/ /_/g' $fileName
    With: rename $1 $fileName $1
    This works for both absolute and relative paths. I've just tested it.

  9. #9
    Just Joined!
    Join Date
    Jul 2007
    Posts
    2
    The following script is tested to work.

    #! /bin/bash
    THEDIR=/home/linux/a/* #define the directory where script will remove the space in file names
    echo ----------------------------------
    for files in $THEDIR;
    do
    newfile=`echo $files | sed -e 's/\ /\_/g'`
    if [ "$newfile" != "$files" ]; then
    echo "oldfilename is $files"
    echo "newfilename is $newfile"
    mv "$files" "$newfile"
    echo "$files is renamed to $newfile"
    else
    echo "$files doesn\'t contain space"
    fi
    echo ----------------------------------
    done
    Last edited by bigcrab; 09-18-2011 at 02:26 AM.

  10. #10
    Linux Newbie lugoteehalt's Avatar
    Join Date
    Jan 2004
    Posts
    232
    Gracias.

    dri: Understand quotes better now. Have even worked out that my improved script above, amounts to: echo $1|rename -v 's/ /_/g' , needs quotes round $1 to work for multiple consecutive spaces.
    All power is violence; all power is evil.
    Money As Debt

Posting Permissions

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