0

I have a directory called "foo bar baz blob". How can I

$ cd "foo bar baz blob"

I have tried with quoting (" and ') and with escaping the blanks (\ ). It does not work. Tab-completion does not work either.

I also have to issue commands with filename arguments that contain spaces. How do I do that? Even MidnightCommander (mc) fails in doing this.

EDIT

Over one year later, I finally found the source of my problem. I have overloaded the builtin cd with a function that automatically lists the files of the changed directory:


$ type cd
cd ist eine Funktion.
cd () 
{ 
    if builtin cd $1; then
        ls;
    fi
}

$ cd Interner\ Speicher/
bash: cd: Interner: Datei oder Verzeichnis nicht gefunden

$ builtin cd Interner\ Speicher/

$ pwd
/run/user/1000/gvfs/mtp:host=%5Busb%3A002%2C018%5D/Interner Speicher

$ ls
Alarms   DCIM      Movies  Notifications  Podcasts   SmsContactsBackup
Android  Download  Music   Pictures   Ringtones

The solution is easy: I just have to put double quotes around the $1:

if builtin cd "$1"; then

Ooof. Thank you.

4
  • 1
    Quoting or escaping should work. Is the foo bar baz blob directory within your current directory? Check with ls - if not you will need to add the absolute or relative path to the directory name. Jan 11, 2014 at 20:01
  • Is the foo bar baz blob folder in the home directory? By default, terminal opens 'in' the home directory, so the command would not work otherwise.This is how it should be escaped in terminal: cd foo\ bar\ baz\ blob/.
    – Wilf
    Jan 11, 2014 at 20:04
  • 1
    do ls in the directory and edit it in your question so we can see exactly what characters we are dealing with.
    – Braiam
    Jan 11, 2014 at 20:56
  • Perhaps did you get these files from a mac? cyberciti.biz/tips/…
    – Mateo
    Jan 11, 2014 at 21:26

3 Answers 3

2

You have three options:

  • Escape the spaces using a backslash character
  • Wrap the directory name in double-quotes ("")
  • Use TAB completion

Creating the directory:

user@pc:~/testfolder$ ls -l
total 0
user@pc:~/testfolder$ mkdir "foo bar baz blob"
user@pc:~/testfolder$ ls
foo bar baz blob

Method 1:

user@pc:~/testfolder$ cd foo\ bar\ baz\ blob/
user@pc:~/testfolder/foo bar baz blob$ echo "This works"
This works

Method 2:

user@pc:~/testfolder$ cd "foo bar baz blob"/
user@pc:~/testfolder/foo bar baz blob$ echo "This works, too"
This works, too

Method 3:

user@pc:~/testfolder$ cd foo<TAB><ENTER>

Based on your comment on the other answer: for accessing the file named, the escaping needs to be done as follows:

Eric\ Burdon\ -\ Starportrait\ -\ CD\ 1\ \(flac\).cue foo\ bar\ baz\ blob/

However, using TAB completion makes this process easier and avoids you having to escape the spaces manually.

2
  • method 3 would be my method 1 :D [tab] seems to work better (it will also resolve unprintable chars and will also do different enconding correctly)
    – Rinzwind
    Jan 11, 2014 at 21:08
  • I forgot to mention that I changed the directory's name into "EricBurdonStarportrait" with the Thunar file manager, so that the problem left was to use the filenames as arguments on the command line.
    – ubuplex
    Jan 12, 2014 at 11:20
1

I just created the same folder, in /home/abed, used tab completion, and it worked. See image below. If the folder was created at a different location, you'll have to navigate to that location before you can CD into your folder.

enter image description here

That's also OK. See image

enter image description here

4
  • What wrong with the answer?
    – Mitch
    Jan 11, 2014 at 20:11
  • OK, Hmmm. Then I think the problem is that there is a "-" in the directory name, e.g., "The Rolling Stones - Sticky Fingers - 1971". Escaping by "\-" does not work.
    – ubuplex
    Jan 11, 2014 at 20:36
  • Then the problem is in a "(", which is also in the filename. The file I want to process is "Eric Burdon - Starportrait - CD 1 (flac).cue". Here, escaping ( and ) does not work: shntool [split]: error: need exactly one file to process
    – ubuplex
    Jan 11, 2014 at 20:52
  • @ubuplex is a file or a directory?
    – Braiam
    Jan 11, 2014 at 20:57
1

Quote your filename with single quotes.

cd 'foo bar baz literal string'

In the shell, nothing is interpreted inside single quotes so you should be safe from having unusual characters.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .