Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 01-18-2008, 04:34 AM   #1
Registered: Jan 2002
Posts: 162

Rep: Reputation: 30
Bash script to create sequential, numbered directories

Hi, I am working on a backup system which needs to create numbered sub dirs for each run. For example, when first run it creates one called "1", next time "2" etc. So it needs to detect how many of these numbered dirs are present, increment that count and call mkdir using it. Thanks.
Old 01-18-2008, 05:08 AM   #2
Senior Member
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Something like this?

cd /home/images
for i in $(ls) ; do
let n=$n+1
let n=$n+1
mkdir "dir$n"
Old 01-18-2008, 05:19 AM   #3
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
I'd suggest to write numbered directories padded with zeros, e.g. 0001, 0002, 0003... instead of 1, 2, 3. In this way they will be sorted sequentially.

It also will be simplier to check the last one and create the new directory at each run. For example:
# -------------------------------------------------------
#  find last created directory: format NNNN
# -------------------------------------------------------
lastdir=$(ls -d [0-9][0-9][0-9][0-9] | tail -1)

# -------------------------------------------------------
#  create new sequential directory
# -------------------------------------------------------
mkdir $(printf "%04u" $newdir)

Last edited by colucix; 01-18-2008 at 08:00 AM. Reason: removed unuseful newline character from the printf format string
Old 01-18-2008, 07:20 AM   #4
Registered: Jan 2002
Posts: 162

Original Poster
Rep: Reputation: 30

Very helpful, cheers.!
Old 01-08-2010, 09:43 AM   #5
LQ Newbie
Registered: Aug 2004
Posts: 13

Rep: Reputation: 0
I am perplexed. I have a situation where I have a folder named parts. In parts I have numerous subfolders labeled sprocket1, sprocket2, spoke1, spoke2, spoke3, rim1, rim2, etc.

I need to occasionally create new sequential subfolders via a bash script. I can't seem to adjust either of the scripts in this thread to do that.

Let's say I need to create a sequential spoke subfolder. As listed above, we know that would be spoke3. But let's say we don't know what the sequence number is for the last created spoke subfolder. How can I determine that via bash and create a new subfolder in sequence?


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
Bash script to search through directories. mcdrr Programming 5 05-11-2007 05:41 PM
bash: make rename script traverse directories morrolan Programming 2 11-08-2006 10:52 AM
create a bash script that restarts? nny0000 Linux - Software 7 09-15-2006 05:14 PM
Script to create multiple directories under users homes shawnbishop Linux - Software 4 03-30-2006 08:11 AM
Bash script about directories tarja Programming 3 11-23-2005 01:19 PM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:18 AM.

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