LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-23-2005, 01:45 AM   #1
citrus
Member
 
Registered: Dec 2003
Location: California
Distribution: Kubuntu 6.1
Posts: 548

Rep: Reputation: 30
easy script question


so i have a bunch of mp3s in this folder but none of them have *.mp3 at the end of them
there just the name of the file


what kinda script or command should i use to attach .mp3 to all the files in this dir?
 
Old 03-23-2005, 01:56 AM   #2
chbin
Member
 
Registered: Mar 2005
Distribution: slackware-current
Posts: 379

Rep: Reputation: 31
You could write a script but what is the point . Just enter the following it a bash shell.

bash$ for i in `ls`; do mv $i $i.mp3; done
 
Old 03-23-2005, 02:01 AM   #3
chbin
Member
 
Registered: Mar 2005
Distribution: slackware-current
Posts: 379

Rep: Reputation: 31
In case you never seen in before the ` in the ls command... are backward quotes (shift + ~). Needed to tell the bash interpreter that ls is a command.
 
Old 03-23-2005, 02:24 AM   #4
chbin
Member
 
Registered: Mar 2005
Distribution: slackware-current
Posts: 379

Rep: Reputation: 31
Oh if you really want to get technical that previous loop added an environment variable i to your current bash shell. If you want to get rid of it without exiting and loging back in then

$ unset i

So to have the command run with cleanup

bash$ for i in `ls`; do mv $i $i.mp3; done; unset i
 
Old 03-23-2005, 02:31 PM   #5
citrus
Member
 
Registered: Dec 2003
Location: California
Distribution: Kubuntu 6.1
Posts: 548

Original Poster
Rep: Reputation: 30
bash$ for i in `ls`; do mv $i $i.mp3; done; unset i

-bash: syntax error near unexpected token `do'
 
Old 03-23-2005, 03:53 PM   #6
koodoo
Member
 
Registered: Aug 2004
Location: a small village faraway in the mountains
Distribution: Fedora Core 1, Slackware 10.0 | 2.4.26 | custom 2.6.14.2, Slackware 10.2 | 11.0, Slackware64-13
Posts: 345

Rep: Reputation: 32
Hi,

I don't know why you get that syntax error. The command worked for me though.I just copied it from here and pasted it in a terminal pressed the enter key and it worked without any syntax error.
You probable entered it in a wrong way.Check the syntax of the command you entered.

However the command given above is an inefficient one :
Code:
Suppose you have the following case : 
[root@localhost tmp]# ls
Avril Lavigne - Knockin On Heavens Door  theme
Boulevard Of Broken Dreams3              thereason
[root@localhost tmp]#
And you wish to add .mp3 extension to these files. If you try the previously given command you have :
Code:
[root@localhost tmp]#  for i in `ls`; do mv $i $i.mp3; done; unset i
mv: cannot stat `Avril': No such file or directory
mv: cannot stat `Lavigne': No such file or directory
mv: invalid option -- .
Try `mv --help' for more information.
mv: cannot stat `Knockin': No such file or directory
mv: cannot stat `On': No such file or directory
mv: cannot stat `Heavens': No such file or directory
mv: cannot stat `Door': No such file or directory
mv: cannot stat `Boulevard': No such file or directory
mv: cannot stat `Of': No such file or directory
mv: cannot stat `Broken': No such file or directory
mv: cannot stat `Dreams3': No such file or directory
[root@localhost tmp]# ls
Avril Lavigne - Knockin On Heavens Door  theme.mp3
Boulevard Of Broken Dreams3              thereason.mp3
[root@localhost tmp]#
The reason for this is that when you loop through the files using ls command if any filename contains spaces in between then i does not take up the name of the entire file but it takes up the successive words in the file name and works with them.In the above case for the first file
Avril Lavigne - Knockin On Heavens Door i takes up values as : "Avril" then "Lavigne" then "-" then "knockin" and so on...........................
With the end result being that only the files with filenames having no spaces in between end up being renamed, rest all remain the same
A more efficient example could be :
Code:
[root@localhost tmp]# ls
Avril Lavigne - Knockin On Heavens Door  theme
Boulevard Of Broken Dreams3              thereason

[root@localhost tmp]# for i in *; do mv "$i" "$i.mp3"; done; unset i 
[root@localhost tmp]# ls
Avril Lavigne - Knockin On Heavens Door.mp3  theme.mp3
Boulevard Of Broken Dreams3.mp3              thereason.mp3
[root@localhost tmp]#
Don't forget to put the quotes around $i otherwise mv: would report an error about moving multiple files


Hope that helps

Last edited by koodoo; 03-23-2005 at 03:55 PM.
 
Old 03-23-2005, 04:52 PM   #7
chbin
Member
 
Registered: Mar 2005
Distribution: slackware-current
Posts: 379

Rep: Reputation: 31
Ah yes, I never took into account that there could be spaces in the file name. I never put spaces in file names in linux, as it is annoying to escape the spaces in the shell. But I guess someone could download a file with spaces in it.
 
Old 03-23-2005, 04:58 PM   #8
koodoo
Member
 
Registered: Aug 2004
Location: a small village faraway in the mountains
Distribution: Fedora Core 1, Slackware 10.0 | 2.4.26 | custom 2.6.14.2, Slackware 10.2 | 11.0, Slackware64-13
Posts: 345

Rep: Reputation: 32
Same here i also don't like those spaces. Coz every time either you have to escape them with a backslash or enclose the entire filename in quotes.

-------neways

-------ciao
 
  


Reply


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
Easy Shell Script Question scriptn00b Linux - Newbie 8 09-04-2005 08:09 AM
easy shell script question RDove Programming 4 02-15-2005 07:26 PM
Easy Bash Script Question skibud2 Linux - Newbie 1 11-17-2004 12:14 PM
Easy bash script question TheRealDeal Linux - General 11 08-26-2003 10:45 PM
Easy script question. TheRealDeal Linux - General 2 08-22-2003 12:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:09 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration