Home > OS >  Create a new folder named the next count
Create a new folder named the next count

Time:12-30

I have a folder of projects, which labels projects based on a number. It starts at 001 and continues counting. I have a bash script I run through Alfred, however, I currently have to type the name of the folder.

QUERY={query}
mkdir /Users/admin/Documents/projects/"$QUERY"

I would like to have the script automatically name the folder to the next number. For example, if the newest folder is "019" then I would like it to automatically name it to "020"

This is what I've whipped up so far:

nextNum = $(find ~/documents/projects/* -maxdepth 1 -type d -print| wc -l)
numP = nextNum   1
mkdir /Users/admin/Documents/projects/00"$numP"

I'm not sure if my variable syntax is correct, or if variables are the best way to do this. I am a complete noob to bash so any help is appreciated.

CodePudding user response:

Give a try to this funny one:

projects_dir="/Users/admin/Documents/projects/"
mkdir "${projects_dir}"/$(printf "d" $(find "${projects_dir}" -maxdepth 1 -type d -printf . | wc -c))

CodePudding user response:

This might be what you're looking for:

#!/bin/bash

cd /Users/admin/Documents/projects || exit
if ! [[ -d 000 ]]; then mkdir 000;  exit; fi

dirs=([0-9][0-9][0-9])
mkdir $(printf d $(( 1   10#${dirs[${#dirs[*]} - 1]} )) )
  • Related