Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 AdvanceSujan Banerjee

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.

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 fileNow 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'`
doneHerez 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 fileThe 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
fileAbove 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

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.shBut 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

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"
doneThe 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

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

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

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 " "`

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

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

![]() |
![]() |
![]() |

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