Home Home > 2014 > 06 > 24 > Command Line Tuesdays – Part Two
Sign up | Login

Command Line Tuesdays – Part Two

June 24th, 2014 by

Heya geekos!

Let’s refresh our memories. Last week, we skimmed through some basic commands, learned what a shell is actually, and made a steady introduction into our CLI Tuesdays series.

Today’s menu offers something else: navigation through the file system.

Now, the best picture I managed to find on the www is from a site called devopsbootcamp. You can find their tutorials and the rest here. But anyway, here’s a very nice diagram of what a linux root filesystem looks like.
Filesys

For example, as stated in the above picture, your user directory (where you usually store your movies, music, documents etc.) is located in the /home folder. /home folder is located under /. Then you have the /etc folder, where most of the files for configuration are located. Anyway, you can find the detailed description here, as we’ll not be getting into which folder is for what, until we start using and configuring them. Today is reserved for navigation only. And on that note, let’s get down to the first command of the day…

 

pwd

pwd, or ‘print working directory’ is a very useful command if you think you are lost navigating through the folders. At any given moment, type pwd, and voila! What appears is a complete pathway you took to arrive to this folder. Those guys in The Cube could sure use it, losers.
Cube

Imagine yourselves walking from room to room inside a massive apartment, loosing your way. pwd is like the breadcrumb trail leading you to your starting point, so you don’t loose your way inside the folder maze!
pwd

cd

Now you know in which directory you’re situated thanks to your usage of the pwd command. Now you want to take the next step and move to another directory. Let’s say you have a folder/directory inside your home folder you want to relocate your top secret terminal operation to. For this, you use the ‘cd’ command. cd, or ‘change directory’, will change the location of wanted directory. How do you use it? Simple, type cd and the path to your folder. Let’s say, for example, you want to enter your Hello Kitty picture collection in your home folder. You type ‘cd /home/username/Hello\ Kitty’.

As you see, we didn’t only use the space bar in the folder name. That’s because the terminal won’t recognize it. Whenever you want to navigate to a folder containing a space in it’s name, you replace it with the backslash character, followed by space. You can also, without using the backslash+space option, just put the whole folder name into quotes, f.ex. cd /home/username/”Hello Kitty”.
cd

Try it out yourself. Navigate to a different directory using cd, and when there, type pwd to see if everything worked as it’s supposed to.

Mr. Shotts’ Shottcuts

Mr. Shotts reminds us there are also some shortcuts available.

If you type only cd, without the path following it, your terminal will change your working directory (whatever it may be) to your /home folder.

The same, if you type cd ~user_name it will lead you to the home folder of the specific user you stated.

 Next Week

Next week, we’ll head over to the next chapter – we’ll learn how to list files and directories, view text files and classify file’s contents so it will be a bit more work than we’re used to, but I hope you’ll have enough time. Command by command, and if you haven’t the time to do it yourself, we’ll learn the basics together in a matter of months!

and remember…

 

…have a lot of fun!

 

 

P.S.: Thanks to bwl’s comment, we fixed an error in the text regarding the space bar in directory names.

P.P.S.: GreatEmerald also added some fresh information about file hierarchy. You can read it in the comments.

Thanks for your input/corrections.

Both comments and pings are currently closed.

12 Responses to “Command Line Tuesdays – Part Two”

  1. jon

    You said:
    “You type ‘cd /home/username/Hello_Kitty’.

    As you see, we didn’t use the space bar in the folder name. That’s because the terminal won’t recognize it. Whenever you want to navigate to a folder containing a space in it’s name, you replace it with the underscore character.”
    =======

    Actually, single or double quotes will work with spaces in Terminal.

    cd /home/username/’Hello Kitty’

    Anyway, thanks for these lessons. I didn’t know about pwd nor that cd would bring me back to the working directory.

    jon

  2. jdd

    don’t forget pwd have options, see the man page if you forget them.

    specially if you use soft links

    for example, I have in my home a link to /data, so:

    jdd@linux-zckr:~> cd
    jdd@linux-zckr:~> cd data
    jdd@linux-zckr:~/data> pwd
    /home/jdd/data
    jdd@linux-zckr:~/data> pwd -L
    /home/jdd/data
    jdd@linux-zckr:~/data> pwd -P
    /data

    :-)
    jdd

  3. bwl

    `You type ‘cd /home/username/Hello_Kitty’.

    This is incorrect, at least in standard bash. Underscore cannot be substituted for space. For space, you either need to contain it in quotes, or use the escape character \ (that’s a slash before the space).

    Also a great time to remind everyone about tab completion. You can press the [tab] key to complete a unique file/folder name automatically:

    > ls
    Hello
    Hello Kitty
    Kitty

    > cd K [press Tab]
    > cd Kitty/

    > cd H [press Tab]
    > cd Hello

    > cd Hello\ [press Tab]
    > cd Hello\ Kitty

    • Roman

      Agreed.
      Bash does not recognize the space in between words. Always use \ (backslash) before the space. Or get into the habit of using an underscore instead of a space when creating descriptive names that use two or three words.

  4. Nenad Latinović

    jdd – options are a whole different chapter alltogether, as i read it :)

    bwl – You’re completely right. I’ll have it edited ASAP. It seems that mr Shotts’ advice to not put spaces into *filenames* (it’s in the same chapter of the book) is what confused me. I don’t know how i managed to misread it. Sorry, and i hope you’re here next week to checkout if we missed anything :)

  5. Note that with the advent of systemd, the filesystem hierarchy has a few changes. For one, /bin and /sbin will no longer exist in the traditional sense, they will link to /usr/bin and /usr/sbin (though it’s not yet done in openSUSE). The difference between /bin and /sbin is that for the most part things in /sbin require root rights to use, and in openSUSE you must write the full path to them to use them (`lspci` won’t work, but `/sbin/lspci` will). /opt is also a secondary hierarchy, for applications that are self-contained (like KDE Frameworks 5, for it to be co-installable with KDE SC 4). /media is no longer the mount point for removable media, now that is moved to where individual users could make changes to the media.

  6. Nenad Latinović

    Thanks, GreatEmerald!

    • jetchisel

      The future shebang for shell scripts would be something like.

      /usr/lib/systemd/bin/bash

      just saying :-)

  7. cstd

    Meh, that font, “Balloon Extra Bold”, is just second to Comic Sans, but quite as deadly.

  8. BeDucky

    Another useful shortcut that can be used with the cd command is .. to move up one directory in the hierarchy. For example, if the user is currently in the /home/nenad directory, the command “cd ..” would move them to the directory /home. And it can be used multiple times in one command to move up more than one directory. So if the user had instead used “cd ../..” then working directory would have been /. Okay, sure, in that specific case it would have been quicker and easier to use the command “cd /”, but imagine if the starting working directory was somewhere deep in the wilds of the file system, and it becomes easier to imagine the usefulness of the option.

    Thinking of being many layers deep in the filesystem, “cd ..” can be helpful in moving sideways, if you will. Suppose the pwd is /who/knows/what/directory/is/this, and the user needs to move the directory named “that” that is also in the “is” directory. Rather than have to type the full directory path to get there, the two commands “cd ..” followed by “cd that” will get the job done, but it would be more efficient to combine them into “cd ../that”.

    I hope that’s useful.