sylcam
August 16, 2011, 12:08am
1
hello:
I have multiple files with names like: somestring_y2010m01d01
somestring_y2010m01d02
..........
somestring_y2010m12d31
How can I rename change the last part (y2010m##d ##) for a sequentially number considering different step. by example if step =2 then
somestring_y2010m01d01 --> somestring_001
somestring_y2010m01d03 --> somestring_002
somestring_y2010m01d05 --> somestring_003
.....................
Thanks in advance
Syl
yazu
August 16, 2011, 12:25am
2
Assuming you do not have spaces in the names of your files:
ls | awk -vSTEP=2 '
NR % STEP { print "echo mv " $0 " something_" sprintf("%03d", ++count)} ' |
sh
If the result looks right, remove 'echo'.
1 Like
If you quote your variables, it doesn't matter whether there are spaces in the name or not.
There is no need for ls
. In fact, in many cases, it will cause problems when there are spaces in the filenames.
n=1
for f in somestring_*
do
num=$( printf "%03d" "$n" )
n=$(( $n + 1 ))
nf=somestring_$num
mv "$f" "$nf"
done
1 Like
yazu
August 16, 2011, 1:31am
4
You forgot about skipping files. And there is nothing wrong in using ls as a generator in a pipe (of course find . -type f
is safer but not because of spaces). I said about spaces just because I was lazy to write "\"" $0 "\""
.
PS And there is a bug in my awk code (I belive I should use ! (NR % STEP)
). But the OP is silent...
sylcam
August 16, 2011, 1:02pm
5
Hello:
Just to say thanks to both of you, I found useful yor suggestion, I really appreciate your help.
Regards
Syl