How to `cd` to a directory with spaces? [duplicate] - bash

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.

Related

Cut off the path of the working directory in the Linux console [duplicate]

This question already has answers here:
How can I shortern my command line prompt's current directory?
(12 answers)
Closed 2 years ago.
When I am getting into my working directory, I have the next pathname in the console:
kravcneger#kravcneger-X751L:~/projects/gcc/my_project
That path is very long, and it increases the width of the terminal window.
How can I make the pathname shorter, so that I wouldn't have to expand the terminal for comfortable work?
A critical condition: to change the machine name and the working directory is prohibited. :)
Add (or change) in your ~/.bashrc file PS1 variable:
PS1='\h \W\$ '
Here, \h is the machine name, \W is the basename of the current directory, and \$ is the literal $.
SEE ALSO:
Controlling the Prompt (Bash Reference Manual)

Aliased command in bashrc does not correctly use $OLDPWD [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 2 years ago.
I have an alias in bashrc that I use to generate a particular LaTeX document (the main command used is pdflatex) but I want to be able to execute it regardless of where I am. Unfortunately, it seems like you can't use use this command with an absolute path unless the path is 'under' your current directory, so I cd into the directory my .tex file is, run pdflatex, then try to cd back to my previous directory. In bashrc, I have my command as
alias nbr="cd ~/path/to/dir && pdflatex file.tex && cd $OLDPWD"
The command works fine outside of putting me back in my previous directory. For some reason, this command works perfectly fine in a regular bash shell, so I'm guessing there's some issue with what $OLDPWD is considered in the context of bashrc but I'm not sure. Any ideas?
#Cyrus's answer fixed it...should've used single quotes.

How to execute an .exe file including blank spaces in it [duplicate]

This question already has answers here:
How to create batch file in Windows using "start" with a path and command with spaces
(7 answers)
Closed 2 years ago.
So normally I have several *.bat files to automate some things in my computer.
But I'm kinda stuck in this call...
cd "C:\Users\user\AppData\Local\Programs\program2Execute"
start program has spaces.exe
I don't get the app executed, instead I get a cmd window with the cd command result, nothing else. I tried to quote "program has spaces.exe" but it doesn't get executed I get the window duplicated instead.
Apologize if this is kinda dumb to ask, but I have spent quite time looking for the answer.
Thanks in advance.
If you use the start command:
start "" "a program with spaces.exe"
start will use the first double quoted string as the title and the 2nd as the command.
Or don't use start:
"a program with spaces.exe"

How can I access a directory that is in $HOME using bash? [duplicate]

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"/*

Bash: Change to Directory with Space [duplicate]

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.

Resources