Link to home
Start Free TrialLog in
Avatar of anacleto
anacleto

asked on

name with spazce

I need a script that rename each file in a file system ending with a dot
with the same name without the dot.
The files contains spaces.
ASKER CERTIFIED SOLUTION
Avatar of sereda
sereda

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sereda
sereda

Of course, substitute $PATH_TO_RMDOT with a real path ;)
Avatar of anacleto

ASKER

I'm sorry but this script doesn't work.
Okay, let's see - what is the error?
(What does it output?)

I tested it on my platform - it was ok.
I'm using ksh on aix 4.3.

Usage: mv [-i | -f]  [--] src target
   or: mv [-i | -f]  [--] src1 ... srcN directory

I think that the problem is with
mv $FILENAME `echo $FILENAME | awk '{printf ("%s", substr ($0, 0, length ($0) - 1));}'`
where filename is a name with spaces....
Have you tried with name with blank inside?
Oh my - i guess you're right.
I thought of blanks in one case and forgot the other.
So, you need to change that line to the following:

mv "$FILENAME" "`echo \"$FILENAME\" | awk '{printf (\"%s\", substr ($0, 0, length ($0) - 1));}'`"

I hope this will help.
Please comment if there are other errors.

PS: if you want to rename only FILES (as said in the first message), then you need to issue more specific find:

find / -name "*." -type f -exec $PATH_TO_RMDOT/rmdot.sh '{}' \;

That will leave directories and links and devices as they are.


Now It is working,and as a workaround I could accept it.
But I have asked ascript to install in my environment...

Could you help me?
ps:we are very close..
Eeeh, i don't quite catch what you really need. If you need a script that performs those actions, just put 'find' command with parameters shown above into another script file and there you have it.

Yes, you will have two scripts actually, but does it matter?

Please clear up with this and i think i can help you.

There are two point in my request.
1) I prefer to have only one script for doing jobs.It's easier to mantain e remember what things are for.
2) It's a scholastic question regarding filenames that contains spaces.

It was my request "I need a script that rename each file in a file ..."

...



Okay, i take it that you need only one script. Unfortunately, it's not obvious how to make "find" run several commands with {} argument, so i propose a workaround here - it is ONE script:

-- start of rmdot.sh
# This script will remove all dots
# from the end of files by issuing
# find command ans executing itself
# for each filename

# Important: Run this script without parameters

# Put here path to this script
PATH_TO_SELF=/

FILENAME=$1

if [ "x$FILENAME" = "x" ]; then
# this is main call without parameters

  find / -name "*." -type f -exec  $PATH_TO_SELF/rmdot.sh '{}' \;

else
# this is call from find - remove dot

  mv "$FILENAME" "`echo \"$FILENAME\" | awk '{printf (\"%s\", substr ($0, 0, length ($0) - 1));}'`"

fi

# That's it.
-- end of file
Oh my, i am really stupid ;)
You may remove $PATH_TO_SELF here - just change 'find' command to

find / -name "*." -type f -exec  $0 '{}' \;

Good suggestions. But It's not exactly what I'm lookin for.(consider the second point..)
I can grade this wiht good. if you want more. You can go on...

It's up to you.
> 2) It's a scholastic question regarding filenames that contains spaces.

So, what's the question?
You can manipulate filenames that contain spaces, but you need to make your shell understand that this is really one parameter and not two-or-more.
For example,
    mv my pretty file my pretty dir/
will run mv command with $1=my $2=pretty etc., and
    mv "my pretty file" "my pretty dir"/
will run mv command with $1=my pretty file and $2=my pretty dir.

I think that's pretty plain, and if you need more explanation, please ask more concrete questions.