0

Why is It that I am not able to create a file? Here is my bash script:

#!/bin/bash

var=$(date +"%D")

sudo cp ./script1.sh "$var _script1.txt"

It's showing this error:

cp: cannot create regular file '01/23/20 _script1.txt': No such file or directory

I am even using sudo so there's definitely no permission problem.

4
  • 1
    There are / in the name so the date should be directories but it seems that the directories doesn't exists Commented Jan 23, 2020 at 11:20
  • 2
    So if you want to create a directory 01 with a subdirectory 23 with a file 20_script1.txt you'll have to use mkdir -p see stackoverflow.com/questions/947954/… Commented Jan 23, 2020 at 11:21
  • @MickaelB. I just want to create one directory with slashes ( / ) in it, is it not possible? Commented Jan 23, 2020 at 11:38
  • No it's not possible. See stackoverflow.com/questions/9847288/… or askubuntu.com/questions/719916/… Commented Jan 23, 2020 at 12:04

2 Answers 2

1

Your date variable contains / but you should not have / inside a filename. See this

I would suggest you change your variable to the below format (using - instead as separators) :

var=$(date +%M-%d-%Y)

With this format your copy operation should work

Sign up to request clarification or add additional context in comments.

Comments

0

You can't it's prohibited. But you can always use work around. For example use one of these sign. ( ⟋ ⁄ ∕ ⧸ ⁄ )

 mkdir "⧸test"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.