1

I would like to copy a file from one location to another. When I use the cp command in a terminal it works well, for example I use the following command:

cp -r projects/Test/ projects/Personal/ 

This leads to a creation of the folder Test in the folder Personal containing all contents of Test in the folder projects. However when I execute the same line in a bash file, instead of having a folder named Test in Personal I have a folder named ~. The contents of the file are correct.

Would you know how I could have the folder named Test instead of ~? This is the code of my bash file:

#!/bin/sh cp -r projects/Test/ projects/Personal/ 
9
  • 1
    The tilde is special in the shell in that it's code for the user's home directory. For cp, it only comes up with the backup options (e.g. cp --backup foo.txt bar.txt effectively renames bar.txt to bar.txt~ and then does the copy). How it could produce a file named just ~, that's harder to understand. I think this would be a case where it would be useful to see exactly the files before and after, and the exact commands used. Better, a set of commands that could be used to replicate the issue, starting from creating the files, etc. You can edit your question from the link below it. Commented Feb 9, 2024 at 9:17
  • 1
    It is unclear what "I have a folder named with a tilde sign" means. Does it mean you have a directory called something~ or just ~? Did you run the cp command against the same target directory several times? Commented Feb 9, 2024 at 9:55
  • 1
    The name of the folder is "~", that is, the tilde only. Commented Feb 9, 2024 at 13:01
  • 3
    No, there is nothing bash specific in your script, so using bash wouldn't make a difference. Can you please edit your question and show us how you run this and an ls before and after showing the result? And please also tell us what operating system you are using. What you describe is very strange so I suspect there must be something more going on. Commented Feb 9, 2024 at 13:24
  • 2
    Your shebang is causing the script to be run under /bin/sh but your question refers to bash. Which shell are you intending to use? Commented Feb 17, 2024 at 20:56

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.