whynot
whynot

Reputation: 1

mkdir no such file or directory syntax error?

I'm trying to auto-create an auto folder that is unique. Therefore i'm trying to name the folder after a date time stamp.

the code below is what i have already tried.

#! /bin/bash
d_space=$(date)
d_nospace= echo ${d_space//[[:blanl:]]/}
echo $d_nospace
mkdir -p "$d_nospace"

WedMay2920:52:47EDT2019
mkdir: cannot create directory '': No such file or directory

Upvotes: 0

Views: 1135

Answers (2)

Keith Thompson
Keith Thompson

Reputation: 263627

d_nospace= echo ${d_space//[[:blanl:]]/}
...
mkdir -p "$d_nospace"

You're trying to create a directory whose name is the empty string. The "No such file or directory" message is a little misleading. The real problem is that the name is invalid.

A variable assignment must have no spaces before or after the = character. You can have a variable assignment as part of another command, for example:

foo=bar some_command

will execute some_command with $foo set to bar.

So your command invokes the echo command with the variable d_nospace set to nothing.

As John's answer points out, you don't need to use echo here -- and in fact it doesn't make sense to do so. If you wanted to delete spaces, you'd write:

d_nospace="${d_space//[[:blanl:]]/}"

(I haven't checked that for accuracy; shouldn't that be [[:blank:]]?

Incidentally, I wouldn't use the output of date as a file or directory name. The spaces and colons can cause problems, the output can vary depending on your locale settings, and the result does not sort well.

Here's how I'd do it:

now="$(date +%F-%H%M%S)"
mkdir -p "$now"

As I write this, the directory name would be 2019-05-29-191819, which is reasonably human-readable, sorts nicely, and avoids problematic characters.

Upvotes: 0

John Kugelman
John Kugelman

Reputation: 362137

d_nospace= echo ${d_space//[[:blanl:]]/}

You're missing a $(...) capture operator. There shouldn't be a space after the equal sign. And blanl should be blank.

d_nospace=$(echo ${d_space//[[:blank:]]/})

Let's get rid of the useless use of echo. There's no need to echo a variable out and then capture the output. Just assign the variable directly.

d_nospace=${d_space//[[:blank:]]/}

(You don't have to get rid of the spaces, by the way. Directory names can contain whitespace, it's fine.)

Upvotes: 2

Related Questions