Operating System - HP-UX
1833451 Members
3004 Online
110052 Solutions
New Discussion

Re: remove special character and space from file name

 
Dhirendra kumar Thakur
Occasional Contributor

remove special character and space from file name

Hi
I have some issue while removing special character and space from file name in hp-ux.
I have 7000 file which have special charater and space between two character in file name.
Which I remove with help of shell script.I used this command.
for file in *
do
mv "$file" `echo "$file" | sed 's/[^A-Za-z0-9_.]//g'`
done
this is working but I am getting lesser file.
I mean I have 7000 file after running this command I will get 6755 file.
So how can I get exact file after running this command.
Please help me any body.
Thanks
Dhirendra
5 REPLIES 5
Steven Schweda
Honored Contributor

Re: remove special character and space from file name

> [...] I have 7000 file after running this
> command I will get 6755 file.

If you have two files, one named "a b", and
one named "a b", your name-changing method
will get "ab" for both of them. You might do
better using something like:
sed 's/[^A-Za-z0-9_.]/_/g'
but that can fail in other cases.

You may need to do something smarter than
simply throwing away unwanted characters, but
my psychic powers are too weak to tell me
waht your file names look like, or how you
would like to handle the more tricky cases.
Suraj K Sankari
Honored Contributor

Re: remove special character and space from file name

Hi,

Please provide the old and new file name.

Suraj
James R. Ferguson
Acclaimed Contributor

Re: remove special character and space from file name

Hi:

> I mean I have 7000 file after running this command I will get 6755 file.

...which probably means that you are overwriting 245 of your files. You need to formulate the new file name and *test* for its presence *before* you 'mv' (rename) the old file to the new file.

Regards!

...JRF...
Dhirendra kumar Thakur
Occasional Contributor

Re: remove special character and space from file name

I put some file name as in my system which I have to make as corrective name so that we can access this file.
ADMIRALM-. 1089 FS-MSDS-English.pdf
ADMIRALM-. 1089 FS-MSDS_CANADA-Canadian English.pdf
ADMIRALM-. 1089 FS-MSDS_CANADA-Canadian French.pdf
ADMIRALM-. 1165 PR -- DELETE FROM MRIF DB 73106-MSDS-English.pdf
ADVANTAGEM-^Y AC1529-MSDS-English.pdf
ADVANTAGEM-^Y AC1529-MSDS_CANADA-Canadian English.pdf
ADVANTAGEM-^Y AC1529-MSDS_CANADA-Canadian French.pdf
XXTRADURA CAS-MSDS-English.pdf
XXTRADURA FLA 3766-MSDS-English.pdf
XXTRADURA FLA 3767-MSDS-English.pdf
YARMOR® 302 -- OBSOLETE PER C SHANNON 41906-MSDS-English.pdf
YARMOR® 302 -- OBSOLETE PER C SHANNON 41906-MSDS_CANADA-Canadian English.pdf
YARMOR® 302 -- OBSOLETE PER C SHANNON 41906-MSDS_CANADA-Canadian French.pdf
YARMOR® 60-SC -- OBSOLETE PER C SHANNON 33006-MSDS-English.pdf
YARMOR® 80 -- OBSOLETE PER C SHANNON 33006-MSDS-English.pdf

and after running this command again and I got 150 lesser file.
sed 's/[^A-Za-z0-9_.]/_/g'
regards
Dhirendra
Steven Schweda
Honored Contributor

Re: remove special character and space from file name

> [...] and I got 150 lesser file.

You could do the suggested test, and don't do
the "mv" if the destination file exists.
Then change "_" to "__" and try again.
Repeat as needed.


Whether it makes sense to rename files in
ways where you don't know what the old name
was is another question entirely.