Home > front end >  Bash script to a new directory with incrementing names based on the last directory created
Bash script to a new directory with incrementing names based on the last directory created

Time:11-29

I'm trying to write a bash script that will create a new directory with a name based on the last directory created. For example - if I have the following folders in a directory:

00001 00002 00003

and I run the script again I would like it to determine that the last folder is '00003' and then create '00004'.

The following command seems to get the last folder name from the 'output' directory but I don't seem to be able to manipulate the value in the 'var' variable as it is a directory and not a string or integer.

var=$(ls -t output | grep -v /$ | head -1)

Any help would be much appreciated.

CodePudding user response:

Continuing from my comment, the simplest way to create sequential 5-digit directory names, creating the next available directory on each run of your script is to form the number using printf -v var ... Example, to create a 5-digit number storing the result in var you use printf -v var "d" "$count". If count contained 4 that would store 00004 in var.

To determine which is the next available directory, loop starting at 1 with while [ -d "parent/$var" ] and increment the number in var each time until you find a number in var for which the parent/$var directory doesn't exist and then simply create the new directory. You can set a default value for parent of . (present working directory) or take the parent path as your first argument. (you can take the number to start counting at as your 2nd argument with a default of 1)

Putting it altogether, you could do:

#!/bin/bash

parent="${1:-.}"    ## parent directory as 1st arg (default .)
count="${2:-1}"     ## initial count as 2nd arg    (default 1)

printf -v dname "d" "$count"           ## store 5 digit number in dname

while [ -d "$parent/$dname" ]             ## while dir exists
do
  ((count  ))                             ## increment count
  printf -v dname "d" "$count"         ## store new 5 digit number in dname
done

printf "creating %s\n" "$parent/$dname"   ## (optional) output dirname
mkdir -p "$parent/$dname"                 ## create dir

Example Use/Output

With the script in createseqdir.sh and the parent directory location as dat, e.g.

$ l dat
total 24
drwxr-xr-x 5 david david 4096 Nov 28 20:16 .
drwxr-xr-x 3 david david 4096 Nov 28 20:16 ..
drwxr-xr-x 2 david david 4096 Nov 28 20:12 00001
drwxr-xr-x 2 david david 4096 Nov 28 20:12 00002
drwxr-xr-x 2 david david 4096 Nov 28 20:12 00003

You could do:

$ bash createseqdir.sh dat
creating dat/00004

Resulting in:

$ l dat
total 28
drwxr-xr-x 6 david david 4096 Nov 28 20:30 .
drwxr-xr-x 3 david david 4096 Nov 28 20:29 ..
drwxr-xr-x 2 david david 4096 Nov 28 20:12 00001
drwxr-xr-x 2 david david 4096 Nov 28 20:12 00002
drwxr-xr-x 2 david david 4096 Nov 28 20:12 00003
drwxr-xr-x 2 david david 4096 Nov 28 20:30 00004

CodePudding user response:

An alternative method using an array and negative array index -1 (which points to the last element of the array) would be:

#!/bin/bash

dirs=([0-9][0-9][0-9][0-9][0-9])
mkdir $(printf 'd' $((1   10#${dirs[-1]}))) || exit

This assumes at least one directory with the pattern [0-9][0-9][0-9][0-9][0-9] exists. The 10# is required because a bare digit sequence starting with 0 is interpreted in octal base.

  • Related