el123
asked on
Linux - rename multiple files in one command
Hi,
I've 100s of files, and need to move to windows, after moving to windows, it added special character after each line, as we have split the actual files on the basis of 400 lines.
At linux, it doesn't have any problem, but after moving to windows, it inserted special character after each line ... which is a problem, now look for 2 options:
1. either; remove this special character
2. rename all the unix files to txt extension, this solves my problem too.
Need your help for any of these 2 solutions, was trying rename cmd, but for extension it is working, but our files are not having any extension, as they are created using split cmd,
there are in the form of aa, ab, ac ...
No extension, need some help, thanks in advance.
I've 100s of files, and need to move to windows, after moving to windows, it added special character after each line, as we have split the actual files on the basis of 400 lines.
At linux, it doesn't have any problem, but after moving to windows, it inserted special character after each line ... which is a problem, now look for 2 options:
1. either; remove this special character
2. rename all the unix files to txt extension, this solves my problem too.
Need your help for any of these 2 solutions, was trying rename cmd, but for extension it is working, but our files are not having any extension, as they are created using split cmd,
there are in the form of aa, ab, ac ...
No extension, need some help, thanks in advance.
for /F "tokens=1,2,* delims=."%%a in ('dir /b') do (
echo move %%a %%a.txt
)
made a small change as your files dont have extension
echo move %%a %%a.txt
)
made a small change as your files dont have extension
ASKER
thanks vikas, but can you pls tell how to run it
I've files like
aa
ab
ac
ad
ae
af
and I want to change them to
aa.txt
ab.txt
ac.txt
ad.txt ... and so on
please just write the syntax/cmd ... whatever I want to run to change these names
thanks.
I've files like
aa
ab
ac
ad
ae
af
and I want to change them to
aa.txt
ab.txt
ac.txt
ad.txt ... and so on
please just write the syntax/cmd ... whatever I want to run to change these names
thanks.
@echo off
for /F "tokens=1,2,* delims=."%%a in ('dir /b') do (
echo %%a %%a.txt
move %%a %%a.txt
)
1.copy the above code and put it into a file name run.bat
2.copy the file into the location where you have your files
3.go to the location form command promt and just say run.bat
4.if you are getting below as output then everything is fine
aa aa.txt
bb bb.txt
..
..
for /F "tokens=1,2,* delims=."%%a in ('dir /b') do (
echo %%a %%a.txt
move %%a %%a.txt
)
1.copy the above code and put it into a file name run.bat
2.copy the file into the location where you have your files
3.go to the location form command promt and just say run.bat
4.if you are getting below as output then everything is fine
aa aa.txt
bb bb.txt
..
..
@echo off
for /F %%a in ('dir /b') do (
echo %%a %%a.txt
move %%a %%a.txt
)
a small change even this will work
for /F %%a in ('dir /b') do (
echo %%a %%a.txt
move %%a %%a.txt
)
a small change even this will work
ASKER
it is giving error
one more thing is possible that we can do the rename at linux too ... is it possible to rename all these files at linux box, before bringing to windows ... doable?
this'll be easy
one more thing is possible that we can do the rename at linux too ... is it possible to rename all these files at linux box, before bringing to windows ... doable?
this'll be easy
ASKER
error file attached
error.doc
error.doc
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I guess we cant rename it in linux because you will again end up in getting the same special character error as the line ending in windows and linux differ
ASKER
it looks it worked ... but will confirm again ..
Vikas ... Big Thanks ... but need to test/verify again ... ;-)
Vikas ... Big Thanks ... but need to test/verify again ... ;-)
You can easily rename them all under linux with the unixtodos tool
unix2dos filename
And if you are moving from windows to linux you have
dos2unix filename
Good Luck
unix2dos filename
And if you are moving from windows to linux you have
dos2unix filename
Good Luck
Hi, could you please explain how do you move these files from linux to windows? ftp, smb shared disk, copy on an external media, or else?
Maybe there is a cleaner and simpler way to avoid changing the newline character, e.g. in ftp by using the "bin" transfer method, in shared disks or external media by avoiding the conv= option in mount command on linux.
Maybe there is a cleaner and simpler way to avoid changing the newline character, e.g. in ftp by using the "bin" transfer method, in shared disks or external media by avoiding the conv= option in mount command on linux.
ASKER
using wiseftp program to move files from linux to windows ... and the file type setting is auto ... do you want me to try something?
Renaming the files on Windows in a Command prompt should be easy.
On Windows, to rename files called ax to ax.txt where x can be any character just use:
ren a? a?.txt
If there are more than 2 characters in the filename but they all start with the letter a you can use:
ren a* a*.txt
Alternatively, at the Linux end use dos2unix to convert the files first:
for i in a?
do
unix2dos $i
done
Again replace a? with a* if you are using more than 2 character filenames.
On Windows, to rename files called ax to ax.txt where x can be any character just use:
ren a? a?.txt
If there are more than 2 characters in the filename but they all start with the letter a you can use:
ren a* a*.txt
Alternatively, at the Linux end use dos2unix to convert the files first:
for i in a?
do
unix2dos $i
done
Again replace a? with a* if you are using more than 2 character filenames.
OK, now I understand the problem:
with auto mode the files with name aa ab, etc. are interpreted as binary and they are transfered without changes, this means that the line terminator is only a linefeed character (UNIX convention) and they look odd in a Windows text editor.
Rather than renaming the files to aa.txt, before transferring them, in order to force the conversion to Windows format in auto mode, I suggest to use the ASCII transfer mode of wiseftp, so that the files will be converted to Windows line terminator convention (carriage return + linefeed) during the transfer.
Alternatively you can convert them under Linux with the unix2dos command before transferring them, as suggested above.
So, just to clarify: auto mode DOES NOT ADD any line terminator character to files like aa (thus the file looks odd on Windows) while it ADDS line terminator characters to file aa.txt (thus they look correct), not the other way round.
with auto mode the files with name aa ab, etc. are interpreted as binary and they are transfered without changes, this means that the line terminator is only a linefeed character (UNIX convention) and they look odd in a Windows text editor.
Rather than renaming the files to aa.txt, before transferring them, in order to force the conversion to Windows format in auto mode, I suggest to use the ASCII transfer mode of wiseftp, so that the files will be converted to Windows line terminator convention (carriage return + linefeed) during the transfer.
Alternatively you can convert them under Linux with the unix2dos command before transferring them, as suggested above.
So, just to clarify: auto mode DOES NOT ADD any line terminator character to files like aa (thus the file looks odd on Windows) while it ADDS line terminator characters to file aa.txt (thus they look correct), not the other way round.
ASKER
THANKS Gurus ... give me some time, need to test/check/verify ... which one is the best and fastest solution, will get back to soon ... stay tuned ;-)
echo move %%a.%%b %%a.txt
)
put this in a bat file and run this in your are ok with whatever is coming on the command prompt you can then remove echo to do the actual renaming