LinuxQuestions.org
Help answer threads with 0 replies.
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 09-26-2008, 05:58 AM   #1
n03x3c
Member
 
Registered: Aug 2008
Location: India
Distribution: Fedora, RedHat
Posts: 101

Rep: Reputation: 17
Unhappy BASH script rename help


This is my BASH script.

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

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

Code:
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, 06:43 AM   #2
burschik
Member
 
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,

Code:
mv $filename $newfilename
becomes

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

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

Rep: Reputation: 65
Even better:
Code:
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, 07:04 AM   #4
n03x3c
Member
 
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.
 
  


Reply

Tags
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 02:07 PM
Help with Bash Script - Rename Multiple Files embsupafly Programming 16 04-02-2010 03:50 AM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-22-2008 11:25 PM
rename mp3s with Bash script jhwilliams Linux - Software 2 07-25-2007 10:42 AM
BASH: rename an argument of the script sylvaticus Programming 2 05-21-2007 08:20 AM

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

All times are GMT -5. The time now is 11:49 PM.

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