Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have files in folder named unix_aaa.txt unix_bbb.txt unix_ccc.txt and so on.

How do I change all the files that start with unix to names like

001_unix_aaa.txt

002_unix_bbb.txt

003_unix_ccc.txt
share|improve this question

migrated from stackoverflow.com Dec 26 '12 at 1:45

This question came from our site for professional and enthusiast programmers.

1  
is it batch or bash? –  anishsane Dec 21 '12 at 6:50
    
is it not the same i dont know both are fine i think > < –  eathapeking Dec 21 '12 at 7:10
2  
batch=>windows, bash=>linux. The script answer would change completely. –  anishsane Dec 21 '12 at 8:32
    
ahhh it's batch :) –  eathapeking Dec 21 '12 at 9:30
add comment

2 Answers 2

up vote 1 down vote accepted

Here is a batch (windows) script that will do this

setlocal enabledelayedexpansion
set n=1
for %%a in (unix_*.txt) do (
if !n! lss 10 (
ren "%%a" "00!n!_%%a"
) else (
if !n! lss 100 (
ren "%%a" "0!n!_%%a"
) else (
if !n! geq 100 (
ren "%%a" "!n!_%%a"
)
)
)
set /a n+=1
)
share|improve this answer
    
can i ask more output came out as 001 - 010 then it is 010 instead of 011 how should i fix it or there is no way –  eathapeking Dec 21 '12 at 9:38
1  
What do you mean? I don't follow :) –  Bali C Dec 21 '12 at 9:54
    
:D meanthat 001_unix_aaa.txt until 009_unix_aaa.txt then at 010 it comeout like 0010_unix_aaa.txt it should be 010_unix_aaa.txt i'm try to explain as much as my english can >_< –  eathapeking Dec 21 '12 at 10:00
1  
Oh right, I get you now :D It should already do that, I made it check so it would go like 001 010 100 etc I will do some testing! –  Bali C Dec 21 '12 at 10:02
    
hey hey i got it now thank you becuase of i didnt type it all correct that why its error thank You :D :D :D :D :D –  eathapeking Dec 21 '12 at 10:03
show 3 more comments

This is for Bourne shells.

n=1; for f in unix*
do
    mv "$f" "`printf %03d $n`_$f"
    n=$((n+1))
done
share|improve this answer
    
it's not working i put in text document then change it .bat everyfile name stil be the same but thank you i found the problem what is it printf in cmd command –  eathapeking Dec 21 '12 at 7:13
    
just normal command line i dont know much about it all i used to do is type in .txt file then change it .bat and run –  eathapeking Dec 21 '12 at 7:19
    
@eathapeking what does echo $0 say ? –  kmkaplan Dec 21 '12 at 7:30
    
nothing change :) here example of filename unixinv_RSSPA_16Nov2012.csv –  eathapeking Dec 21 '12 at 7:40
2  
From 1st comment, looks like it's batch & not bash –  anishsane Dec 21 '12 at 8:40
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.