Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

A programme to change name of a file in linux

  1. Jun 21, 2008 #1
    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. jcsd
  3. Jun 21, 2008 #2

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    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.
     
  4. Jun 21, 2008 #3
    i am a little bit weak in shell.... what is the command that uses this shell script?
     
  5. Jun 22, 2008 #4
    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.
     
  6. Jun 22, 2008 #5
    It didnt work... i put
    $ sh doit.sh
    is it rite?
     
  7. Jun 22, 2008 #6

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    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"
     
  8. Jun 22, 2008 #7
    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?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: A programme to change name of a file in linux
Loading...