Creating directories with name expansion using mkdir

I would like to have a bash script that creates a folder called "exam_folder", and if that folder already exists (I would run the script again), the script will create "exam_folder_1", and if this folder exists, I want the script to create " exam_folder_2 "and so on and on. I would like it to be smart :) I have something like this:

function create_folder { if [ -d "$1" ]; then # if exists mkdir $1_`i_max` # function i_max would find out the max number and add 1 else mkdir $1 fi } 

Thank you for your help.

+4
source share
3 answers

You can do it:

 max=`ls -1d exam_folder* | tr -dc '[0-9\n]' | sort -k 1,1n | tail -1` mkdir exam_folder_$((max + 1)) 

Please note that it will not fill the “holes” in the sequence if you are ok with this.

0
source

Two ansers: a compatible shell and pure bash :

Last edit: Make the regex more accurate and fix some counting errors.

Sort of:

 create_folder() { if [ -e "$1" ] ;then counter=1; while [ -e "$1-$counter" ] ;do counter=$((counter+1)) done mkdir "$1-$counter"; else mkdir "$1"; fi; } 

:

 $ ls -ltr /tmp |grep foo $ create_folder /tmp/foo\ bar $ create_folder /tmp/foo\ bar $ create_folder /tmp/foo\ bar $ ls -ltr /tmp |grep foo drwxr-xr-x 2 user user 4096 1 mai 10:53 foo bar drwxr-xr-x 2 user user 4096 1 mai 10:53 foo bar-1 drwxr-xr-x 2 user user 4096 1 mai 10:53 foo bar-2 

or with better formatting and another way of counting (faster if there are many directories, because it uses a specialized counting function, but longer if the other is due to forks.):

 create_folder() { if [ -e "$1" ] ;then counter=`/bin/ls -1 "$1" "$1-"* 2>&1 | grep -E "^$1(-[0-9]*)?" | wc -l`; nname="`printf "%s-%04d" "$1" $counter`"; mkdir "$nname"; else mkdir "$1"; fi; } 

This has been tested under another implementation: , , and

 $ create_folder /tmp/test $ ls -ltr /tmp/| grep test drwxr-xr-x 2 user user 4096 Apr 30 13:25 test $ create_folder /tmp/test $ create_folder /tmp/test $ ls -ltr /tmp/| grep test drwxr-xr-x 2 user user 4096 Apr 30 13:25 test drwxr-xr-x 2 user user 4096 Apr 30 13:26 test-0001 drwxr-xr-x 2 user user 4096 Apr 30 13:26 test-0002 

And with the name dirname containing spaces:

 $ create_folder "foo bar" $ create_folder "foo bar" $ create_folder "foo bar" $ create_folder "foo bar" $ ls -ltr | grep foo drwxr-xr-x 2 user user 4096 Apr 30 13:35 foo bar drwxr-xr-x 2 user user 4096 Apr 30 13:35 foo bar-0001 drwxr-xr-x 2 user user 4096 Apr 30 13:35 foo bar-0002 drwxr-xr-x 2 user user 4096 Apr 30 13:35 foo bar-0003 

And for fans:

 create_folder() { local -a list local counter nname if [ -e "$1" ] ;then list=("$1-"*) list=(${list[@]// /_}) list=(${list[@]//*\*}) counter=$((${#list[@]}+1)) printf -v nname "%s-%04d" "$1" $counter mkdir "$nname"; else mkdir "$1"; fi; } 

Very fast, without plug and good formatting:

 $ create_folder "/tmp/foo bar" $ create_folder "/tmp/foo bar" $ create_folder "/tmp/foo bar" $ create_folder "/tmp/foo bar" $ ls -ltr /tmp/ | tail -n 4 drwxr-xr-x 2 user user 4096 1 mai 11:17 foo bar drwxr-xr-x 2 user user 4096 1 mai 11:17 foo bar-0001 drwxr-xr-x 2 user user 4096 1 mai 11:17 foo bar-0002 drwxr-xr-x 2 user user 4096 1 mai 11:17 foo bar-0003 
0
source

This should work under any POSIX compatible shell (although I'm not 100% sure of [ "$foo" -eq "$foo" ] is-number-check). I gave him a quick test in dash and busybox' ash .

 create_folder() { if [ -e "$1" ]; then i=0 for d in "$1"_*; do _i="${d##*_}" [ "${_i}" -eq "${_i}" ] 2>/dev/null && i="${_i}" done i=$((i+1)) mkdir "$1_${i}" else mkdir "$1" fi } 
0
source

All Articles