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. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
01-18-2008, 04:34 AM
|
#1
|
Member
Registered: Jan 2002
Posts: 162
Rep:
|
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.
|
|
|
01-18-2008, 05:08 AM
|
#2
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
Something like this?
Code:
#!/bin/bash
cd /home/images
n=0
for i in $(ls) ; do
let n=$n+1
done
let n=$n+1
mkdir "dir$n"
|
|
|
01-18-2008, 05:19 AM
|
#3
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
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:
Code:
#!/bin/bash
# -------------------------------------------------------
# find last created directory: format NNNN
# -------------------------------------------------------
lastdir=$(ls -d [0-9][0-9][0-9][0-9] | tail -1)
# -------------------------------------------------------
# create new sequential directory
# -------------------------------------------------------
newdir=$((++lastdir))
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
|
|
|
01-18-2008, 07:20 AM
|
#4
|
Member
Registered: Jan 2002
Posts: 162
Original Poster
Rep:
|
Thanks!
Very helpful, cheers.!
|
|
|
01-08-2010, 09:43 AM
|
#5
|
LQ Newbie
Registered: Aug 2004
Posts: 13
Rep:
|
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?
|
|
|
All times are GMT -5. The time now is 06:31 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|