Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 09-26-2008, 06:58 AM   #1
Registered: Aug 2008
Location: India
Distribution: Fedora, RedHat
Posts: 101

Rep: Reputation: 17
Unhappy BASH script rename help

This is my BASH script.

newfilename=`echo $filename | tr [:blank:] [_]`
mv -i $filename $newfilename
How I use it: ./ "file name"

I echo the newfilename var and I see its perfect. But error I get is...

mv: target 'file name' is not a directory.
Can anyone tell me what is my mistake? I have just started to learn BASH scripting...
Old 09-26-2008, 07:43 AM   #2
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
Since you are doing this to get rid of blanks, I will assume that filename contains blanks: "no such file", for example. Thus,

mv $filename $newfilename

mv no such file no_such_file
Which won't work. You need to say

mv "$filename" $newfilename
Old 09-26-2008, 07:46 AM   #3
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Even better:
mv "$filename" "$newfilename"
I found the habit of quoting in these cases, even when it is not strictly necessary, saves a lot of agro in the long run. You should carefully read the quoting section in the bash manual page and make sure you understand it. It will save you a lot of headaches.
Old 09-26-2008, 08:04 AM   #4
Registered: Aug 2008
Location: India
Distribution: Fedora, RedHat
Posts: 101

Original Poster
Rep: Reputation: 17
Thx a lot. I will keep habit of this thing. Problem solved.


bash, ifs, quoting

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash script? how to rename files in random way? lefty.crupps Linux - Software 16 11-03-2015 03:07 PM
Help with Bash Script - Rename Multiple Files embsupafly Programming 16 04-02-2010 04:50 AM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-23-2008 12:25 AM
rename mp3s with Bash script jhwilliams Linux - Software 2 07-25-2007 11:42 AM
BASH: rename an argument of the script sylvaticus Programming 2 05-21-2007 09:20 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:54 PM.

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