LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-28-2011, 05:45 AM   #1
Reda01
LQ Newbie
 
Registered: May 2008
Posts: 8

Rep: Reputation: 0
rename file names : replace dots by spaces, keep the extension


Hello,

I am trying to rename a lot of files but need to keep the extension :

the files are films names :

a.b.c.d.***.iso

the result should be replacing "." by spaces " "

this command :

for files in *.iso ; do mv "$files" `echo "$files" | tr '.' ' '`; done

put a space also before the extension iso ??

How can I rename files from

"a.b.c.d.iso" to "a b c d.iso"

thank you
 
Old 03-28-2011, 06:22 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
I do not consider myself an expert with Bash, but until someone else chimes in with a better solution, you can try the following:
Code:
#!/bin/bash

EXT=".iso"   # the extension of files that we are interested in

for file in *$EXT; do

        FILE_LEN=(${#file}-${#EXT})   # deduce length of filename w/o extension

        MODFILE=`echo ${file:0:$FILE_LEN} | tr '.' ' '`   # convert file name using substring of full file name

        mv $file "${MODFILE}${EXT}"   # perform move; surround modified filename with quotes so spaces aren't literally interpreted.

done

Last edited by dwhitney67; 03-28-2011 at 06:27 AM.
 
Old 03-28-2011, 06:36 AM   #3
Reda01
LQ Newbie
 
Registered: May 2008
Posts: 8

Original Poster
Rep: Reputation: 0
Perfect, it works
thank you
 
Old 03-28-2011, 07:05 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well to add to dwight's code, you could also do it all in bash:
Code:
#!/bin/bash

EXT=".iso"   # the extension of files that we are interested in

for file in *$EXT; do

        MODFILE=${file%$EXT}   # remove extension

        mv $file "${MODFILE//./ }${EXT}"   # perform move; change dots for spaces; surround modified filename with quotes so spaces aren't literally interpreted.

done
I thought I would add, interestingly you cannot do the reverse once you have files with spaces as the for loop will break it up based on the space.
For this and other word splitting reasons most would try not to create files with spaces if it can be helped. (just my 2 cents)

Don't forget to mark as SOLVED once you have a solution.
 
1 members found this post helpful.
Old 03-28-2011, 08:19 AM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by Reda01 View Post
How can I rename files from "a.b.c.d.iso" to "a b c d.iso"
I recommend either using the shell argument array, or using find to list the file names one per line, so you can safely work with file names containing any characters.

Using shell argument array:
Code:
#!/bin/bash
if [ $# -lt 1 ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
    echo "" >&2
    echo "Usage: $0 [ -h | --help ]" >&2
    echo "       $0 file .." >&2
    echo "" >&2
    echo "This will transform the name of the specified file(s)." >&2
    echo "" >&2
    exit 0
fi

for path in "$@" ; do
    dir="`dirname "$path"`"
    file="`basename "$path"`"

    # Split file name and extension
    ext=".${file##*.}"
    old="${file%.*}"
    [ "$old" == "$file" ] && ext=""
    new="$old"

    # Modify "$new" to suit your needs. Dots to spaces:
    new="${new//./ }"

    if [ "$new" != "$old" ]; then
        mv -vi "$dir/$old$ext" "$dir/$new$ext" || exit $?
    fi
done
If you save the above as fix-file-name, you can then run e.g.
Code:
./fix-file-name *.iso
Check the shell parameter expansion in the Bash Reference Manual for further tricks you can do to the file names with the above.

If you wish to operate on all files in a directory tree, it's easier to use find. The following script takes at least one parameter; see man find for details:
Code:
#!/bin/bash
if [ $# -lt 1 ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
    echo "" >&2
    echo "Usage: $0 [ -h | --help ]" >&2
    echo "       $0 find-parameters ..." >&2
    echo "" >&2
    echo "This will transform all file and directory names matching the find-parameters." >&2
    echo "See 'man find' for details and explanations." >&2
    echo "" >&2
    echo "Example:" >&2
    echo "       $0 . -name '*.iso'" >&2
    echo "" >&2
    exit 0
fi
EOS="`echo -en '\0'`"
find "$@" -depth -printf '%p\0' | while read -d "$EOS" path ; do
    dir="`dirname "$path"`"
    file="`basename "$path"`"

    ext=".${file##*.}"
    old="${file%.*}"
    [ "$old" == "$file" ] && suffix=""
    new="$old"

    # Modify "$new" to suit your needs.
    new="${new//./ }"

    # Only rename if changed.
    if [ "$new" != "$old" ]; then
        mv -vi "$dir/$old$ext" "$dir/$new$ext" || exit $?
    fi
done
exit $?
If you save the above as find-rename, you can then run e.g.
Code:
./find-rename .
Hope this helps.

Last edited by Nominal Animal; 03-28-2011 at 08:28 AM. Reason: Added the EOS to the find script.
 
1 members found this post helpful.
Old 03-28-2011, 08:54 AM   #6
Reda01
LQ Newbie
 
Registered: May 2008
Posts: 8

Original Poster
Rep: Reputation: 0
Hello Guys,

I tested all the scripts, all of them work fine.

Thank you very much
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash file names with spaces alex1986 Programming 5 07-26-2010 09:47 PM
bash - replace all spaces in file, folder names babag Programming 24 04-20-2008 12:17 AM
du or wc and file names with spaces bramadams Slackware 2 01-27-2005 11:43 AM
Spaces in file names JohnKFT Slackware 3 11-09-2004 03:44 PM
File/Direcory Names with Spaces ar1 Linux - General 3 01-15-2004 10:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration