In this post we will learn, how we can convert hyphen to underscore in between of all filenames with the help of shell script or bash script.

For eg. if you want to convert the file haveing hyphen sign in between its name to Underscore .

Use the below given method.

After using the script the file will be named like this ;

Before using script: file-name
After using script: file_name

Create a file ,paste the below given contents and make it executable by “chmod +x filename” command.

 

#!/bin/bash
# Example of converting - to _ in all files in current directory where this script is.

for i in `ls *-*`
do
NEW=`echo $i|tr '-' '_'`
mv $i $NEW

done

 

Read Some More Articles

3 Comments

  1. Hi,
    there is a problems with the quotes. Here’s the working code :
    #!/bin/bash
    # Example of converting – to _ in all files in current directory where this script is.

    cd /Users/…
    for i in `ls *-*`
    do

    NEW=`echo $i|tr ‘-‘ ‘_’`
    mv $i $NEW

    done

    I’m not sure wether it will work, might be wordpress related

    1. I am updating my comment.The problem was due to I used blockquotes. The problem is fixed now,with the help of code viewer it can be properly copy pasted

    2. Hello Smaghe,
      I am updating my comment.The problem was due to I used blockquotes in blog. The
      problem is fixed now,with the help of code viewer it can be properly
      copy pasted.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.