Operating System - HP-UX
1827602 Members
3421 Online
109966 Solutions
New Discussion

Re: I have created some document files with a ^M at the end of the file names ...

 
John O'Neill_6
Regular Advisor

I have created some document files with a ^M at the end of the file names ...

Hi,

Yep, I'm an idiot. I uploaded a script to rename some document files on our HP UX box that were created in notepad, i forgot to do the dos2ux thing on them and as such each line ended with a ^M symbol.

The script worked, however it has done this...

apples.txt is now apples.txt.old^M

If i type 'view apples.txt.old' i am told by HP UX that there is no file by that name, which is correct.

If i type 'view apples.txt.old*' i can view the file happily and at the bottom of the VI viewer i am told that my file name is 'apples.txt.old^M'.

Here's my pickle... how the heck do I rename my files so that I can access them? I can work on them by being a bit careful with pattern matching but I would really like to rename these files (there's about a dozen of them) so that they can be accessed directly as needed.

Any advice?

Sorry for being such an idiot, i am normally so paranoid about checking this sort of thing but was being rushed last week and this got under the radar so to speak...

16 REPLIES 16
John O'Neill_6
Regular Advisor

Re: I have created some document files with a ^M at the end of the file names ...

I worked out that if i do a:

ls -b apples.txt.old*

I get a:

apples.txt.old\015

So there is an 'unprintable nonvisible' character appended to the file name.

I suppose what I need to do is work out how I can do a:

mv apples.txt.old\015 apples.txt.old

The above command doesn't work, is there a way i can do this?

If not, do i need to revert to the following?

cp apples.txt.old apples.txt.backup
rm -i apples.txt.old*

?? any help appreciated.
V. Nyga
Honored Contributor

Re: I have created some document files with a ^M at the end of the file names ...

Hi,

a 'mv apples.txt.old* apples.txt.old' should work.

Volkmar
*** Say 'Thanks' with Kudos ***
JoonTae Park
Occasional Advisor

Re: I have created some document files with a ^M at the end of the file names ...

I tried as below and it worked out;

- Created files as you did
- Typed "cat apples.txt.old[Esc][Esc]" [Enter]
- Typed "mv apples.txt.old[Esc][Esc] apples.txt.old" [Enter]

* please set EDITOR=vi, before you type [Esc][Esc]
$ export EDITOR=vi

If you got a lot of files, then run the below script;

for xfile in *.txt.old*
do
tfile=`echo $xfile | awk -F. '{printf("%s%s%s",$1,FS,$2)}'`
tfile=${tfile}.old
echo "Rename file $xfile to $tfile"
mv $xfile $tfile
done
Kapil Jha
Honored Contributor

Re: I have created some document files with a ^M at the end of the file names ...

u may check
strings apple.txt>apple.txt.1
and apple.txt1 is ur file.
Hope this help,
BR,
Kapil
I am in this small bowl, I wane see the real world......
Hein van den Heuvel
Honored Contributor

Re: I have created some document files with a ^M at the end of the file names ...

I use perl for my renames:
Something like:

# perl -e 'foreach (<*>){ $old=$_; s/\r$//; rename $old,$_; print}"

fwiw,
Hein.
Yogeeraj_1
Honored Contributor

Re: I have created some document files with a ^M at the end of the file names ...

hi,

the easiest way maybe will be to run a windows client of any ftp software GUI and rename the file.
e.g. using Winscp (http://winscp.net/eng/download.php)


hope this helps too!

kind regards,
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
V. Nyga
Honored Contributor

Re: I have created some document files with a ^M at the end of the file names ...

The easiest way - from my opinion - is to open the file manager of hp-ux cde and rename files by editing the file name. ;-)

Volkmar
*** Say 'Thanks' with Kudos ***
TwoProc
Honored Contributor

Re: I have created some document files with a ^M at the end of the file names ...

One more way:

$> vi apples.txt.old*

And then in vi, just save the file
:w apples.txt.new
:q

$> ls apples.txt.*

And you'll see your new file.

To remove the old one:
$> rm -i apples.txt.old*

Say "y" to the one that is the old file.
We are the people our parents warned us about --Jimmy Buffett
TwoProc
Honored Contributor

Re: I have created some document files with a ^M at the end of the file names ...

Another one:
Use file name completion from a bash shell...

$> bash
$> mv apples.txt.o[esc esc] apples.txt.old

Another one:
Or, use visual mode from the shell (ksh)
$> mv apples.txt.old^V^M apples.txt.old

(that's ctrl-v,ctrlm)



We are the people our parents warned us about --Jimmy Buffett
John O'Neill_6
Regular Advisor

Re: I have created some document files with a ^M at the end of the file names ...

Thanks peoples :)

All fixed, my files are now happy.

Much appreciated the very helpful responses I got and have allocated points accordingly :)
John O'Neill_6
Regular Advisor

Re: I have created some document files with a ^M at the end of the file names ...

Thread Closed.

:)
Dennis Handly
Acclaimed Contributor

Re: I have created some document files with a ^M at the end of the file names ...

>TwoProc: Or, use visual mode from the shell (ksh)
$> mv apples.txt.old^V^M apples.txt.old

I can't get this to work with ksh. I get control J for ^V^J, ^Venter or ^V^M.
Only with sh do I get control M for ^V^M and ^Venter.

The only easy way to get control M is to enter that ^V^M in vi.

>Volkmar: The easiest way - from my opinion - is to open the file manager

I'm not sure this is "easy" if you have more than a few of them. After one, two, three, many, you then write a script.

Which could be simple, ls the files into a file. Then for everyone with a ^M, strip the last char off:
for file in $(< list); do
filenew=${file%.}
mv $file $filenew
done
TwoProc
Honored Contributor

Re: I have created some document files with a ^M at the end of the file names ...

Volkmar,

That's funny, I created file just like that over here, and got it to work before posting.

I'm going to check to make sure I wasn't dreaming and that I was on a HPUX box running ksh...

Lessee here...

(re-running tests)

...

YUP.. works fine on my HPUX box...
$> uname -a
HP-UX xxxxxxxxx B.11.11 U 9000/800 xxxxxxxxx unlimited-user
$> echo $SHELL
/usr/bin/ksh

Could it be that your terminal def or tty settings get in your way? I've been doing the ctrl-v,ctrl-m trick on Unix boxes since 1929 (think Richard Pryor joke for that one).
We are the people our parents warned us about --Jimmy Buffett
TwoProc
Honored Contributor

Re: I have created some document files with a ^M at the end of the file names ...

Oh Crud, not Volkmar - Dennis.

Please excuse the crud in the thread.
We are the people our parents warned us about --Jimmy Buffett
Dennis Handly
Acclaimed Contributor

Re: I have created some document files with a ^M at the end of the file names ...

>TwoProc: Could it be that your terminal def or tty settings get in your way?

Very possible, except sh works.

>I've been doing the ctrl-v,ctrl-m trick

Sure in vi. I wouldn't usually use it in the shell.
piyush mathiya
Trusted Contributor

Re: I have created some document files with a ^M at the end of the file names ...

John,
There are two easiest way.
1). Open that file in vi as you told that you can able to open that file into the vi. and after that you can save this file to another name. Press Esc, after that press ":" then press "w" and file name whithout any space between w and file name. . .
2). cat apples.txt.old* > apples.txt.old

I think it works.

Regards,
Piyush Mathiya