Tux

...making Linux just a little more fun!

Space in Directory Names

Amit Kumar Saha [amitsaha.in at gmail.com]


Thu, 7 Feb 2008 10:44:23 +0530

Hello,

I have a directory named "Book Reviews", so when I try to CD to it using:

$ cd /media/sda5/Writings\ \&\ Presentations/Book Reviews

I get:

bash: cd: /media/sda5/Writings & Presentations/Book: No such file or directory

of course, I do not have a directory named 'Book'

I then come across this http://librenix.com/?inode=7041

I got away with it after using:

$ cd /media/sda5/Writings\ \&\ Presentations/"Book Reviews"

Is there any other way other to deal with spaces?

Comments much appreciated!

Thanks, Amit

-- 
Amit Kumar Saha
*NetBeans Community
Docs Coordinator*
Writer, Programmer, Researcher
http://amitsaha.in.googlepages.com
http://amitksaha.blogspot.com


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Thu, 7 Feb 2008 01:40:08 -0500

On Thu, Feb 07, 2008 at 10:44:23AM +0530, Amit Kumar Saha wrote:

> Hello,
> 
> I have a directory named "Book Reviews", so when I try to CD to it using:
> 
> $ cd /media/sda5/Writings\ \&\ Presentations/Book Reviews
> 
> I get:
> 
> bash: cd: /media/sda5/Writings & Presentations/Book: No such file or directory
> 
> of course, I do not have a directory named 'Book'
> 
> I then come across this
> http://librenix.com/?inode=7041
> 
> I got away with it after using:
> 
> $ cd /media/sda5/Writings\ \&\ Presentations/"Book Reviews"
> 
> Is there any other way other to deal with spaces?

Sure - use Bash completion.

cd /media/sda5/Writ<Tab>

That'll get you where you're going - possibly modulo a couple more characters, but it won't take much.

In general, though - I deal with this kind of thing regularly. I have files that have Russian filenames, filenames with spaces, filenames with quotes in them... it's still fairly easy; just do it the way the escape modes in the 'ls' command show it.

# I'll switch to a directory where I know I have files with spaces
ben@Tyr:~$ cd ~/Docs/MealMaster/categories/Algerian
ben@Tyr:~/Docs/MealMaster/categories/Algerian$ ls -1
ALGERIAN COOKED CARROT SALAD - KITCHEN SAFARI
BOUREK (BEEF-STUFFED PASTRY ROLLS)
EL HAM LAHLOU (SWEET LAMB FOR RAMADHAN)
LOUBIA BIL LUZ
 
# Now, I'll display them in "escaped" mode
ben@Tyr:~/Docs/MealMaster/categories/Algerian$ ls -1b
ALGERIAN\ COOKED\ CARROT\ SALAD\ -\ KITCHEN\ SAFARI
BOUREK\ (BEEF-STUFFED\ PASTRY\ ROLLS)
EL\ HAM\ LAHLOU\ (SWEET\ LAMB\ FOR\ RAMADHAN)
LOUBIA\ BIL\ LUZ
 
# And now, quoted.
ben@Tyr:~/Docs/MealMaster/categories/Algerian$ ls -1Q
"ALGERIAN COOKED CARROT SALAD - KITCHEN SAFARI"
"BOUREK (BEEF-STUFFED PASTRY ROLLS)"
"EL HAM LAHLOU (SWEET LAMB FOR RAMADHAN)"
"LOUBIA BIL LUZ"

Either of the last two will give you what you want.

-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


Amit Kumar Saha [amitsaha.in at gmail.com]


Thu, 7 Feb 2008 12:26:20 +0530

On 2/7/08, Ben Okopnik <ben@linuxgazette.net> wrote:

> On Thu, Feb 07, 2008 at 10:44:23AM +0530, Amit Kumar Saha wrote:
> > Hello,
> >
> > I have a directory named "Book Reviews", so when I try to CD to it using:
> >
> > $ cd /media/sda5/Writings\ \&\ Presentations/Book Reviews
> >
> > I get:
> >
> > bash: cd: /media/sda5/Writings & Presentations/Book: No such file or directory
> >
> > of course, I do not have a directory named 'Book'
> >
> > I then come across this
> > http://librenix.com/?inode=7041
> >
> > I got away with it after using:
> >
> > $ cd /media/sda5/Writings\ \&\ Presentations/"Book Reviews"
> >
> > Is there any other way other to deal with spaces?
>
> Sure - use Bash completion.

Yes, but this fails if I have a directory name, such as 'Book'.

-- 
Amit Kumar Saha
*NetBeans Community
Docs Coordinator*
Writer, Programmer, Researcher
http://amitsaha.in.googlepages.com
http://amitksaha.blogspot.com


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Thu, 7 Feb 2008 02:09:53 -0500

On Thu, Feb 07, 2008 at 12:26:20PM +0530, Amit Kumar Saha wrote:

> On 2/7/08, Ben Okopnik <ben@linuxgazette.net> wrote:
> > On Thu, Feb 07, 2008 at 10:44:23AM +0530, Amit Kumar Saha wrote:
> > >
> > > Is there any other way other to deal with spaces?
> >
> > Sure - use Bash completion.
> 
> Yes, but this fails if I have a directory name, such as 'Book'.

Really? It works fine for me.

ben@Tyr:~$ cd /usr/src/linux/Documentation/ABI/removed/

I got to the above by typing 'cd /u<tab>sr<tab>l<tab>D<tab>A<tab>r<tab>' - and all of the above are directories.

If the console beeps at you when you hit the tab, you may have more than one entry that matches what you've entered so far. Hit 'tab' again to see the options and resolve them by adding a character.

-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


Amit Kumar Saha [amitsaha.in at gmail.com]


Thu, 7 Feb 2008 12:47:26 +0530

On 2/7/08, Ben Okopnik <ben@linuxgazette.net> wrote:

> On Thu, Feb 07, 2008 at 12:26:20PM +0530, Amit Kumar Saha wrote:
> > On 2/7/08, Ben Okopnik <ben@linuxgazette.net> wrote:
> > > On Thu, Feb 07, 2008 at 10:44:23AM +0530, Amit Kumar Saha wrote:
> > > >
> > > > Is there any other way other to deal with spaces?
> > >
> > > Sure - use Bash completion.
> >
> > Yes, but this fails if I have a directory name, such as 'Book'.
>
> Really? It works fine for me.

I have 2 directories - 'Book' and 'Book Reviews'

$ cd Book

when I do this and press TAB, I get:

Book/         Book Reviews/

Now I do this,

$ cd Book R <TAB> <TAB> <TAB>.....

pressing any number of TABS doesn't show up anything

Am I missing something?

-- 
Amit Kumar Saha
*NetBeans Community
Docs Coordinator*
Writer, Programmer, Researcher
http://amitsaha.in.googlepages.com
http://amitksaha.blogspot.com


Top    Back


Jim Jackson [jj at franjam.org.uk]


Thu, 7 Feb 2008 11:50:41 +0000 (GMT)

On Thu, 7 Feb 2008, Amit Kumar Saha wrote:

> On 2/7/08, Ben Okopnik <ben@linuxgazette.net> wrote:
>> On Thu, Feb 07, 2008 at 12:26:20PM +0530, Amit Kumar Saha wrote:
>>> On 2/7/08, Ben Okopnik <ben@linuxgazette.net> wrote:
>>>> On Thu, Feb 07, 2008 at 10:44:23AM +0530, Amit Kumar Saha wrote:
>>>>>
>>>>> Is there any other way other to deal with spaces?
>>>>
>>>> Sure - use Bash completion.
>>>
>>> Yes, but this fails if I have a directory name, such as 'Book'.
>>
>> Really? It works fine for me.
>
> I have 2 directories - 'Book' and 'Book Reviews'
>
> $ cd Book
>
> when I do this and press TAB, I get:
>
> Book/         Book Reviews/
>
> Now I do this,
> $ cd Book R <TAB> <TAB> <TAB>.....
>
> pressing any number of TABS doesn't show up anything
>
> Am I missing something?

Yes.

The shell, bash more than likely, interprets the line you type. Ordinarily it treats the space character as the seperator between arguments on the command line. It also treats some other characters in a special way too, e.g. '$' - it assumes you are accessing a shell variable.

To stop bash from treating the ' ' or the '$' or anyother special character as special you must somehow tell the shell that in this case it is not special. You can do that for a single character by prefix the character '\' before the otherwise special character.

e.g. echo 1            2                      3
      1 2 3
 
      echo 1\ \ \ \ \ \ \ \ \ 2\ \ \ 3
      1         2   3
 
      try  echo $HOME  and  echo \$HOME

So if you have spaces (or e.g. '$'s) in your filesnames you needs to prefix, or "escape", them with a '\'.

In Tab completion, if you have directories Book and Book\ Reviews, then if you enter

    ls Book<tab>
nothing happens because there are 2 entries that could begin like that. To get the longer directory you must enter a space, but you must "escape" it with a '\'
    ls Book\ <tab>
will do the completion for you.

Another way to stop the shell from treating some special characters as special, is to quote with double quotes "...s t r i n g...". However some characters are still special with double quotes,

   e.g. echo "Home directory is $HOME"
still works - the HOME environment variable is still substituted.

Yet another way is to quote using single quotes '...$ t r i n g ....'. This stops the shell from messing with the string at all. No variable substitutions, no nothing.

There is chapter and verse on this in the bash manual page

   man bash
Then search for the quoting section
   /quoting
it explains it all in glorious technical colour.


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Thu, 7 Feb 2008 08:36:30 -0500

On Thu, Feb 07, 2008 at 12:47:26PM +0530, Amit Kumar Saha wrote:

> On 2/7/08, Ben Okopnik <ben@linuxgazette.net> wrote:
> > On Thu, Feb 07, 2008 at 12:26:20PM +0530, Amit Kumar Saha wrote:
> > > On 2/7/08, Ben Okopnik <ben@linuxgazette.net> wrote:
> > > > On Thu, Feb 07, 2008 at 10:44:23AM +0530, Amit Kumar Saha wrote:
> > > > >
> > > > > Is there any other way other to deal with spaces?
> > > >
> > > > Sure - use Bash completion.
> > >
> > > Yes, but this fails if I have a directory name, such as 'Book'.
> >
> > Really? It works fine for me.
> 
> I have 2 directories - 'Book' and 'Book Reviews'
> 
> $ cd Book
> 
> when I do this and press TAB, I get:
> 
> Book/         Book Reviews/
>
> Now I do this,
> $ cd Book R <TAB> <TAB> <TAB>.....

That's not going to work, because there isn't a 'Book R' string anywhere in it (there is, however, a '"Book R"' or a 'Book\ R') - so Bash is going to keep beeping. You could do

cd Book\ 

where the backslash is followed by a space - or, given that it's Bash, you could just say

cd *s

or, from the very start of your 'cd' string,

cd /foo/bar/zotz/qux/*s
> pressing any number of TABS doesn't show up anything
> 
> Am I missing something?

Yep. You added characters to the string that aren't part of the filename, as far as Bash is concerned.

-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


Amit Kumar Saha [amitsaha.in at gmail.com]


Thu, 7 Feb 2008 23:51:34 +0530

@JIm, @Ben,

Thanks for your pointers, I have got them.

Regards, Amit

-- 
Amit Kumar Saha
*NetBeans Community
Docs Coordinator*
Writer, Programmer, Researcher
http://amitsaha.in.googlepages.com
http://amitksaha.blogspot.com


Top    Back


Suramya Tomar [security at suramya.com]


Thu, 07 Feb 2008 12:12:26 +0530

Hey Amit,

> I then come across this
> http://librenix.com/?inode=7041
> 
> I got away with it after using:
> 
> $ cd /media/sda5/Writings\ \&\ Presentations/"Book Reviews"
> 
> Is there any other way other to deal with spaces?

You can also escape out the spaces instead out using quotes:

  cd /media/sda5/Writings\ \&\ Presentations/Book\ Reviews

I personally prefer using quotes instead of escaping each and every space.

- Suramya

-- 
Name : Suramya Tomar
Homepage URL: http://www.suramya.com
************************************************************


Top    Back