This question already has answers here:
"~/Desktop/test.txt: No such file or directory"
(2 answers)
Why isn't tilde (~) expanding inside double quotes? [duplicate]
(1 answer)
Closed 4 years ago.
I have a couple of scripts for configuring new computers. I have a section dedicated to osx, where I create specific osx stuff. I wanted to change the default location for screenshots,but first I need to create the required folder. However, when I try to create the folder at ~/Documents/screenshots I get a variety of errors:
If I just use mkdir, then it tells me that the file already exists
If I add the -p option then I get no error, but nothing happens, the file does not get created
I tried the same code on the console directly with identical results.
Here is a small part of the code. The get boolean response is just a small function that returs true or false depending if the user types Yes or No
#!/bin/bash
screenshotsFolder="~/Documents/screenshots"
if get_boolean_response "Create a folder for the screenshots at $screenshotsFolder?"; then
mkdir "$screenshotsFolder"
fi
I tried changing the name to a random one, but I get exactly the same results.
Any advice?
Related
This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 1 year ago.
I am trying to write a bash script that takes 2 inputs: a list of IDs and 2) a directory. The idea is that the script will move a subset of files corresponding to the list of IDs into a a new folder.
#! /usr/bin/bash
LIST_FILE=$(cat ${1}) # List of file IDs (example entry: 1.2345)
PATH=${2} # directory name (i.e group_1)
for i in ${LIST_FILE}
do
/usr/bin/mv /tmp/project/project_data/data_all/${i}.annotated.gz /tmp/project/project_data/${PATH}/
done
The script manages to loop ok, however I get the following error for each iteration:
/usr/bin/mv: cannot stat '/tmp/project/project_data/data_all/1.2345'$'\r''.annotated.gz': No such file or directory
It looks like the file name hasn't concatenated properly and I'm not sure why. I've tried researching the problem but I'm also quite new to bash and finding it hard to grasp the concept of the stat error. I appreciate any advice and possible solutions.
Thanks everyone.
I think that the file whose name you pass as the first argument to your script is in dos format instead of unix so you are getting extra \r characters in your file names.
You could change your third line to:
LIST_FILE=$(cat ${1}|tr -d '\r')
Bobby
This question already has answers here:
Bash script to cd to directory with spaces in pathname
(14 answers)
Closed 2 years ago.
I am trying to navigate the bash command line to a subdirectory of my /projects directory, and I keep getting 'No such file or directory'.
The ls command clearly shows the directory in existence.
What am I missing?
Note: I am trying to navigate to the directory to run npm tests, if that context matters :)
This may be due to the space in your directory name.
You can either rename it for easier access or just start writing the name and use autocompletion (with tab) to let the system write it properly for you.
If the above solutions still don't work, try to escape the space with a backslash (Content\ Creator) to see what happens.
You may try to put quotation marks around 'Content Creator' like that.
When you try to navigate to any directory, it is better you always use quotation marks even there is no space character. Because it could have similarity with a command. To prevent any trouble.
This question already has answers here:
"~/Desktop/test.txt: No such file or directory"
(2 answers)
Why isn't tilde (~) expanding inside double quotes? [duplicate]
(1 answer)
Closed 4 years ago.
I'm new to Linux as well as bash. I made a script that accesses a folder that is located in the home directory, but the script will not always be called from the home directory. The prompt I'm getting when calling it from any subdirectories specifies that it can not find the file.
Here's an example of what I'm trying to do:
for entry in "~/.directory"/*
do
echo "$entry"
done
If I place the script in a subdirectory of /home and try to call it, the script is unable to find the directory. I know it exists as if I run ls ~/.directory in the subdirectory it is able to find the files and print them with no problem. Is there a different way I should be trying to access the directory in the bash shell? Thanks!
Voted to close my question. It seems rather specific to me, and the general solution was something I found earlier and was also posted in the comments below. I'll figure it out eventually -
Only unquoted tildes are expanded.
for entry in ~/".directory"/*
This question already has answers here:
Why do you need ./ (dot-slash) before executable or script name to run it in bash?
(9 answers)
Closed 8 years ago.
As the question asked I thought "." means the current directory so why can't we directly type helloworld to run the program?
Because '.', the current directory, is not in your environment's $PATH, which contains the list of paths where executables get searched. To see your PATH variable, type
echo $PATH
This is most likely for security reasons, to prevent execution of local executables named after system or other trusted installed ones. I have worked on systems where '.' was in the PATH and at the very least it lead to some confusing moments (the test utility being a favourite candidate for accidental replacement.)
I would advise against appending '.' to PATH for those reasons.
This question already has answers here:
How to cd into a directory with space in the name?
(18 answers)
Closed 9 years ago.
I have no idea what could be wrong here, I have just installed Ubuntu and I am trying to run a program called Exercise2.cpp. Exercise2.cpp is inside a folder called C++ Development in the Documents directory.
I can get as far as the Documents directroy, however when I try to change in to C++ Development it says " bash:cd: C++: No such file or directory"
What does this mean, I have been trying different things, like adding C++ Development in a quote but it still does not work.?
Escape the spaces with a backslash.
cd C++\ Development
In general, though, it's good practice to avoid using spaces in file and directory names. Avoiding spaces makes it much easier to use the simple UNIX tools to do sophisticated stuff with your data.