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.

Shivansh
  • 37
  • 1
  • 10
  • 1
    There are `/` in the name so the date should be directories but it seems that the directories doesn't exists – Mickael B. Jan 23 '20 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 https://stackoverflow.com/questions/947954/how-to-have-the-cp-command-create-any-necessary-folders-for-copying-a-file-to-a – Mickael B. Jan 23 '20 at 11:21
  • @MickaelB. I just want to create one directory with slashes ( / ) in it, is it not possible? – Shivansh Jan 23 '20 at 11:38
  • No it's not possible. See https://stackoverflow.com/questions/9847288/is-it-possible-to-use-in-a-filename or https://askubuntu.com/questions/719916/how-to-have-a-forward-slash-in-a-file-name – Mickael B. Jan 23 '20 at 12:04

2 Answers2

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

nullPointer
  • 4,419
  • 1
  • 15
  • 27
0

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

 mkdir "⧸test"
DamianK
  • 387
  • 2
  • 6