Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6173957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:39:58+00:00 2026-05-23T23:39:58+00:00

mv $1 $(echo $1 | sed s:\ :_:g) It’s a simple script that renames

  • 0
mv $1 $(echo $1 | sed s:\ :_:g)

It’s a simple script that renames the file passed as argument, exchanging spaces to underlines. However, when I try to rename the file “a e i” to “a_e_i” for example, it returns the following error:

./spc2und a\ e\ i 
mv: target `a_e_i' is not a directory
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:39:59+00:00Added an answer on May 23, 2026 at 11:39 pm

    You need double-quotes around the variables and command substitution to prevent spaces in the filename from being mistaken for argument separators. Also, you don’t need sed, since bash can do character replacement by itself:

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

    Edit: a few more things occurred to me. First, you really should use mv -i in case there’s already a file with underscores (“a_e_i” or whatever). Second, this only works on simple filenames — if you give it a file path with spaces in an enclosing directory, (e.g. “foo bar/baz quux/a e i”), it tries to rename it into a directory with the spaces converted, which doesn’t exist, leading to comedy. So here’s a proposed better version:

    mv -i "$1" "$(dirname "$1")/$(basename "${1// /_}")"
    

    BTW, the other answers leave off the double-quotes on the filename after replacing spaces with underscores — this isn’t entirely safe, as there are other funny characters that might still cause trouble. Rule 1: when in doubt, wrap it in double-quotes for safety. Rule 2: be in doubt.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I execute the following bash script: #!/bin/bash version=$1 echo $version sed 's/\${version.number}/$version/' template.txt >
HI! I would like to use this simple script.sh: #!/bin/sh filename=$1 echo $filename |
I am sending the echo to mail function via PHP from variable that includes
Is there a NAnt task that will echo out all property names and values
I've discovered that any time I do the following: echo '<a href=http:// title=bla>huzzah</a>'; I
echo sed -i 's/NULL/\\N/g' .$_REQUEST['para']..sql; The above statement works. But it fail when I
I have am trying to use sed to get some info that is encoded
This is probably a very simple question to an experienced person with UNIX however
This is probably a very simple question to an experienced person with UNIX however
Why does this produce these results? C:\>(echo a && echo b) | sed 1!G;h;$p

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.