?
Solved

Linux - rename multiple files in one command

Posted on 2010-01-11
16
Medium Priority
?
990 Views
Last Modified: 2013-12-21
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.
0
Comment
Question by:el123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
  • 2
  • +2
16 Comments
 
LVL 7

Expert Comment

by:vikas_madhusudana
ID: 26290478
for /F "tokens=1,2,* delims=."%%a in ('dir /b') do (
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
0
 
LVL 7

Expert Comment

by:vikas_madhusudana
ID: 26290484
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  
0
 

Author Comment

by:el123
ID: 26290515
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:vikas_madhusudana
ID: 26290556
@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
..
..


0
 
LVL 7

Expert Comment

by:vikas_madhusudana
ID: 26290567
@echo off
for /F %%a in ('dir /b') do (
echo %%a %%a.txt
move %%a %%a.txt
)

a small change even this will work
0
 

Author Comment

by:el123
ID: 26290590
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
0
 

Author Comment

by:el123
ID: 26290596
error file attached
error.doc
0
 
LVL 7

Accepted Solution

by:
vikas_madhusudana earned 2000 total points
ID: 26290612
Try this
@echo off
for /F %%a in ('dir /b') do (
echo %%a %%a.txt
move %%a %%a.txt
)

Open in new window

0
 
LVL 7

Expert Comment

by:vikas_madhusudana
ID: 26290623
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
0
 

Author Comment

by:el123
ID: 26290661
it looks it worked ... but will confirm again ..

Vikas ... Big Thanks ... but need to test/verify again ... ;-)
0
 
LVL 14

Expert Comment

by:Monis Monther
ID: 26290953
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
0
 
LVL 5

Expert Comment

by:dcesari
ID: 26292181
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.
0
 

Author Comment

by:el123
ID: 26293449
using wiseftp program to move files from linux to windows ... and the file type setting is auto ... do you want me to try something?
0
 
LVL 10

Expert Comment

by:Martin_J_Parker
ID: 26293776
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.
0
 
LVL 5

Expert Comment

by:dcesari
ID: 26293791
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.
0
 

Author Comment

by:el123
ID: 26295476
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 ;-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After running Ubuntu some time, you will be asked to download updates for fixing bugs and security updates. All the packages you download replace the previous ones, except for the kernel, also called "linux-image". This is due to the fact that w…
You ever wonder how to backup Linux system files just like Windows System Restore?  Well you can use Timeshift in Linux to perform those similar action.  This tutorial will show you how to backup your system files and keep regular intervals. Note…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month12 days, 22 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question