# A programme to change name of a file in linux

1. Jun 21, 2008

### leon1127

Hello. I am writing a programme that changes the names of files in the linux directory. More specifically I have transfered a lot of files from windows directory which contains a lot of spaces and special characters that can get annoying in linux, so I am trying to write a programme to replace all the spaces and special character into _.

I tried to look up reference in C but most of them just talking about now to do that in the content of files but not the name of the file. Does anyone have an idea how to do it?

I will appreciate any comment.

Leon

2. Jun 21, 2008

### mgb_phys

In bash,

for f in *; do
mv \"$f\" echo \"$f\" | tr [:space:] _
done

Since I'm paranoid I would make the line "echo mv \"$f\" echo \"$f\" | tr [:space:] _ "
and capture the output of the command to a file with >> doit.sh
Then you can check the commands in the file before doing "source doit.sh" to run them.

3. Jun 21, 2008

### leon1127

i am a little bit weak in shell.... what is the command that uses this shell script?

4. Jun 22, 2008

### e(ho0n3

You don't want make a C program to just rename files. That's overkill. Save the code mgb_phys gave you into a file and run it with the bash command.

5. Jun 22, 2008

### leon1127

It didnt work... i put
\$ sh doit.sh
is it rite?

6. Jun 22, 2008

### mgb_phys

You can either type the commands at the prompt (tricky)
Or if you put a series of commands in a file you can either run it with sh by doing "bash blah.sh" or put "#! /bin/bash" as the first line, make it executable with "chmod +x blah.sh" then just do "blah.sh"
Bash might be the default shell on your system, in which case you can just do sh.

In addition to all this, you can also make the command just echo the comand it would have run and capture those lists of commands to a file, you can then run that file by doing "source doit.sh". The names of the files and extentions don't matter, it's just a convention to call them .sh and to call use once throwaway files "doit"

7. Jun 22, 2008

### leon1127

i run it and got error like

mv: target "Hull_J._Options,_futures,_and_other_derivative_securities_(2ed.,_PH,_1993)(K)(T)(505s)_MVspf_.djvu"_' is not a directory
mv "Hull J. Options, futures, and other derivative securities (2ed., PH, 1993)(K)(T)(505s)_MVspf_.djvu" "Hull_J._Options,_futures,_and_other_derivative_securities_(2ed.,_PH,_1993)(K)(T)(505s)_MVspf_.djvu"_
mv: cannot stat "Interest_Rate_Models_an_Infinite_Dimensional_Stochastic_Analysis_Perspective.pdf"': No such file or directory
mv "Interest_Rate_Models_an_Infinite_Dimensional_Stochastic_Analysis_Perspective.pdf" "Interest_Rate_Models_an_Infinite_Dimensional_Stochastic_Analysis_Perspective.pdf"_
mv: target "J.London_-_Modeling_Derivatives_in_C++.pdf"_' is not a directory
mv "J.London - Modeling Derivatives in C++.pdf" "J.London_-_Modeling_Derivatives_in_C++.pdf"_
mv: target "JORION_FRM_Handbook_3rd_ed..pdf"_' is not a directory
mv "JORION FRM Handbook 3rd ed..pdf" "JORION_FRM_Handbook_3rd_ed..pdf"_
mv: cannot stat "John.Wiley.and.Sons.Financial.Instrument.Pricing.Using.C.Plus.Plus.eBook-LiB.chm"': No such file or directory
mv "John.Wiley.and.Sons.Financial.Instrument.Pricing.Using.C.Plus.Plus.eBook-LiB.chm" "John.Wiley.and.Sons.Financial.Instrument.Pricing.Using.C.Plus.Plus.eBook-LiB.chm"_
mv: target "Luenberger_D.G._Investment_science_(Oxford,_1998)(T)(510s).djvu"_' is not a directory
mv "Luenberger D.G. Investment science (Oxford, 1998)(T)(510s).djvu" "Luenberger

and idea?