Jump to content

Remove Or Replace Spaces


NCC10281982B
 Share

Recommended Posts

My webserver runs linux. So it does not like spaces (duh) I get a lot of reviews that have spaces in the file and folder names. Are there any commands in can run in a bash script to replace spaces with underscores or dashs?

Share this post


Link to post
Share on other sites

Something like this should be along the right lines:

 

#!/bin/sh

for LINE in "[email protected]"
do
 NEWNAME=`echo ${LINE} | sed 's/ /_/g'`
 if [ x"${LINE}" != x"${NEWNAME}" ]; then
echo "mv -- \"${LINE}\" ${NEWNAME}"
#mv -- "${LINE}" ${NEWNAME}
 fi
done

 

([email protected])/home/mrboo$ ./rename_spaces.sh *

mv -- "ABIT BIOS" ABIT_BIOS

mv -- "ABIT NF7-S 3.0.00 Fire Bpl 3.19C.txt" ABIT_NF7-S_3.0.00_Fire_Bpl_3.19C.txt

mv -- "Copy (2) of Coursework 1.nqc" Copy_(2)_of_Coursework_1.nqc

mv -- "Copy of Coursework 1.nqc" Copy_of_Coursework_1.nqc

mv -- "Coursework 1.nqc" Coursework_1.nqc

mv -- "b26 hand in 1.txt" b26_hand_in_1.txt

mv -- "b26 hand in sql.txt" b26_hand_in_sql.txt

mv -- "b26 proper test data.txt" b26_proper_test_data.txt

mv -- "b26 table def.txt" b26_table_def.txt

mv -- "b26 test data.txt" b26_test_data.txt

mv -- "internet tech individual report.doc" internet_tech_individual_report.doc

mv -- "p20 review.doc" p20_review.doc

 

Do I get a thank you this time? :P

Share this post


Link to post
Share on other sites

THANK YOU

 

here is you cookie

 

btw my shell gives me this

 

At a guess, I would say that /bin/sh isn't actually sh. That's quite common these days, especially with Linux distro's.

 

Try the following instead and see if it makes any difference:

 

#!/bin/sh

for LINE in "[email protected]"
do
 NEWNAME=`echo ${LINE} | sed 's/ /_/g'`
 if [ x"${LINE}" != x"${NEWNAME}" ]
 then
echo "mv -- \"${LINE}\" ${NEWNAME}"
#mv -- "${LINE}" ${NEWNAME}
 fi
done

 

If not, try changing the #!/bin/sh to #!/whereever/bash/is.

Share this post


Link to post
Share on other sites

At a guess, I would say that /bin/sh isn't actually sh. That's quite common these days, especially with Linux distro's.

 

Try the following instead and see if it makes any difference:

 

#!/bin/sh

for LINE in "[email protected]"
do
 NEWNAME=`echo ${LINE} | sed 's/ /_/g'`
 if [ x"${LINE}" != x"${NEWNAME}" ]
 then
echo "mv -- \"${LINE}\" ${NEWNAME}"
#mv -- "${LINE}" ${NEWNAME}
 fi
done

 

If not, try changing the #!/bin/sh to #!/whereever/bash/is.

 

same error. :(

Share this post


Link to post
Share on other sites

same error. :(

 

Either your shell is very broken, or you've typed it in wrong. One thing that immediately comes to mind is that you *MUST* have spaces after and before the [ and ].

 

This is wrong:

 

if [x"${SOMETHING}" != x"${SOMETHINGELSE}"]; then

 

This is right (note the spaces):

 

if [ x"${SOMETHING" != x"${SOMETHINGELSE}" ]; then

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...