LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-17-2009, 12:38 PM   #1
kiranmannava
LQ Newbie
 
Registered: Nov 2009
Posts: 2

Rep: Reputation: 0
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 ??

Last edited by kiranmannava; 11-17-2009 at 01:20 PM.
 
Old 11-17-2009, 01:35 PM   #2
sarum1990
Member
 
Registered: Dec 2008
Distribution: Gentoo, Debian
Posts: 31

Rep: Reputation: 21
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
 
Old 11-17-2009, 05:45 PM   #3
kiranmannava
LQ Newbie
 
Registered: Nov 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks Sarum, you gave me exact solution.





Quote:
Originally Posted by sarum1990 View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
mulitple copy a existing directory and rename it at the end sequentially in BASH neo2k Linux - Newbie 2 08-20-2008 10:45 AM
cant rename directories andy753421 Linux - General 1 08-16-2004 08:24 AM
Rename a bunch of directories cessburn Linux - Software 1 06-07-2004 08:52 PM
How to rename directories in one line smc_one Linux - Newbie 3 09-24-2003 03:48 PM
how to rename directories Chijtska Linux - General 1 03-04-2002 07:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration