A programme to change name of a file in linux

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
6 replies · 4K views
leon1127
Messages
484
Reaction score
0
Hello. I am writing a programme that changes the names of files in the linux directory. More specifically I have transferred 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
 
Physics news on Phys.org
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.
 
i am a little bit weak in shell... what is the command that uses this shell script?
 
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.
 
mgb_phys said:
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?
 
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"
 
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" "Luenbergerand idea?