View Full Version : A programme to change name of a file in linux
leon1127
Jun21-08, 10:30 PM
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
mgb_phys
Jun21-08, 11:12 PM
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.
leon1127
Jun21-08, 11:38 PM
i am a little bit weak in shell.... what is the command that uses this shell script?
e(ho0n3
Jun22-08, 12:01 AM
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.
leon1127
Jun22-08, 10:05 AM
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.
It didnt work... i put
$ sh doit.sh
is it rite?
mgb_phys
Jun22-08, 11:14 AM
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"
leon1127
Jun22-08, 01:02 PM
i run it and got error like
mv: target `"Hull_J._Options,_futures,_and_other_derivative_sec urities_(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_sec urities_(2ed.,_PH,_1993)(K)(T)(505s)_MVspf_.djvu"_
mv: cannot stat `"Interest_Rate_Models_an_Infinite_Dimensional_Stoch astic_Analysis_Perspective.pdf"': No such file or directory
mv "Interest_Rate_Models_an_Infinite_Dimensional_Stoch astic_Analysis_Perspective.pdf" "Interest_Rate_Models_an_Infinite_Dimensional_Stoch astic_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.U sing.C.Plus.Plus.eBook-LiB.chm"': No such file or directory
mv "John.Wiley.and.Sons.Financial.Instrument.Pricing.U sing.C.Plus.Plus.eBook-LiB.chm" "John.Wiley.and.Sons.Financial.Instrument.Pricing.U sing.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?
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.