A programme to change name of a file in linux

In summary: 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.
  • #1
leon1127
486
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
 
Technology news on Phys.org
  • #2
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
i am a little bit weak in shell... what is the command that uses this shell script?
 
  • #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.
 
  • #5
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?
 
  • #6
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
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?
 

What is the purpose of changing the name of a file in Linux?

The purpose of changing the name of a file in Linux is to easily identify and organize files, as well as to make it more descriptive and meaningful for the user.

How can I change the name of a file in Linux?

You can change the name of a file in Linux by using the "mv" command in the terminal. The syntax for this command is "mv [current name] [new name]".

Can I change the name of multiple files at once in Linux?

Yes, you can change the name of multiple files at once in Linux by using the "mv" command with the wildcard symbol (*). For example, "mv *oldname* *newname*" will change the name of all files that contain "oldname" in their name to "newname".

What happens if I try to change the name of a file that is currently in use in Linux?

If a file is currently in use, you will not be able to change its name. You will receive an error message stating that the file is in use and the name cannot be changed.

Is it possible to change the name of a file without using the terminal in Linux?

Yes, you can also change the name of a file in Linux by using the file manager. Simply right-click on the file and select "Rename" or press the F2 key. Then, enter the new name and press enter to save the changes.

Similar threads

  • Programming and Computer Science
Replies
4
Views
755
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
20
Views
525
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
5
Views
985
  • Programming and Computer Science
Replies
2
Views
377
  • Programming and Computer Science
Replies
2
Views
4K
  • Programming and Computer Science
Replies
16
Views
4K
  • Programming and Computer Science
3
Replies
81
Views
5K
Back
Top