LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   easy script question (https://www.linuxquestions.org/questions/linux-newbie-8/easy-script-question-304931/)

citrus 03-23-2005 12:45 AM

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?

chbin 03-23-2005 12:56 AM

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

chbin 03-23-2005 01:01 AM

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.

chbin 03-23-2005 01:24 AM

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

citrus 03-23-2005 01:31 PM

bash$ for i in `ls`; do mv $i $i.mp3; done; unset i

-bash: syntax error near unexpected token `do'

koodoo 03-23-2005 02:53 PM

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

chbin 03-23-2005 03:52 PM

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.

koodoo 03-23-2005 03:58 PM

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


All times are GMT -5. The time now is 05:07 AM.