B333F
B333F

Reputation: 35

mkdir: cannot create directory No such file or directory

I can't find a solution for this problem. I would like to create a script that creates a folder, using the current date as its name. The script should then copy all content from the current folder to newly created folder. I tried following things already that doesn't work:

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

Upvotes: 1

Views: 22831

Answers (1)

kettlewell
kettlewell

Reputation: 11

This really looks like the backup directory didn't exist, and wasn't getting created

!/usr/bin/env bash                                                                                                                                                                            

# for extra debugging / tracing, uncomment this:                                                                                                                                               
# set -x                                                                                                                                                                                       

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

# printing out our variables, to make sure there's nothing weird in them:                                                                                                                      
printf "\nDATE:     %s\n" "${DATE}"
printf "PWD:      %s\n" "${PWD}"
printf "FILENAME: %s\n\n" "${FILENAME}"

# I took out the if statments ( they become redundant with mkdir -p )                                                                                                                          
# and also removed the cp action, since it's not really relevant here.                                                                                                                         


printf "Without the backup directory created, I'll spit out the error you had:\n\n"
mkdir    backup/${FILENAME}


printf "\n\nBut by using the -p argument, it can create the directory:\n\n"
mkdir -p backup/${FILENAME}

ls -l backup

printf "\n\n"

and looking at the output:

$ ./backup.sh 

DATE:     09-02-2024_15:49
PWD:      /home/matt/data/playground/stackoverflow
FILENAME: backup09-02-2024_15:49

Without the backup directory created, Ill spit out the error you had:

mkdir: cannot create directory ‘backup/backup09-02-2024_15:49’: No such file or directory


But by using the -p argument, it can create the directory:

total 4
drwxr-xr-x 2 matt matt 4096 Feb  9 15:49 backup09-02-2024_15:49

the output message is identical to what was originally displaying.

I modified the code you had a bit that includes set -x which is a tracing / debugging utility for bash... and I printed out your variables ( just in case something was weird with them ) ... and the if/else/fi statement was doing the same thing if the directory was there or not, because mkdir -p backup was in both, so they were effectively redundant.

By running the code multiple times the error doesn't exist, and you can see the output of the new backup directories with the timestamp in them.

There wasn't really more to go on, but being able to replicate the output gives me a fair bit of confidence that the directory just wasn't getting properly created ( perhaps from a previous iteration of code without mkdir -p)

hopefully this gives a bit of insight into how to debug things in the future.

Upvotes: 1

Related Questions