Remove 'blanks' in a filename

Score
0
Vote Up
April 30, 2009 at 07:59:28 Pacific
Specs: Macintosh

Hello all,
I have many files labeled: 001 _MP, 002 _MP...160 _MP

How do I rename all the files to 001_MP, 002_MP? That is remove the space?
I may need to script this as this occurs quite often

Thanks for your time


Reply ↓  Report •


#1
Vote Down
Score
0
Vote Up
April 30, 2009 at 08:55:29 Pacific

This topic was discussed in this thread:

http://www.computing.net/answers/un...


Reply ↓  Report •

#2
Vote Down
Score
3
Vote Up
April 30, 2009 at 20:31:39 Pacific

if you are using bash, no need to use external tools
for files in *MP
do
 mv "$files" "${files// /}"
done

Unix Win32 tools | Gawk for Windows


Reply ↓  Report •

#3
Vote Down
Score
2
Vote Up
May 1, 2009 at 10:01:18 Pacific

OK Thanks here's what I end up using:

#!/bin/sh
find . -name "* *"|while read file
do
target=`echo "$file"|tr -s ' ' '_'`
mv "$file" "$target"
done


Reply ↓  Report •

#4
Vote Down
Score
0
Vote Up
May 20, 2009 at 22:50:00 Pacific

This one is for Korn Shell.

#!/bin/ksh

for file in *MP
do
print $file | while read part1 part2
do
mv "$file" "$part1$part2"
done
done

ghostdog's bash solution is also excellent. Both his solution and mine avoid the need to go outside the shell to accomplish the task which is always the best way to do things. Your solution uses the find, echo and the tr commands unnecessarily.


Reply ↓  Report •

Reply to Message Icon Start New Discussion
Related Posts

« Unix Shell Scripting $! Need help printing in awk... »

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.

Ask the Community!
Describe your Problem
Example: Hard Drive Not Detected on My PC