Mkdir: Cannot Create Directory No Such File or Directory

I can't find a solution for this problem. I am trying to write a script that creates a folder, which name is the current date, with all content of the current directory so I can backup a bunch of files. I tried following things already that doesn't work:

  • use "mkdir -p" to create parent directories
  • not using relative path with "pwd"

But when I try and create a directory out of an script in the terminal(also bash) I can create perfectly fine directories with the date inside. (following command)

mkdir backup$(date +%d-%m-%Y_%H:%M)

My Code

#!/bin/bash

DATE=$(date +%d-%m-%Y_%H:%M)
PWD=$(pwd)
FILENAME=backup$DATE

if [ -d "backup/" ]; then
    mkdir -p backup/$FILENAME
    cp -r * backup/$FILENAME
else
    mkdir -p backup/
    mkdir -p backup/$FILENAME
    cp -r * backup/$FILENAME
fi

Error thrown

mkdir: cannot create directory ‘backup/backup18-01-2022_12:43’: No such file or directory

9
Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.