Change blank spaces with underscores in Bash -


I would like to write a script in Bash:

  1. moves a file In the form of input.

  2. If the file name has an empty space, then it changes with an underscore.

My first question is whether a script is running through script commands, when a bidding script can take arguments as input or provided an input can go? The second question will be how to use the mv command to point 2.

Your script may be simple:

  #! / Bin / bash mv "$ 1" "$ (sed 's / \ {1, \} / _ / g' <<" $ 1 ")"  

Use this as script.sh file \ name . The file file name will be renamed to file_name .

As mentioned in the comments (thanks), it is also possible to use underlying character substitution if you are using bash:

  mv "$ 1" "$ {1 //}"  

Its behavior is slightly different, because it will replace each individual location with underscore, while in the approach that uses SAD In a single underscore, the group of spaces is also rotated. If you enable extended globbing, you can treat this version in the same way:

  shopt -s extglob mv "$ 1" "$ {1 // + () / _} " 

Depending on whether you want to replace the space character or all types of white space (like tabs, newlines, etc.) You may want to use it, [[space:]] instead of in each of these examples.


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -