UNIX Bash: Create incrementally numbered directories

The Problem

You would like to be able to create UNIX directories with numerical names, which increase incrementally, for instance:






The Solution

Just execute the following bash script in a directory, which contains at least the subdirectory ‘0000’. The script will create a new directory with a numerical name with trailing zeros precisely being the last created directories name +1.

echo "=== Create new deployment dir ==="

lastdir=$(ls -d [0-9][0-9][0-9][0-9] | tail -1)

while [ "$newvalue" != "$lastvalue" ]
newvalue="$(echo $myvalue | sed 's/^0//')"

echo "Last created dir: $lastdir"

newdirnr=$((($lastdirnr) + 1))
newdir=$(printf "%04u" $newdirnr)

echo "Create new dir: $newdir"

mkdir $newdir


How do I strip leading zeros

(there was one more post I got the ‘ls’ and ‘printf’ part from but I cannot find it at the moment 😦 )

Run a Background Unix Process (even if user is disconnected)

Given we have a script test.sh, it is easy to run this script in the background while continuing to work in the current shell session. The most cited way is by adding ‘&’ to the end of the command:

./test.sh &

However, this approach has one major disadvantage: When the shell session of the user is disconnected or ended in any way, the script we have started in the background will cease to work.Luckily, it is quite easy to start a process in a way, which will prevent the process from ending upon an error with the current user’s session: meet nohup. Using the nohup command, we can start the background script as follows …

nohub ./test.sh &

… the the process will continue to run even if the shell session ends.

Depending on your OS it can be a bit tricky to ‘find’ the process again. You may want to try the following commands to get the process id of your nohub process:

ps -A
sudo ps -A

Note: Depending on your use case, it might be a better idea to install a UNIX daemon instead of using nohub.


Unix Nohup: Run a Command or Shell-Script Even after You Logout

nohup(1) – Linux man page

stackoverflow.com: What’s the difference between nohup and a daemon?

Java NIO HttpClients

Java’s non-blocking IO (NIO) is a complex, yet amazingly powerful addition to Java to build scalable and performant network applications. The capabilities of Java NIO are particularly impressive if compared with alternative ways in the JRE to connect to web resources over http. Here is a small collection of libraries and tutorial, which can aid in building asynchronous Java NIO based http clients.

Open Source Projects

Project License Dependencies
Async Http Client Apache 2.0 Netty 3.9.1
httpnio LGPL None
NIO Framework LGPLv3 None


High-Performance I/O with Java NIO: Nice tutorial with lots of code samples

The Rox Java NIO Tutorial: Great in-depth tutorial, including examples for SSL

SSL Echo Client and SSL Echo Server: SSL Client/Server example using NIO Framework listed above.

Java NIO Tutorials by Jakob Jenkov: Great tutorials to get started with Java NIO

SSL NIO from onjava.com: In depth discussion on SSL and NIO