LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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
 
LinkBack Search this Thread
Old 08-06-2009, 11:31 AM   #1
dai_bach
LQ Newbie
 
Registered: Jan 2008
Distribution: Ubuntu
Posts: 2

Rep: Reputation: 0
mv: target `filename' is not a directory


I'm trying to write a short script that changes the file names in the folders containing music (mp3) files. The current format is often along the lines of:
"Artist Name - Album Name - tracknum - Trackname.mp3"
and I'd like to shorten this to just:
"tracknum Trackname.mp3"

So, I've tried the following approach
Code:
for i in *.mp3; do
  j=`echo $i | cut -d "-" -f3`; #results in "tracknum"
  k=`echo $i | cut -d "-" -f4`; #results in "Trackname.mp3"
  mv $i $j$k;
done
but I get the error:
Code:
mv: target `Trackname.mp3' is not a directory
I'm not expecting it to be a directory! As far as I'm aware, $j$k should give me "tracknum Trackname.mp3". Indeed, this is the result of
Code:
echo $j$k
Could someone please tell me where I'm going wrong or how to solve my problem?

Cheers
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 08-06-2009, 11:42 AM   #2
David the H.
Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 5,334

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
I'll bet the problem is spaces in your filenames. They can really mess up scripting. Most *nix users learn to avoid them in filenames whenever possible.

Add this to the start of your script. It changes the internal field separator to a newline only, and your script will treat spaces as just another character from then on.
Code:
#!/bin/bash

IFS='
'

<your script>
Also, take some time to learn about how quotes work in shells and scripts. Surrounding your variables in double-quotes will generally work too. I usually prefer changing the IFS though, as it's more foolproof.

Last edited by David the H.; 08-06-2009 at 11:48 AM. Reason: added final note
 
2 members found this post helpful.
Old 08-07-2009, 01:07 PM   #3
David the H.
Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 5,334

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
So I take it it worked then.
 
Old 08-08-2009, 02:00 AM   #4
dai_bach
LQ Newbie
 
Registered: Jan 2008
Distribution: Ubuntu
Posts: 2

Original Poster
Rep: Reputation: 0
Aye, worked a treat. Cheers!
 
  


Reply

Tags
mv


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Longest filename in a directory dresch Linux - Newbie 6 01-05-2007 03:29 PM
How do I stop the filename and directory from printing? BajaNick Linux - Hardware 1 08-31-2004 04:31 PM
read filename from directory newbielinux Linux - Newbie 2 03-02-2004 03:48 PM
samba + target directory robyso Linux - Networking 0 07-11-2003 05:07 AM
Changing to a directory with a space in the filename? guitargeek Linux - General 5 02-13-2003 05:38 PM


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

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration