Linux - Replacing spaces in the file names









up vote
73
down vote

favorite
35












I have a number of files in a folder, and I want to replace every space character in all file names with underscores. How can I achieve this?










share|improve this question



























    up vote
    73
    down vote

    favorite
    35












    I have a number of files in a folder, and I want to replace every space character in all file names with underscores. How can I achieve this?










    share|improve this question

























      up vote
      73
      down vote

      favorite
      35









      up vote
      73
      down vote

      favorite
      35






      35





      I have a number of files in a folder, and I want to replace every space character in all file names with underscores. How can I achieve this?










      share|improve this question















      I have a number of files in a folder, and I want to replace every space character in all file names with underscores. How can I achieve this?







      linux replace command filenames spaces






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 30 '15 at 12:16









      Peter Mortensen

      13.4k1983111




      13.4k1983111










      asked Nov 27 '09 at 5:08









      Mithun Sreedharan

      21.6k65160223




      21.6k65160223






















          11 Answers
          11






          active

          oldest

          votes

















          up vote
          129
          down vote



          accepted










          This should do it:



          for file in *; do mv "$file" `echo $file | tr ' ' '_'` ; done





          share|improve this answer






















          • This didn't work for me. It claimed identical files existed (with the wrong filenames). E.g. trying to rename 1 - foo.jpg and my folder already had 1.jpg in it.
            – byxor
            Oct 18 '17 at 14:05







          • 1




            I find backticks bit hard to read when they are near quotes. The same but more readable would be for file in *; do mv "$file" $(echo $file | tr ' ' '_') ; done
            – Kamil S Jaron
            Jul 3 at 8:58


















          up vote
          50
          down vote













          I prefer to use the command 'rename', which takes Perl-style regexes:



          rename "s/ /_/g" *


          You can do a dry run with the -n flag:



          rename -n "s/ /_/g" *





          share|improve this answer
















          • 1




            this will work if you have the perl-style rename and not the simpler redhat/fedora one
            – David Dean
            Nov 27 '09 at 5:56






          • 5




            the fedora version would be rename " " "_" *
            – David Dean
            Nov 27 '09 at 5:57






          • 4




            rename not available in OSX it appears.
            – Etienne Low-Décarie
            Feb 12 '14 at 16:53










          • On macOS 10.12.3, rename is available.
            – Sung Won Cho
            Mar 14 '17 at 6:09










          • For some reason rename does not do a single thing on my CentOS box. Either with regex or not.
            – Buttle Butkus
            Jun 28 '17 at 6:32

















          up vote
          9
          down vote













          Use sh...



          for i in *' '*; do mv "$i" `echo $i | sed -e 's/ /_/g'`; done


          If you want to try this out before pulling the trigger just change mv to echo mv.






          share|improve this answer




















          • This one also worked fine
            – Mithun Sreedharan
            Nov 27 '09 at 7:03

















          up vote
          4
          down vote













          What if you want to apply the replace task recursively? How would you do that?



          Well, I just found the answer myself. No the most elegant solution (tries to rename also files that do not comply with the condition) but works. (BTW, in my case I needed to rename the files with '%20', not with an underscore)



          #!/bin/bash
          find . -type d | while read N
          do
          (
          cd "$N"
          if test "$?" = "0"
          then
          for file in *; do mv "$file" $file// /%20; done
          fi
          )
          done





          share|improve this answer





























            up vote
            3
            down vote













            If you use bash:



            for file in *; do mv "$file" $file// /_; done





            share|improve this answer






















            • when i tried, i got mv: when moving multiple files, last argument must be a directory Try mv --help' for more information. mv: when moving multiple files, last argument must be a directory Try mv --help' for more information.
              – Mithun Sreedharan
              Nov 27 '09 at 5:32










            • Should be $FILE/ /_
              – soulmerge
              Nov 27 '09 at 5:49










            • that only replace 1 space
              – ghostdog74
              Nov 27 '09 at 5:56










            • Again error mv: missing file argument Try mv --help' for more information. mv: missing file argument Try mv --help' for more information. mv: missing file argument Try mv --help' for more information. mv: missing file argument Try mv --help' for more information.
              – Mithun Sreedharan
              Nov 27 '09 at 7:07










            • @levislevis85: Thx, didn't know that.
              – soulmerge
              Nov 27 '09 at 7:13

















            up vote
            1
            down vote













            Quote your variables:



            for file in *; do echo mv "'$file'" "$file// /_"; done


            Remove the "echo" to do the actual rename.






            share|improve this answer






















            • It is echoing the mv commands prperly, but not really renaming the file!
              – Mithun Sreedharan
              Nov 27 '09 at 7:05










            • removing echo produces error like mv: cannot stat '1130 lake micigan view.jpg'': No such file or directory mv: cannot stat '1130_1_bedroom_floor_plan.jpg'': No such file or directory mv: cannot stat '1130_BedPicture_8.jpg'': No such file or directory mv: cannot stat '1130_diningroom_table.jpg'': No such file or directory
              – Mithun Sreedharan
              Nov 27 '09 at 7:08










            • what is your OS?
              – ghostdog74
              Nov 27 '09 at 7:26










            • what shell are you using?
              – ghostdog74
              Nov 27 '09 at 7:27










            • Linux Linux 2.6.9-42.0.3.EL.wh1smp #1 SMP Fri Aug 14 15:48:17 MDT 2009 i686 i686 i386 GNU/Linux
              – Mithun Sreedharan
              Nov 27 '09 at 11:54

















            up vote
            1
            down vote













            Try something like this, assuming all of your files were .txt's:



            for files in *.txt; do mv “$files” `echo $files | tr ‘ ‘ ‘_’`; done





            share|improve this answer


















            • 2




              got the below error tr: two strings must be given when translating mv: missing file argument Try `mv --help' for more information.
              – Mithun Sreedharan
              Nov 27 '09 at 5:31






            • 2




              Agin error tr: too many arguments Try tr --help' for more information. mv: missing file argument Try mv --help' for more information.
              – Mithun Sreedharan
              Nov 27 '09 at 7:06

















            up vote
            1
            down vote













            Here is another solution:



            ls | awk 'printf(""%s"n", $0)' | sed 'p; s/ /_/g' | xargs -n2 mv


            1. uses awk to add quotes around the name of the file

            2. uses sed to replace space with underscores; prints the original name with quotes(from awk); then the substituted name

            3. xargs takes 2 lines at a time and passes it to mv





            share|improve this answer



























              up vote
              0
              down vote













              I believe your answer is in Replace spaces in filenames with underscores.






              share|improve this answer


















              • 1




                While this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
                – Bill the Lizard
                Oct 27 '11 at 14:58


















              up vote
              0
              down vote













              The easiest way to replace a string (space character in your case) with another string in Linux is using sed. You can do it as follows



              sed -i 's/s/_/g' *


              Hope this helps.






              share|improve this answer



























                up vote
                0
                down vote













                To rename all the files with a .py extension use,

                find . -iname "*.py" -type f | xargs -I% rename "s/ /_/g" "%"



                Sample output,



                $ find . -iname "*.py" -type f 
                ./Sample File.py
                ./Sample/Sample File.py
                $ find . -iname "*.py" -type f | xargs -I% rename "s/ /_/g" "%"
                $ find . -iname "*.py" -type f
                ./Sample/Sample_File.py
                ./Sample_File.py





                share|improve this answer




















                  Your Answer






                  StackExchange.ifUsing("editor", function ()
                  StackExchange.using("externalEditor", function ()
                  StackExchange.using("snippets", function ()
                  StackExchange.snippets.init();
                  );
                  );
                  , "code-snippets");

                  StackExchange.ready(function()
                  var channelOptions =
                  tags: "".split(" "),
                  id: "1"
                  ;
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function()
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled)
                  StackExchange.using("snippets", function()
                  createEditor();
                  );

                  else
                  createEditor();

                  );

                  function createEditor()
                  StackExchange.prepareEditor(
                  heartbeatType: 'answer',
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader:
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  ,
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  );



                  );













                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function ()
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1806868%2flinux-replacing-spaces-in-the-file-names%23new-answer', 'question_page');

                  );

                  Post as a guest















                  Required, but never shown

























                  11 Answers
                  11






                  active

                  oldest

                  votes








                  11 Answers
                  11






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  129
                  down vote



                  accepted










                  This should do it:



                  for file in *; do mv "$file" `echo $file | tr ' ' '_'` ; done





                  share|improve this answer






















                  • This didn't work for me. It claimed identical files existed (with the wrong filenames). E.g. trying to rename 1 - foo.jpg and my folder already had 1.jpg in it.
                    – byxor
                    Oct 18 '17 at 14:05







                  • 1




                    I find backticks bit hard to read when they are near quotes. The same but more readable would be for file in *; do mv "$file" $(echo $file | tr ' ' '_') ; done
                    – Kamil S Jaron
                    Jul 3 at 8:58















                  up vote
                  129
                  down vote



                  accepted










                  This should do it:



                  for file in *; do mv "$file" `echo $file | tr ' ' '_'` ; done





                  share|improve this answer






















                  • This didn't work for me. It claimed identical files existed (with the wrong filenames). E.g. trying to rename 1 - foo.jpg and my folder already had 1.jpg in it.
                    – byxor
                    Oct 18 '17 at 14:05







                  • 1




                    I find backticks bit hard to read when they are near quotes. The same but more readable would be for file in *; do mv "$file" $(echo $file | tr ' ' '_') ; done
                    – Kamil S Jaron
                    Jul 3 at 8:58













                  up vote
                  129
                  down vote



                  accepted







                  up vote
                  129
                  down vote



                  accepted






                  This should do it:



                  for file in *; do mv "$file" `echo $file | tr ' ' '_'` ; done





                  share|improve this answer














                  This should do it:



                  for file in *; do mv "$file" `echo $file | tr ' ' '_'` ; done






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 30 '15 at 12:17









                  Peter Mortensen

                  13.4k1983111




                  13.4k1983111










                  answered Nov 27 '09 at 5:42









                  neesh

                  3,63252528




                  3,63252528











                  • This didn't work for me. It claimed identical files existed (with the wrong filenames). E.g. trying to rename 1 - foo.jpg and my folder already had 1.jpg in it.
                    – byxor
                    Oct 18 '17 at 14:05







                  • 1




                    I find backticks bit hard to read when they are near quotes. The same but more readable would be for file in *; do mv "$file" $(echo $file | tr ' ' '_') ; done
                    – Kamil S Jaron
                    Jul 3 at 8:58

















                  • This didn't work for me. It claimed identical files existed (with the wrong filenames). E.g. trying to rename 1 - foo.jpg and my folder already had 1.jpg in it.
                    – byxor
                    Oct 18 '17 at 14:05







                  • 1




                    I find backticks bit hard to read when they are near quotes. The same but more readable would be for file in *; do mv "$file" $(echo $file | tr ' ' '_') ; done
                    – Kamil S Jaron
                    Jul 3 at 8:58
















                  This didn't work for me. It claimed identical files existed (with the wrong filenames). E.g. trying to rename 1 - foo.jpg and my folder already had 1.jpg in it.
                  – byxor
                  Oct 18 '17 at 14:05





                  This didn't work for me. It claimed identical files existed (with the wrong filenames). E.g. trying to rename 1 - foo.jpg and my folder already had 1.jpg in it.
                  – byxor
                  Oct 18 '17 at 14:05





                  1




                  1




                  I find backticks bit hard to read when they are near quotes. The same but more readable would be for file in *; do mv "$file" $(echo $file | tr ' ' '_') ; done
                  – Kamil S Jaron
                  Jul 3 at 8:58





                  I find backticks bit hard to read when they are near quotes. The same but more readable would be for file in *; do mv "$file" $(echo $file | tr ' ' '_') ; done
                  – Kamil S Jaron
                  Jul 3 at 8:58













                  up vote
                  50
                  down vote













                  I prefer to use the command 'rename', which takes Perl-style regexes:



                  rename "s/ /_/g" *


                  You can do a dry run with the -n flag:



                  rename -n "s/ /_/g" *





                  share|improve this answer
















                  • 1




                    this will work if you have the perl-style rename and not the simpler redhat/fedora one
                    – David Dean
                    Nov 27 '09 at 5:56






                  • 5




                    the fedora version would be rename " " "_" *
                    – David Dean
                    Nov 27 '09 at 5:57






                  • 4




                    rename not available in OSX it appears.
                    – Etienne Low-Décarie
                    Feb 12 '14 at 16:53










                  • On macOS 10.12.3, rename is available.
                    – Sung Won Cho
                    Mar 14 '17 at 6:09










                  • For some reason rename does not do a single thing on my CentOS box. Either with regex or not.
                    – Buttle Butkus
                    Jun 28 '17 at 6:32














                  up vote
                  50
                  down vote













                  I prefer to use the command 'rename', which takes Perl-style regexes:



                  rename "s/ /_/g" *


                  You can do a dry run with the -n flag:



                  rename -n "s/ /_/g" *





                  share|improve this answer
















                  • 1




                    this will work if you have the perl-style rename and not the simpler redhat/fedora one
                    – David Dean
                    Nov 27 '09 at 5:56






                  • 5




                    the fedora version would be rename " " "_" *
                    – David Dean
                    Nov 27 '09 at 5:57






                  • 4




                    rename not available in OSX it appears.
                    – Etienne Low-Décarie
                    Feb 12 '14 at 16:53










                  • On macOS 10.12.3, rename is available.
                    – Sung Won Cho
                    Mar 14 '17 at 6:09










                  • For some reason rename does not do a single thing on my CentOS box. Either with regex or not.
                    – Buttle Butkus
                    Jun 28 '17 at 6:32












                  up vote
                  50
                  down vote










                  up vote
                  50
                  down vote









                  I prefer to use the command 'rename', which takes Perl-style regexes:



                  rename "s/ /_/g" *


                  You can do a dry run with the -n flag:



                  rename -n "s/ /_/g" *





                  share|improve this answer












                  I prefer to use the command 'rename', which takes Perl-style regexes:



                  rename "s/ /_/g" *


                  You can do a dry run with the -n flag:



                  rename -n "s/ /_/g" *






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 27 '09 at 5:45









                  DF.

                  53133




                  53133







                  • 1




                    this will work if you have the perl-style rename and not the simpler redhat/fedora one
                    – David Dean
                    Nov 27 '09 at 5:56






                  • 5




                    the fedora version would be rename " " "_" *
                    – David Dean
                    Nov 27 '09 at 5:57






                  • 4




                    rename not available in OSX it appears.
                    – Etienne Low-Décarie
                    Feb 12 '14 at 16:53










                  • On macOS 10.12.3, rename is available.
                    – Sung Won Cho
                    Mar 14 '17 at 6:09










                  • For some reason rename does not do a single thing on my CentOS box. Either with regex or not.
                    – Buttle Butkus
                    Jun 28 '17 at 6:32












                  • 1




                    this will work if you have the perl-style rename and not the simpler redhat/fedora one
                    – David Dean
                    Nov 27 '09 at 5:56






                  • 5




                    the fedora version would be rename " " "_" *
                    – David Dean
                    Nov 27 '09 at 5:57






                  • 4




                    rename not available in OSX it appears.
                    – Etienne Low-Décarie
                    Feb 12 '14 at 16:53










                  • On macOS 10.12.3, rename is available.
                    – Sung Won Cho
                    Mar 14 '17 at 6:09










                  • For some reason rename does not do a single thing on my CentOS box. Either with regex or not.
                    – Buttle Butkus
                    Jun 28 '17 at 6:32







                  1




                  1




                  this will work if you have the perl-style rename and not the simpler redhat/fedora one
                  – David Dean
                  Nov 27 '09 at 5:56




                  this will work if you have the perl-style rename and not the simpler redhat/fedora one
                  – David Dean
                  Nov 27 '09 at 5:56




                  5




                  5




                  the fedora version would be rename " " "_" *
                  – David Dean
                  Nov 27 '09 at 5:57




                  the fedora version would be rename " " "_" *
                  – David Dean
                  Nov 27 '09 at 5:57




                  4




                  4




                  rename not available in OSX it appears.
                  – Etienne Low-Décarie
                  Feb 12 '14 at 16:53




                  rename not available in OSX it appears.
                  – Etienne Low-Décarie
                  Feb 12 '14 at 16:53












                  On macOS 10.12.3, rename is available.
                  – Sung Won Cho
                  Mar 14 '17 at 6:09




                  On macOS 10.12.3, rename is available.
                  – Sung Won Cho
                  Mar 14 '17 at 6:09












                  For some reason rename does not do a single thing on my CentOS box. Either with regex or not.
                  – Buttle Butkus
                  Jun 28 '17 at 6:32




                  For some reason rename does not do a single thing on my CentOS box. Either with regex or not.
                  – Buttle Butkus
                  Jun 28 '17 at 6:32










                  up vote
                  9
                  down vote













                  Use sh...



                  for i in *' '*; do mv "$i" `echo $i | sed -e 's/ /_/g'`; done


                  If you want to try this out before pulling the trigger just change mv to echo mv.






                  share|improve this answer




















                  • This one also worked fine
                    – Mithun Sreedharan
                    Nov 27 '09 at 7:03














                  up vote
                  9
                  down vote













                  Use sh...



                  for i in *' '*; do mv "$i" `echo $i | sed -e 's/ /_/g'`; done


                  If you want to try this out before pulling the trigger just change mv to echo mv.






                  share|improve this answer




















                  • This one also worked fine
                    – Mithun Sreedharan
                    Nov 27 '09 at 7:03












                  up vote
                  9
                  down vote










                  up vote
                  9
                  down vote









                  Use sh...



                  for i in *' '*; do mv "$i" `echo $i | sed -e 's/ /_/g'`; done


                  If you want to try this out before pulling the trigger just change mv to echo mv.






                  share|improve this answer












                  Use sh...



                  for i in *' '*; do mv "$i" `echo $i | sed -e 's/ /_/g'`; done


                  If you want to try this out before pulling the trigger just change mv to echo mv.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 27 '09 at 5:39









                  DigitalRoss

                  122k18206296




                  122k18206296











                  • This one also worked fine
                    – Mithun Sreedharan
                    Nov 27 '09 at 7:03
















                  • This one also worked fine
                    – Mithun Sreedharan
                    Nov 27 '09 at 7:03















                  This one also worked fine
                  – Mithun Sreedharan
                  Nov 27 '09 at 7:03




                  This one also worked fine
                  – Mithun Sreedharan
                  Nov 27 '09 at 7:03










                  up vote
                  4
                  down vote













                  What if you want to apply the replace task recursively? How would you do that?



                  Well, I just found the answer myself. No the most elegant solution (tries to rename also files that do not comply with the condition) but works. (BTW, in my case I needed to rename the files with '%20', not with an underscore)



                  #!/bin/bash
                  find . -type d | while read N
                  do
                  (
                  cd "$N"
                  if test "$?" = "0"
                  then
                  for file in *; do mv "$file" $file// /%20; done
                  fi
                  )
                  done





                  share|improve this answer


























                    up vote
                    4
                    down vote













                    What if you want to apply the replace task recursively? How would you do that?



                    Well, I just found the answer myself. No the most elegant solution (tries to rename also files that do not comply with the condition) but works. (BTW, in my case I needed to rename the files with '%20', not with an underscore)



                    #!/bin/bash
                    find . -type d | while read N
                    do
                    (
                    cd "$N"
                    if test "$?" = "0"
                    then
                    for file in *; do mv "$file" $file// /%20; done
                    fi
                    )
                    done





                    share|improve this answer
























                      up vote
                      4
                      down vote










                      up vote
                      4
                      down vote









                      What if you want to apply the replace task recursively? How would you do that?



                      Well, I just found the answer myself. No the most elegant solution (tries to rename also files that do not comply with the condition) but works. (BTW, in my case I needed to rename the files with '%20', not with an underscore)



                      #!/bin/bash
                      find . -type d | while read N
                      do
                      (
                      cd "$N"
                      if test "$?" = "0"
                      then
                      for file in *; do mv "$file" $file// /%20; done
                      fi
                      )
                      done





                      share|improve this answer














                      What if you want to apply the replace task recursively? How would you do that?



                      Well, I just found the answer myself. No the most elegant solution (tries to rename also files that do not comply with the condition) but works. (BTW, in my case I needed to rename the files with '%20', not with an underscore)



                      #!/bin/bash
                      find . -type d | while read N
                      do
                      (
                      cd "$N"
                      if test "$?" = "0"
                      then
                      for file in *; do mv "$file" $file// /%20; done
                      fi
                      )
                      done






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Apr 2 '10 at 21:08

























                      answered Apr 2 '10 at 20:33









                      javipas

                      2061414




                      2061414




















                          up vote
                          3
                          down vote













                          If you use bash:



                          for file in *; do mv "$file" $file// /_; done





                          share|improve this answer






















                          • when i tried, i got mv: when moving multiple files, last argument must be a directory Try mv --help' for more information. mv: when moving multiple files, last argument must be a directory Try mv --help' for more information.
                            – Mithun Sreedharan
                            Nov 27 '09 at 5:32










                          • Should be $FILE/ /_
                            – soulmerge
                            Nov 27 '09 at 5:49










                          • that only replace 1 space
                            – ghostdog74
                            Nov 27 '09 at 5:56










                          • Again error mv: missing file argument Try mv --help' for more information. mv: missing file argument Try mv --help' for more information. mv: missing file argument Try mv --help' for more information. mv: missing file argument Try mv --help' for more information.
                            – Mithun Sreedharan
                            Nov 27 '09 at 7:07










                          • @levislevis85: Thx, didn't know that.
                            – soulmerge
                            Nov 27 '09 at 7:13














                          up vote
                          3
                          down vote













                          If you use bash:



                          for file in *; do mv "$file" $file// /_; done





                          share|improve this answer






















                          • when i tried, i got mv: when moving multiple files, last argument must be a directory Try mv --help' for more information. mv: when moving multiple files, last argument must be a directory Try mv --help' for more information.
                            – Mithun Sreedharan
                            Nov 27 '09 at 5:32










                          • Should be $FILE/ /_
                            – soulmerge
                            Nov 27 '09 at 5:49










                          • that only replace 1 space
                            – ghostdog74
                            Nov 27 '09 at 5:56










                          • Again error mv: missing file argument Try mv --help' for more information. mv: missing file argument Try mv --help' for more information. mv: missing file argument Try mv --help' for more information. mv: missing file argument Try mv --help' for more information.
                            – Mithun Sreedharan
                            Nov 27 '09 at 7:07










                          • @levislevis85: Thx, didn't know that.
                            – soulmerge
                            Nov 27 '09 at 7:13












                          up vote
                          3
                          down vote










                          up vote
                          3
                          down vote









                          If you use bash:



                          for file in *; do mv "$file" $file// /_; done





                          share|improve this answer














                          If you use bash:



                          for file in *; do mv "$file" $file// /_; done






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 27 '09 at 5:44

























                          answered Nov 27 '09 at 5:24









                          Murali VP

                          3,84112331




                          3,84112331











                          • when i tried, i got mv: when moving multiple files, last argument must be a directory Try mv --help' for more information. mv: when moving multiple files, last argument must be a directory Try mv --help' for more information.
                            – Mithun Sreedharan
                            Nov 27 '09 at 5:32










                          • Should be $FILE/ /_
                            – soulmerge
                            Nov 27 '09 at 5:49










                          • that only replace 1 space
                            – ghostdog74
                            Nov 27 '09 at 5:56










                          • Again error mv: missing file argument Try mv --help' for more information. mv: missing file argument Try mv --help' for more information. mv: missing file argument Try mv --help' for more information. mv: missing file argument Try mv --help' for more information.
                            – Mithun Sreedharan
                            Nov 27 '09 at 7:07










                          • @levislevis85: Thx, didn't know that.
                            – soulmerge
                            Nov 27 '09 at 7:13
















                          • when i tried, i got mv: when moving multiple files, last argument must be a directory Try mv --help' for more information. mv: when moving multiple files, last argument must be a directory Try mv --help' for more information.
                            – Mithun Sreedharan
                            Nov 27 '09 at 5:32










                          • Should be $FILE/ /_
                            – soulmerge
                            Nov 27 '09 at 5:49










                          • that only replace 1 space
                            – ghostdog74
                            Nov 27 '09 at 5:56










                          • Again error mv: missing file argument Try mv --help' for more information. mv: missing file argument Try mv --help' for more information. mv: missing file argument Try mv --help' for more information. mv: missing file argument Try mv --help' for more information.
                            – Mithun Sreedharan
                            Nov 27 '09 at 7:07










                          • @levislevis85: Thx, didn't know that.
                            – soulmerge
                            Nov 27 '09 at 7:13















                          when i tried, i got mv: when moving multiple files, last argument must be a directory Try mv --help' for more information. mv: when moving multiple files, last argument must be a directory Try mv --help' for more information.
                          – Mithun Sreedharan
                          Nov 27 '09 at 5:32




                          when i tried, i got mv: when moving multiple files, last argument must be a directory Try mv --help' for more information. mv: when moving multiple files, last argument must be a directory Try mv --help' for more information.
                          – Mithun Sreedharan
                          Nov 27 '09 at 5:32












                          Should be $FILE/ /_
                          – soulmerge
                          Nov 27 '09 at 5:49




                          Should be $FILE/ /_
                          – soulmerge
                          Nov 27 '09 at 5:49












                          that only replace 1 space
                          – ghostdog74
                          Nov 27 '09 at 5:56




                          that only replace 1 space
                          – ghostdog74
                          Nov 27 '09 at 5:56












                          Again error mv: missing file argument Try mv --help' for more information. mv: missing file argument Try mv --help' for more information. mv: missing file argument Try mv --help' for more information. mv: missing file argument Try mv --help' for more information.
                          – Mithun Sreedharan
                          Nov 27 '09 at 7:07




                          Again error mv: missing file argument Try mv --help' for more information. mv: missing file argument Try mv --help' for more information. mv: missing file argument Try mv --help' for more information. mv: missing file argument Try mv --help' for more information.
                          – Mithun Sreedharan
                          Nov 27 '09 at 7:07












                          @levislevis85: Thx, didn't know that.
                          – soulmerge
                          Nov 27 '09 at 7:13




                          @levislevis85: Thx, didn't know that.
                          – soulmerge
                          Nov 27 '09 at 7:13










                          up vote
                          1
                          down vote













                          Quote your variables:



                          for file in *; do echo mv "'$file'" "$file// /_"; done


                          Remove the "echo" to do the actual rename.






                          share|improve this answer






















                          • It is echoing the mv commands prperly, but not really renaming the file!
                            – Mithun Sreedharan
                            Nov 27 '09 at 7:05










                          • removing echo produces error like mv: cannot stat '1130 lake micigan view.jpg'': No such file or directory mv: cannot stat '1130_1_bedroom_floor_plan.jpg'': No such file or directory mv: cannot stat '1130_BedPicture_8.jpg'': No such file or directory mv: cannot stat '1130_diningroom_table.jpg'': No such file or directory
                            – Mithun Sreedharan
                            Nov 27 '09 at 7:08










                          • what is your OS?
                            – ghostdog74
                            Nov 27 '09 at 7:26










                          • what shell are you using?
                            – ghostdog74
                            Nov 27 '09 at 7:27










                          • Linux Linux 2.6.9-42.0.3.EL.wh1smp #1 SMP Fri Aug 14 15:48:17 MDT 2009 i686 i686 i386 GNU/Linux
                            – Mithun Sreedharan
                            Nov 27 '09 at 11:54














                          up vote
                          1
                          down vote













                          Quote your variables:



                          for file in *; do echo mv "'$file'" "$file// /_"; done


                          Remove the "echo" to do the actual rename.






                          share|improve this answer






















                          • It is echoing the mv commands prperly, but not really renaming the file!
                            – Mithun Sreedharan
                            Nov 27 '09 at 7:05










                          • removing echo produces error like mv: cannot stat '1130 lake micigan view.jpg'': No such file or directory mv: cannot stat '1130_1_bedroom_floor_plan.jpg'': No such file or directory mv: cannot stat '1130_BedPicture_8.jpg'': No such file or directory mv: cannot stat '1130_diningroom_table.jpg'': No such file or directory
                            – Mithun Sreedharan
                            Nov 27 '09 at 7:08










                          • what is your OS?
                            – ghostdog74
                            Nov 27 '09 at 7:26










                          • what shell are you using?
                            – ghostdog74
                            Nov 27 '09 at 7:27










                          • Linux Linux 2.6.9-42.0.3.EL.wh1smp #1 SMP Fri Aug 14 15:48:17 MDT 2009 i686 i686 i386 GNU/Linux
                            – Mithun Sreedharan
                            Nov 27 '09 at 11:54












                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          Quote your variables:



                          for file in *; do echo mv "'$file'" "$file// /_"; done


                          Remove the "echo" to do the actual rename.






                          share|improve this answer














                          Quote your variables:



                          for file in *; do echo mv "'$file'" "$file// /_"; done


                          Remove the "echo" to do the actual rename.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Mar 30 '15 at 12:17









                          Peter Mortensen

                          13.4k1983111




                          13.4k1983111










                          answered Nov 27 '09 at 5:42









                          ghostdog74

                          214k39210296




                          214k39210296











                          • It is echoing the mv commands prperly, but not really renaming the file!
                            – Mithun Sreedharan
                            Nov 27 '09 at 7:05










                          • removing echo produces error like mv: cannot stat '1130 lake micigan view.jpg'': No such file or directory mv: cannot stat '1130_1_bedroom_floor_plan.jpg'': No such file or directory mv: cannot stat '1130_BedPicture_8.jpg'': No such file or directory mv: cannot stat '1130_diningroom_table.jpg'': No such file or directory
                            – Mithun Sreedharan
                            Nov 27 '09 at 7:08










                          • what is your OS?
                            – ghostdog74
                            Nov 27 '09 at 7:26










                          • what shell are you using?
                            – ghostdog74
                            Nov 27 '09 at 7:27










                          • Linux Linux 2.6.9-42.0.3.EL.wh1smp #1 SMP Fri Aug 14 15:48:17 MDT 2009 i686 i686 i386 GNU/Linux
                            – Mithun Sreedharan
                            Nov 27 '09 at 11:54
















                          • It is echoing the mv commands prperly, but not really renaming the file!
                            – Mithun Sreedharan
                            Nov 27 '09 at 7:05










                          • removing echo produces error like mv: cannot stat '1130 lake micigan view.jpg'': No such file or directory mv: cannot stat '1130_1_bedroom_floor_plan.jpg'': No such file or directory mv: cannot stat '1130_BedPicture_8.jpg'': No such file or directory mv: cannot stat '1130_diningroom_table.jpg'': No such file or directory
                            – Mithun Sreedharan
                            Nov 27 '09 at 7:08










                          • what is your OS?
                            – ghostdog74
                            Nov 27 '09 at 7:26










                          • what shell are you using?
                            – ghostdog74
                            Nov 27 '09 at 7:27










                          • Linux Linux 2.6.9-42.0.3.EL.wh1smp #1 SMP Fri Aug 14 15:48:17 MDT 2009 i686 i686 i386 GNU/Linux
                            – Mithun Sreedharan
                            Nov 27 '09 at 11:54















                          It is echoing the mv commands prperly, but not really renaming the file!
                          – Mithun Sreedharan
                          Nov 27 '09 at 7:05




                          It is echoing the mv commands prperly, but not really renaming the file!
                          – Mithun Sreedharan
                          Nov 27 '09 at 7:05












                          removing echo produces error like mv: cannot stat '1130 lake micigan view.jpg'': No such file or directory mv: cannot stat '1130_1_bedroom_floor_plan.jpg'': No such file or directory mv: cannot stat '1130_BedPicture_8.jpg'': No such file or directory mv: cannot stat '1130_diningroom_table.jpg'': No such file or directory
                          – Mithun Sreedharan
                          Nov 27 '09 at 7:08




                          removing echo produces error like mv: cannot stat '1130 lake micigan view.jpg'': No such file or directory mv: cannot stat '1130_1_bedroom_floor_plan.jpg'': No such file or directory mv: cannot stat '1130_BedPicture_8.jpg'': No such file or directory mv: cannot stat '1130_diningroom_table.jpg'': No such file or directory
                          – Mithun Sreedharan
                          Nov 27 '09 at 7:08












                          what is your OS?
                          – ghostdog74
                          Nov 27 '09 at 7:26




                          what is your OS?
                          – ghostdog74
                          Nov 27 '09 at 7:26












                          what shell are you using?
                          – ghostdog74
                          Nov 27 '09 at 7:27




                          what shell are you using?
                          – ghostdog74
                          Nov 27 '09 at 7:27












                          Linux Linux 2.6.9-42.0.3.EL.wh1smp #1 SMP Fri Aug 14 15:48:17 MDT 2009 i686 i686 i386 GNU/Linux
                          – Mithun Sreedharan
                          Nov 27 '09 at 11:54




                          Linux Linux 2.6.9-42.0.3.EL.wh1smp #1 SMP Fri Aug 14 15:48:17 MDT 2009 i686 i686 i386 GNU/Linux
                          – Mithun Sreedharan
                          Nov 27 '09 at 11:54










                          up vote
                          1
                          down vote













                          Try something like this, assuming all of your files were .txt's:



                          for files in *.txt; do mv “$files” `echo $files | tr ‘ ‘ ‘_’`; done





                          share|improve this answer


















                          • 2




                            got the below error tr: two strings must be given when translating mv: missing file argument Try `mv --help' for more information.
                            – Mithun Sreedharan
                            Nov 27 '09 at 5:31






                          • 2




                            Agin error tr: too many arguments Try tr --help' for more information. mv: missing file argument Try mv --help' for more information.
                            – Mithun Sreedharan
                            Nov 27 '09 at 7:06














                          up vote
                          1
                          down vote













                          Try something like this, assuming all of your files were .txt's:



                          for files in *.txt; do mv “$files” `echo $files | tr ‘ ‘ ‘_’`; done





                          share|improve this answer


















                          • 2




                            got the below error tr: two strings must be given when translating mv: missing file argument Try `mv --help' for more information.
                            – Mithun Sreedharan
                            Nov 27 '09 at 5:31






                          • 2




                            Agin error tr: too many arguments Try tr --help' for more information. mv: missing file argument Try mv --help' for more information.
                            – Mithun Sreedharan
                            Nov 27 '09 at 7:06












                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          Try something like this, assuming all of your files were .txt's:



                          for files in *.txt; do mv “$files” `echo $files | tr ‘ ‘ ‘_’`; done





                          share|improve this answer














                          Try something like this, assuming all of your files were .txt's:



                          for files in *.txt; do mv “$files” `echo $files | tr ‘ ‘ ‘_’`; done






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Apr 9 '15 at 15:00









                          Peter Mortensen

                          13.4k1983111




                          13.4k1983111










                          answered Nov 27 '09 at 5:21









                          Amir Afghani

                          28.6k1568112




                          28.6k1568112







                          • 2




                            got the below error tr: two strings must be given when translating mv: missing file argument Try `mv --help' for more information.
                            – Mithun Sreedharan
                            Nov 27 '09 at 5:31






                          • 2




                            Agin error tr: too many arguments Try tr --help' for more information. mv: missing file argument Try mv --help' for more information.
                            – Mithun Sreedharan
                            Nov 27 '09 at 7:06












                          • 2




                            got the below error tr: two strings must be given when translating mv: missing file argument Try `mv --help' for more information.
                            – Mithun Sreedharan
                            Nov 27 '09 at 5:31






                          • 2




                            Agin error tr: too many arguments Try tr --help' for more information. mv: missing file argument Try mv --help' for more information.
                            – Mithun Sreedharan
                            Nov 27 '09 at 7:06







                          2




                          2




                          got the below error tr: two strings must be given when translating mv: missing file argument Try `mv --help' for more information.
                          – Mithun Sreedharan
                          Nov 27 '09 at 5:31




                          got the below error tr: two strings must be given when translating mv: missing file argument Try `mv --help' for more information.
                          – Mithun Sreedharan
                          Nov 27 '09 at 5:31




                          2




                          2




                          Agin error tr: too many arguments Try tr --help' for more information. mv: missing file argument Try mv --help' for more information.
                          – Mithun Sreedharan
                          Nov 27 '09 at 7:06




                          Agin error tr: too many arguments Try tr --help' for more information. mv: missing file argument Try mv --help' for more information.
                          – Mithun Sreedharan
                          Nov 27 '09 at 7:06










                          up vote
                          1
                          down vote













                          Here is another solution:



                          ls | awk 'printf(""%s"n", $0)' | sed 'p; s/ /_/g' | xargs -n2 mv


                          1. uses awk to add quotes around the name of the file

                          2. uses sed to replace space with underscores; prints the original name with quotes(from awk); then the substituted name

                          3. xargs takes 2 lines at a time and passes it to mv





                          share|improve this answer
























                            up vote
                            1
                            down vote













                            Here is another solution:



                            ls | awk 'printf(""%s"n", $0)' | sed 'p; s/ /_/g' | xargs -n2 mv


                            1. uses awk to add quotes around the name of the file

                            2. uses sed to replace space with underscores; prints the original name with quotes(from awk); then the substituted name

                            3. xargs takes 2 lines at a time and passes it to mv





                            share|improve this answer






















                              up vote
                              1
                              down vote










                              up vote
                              1
                              down vote









                              Here is another solution:



                              ls | awk 'printf(""%s"n", $0)' | sed 'p; s/ /_/g' | xargs -n2 mv


                              1. uses awk to add quotes around the name of the file

                              2. uses sed to replace space with underscores; prints the original name with quotes(from awk); then the substituted name

                              3. xargs takes 2 lines at a time and passes it to mv





                              share|improve this answer












                              Here is another solution:



                              ls | awk 'printf(""%s"n", $0)' | sed 'p; s/ /_/g' | xargs -n2 mv


                              1. uses awk to add quotes around the name of the file

                              2. uses sed to replace space with underscores; prints the original name with quotes(from awk); then the substituted name

                              3. xargs takes 2 lines at a time and passes it to mv






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 9 at 13:51









                              yoogottamk

                              111




                              111




















                                  up vote
                                  0
                                  down vote













                                  I believe your answer is in Replace spaces in filenames with underscores.






                                  share|improve this answer


















                                  • 1




                                    While this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
                                    – Bill the Lizard
                                    Oct 27 '11 at 14:58















                                  up vote
                                  0
                                  down vote













                                  I believe your answer is in Replace spaces in filenames with underscores.






                                  share|improve this answer


















                                  • 1




                                    While this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
                                    – Bill the Lizard
                                    Oct 27 '11 at 14:58













                                  up vote
                                  0
                                  down vote










                                  up vote
                                  0
                                  down vote









                                  I believe your answer is in Replace spaces in filenames with underscores.






                                  share|improve this answer














                                  I believe your answer is in Replace spaces in filenames with underscores.







                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Apr 9 '15 at 14:59









                                  Peter Mortensen

                                  13.4k1983111




                                  13.4k1983111










                                  answered Nov 27 '09 at 5:18









                                  Arthur Frankel

                                  3,24532950




                                  3,24532950







                                  • 1




                                    While this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
                                    – Bill the Lizard
                                    Oct 27 '11 at 14:58













                                  • 1




                                    While this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
                                    – Bill the Lizard
                                    Oct 27 '11 at 14:58








                                  1




                                  1




                                  While this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
                                  – Bill the Lizard
                                  Oct 27 '11 at 14:58





                                  While this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
                                  – Bill the Lizard
                                  Oct 27 '11 at 14:58











                                  up vote
                                  0
                                  down vote













                                  The easiest way to replace a string (space character in your case) with another string in Linux is using sed. You can do it as follows



                                  sed -i 's/s/_/g' *


                                  Hope this helps.






                                  share|improve this answer
























                                    up vote
                                    0
                                    down vote













                                    The easiest way to replace a string (space character in your case) with another string in Linux is using sed. You can do it as follows



                                    sed -i 's/s/_/g' *


                                    Hope this helps.






                                    share|improve this answer






















                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      The easiest way to replace a string (space character in your case) with another string in Linux is using sed. You can do it as follows



                                      sed -i 's/s/_/g' *


                                      Hope this helps.






                                      share|improve this answer












                                      The easiest way to replace a string (space character in your case) with another string in Linux is using sed. You can do it as follows



                                      sed -i 's/s/_/g' *


                                      Hope this helps.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Dec 9 '16 at 13:03









                                      Desta Haileselassie Hagos

                                      9,23422838




                                      9,23422838




















                                          up vote
                                          0
                                          down vote













                                          To rename all the files with a .py extension use,

                                          find . -iname "*.py" -type f | xargs -I% rename "s/ /_/g" "%"



                                          Sample output,



                                          $ find . -iname "*.py" -type f 
                                          ./Sample File.py
                                          ./Sample/Sample File.py
                                          $ find . -iname "*.py" -type f | xargs -I% rename "s/ /_/g" "%"
                                          $ find . -iname "*.py" -type f
                                          ./Sample/Sample_File.py
                                          ./Sample_File.py





                                          share|improve this answer
























                                            up vote
                                            0
                                            down vote













                                            To rename all the files with a .py extension use,

                                            find . -iname "*.py" -type f | xargs -I% rename "s/ /_/g" "%"



                                            Sample output,



                                            $ find . -iname "*.py" -type f 
                                            ./Sample File.py
                                            ./Sample/Sample File.py
                                            $ find . -iname "*.py" -type f | xargs -I% rename "s/ /_/g" "%"
                                            $ find . -iname "*.py" -type f
                                            ./Sample/Sample_File.py
                                            ./Sample_File.py





                                            share|improve this answer






















                                              up vote
                                              0
                                              down vote










                                              up vote
                                              0
                                              down vote









                                              To rename all the files with a .py extension use,

                                              find . -iname "*.py" -type f | xargs -I% rename "s/ /_/g" "%"



                                              Sample output,



                                              $ find . -iname "*.py" -type f 
                                              ./Sample File.py
                                              ./Sample/Sample File.py
                                              $ find . -iname "*.py" -type f | xargs -I% rename "s/ /_/g" "%"
                                              $ find . -iname "*.py" -type f
                                              ./Sample/Sample_File.py
                                              ./Sample_File.py





                                              share|improve this answer












                                              To rename all the files with a .py extension use,

                                              find . -iname "*.py" -type f | xargs -I% rename "s/ /_/g" "%"



                                              Sample output,



                                              $ find . -iname "*.py" -type f 
                                              ./Sample File.py
                                              ./Sample/Sample File.py
                                              $ find . -iname "*.py" -type f | xargs -I% rename "s/ /_/g" "%"
                                              $ find . -iname "*.py" -type f
                                              ./Sample/Sample_File.py
                                              ./Sample_File.py






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Apr 7 at 9:21









                                              akilesh raj

                                              1221112




                                              1221112



























                                                  draft saved

                                                  draft discarded
















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid


                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.

                                                  To learn more, see our tips on writing great answers.





                                                  Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                  Please pay close attention to the following guidance:


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid


                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.

                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function ()
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1806868%2flinux-replacing-spaces-in-the-file-names%23new-answer', 'question_page');

                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                                                  PHP code is not being executed, instead code shows on the page