Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h3>Two ansers: The compatible <a href="/questions/tagged/shell" class="post-tag" title="show questions tagged 'shell'" rel="tag">shell</a> and the pure <a href="/questions/tagged/bash" class="post-tag" title="show questions tagged 'bash'" rel="tag">bash</a>:</h3> <p><em><strong>Last edit:</em></strong> Make regexp more precise and correct some counting bugs.</p> <p>Something like:</p> <pre><code>create_folder() { if [ -e "$1" ] ;then counter=1; while [ -e "$1-$counter" ] ;do counter=$((counter+1)) done mkdir "$1-$counter"; else mkdir "$1"; fi; } </code></pre> <p>could do:</p> <pre><code>$ 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 </code></pre> <p>or with a better formatting and another way of counting (quicker if many directories exists as it use specialized count function, but longer if else because of forks.):</p> <pre><code>create_folder() { if [ -e "$1" ] ;then counter=`/bin/ls -1 "$1" "$1-"* 2&gt;&amp;1 | grep -E "^$1(-[0-9]*)?" | wc -l`; nname="`printf "%s-%04d" "$1" $counter`"; mkdir "$nname"; else mkdir "$1"; fi; } </code></pre> <p>This was tested under different <a href="/questions/tagged/shell" class="post-tag" title="show questions tagged 'shell'" rel="tag">shell</a> implementations: <a href="/questions/tagged/bash" class="post-tag" title="show questions tagged 'bash'" rel="tag">bash</a>, <a href="/questions/tagged/dash" class="post-tag" title="show questions tagged 'dash'" rel="tag">dash</a>, <a href="/questions/tagged/ash" class="post-tag" title="show questions tagged 'ash'" rel="tag">ash</a> and <a href="/questions/tagged/ksh" class="post-tag" title="show questions tagged 'ksh'" rel="tag">ksh</a></p> <pre><code>$ 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 </code></pre> <p>And with dirname containing whitespaces:</p> <pre><code>$ 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 </code></pre> <h3>And for <a href="/questions/tagged/bash" class="post-tag" title="show questions tagged 'bash'" rel="tag">bash</a> fans:</h3> <pre><code>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; } </code></pre> <p>Very quick, no fork and nice formatting:</p> <pre><code>$ 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload