Results 1 to 10 of 10
Thread: Shell script confusion.
|
Enjoy an ad free experience by logging in. Not a member yet? Register.
|
|
-
09-16-2011 #1
Shell script confusion.
This script does not work:Code:#! /bin/bash # Remove spaces from filenames and replace with underscores. echo "$1 is " $1 fileName=`echo "$1"|sed 's/ /\\ /g'` echo "fileName = " $fileName rename -v 's/ /_/g' $fileName unset fileName exit=0 ~ ~ ~ ~ ~ ~ ~ ~ "rmspace.sh" 11L, 212C 11,0-1 All
Code:$ rmspace.sh ~/Videos/Medicine/Vaccine\ Nation\ -\ FULL\ LENGTH.flv /home/lugo/Videos/Medicine/Vaccine Nation - FULL LENGTH.flv is /home/lugo/Videos/Medicine/Vaccine Nation - FULL LENGTH.flv fileName = /home/lugo/Videos/Medicine/Vaccine Nation - FULL LENGTH.flv $
Code:$ echo "/home/lugo/Videos/Medicine/Vaccine Nation - FULL LENGTH.flv" | sed 's/ /\\ /g' /home/lugo/Videos/Medicine/Vaccine\ Nation\ -\ FULL\ LENGTH.flv $ $ rename -v 's/ /_/g' ~/Videos/Medicine/Vaccine\ Nation\ -\ FULL\ LENGTH.flv /home/lugo/Videos/Medicine/Vaccine Nation - FULL LENGTH.flv renamed as /home/lugo/Videos/Medicine/Vaccine_Nation_-_FULL_LENGTH.flv $
-
09-17-2011 #2
- Join Date
- Oct 2008
- Posts
- 153
Does this work for you?
for i in *\ *
do
mv "$i" "${i// /_}"
done
If you want to do one at a time you could just use the third line and replace the two letters i with a 1.Last edited by kurtdriver; 09-17-2011 at 02:02 PM.
-
09-17-2011 #3
- Join Date
- Jan 2011
- Location
- Cambridge, Ontario, Canada
- Posts
- 24
Replace: fileName=`echo "$1"|sed 's/ /\\ /g'`
With: fileName=`echo "$1"|sed 's/ /_/g'`
and
Replace: rename -v 's/ /_/g' $fileName
With: rename $1 $fileName $1Last edited by PairOfBlanks2; 09-17-2011 at 02:22 AM. Reason: Completeness
-
09-17-2011 #4
- Join Date
- Oct 2008
- Posts
- 153
Why not just run sed on $1 directly, without piping it through echo?
-
09-17-2011 #5
- Join Date
- Aug 2006
- Posts
- 2
I have a script posted in the opensuse forums that will do what you want with little or no editing I think.
Post # 11 has the last version I left off with, it appears to work correctly but has not been extensively tested. I suggest you make a test run first.
It wont let me post a url yet so:
h-t-t-p-://forums.opensuse.org/english/get-technical-help-here/applications/457475-can-you-recommend-me-script-app.html#post2324828
-
09-17-2011 #6
Thanks all. Just got it to work:
Code:#! /bin/bash # Remove spaces from filenames and replace with underscores. echo "$1 is " $1 #fileName=$(echo $1|sed 's/ /\\ /g') #echo "fileName = " $fileName #rename -v 's/ /_/g' $(echo $1|sed 's/ /\\ /g') echo $1|rename -v 's/ /_/g' #unset fileName exit=0 ~ ~ ~ ~ ~ ~ ~ "rmspace.sh" 12L, 260C 12,0-1 All
Must say I don't understand what was wrong before. I'll try sugestions and see what happens.
-
09-17-2011 #7
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
- Posts
- 1,471
Hi.
Quoting issue. See the differences between this script in file "user1" and your original:
Code:#! /bin/bash # Remove spaces from filenames and replace with underscores. # Remove debris from previous runs and set up file as parameter 1. echo rm -f "f 1" f__1 touch "f 1" ls -lgG f* set -- "f 1" echo echo "arg 1 is \"$1\"" # fileName=`echo "$1"|sed 's/ /\\ /g'` fileName=$1 echo "fileName = " $fileName echo "fileName = " "$fileName" echo # rename -v 's/ /_/g' $fileName rename -v 's/ /_/g' "$fileName" unset fileName ls -lgG f* # exit=0 exit 0
Code:% ./user1 -rw-r--r-- 1 0 Sep 17 06:17 f 1 arg 1 is "f 1" fileName = f 1 fileName = f 1 f 1 renamed as f__1 -rw-r--r-- 1 0 Sep 17 06:17 f__1
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
-
09-17-2011 #8
- Join Date
- Jan 2011
- Location
- Cambridge, Ontario, Canada
- Posts
- 24
-
09-18-2011 #9
- Join Date
- Jul 2007
- Posts
- 2
The following script is tested to work.
#! /bin/bash
THEDIR=/home/linux/a/* #define the directory where script will remove the space in file names
echo ----------------------------------
for files in $THEDIR;
do
newfile=`echo $files | sed -e 's/\ /\_/g'`
if [ "$newfile" != "$files" ]; then
echo "oldfilename is $files"
echo "newfilename is $newfile"
mv "$files" "$newfile"
echo "$files is renamed to $newfile"
else
echo "$files doesn\'t contain space"
fi
echo ----------------------------------
doneLast edited by bigcrab; 09-18-2011 at 02:26 AM.
-
09-18-2011 #10
Gracias.
dri: Understand quotes better now. Have even worked out that my improved script above, amounts to: echo $1|rename -v 's/ /_/g' , needs quotes round $1 to work for multiple consecutive spaces.