• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 243
  • Last Modified:

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.
0
anacleto
Asked:
anacleto
  • 8
  • 5
1 Solution
 
seredaCommented:
make the following script:
-- file: rmdot.sh
# removes dot
FILENAME=$1
mv $FILENAME `echo $FILENAME | awk '{printf ("%s", substr ($0, 0, length ($0) - 1));}'`
-- end of file

Then run

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

0
 
seredaCommented:
Of course, substitute $PATH_TO_RMDOT with a real path ;)
0
 
anacletoAuthor Commented:
I'm sorry but this script doesn't work.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
seredaCommented:
Okay, let's see - what is the error?
(What does it output?)

I tested it on my platform - it was ok.
0
 
anacletoAuthor Commented:
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?
0
 
seredaCommented:
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.


0
 
anacletoAuthor Commented:
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..
0
 
seredaCommented:
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.

0
 
anacletoAuthor Commented:
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 ..."

...



0
 
seredaCommented:
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
0
 
seredaCommented:
Oh my, i am really stupid ;)
You may remove $PATH_TO_SELF here - just change 'find' command to

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

0
 
anacletoAuthor Commented:
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.
0
 
seredaCommented:
> 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.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now