Computing.Net > Forums > Unix > How to remove spaces in filenames

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

How to remove spaces in filenames

Reply to Message Icon

Name: Sujan (by Sujan Banerjee)
Date: September 11, 2008 at 04:00:23 Pacific
OS: Solaris
CPU/Ram: 2GB
Product: Sparc
Comment:

Hi All,

In this Unix forum, i saw a msg from Gopalkrishna V titled "Deleting a file(Name) with spaces".
I was just wondering how can I find files with spaces in a large directory and rename files by removing those spaces.
Files can have multiple apaces,viz. "two space file" OR "double space".


Thanx in Advance

Sujan Banerjee



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: September 11, 2008 at 08:38:19 Pacific
Reply:

Should work with sh/ksh/bash:


filename="file with many spaces"
touch "$filename"
mv "$filename" `echo "$filename"|sed 's/[ ]*//g'`


Let me know if you have any questions.

0

Response Number 2
Name: Sujan (by Sujan Banerjee)
Date: September 12, 2008 at 04:04:19 Pacific
Reply:

Hi Nails,

I dont seem to be getting much success with the code snippet that u gave in the above message.
Here is a directory with files with spacing:-
bash-2.03# find `pwd` -name "* *"
/elite/prof/source/double space
/elite/prof/source/file one
/elite/prof/source/new file
/elite/prof/source/dpuble space file

Now Herez a script to rename files by replacing spacing with hyphen using code snippet that u gave:-
bash-2.03# cat rnm.sh
#!/usr/bin/ksh
for file in $(find `pwd` -name "* *")
do
touch "$file"
mv "$file" `echo "$file"|sed 's/ */-/g'`
done

Herez what happens on running the script:-
ash-2.03# ksh rnm.sh
mv: cannot rename /elite/prof/source/double: No such file or directory
mv: cannot rename /elite/prof/source/file: No such file or directory
mv: cannot rename /elite/prof/source/new: No such file or directory
mv: cannot rename /elite/prof/source/dpuble: No such file or directory
You have new mail in /var/mail/root

(Instead some other junk-files are created)
bash-2.03# ls -CF
-f-i-l-e- -s-p-a-c-e- double space dpuble space file new rnm.sh*
-o-n-e- double dpuble file one new file

The reason as far as I can see is that we are unable to bind those filenames into a single entity(See below):-
bash-2.03# for file in $(find /elite/prof -name "* *"); do echo $file; done
/elite/prof/source/double
space
/elite/prof/source/file
one
/elite/prof/source/new
file
/elite/prof/source/dpuble
space
file

Above output means all the words in filename are being read as individual entities.
ANOTHER QUESTION:What's purpose of touch "$file" in above script as it is inadvertently creating all those junk files ??
I am clueless.Any Solutions?Thanx in Advance.

Regards,
Sujan


0

Response Number 3
Name: Sujan (by Sujan Banerjee)
Date: September 12, 2008 at 04:30:26 Pacific
Reply:

Hi Nails,

I seem to have solved the issue with 1 of ur earlier posts.
Here it is:
bash-2.03# cat rnm.sh
#!/bin/ksh
find `pwd` -name "* *"|while read file
do
target=`echo "$file"|tr -s ' '|tr ' ' '-'`
mv "$file" "$target"
done
bash-2.03# sh rnm.sh
bash-2.03# ls
double-space dpuble-space-file file-one new-file rnm.sh

But this is one ghost system,Earlier
target=$(echo "$file"|tr -s ' '|tr ' ' '-')
because of shell dependency.

But still u can comment on my earlier post.

Regards,
Sujan


0

Response Number 4
Name: nails
Date: September 14, 2008 at 23:10:33 Pacific
Reply:

OK, here are my comments:

First, the touch command:


filename="file with many spaces"touch "$filename"
touch "$filename"

simply creats the file I was using for the demonstration. I didn't think you'd use it in a script. I suggest you read the man pag
e on touch.

Second, as you've probably learned, never use the for command when processing commands that return output with spaces:

for file in $(find . -print)
do
echo "$file"
done

The shell parses using with space which the default field separator.

Use the while loop:


find . -print|while read file
do
echo "$file"
done


Third, there is no reason to user the pwd command embedded in the find command:

find `pwd` -name ...

Above, this says to search the current directory which is no different than you the period:

find . -name ...

Fourth, your tr solution works, but I prefer this awk solution:


find . -name "* *"|while read file
do
echo "$file"
mv "$file" "`echo "$file"| nawk ' BEGIN {OFS="_"} $1=$1 '`"
done


0

Response Number 5
Name: Sujan (by Sujan Banerjee)
Date: September 15, 2008 at 00:21:40 Pacific
Reply:

Hi Nails
Thanx a lot for ur feedback.
It was really enriching.
Actually I was fooled into believing that u were using touch to bind the string into a single variable.

Thanx and Regards,
Sujan


0

Related Posts

See More



Response Number 6
Name: hemanty4u
Date: February 28, 2009 at 16:18:16 Pacific
Reply:

filename="file with many spaces"
touch "$filename"
mv "$filename" `echo "$filename"|sed 's/[ ]*//g'`

This script when I put in some shell and call that it does not work


0

Response Number 7
Name: nails
Date: March 1, 2009 at 10:45:18 Pacific
Reply:

These are basic Unix commands so they should work. First, tell us what version of unix you are using and which shell, i.e. bourne, bash, ksh, etc.

Second, do you have an error message? Does the touch command create the file name:

file with many spaces

my quess is that you have a portability issue with sed. I've seen some seds have trouble with character classes. You might try replacing this:

sed 's/[ ]*//g'

with this:

sed 's/ *//g'

Third, you might try replacing the sed command completely and use tr:

filename="file with many spaces"
touch "$filename"
mv "$filename" `echo "$filename"| tr -d " "`



0

Response Number 8
Name: Coolmay
Date: March 4, 2009 at 05:34:07 Pacific
Reply:

Try this code:

for i in `find . -name '* *' | cut -c3- |tr -d '/' | tr ' ' '~'`;
do
echo $i;
file1=`echo "$i" | tr '~' '\ '`;
file2=`echo "$i" | tr '~' '_'`;
echo $file1;
echo $file2;
mv -f "$file1" $file2;
done


0

Response Number 9
Name: nails
Date: March 4, 2009 at 17:41:47 Pacific
Reply:

Coolmay:

I disagree with your code implementation. You are jumping thru many hoops to replace spaces just to keep the for loop from failing:

>>for i in `find . -name '* *' | cut -c3- |tr -d '/' | tr ' ' '~'`;

IMO, using the while loop is still the way to go.

find . -name "* *"|while read file
do
   mv "$file" "`echo "$file"| nawk ' BEGIN {OFS="-"} $1=$1 '`"
done


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: How to remove spaces in filenames

How to Remove LILO from Windows NT boot www.computing.net/answers/unix/how-to-remove-lilo-from-windows-nt-boot-/2230.html

How to remove files www.computing.net/answers/unix/how-to-remove-files/7421.html

How to remove '$' from a $ variable www.computing.net/answers/unix/how-to-remove-from-a-variable/7936.html