• Skip to primary navigation
  • Skip to main content
sharadchhetri

sharadchhetri

Tutorials On Linux, Unix & Open Source

  • Home
  • Linux Commands
  • Resources
    • Learn Linux
  • My WordPress plugins

Convert hyphen to underscore in between all filenames shell script

December 23, 2011 by Sharad Chhetri 3 Comments

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

 

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • LinkedIn
  • Reddit
  • Tumblr
  • Pinterest
  • Pocket
  • Telegram
  • WhatsApp
  • Mastodon

Related posts:

  1. Print double hyphen sign simultaneously in post of Octopress
  2. sed command to display text between two strings or keywords
  3. sed : remove lines between two keywords [ including keywords ]
  4. How to convert rpm file into deb file
  5. How to convert float to integer number
  6. Convert video file into gif file through command line in linux
  7. Convert new line to space by using sed command
  8. convert space into new line using sed command
  9. Convert website page to pdf and image
  10. How to install korn shell ksh in Linux

Filed Under: Linux, Tips And Tricks Tagged With: shell script

Reader Interactions

Comments

  1. smaghe says

    May 24, 2013 at 1:28 pm

    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

    Reply
    • admin says

      May 25, 2013 at 1:15 am

      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

      Reply
    • sharadchhetri says

      August 14, 2013 at 2:49 am

      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.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

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

Copyright © 2023 ·
The material in this site cannot be republished either online or offline, without our permission.
Proudly Blogging From Bharat.

  • Contact
  • About Me
  • My WordPress plugins
  • Privacy Policy