-2

i want to create directory called "A" inside my home directory but i don't know if such a directory already exists. i want to write a command that creates the directory if it does not exist. But, if the directory exists the command will create a new directory with the same name with a small addition that changes the name of the directory. The small addition should be random - that is, each time the command is run, a different addition will be received. For example, instead of "A" the command will create directory "A15" or "A7" or any such addition. It can be assumed that the new directory with the addition does not exist. The command will print the message !DONE in case the original directory "A" does not exist and it is created successfully or the message Directory exists. A new one has been created A05 where A05 is the new name

2
  • 1
    See test (which can also be invoked via square brackets. E.g. [ -d A ] && mkdir A1 || mkdir A ). How you come up with the random part is not specified, nor is how you would handle the problem of that directory also existing. But see also the $RANDOM variable. Commented Jan 19, 2023 at 15:54
  • "i want to do this without bash script" and " i want to write a command" then what do you want to write it in? C? Python? Those are offtopic here.
    – muru
    Commented Jan 19, 2023 at 16:08

1 Answer 1

2

I'm assuming that "without a bash script" means to use a series of commands or a handy shell function. I will give you a handy shell function.

This function tries to create a directory of the given name and will retry with different numerical suffixes until the directory has been created without error. The numerical indexes will be tested in order from 1 to 99. If the numerical range is exhausted, a diagnostic message is outputted, and no directory is created. If a directory is created, its pathname is written to standard output.

makedir () {
    local name="$1"
    local suffix=

    until mkdir -- "$name$suffix" 2>/dev/null; do
        suffix=$(( suffix + 1 ))
        if [ "$suffix" -eq 100 ]; then
            echo 'Cannot create directory' >&2
            return 1
        fi
    done

    printf '%s\n' "$name$suffix"
}

This iterates using until until the mkdir command succeeds. If the suffix counter reaches 100, a message is outputted to the error stream and tho function returns a non-zero exit status.

This function definition could be added to wherever you normally add aliases. It would then be available in your next shell session.

If you want a random number rather than monotonically increasing numbers from the range from 1 to 99:

makedir () {
    local name="$1"
    local tries=0
    local suffix=

    until mkdir -- "$name$suffix" 2>/dev/null; do
        tries=$(( tries + 1 ))
        if [ "$tries" -eq 100 ]; then
            echo 'Cannot create directory' >&2
            return 1
        fi
        suffix=$RANDOM
    done

    printf '%s\n' "$name$suffix"
}

This is almost the same function, but it uses $RANDOM as the suffix, which will be some integer. As before, the function bails out if it hasn't been able to create the directory in less than 100 tries.

If this is to create a temporary directory, it would be better to use mktemp -d, though, rather than some home-grown thing. This would, by default, give you the name of a directory under /tmp:

tmpdir=$( mktemp -d )

# Do some work beneath "$tmpdir".

# Then...
rm -r "$tmpdir"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .