script

Joined
Jun 14, 2006
Messages
140
Reaction score
1
Points
18
Not sure if this is the right forum, but can someone write me a script that will strip all 2nd spaces in a file name?

Example. If a file name is: Junk Mail.txt it will make it Junk Mail.txt (gets rid of the extra space. Everytime I transfer files to my linux box that have more than one consecutive space, it doesn't seem to open, transfer back, or anything. I'm not sure if there is an option in linux to fix this (running SuSe 10.1). But I would like to have a script to be able to do this to all files in a single directory.

Thanks!
 
Joined
Jun 6, 2006
Messages
1,153
Reaction score
94
Points
48
Your Mac's Specs
MacBook 2.0GHz White, 512MB RAM, 60GB HDD
There's no reason you can't have multiple consecutive spaces in filenames in linux. You just have to remember to put quotes round the filename or escape each space individually.

However, to do what you want, try this (it should work under Linux, and quite possibly under Mac OS X too, as long as you are using bash)

Code:
for f in *
  do
  n=${f//  */ /}  # <-- note that this is f//{space}{space}*/{space}/
  mv "$f" "$n"
done

Run this in the directory containing the files and it will do the magic. The 'for f in *' line can be changed to match just the files (e.g. for f in *.txt).
 
Joined
Jun 6, 2006
Messages
1,153
Reaction score
94
Points
48
Your Mac's Specs
MacBook 2.0GHz White, 512MB RAM, 60GB HDD
.sh should do, although it's just for your benefit really. To run it from a script, use `sh {scriptname}`
 
OP
U
Joined
Jun 14, 2006
Messages
140
Reaction score
1
Points
18
alright, thanks! I need to learn how to write more shell commands.. would make organizing my files a lot easier :)
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top