Please enable JavaScript eh!

 ⌘ Web Mechanic ⌘ 

Bash Scripting


mkdir 2

It's one thing to create a directory, but what if you need several? We can do that!

Back in Terminal enter the following commands:

cd ~/Desktop
mkdir 3 1 2

Now take a look at your Desktop, either with Finder or here in Terminal. You should see 3 new directories as you named them - k00l!

Not done yet. What if we want to make a directory that contains another one?

mkdir -p 4/5/6
The '-p' option allows you to create a directory hierarchy, including parent directories that don't exist. It prevents errors if the specified directories already exist.

Now you have a directory named 4 that also contains a directory 5, that also contains a directory 6.

Now that you've cluttered up your Desktop with some oddly-named directories, let's get rid of them.

rmdir

$RANODM

Another interesting way to create directories is to use the built-in system variable $RANDOM or the command shuf.

$RANDOM only generates pseudo-random integers in the range 0 - 32767 inclusive, but this can be expanded into the range you need using an arithmetic expansion.

First we need to get the value of $RANDOM into a variable:

temp_dir=$RANDOM

Then we can use it like this:

mkdir -p -m 0700 $temp_dir

On my system I created 2 new randomly named directories:

24286
30854

shuf

This is yet another way to create random integers in bash. It needs a few arguments to work:

shuf -i 1-100000 -n 10
60683
26319
37995
16348
35165
43051
63624
29031
42677
96133

To use this to create directories, we need to get the value into a variable:

temper=$(shuf -i 1-500000 -n 1)
echo $temper
272159

Now we can use that with mkdir:

mkdir $temper

drwxr-xr-x  2 trudge  staff    64 Sep  4 15:17 272159

K00L!