- How to cd into a directory with space in the name?
- 18 Answers 18
- How to access files/directories with spaces in the name? [duplicate]
- 2 Answers 2
- How to Tackle Filenames With Spaces in Linux
- Read a file with spaces in filename
- Create a file with space in filename
- Dealing with space in folder name
- How do I navigate to folders with spaces in their names? I get "no such file or directory" when I try [duplicate]
- 2 Answers 2
How to cd into a directory with space in the name?
When I manually type it in, the backspace does its escape character thing, but not when I use parameter expansion with the variable DOCS . I tried other variations such as no backslash.
$ DOCS=/cygdrive/c/Users\ dir/Documents $ echo $DOCS /cygdrive/c/Users/my dir/Documents $ cd $DOCS -bash: cd: /cygdrive/c/Users/my: No such file or directory
$ DOCS="/cygdrive/c/Users/my dir/Documents" $ echo $DOCS /cygdrive/c/Users/my dir/Documents $ cd $DOCS -bash: cd: /cygdrive/c/Users/my: No such file or directory
$ DOCS="\"/cygdrive/c/Users/my dir/Documents\"" $ echo $DOCS "/cygdrive/c/Users/my dir/Documents" $ cd $DOCS -bash: cd: "/cygdrive/c/Users/my: No such file or directory
No realy difference if I escape the space using a backslash or quotes, but I did it again anyway. See revision.
18 Answers 18
You need to quote «$DOCS» to prevent spaces from being parsed as word separators. More often than not, variable references should be quoted.
Note that $HOME would have the same problem. The issue is coming from when the shell evaluates variable references; it’s nothing to do with what variables you use or how you assign to them. It’s the expansion that needs to be quoted.
This is deceptive. echo is actually echoing the two strings /home/my and dir . If you use cd or ls you’ll see how it’s actually working.
$ ls $HOME ls: cannot access /home/my: No such file or directory ls: cannot access dir: No such file or directory $ cd $HOME bash: cd: /home/my: No such file or directory $ cd "$HOME"
Can I ask why it works when I manually type it in but not in a variable?
Great question! Let’s examine the commands you typed:
$ DOCS="\"/cygdrive/c/Users/my dir/Documents\"" $ echo $DOCS "/cygdrive/c/Users/my dir/Documents" $ cd $DOCS -bash: cd: "/cygdrive/c/Users/my: No such file or directory
The reason this doesn’t work is because Bash doesn’t parse quotes inside variable expansions. It does perform word splitting, so whitespace in unquoted variable expansions is taken as word separators. It doesn’t parse quotes in any way, meaning you can’t put double quotes inside a variable to override word splitting.
Because of this, cd is passed two parameters. As far as cd knows it looks like you wrote:
$ cd '"/cygdrive/c/Users/my' 'dir/Documents"'
Two parameters, with double quotes intact.
Oh.. what the. I could’ve sworn I could do cd $HOME, but. hm. Okay, is there a way to include » inside the variable? I will try that now.
The reason I’m submitting this answer is you’ll find that StackOverflow is being used by every day users (not just web devs, programmers or power users) and this was the number one result for a simple Windows user question on Google.
People are becoming more tech-savvy, but aren’t necessarily familiar with command line in the cases above.
To change to a directory with spaces on the name you just have to type like this:
Hit enter and you will be good
$ DOCS="/cygdrive/c/Users/my\ dir/Documents"
Here’s your first problem. This puts an actual backslash character into $DOCS , as you can see by running this command:
$ echo "$DOCS" /cygdrive/c/Users/my\ `
When defining DOCS , you do need to escape the space character. You can quote the string (using either single or double quotes) or you can escape just the space character with a backslash. You can’t do both. (On most Unix-like systems, you can have a backslash in a file or directory name, though it’s not a good idea. On Cygwin or Windows, \ is a directory delimiter. But I’m going to assume the actual name of the directory is my dir , not my\ dir .)
This passes two arguments to cd . The first is cygdrive/c/Users/my\ , and the second is dir/Documents . It happens that cd quietly ignores all but its first argument, which explains the error message:
-bash: cd: /cygdrive/c/Users/my\: No such file or directory
To set $DOCS to the name of your Documents directory, do any one of these:
$ DOCS="/cygdrive/c/Users/my dir/Documents" $ DOCS='/cygdrive/c/Users/my dir/Documents' $ DOCS=/cygdrive/c/Users/my\ dir/Documents
Once you’ve done that, to change to your Documents directory, enclose the variable reference in double quotes (that’s a good idea for any variable reference in bash, unless you’re sure the value doesn’t have any funny characters):
You might also consider giving that directory a name without any spaces in it — though that can be hard to do in general on Windows.
How to access files/directories with spaces in the name? [duplicate]
Through terminal I can’t access files or directories with a spaces in their names. The cd command says no such file or directory . Is there any way to do it or should I rename all files with spaces?
2 Answers 2
To access a directory having space in between the name use \ to access it. You can also use Tab button to auto completion of name.
guru@guru-Aspire-5738:~$ cd /media/Data/My\ Data/ guru@guru-Aspire-5738:/media/Data/My Data$.
To to use files with spaces you can either use the escape character or youse the double quotes.
\ is called escape character, used to not expansion of space, so now bash read the space as part of file name.
Now to rename files, it’s so easy to rename all files with spaces and replace space with underscore:
for file in * ; do mv "$f" "$" ; done
look at answer here there is a script to rename all files and dirs recursively.
The script is:(All rights go to its owner)
#!/bin/bash # set -o xtrace # uncomment for debugging declare weirdchars=" &\'" function normalise_and_rename() < declare -a list=("$") for fileordir in "$"; do newname="$]/_>" [[ ! -a "$newname" ]] && \ mv "$fileordir" "$newname" || \ echo "Skipping existing file, $newname." done > declare -a dirs files while IFS= read -r -d '' dir; do dirs+=("$dir") done < <(find -type d -print0 | sort -z) normalise_and_rename dirs[@] while IFS= read -r -d '' file; do files+=("$file") done < <(find -type f -print0 | sort -z) normalise_and_rename files[@]
How to Tackle Filenames With Spaces in Linux
Spaces in the file names could be tricky, specially for new Linux users. Learn how to deal with them.
The one thing you'll notice that files in Linux usually do not contain names. Your teacher or colleague use underscore instead of spaces in file and directory names.
It's not that you cannot use spaces in file names in Linux terminal. It's just that it creates additional pain and that's why you should avoid it wherever possible.
Why? Let me show that with examples. You know the generic syntax for Linux commands:
command [options] argument1 argument2
In here, the arguments are separated by spaces. If you try to use filenames with spaces directly, it will be treated as separate arguments rather than just one argument.
In the above screenshot, when I try to use cat agatha books command, it doesn't understand that agatha books is a single argument. It treats agatha and books as different filenames.
How do you deal with spaces in filename, then? There are two ways:
Wrap the entire filename between quotes:
Escape every space using backslash key:
Tab completion often works with spaces as well. Your terminal may show the file name with space escaped with backslash if you press tab key for the filename.
Read a file with spaces in filename
To use a filename with spaces in it, you can wrap it in quotes like this:
cat "file name with spaces"
You may also escape every space with backslash but it is more work and more confusing than the previous method:
cat file\ name\ with\ spaces
Basically, you put a \ before every space in the filename.
You could also use single quotes instead of double quotes.
cat 'file name with spaces'
Single quotes ignore any special characters. Double quotes ignores all except $, back quotes and baclslashes. More on it in some other tutorial.
Create a file with space in filename
Now, you need to type space in terminal to create the filename here. Use backslash or quotes again.
Similar to the previous section, you can create new files with spaces in the filename using quotes:
touch "file name with spaces"
touch file\ name\ with\ spaces
Dealing with space in folder name
You can create a directory with space in its name the same way you create a file.
Now, if you want to switch to this directory, you'll have a path with spaces.
But that should not be a problem for you anymore. To cd into a directory with space, use quotes or backslash again.
Basically, whenever you have to deal with spaces in names, you use quotes or backslash keys.
Suppose you have to copy a file my file from this new dir . Here's what you can do:
Now it starts to get confusing a bit, right? There are backslashes and forward slashes. It could intimidate a new user or even a seasoned one if there are way too many of those slashes.
It gets even messier when there are backslashes in the filename. Then you'll be seeing double backsplashes.
This is the reason why you should try and avoid using spaces or other special characters in file names. To separate the words in a file name, use underscore.
touch a_very_long_file_name_with_too_many_words
This makes the filenames easier to read and you won't have to make the extra effort to deal with those spaces in the filenames.
How do I navigate to folders with spaces in their names? I get "no such file or directory" when I try [duplicate]
I'm completely new and lost in Ubuntu 12.04, I'm having trouble navigating to my Sublime folder which I know is in my /opt folder. Yet I've tried to no avail to navigate into it via Terminal window. The directory is colored in blue, no idea what this means. but I am able to access the folder using the GUI explorer.
k@k-Ubuntu:~$ cd /opt k@k-Ubuntu:/opt$ ls Sublime Text 2 k@k-Ubuntu:/opt$ cd Sublime Text 2 bash: cd: Sublime: No such file or directory k@k-Ubuntu:/opt$ cd /Sublime Text 2 bash: cd: /Sublime: No such file or directory
2 Answers 2
Go inside the /opt directory via terminal and then run below command,
k@k-Ubuntu:~$ cd /opt k@k-Ubuntu:/opt$ cd "Sublime Text 2"
As the folder you want to connect has spaces in the name, you must surround the name with quotes in order for the Shell to read it correctly (as one name). In other case it will read only the Sublime and this doesn’t exist.
Another way to avoid this issue, is to use Tab Completion. This is a feature that will help you to auto-fill weird names and/or long names. Read here about Tab Completion and learn it. It is very useful(in Ubuntu is pre-installed).
A third way for names with spaces is the backslash \ . Above command with quotes could be
the backslash followed by a space explicitly denotes a space.
A suggestion could be: Do not create folders in Linux with space in name. Prefer something like Sublime-Text-2 or Sublime_Text_2