LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Rename Directories Sequentially (Hexadecimal Format) (https://www.linuxquestions.org/questions/linux-newbie-8/rename-directories-sequentially-hexadecimal-format-769729/)

kiranmannava 11-17-2009 12:38 PM

Rename Directories Sequentially (Hexadecimal Format)
 
Hi,

I have a huge number of directories in a folder and I need suggestions to write a Linux script to rename those directories sequentially like the following format (Set_SIXDIGITHEXADECIMALNUMBER).

Set_001220
Set_001221
Set_001222
Set_001223
Set_001224
Set_001225
Set_001226
Set_001227
Set_001228
Set_001229
Set_00122a
Set_00122b
Set_00122c
Set_00122d
Set_00122e
Set_00122f
Set_001230
Set_001231
Set_001232
Set_001233
Set_001234
Set_001235

Can anyone help me? Thanks.

IS IT POSSIBLE ??

sarum1990 11-17-2009 01:35 PM

I'm not sure 100% what you are asking for, and I caution you to be careful when doing this so you don't accidentally overwrite directories, but I believe the following bash script should work

Code:

#/bin/bash

DIRS=`find . -mindepth 1 -maxdepth 1 -type d` #all directories in current
NUM=1
PREFIX="Set_"
PRECISION="000000"

for DIR in $DIRS
do
  LEN=${#PRECISION}-${#NUM}                    #num of zeros needed for len 6
  mv -i $DIR $PREFIX${PRECISION:0:$LEN}$NUM    #-i should prompt before overwriting your data
  NUM=`echo "obase=16; ibase=16; $NUM+1" | bc` #use bc for hex addition
done


kiranmannava 11-17-2009 05:45 PM

Thanks Sarum, you gave me exact solution.





Quote:

Originally Posted by sarum1990 (Post 3760343)
I'm not sure 100% what you are asking for, and I caution you to be careful when doing this so you don't accidentally overwrite directories, but I believe the following bash script should work

Code:

#/bin/bash

DIRS=`find . -mindepth 1 -maxdepth 1 -type d` #all directories in current
NUM=1
PREFIX="Set_"
PRECISION="000000"

for DIR in $DIRS
do
  LEN=${#PRECISION}-${#NUM}                    #num of zeros needed for len 6
  mv -i $DIR $PREFIX${PRECISION:0:$LEN}$NUM    #-i should prompt before overwriting your data
  NUM=`echo "obase=16; ibase=16; $NUM+1" | bc` #use bc for hex addition
done




All times are GMT -5. The time now is 09:13 PM.