Laughing at Linux: Command Line Basics (Part 1 of 5)

Oh yes.  yes.  yes yes.  It’s the Linux command line.

Today is a fun day.  So here’s the deal.  We’re going to surmount your fear of Linux.   Contrary to popular belief the Linux command line isn’t an insuperable hurdle that knocks down newbies.

Nope!  It’s fun and powerful and after reading this guide today you’ll feel a whole lot better using it.

It all starts with the Linux Shell.

Shellfish Shells

That black screen that you usually link to Linux is known as a command interpreter.  Think of it like a translator.

Let’s say you went to France for the first time but had no idea how to speak the language.  If you hired an interpreter to travel with you.  Then you could give the interpreter your commands in English and have her translate them into French.  And then she could translate the French to English too.

That’s what that black terminal screen in Linux does.

The Linux command line interface is the heart and soul of the operating system.  And it’s chief purpose in life is to let you talk to your computer.

It patiently listens for your commands and then executes your requests with alacrity.

Today we’re going to learn about 4 things:

  • Commands, Arguments and Options
  • Globs
  • Quotes
  • Variables

I know it sounds crazy now; globs?  But don’t worry after reading this guide you’ll understand everything.

Let’s do this!

Keeping up with the Commands

Ha, for some reason this title reminded me of “Keeping up with the Kardashians”.  Yes! I do watch that show but never by myself – only when my wife is watching

First I want you to visit the command line and type ls.

Listening to LS

Windows users have DIR to list the directory Linux users have LS (short for list) that does the same thing.

ls

The Linux LS command

First I want you to pay attention to the very first line.

vhudson@fbvxubfb:~/Documents$

What the heck does that mean?

Let’s break it down:

Breaking down the Linux command prompt

  • vhudson is my username.
  • @fbvxubfb is my hostname; the name of my computer.
  • ~/Documents is the current folder I’m in.  The tilde is a shortcut for my home folder.
  • $ is the command prompt.  It says: “what follows is a command”

Cool.

Alright, the second line shows two files:

  • MyInfo
  • Todo.txt

The first file doesn’t have an extension.  In other words, it’s not a text file.  This might confuse some Windows users at first but Linux has no problem with extensionless files.

So two files, MyInfo and Todo.txt are sitting in a folder called Documents in my computer.

In the graphic below you can see both files proudly displayed in the File Manager.

You can also see that the tilde is an alias for the home folder: “/home/vhudson”

Xubuntu FilesCool.

So this is where we are in the filesystem.

If we retype ls but add the command flag dash “L” we’ll see the long format of the file listening.

ls -l

ls -l

Before you freak out notice that ls -l still shows us the same information as the regular ls command.  In the far right of each line you’ll see MyInfo and Todo.txt.

But what’s all that junk in front of it?

I’ve give you quick rundown:

linux ls -l

All that rw dash stuff refer to different permissions.  The first vhudson is the username that owns the file.  The second vhudson is the name of the group that the file belongs to.  153 is the filesize in bytes and May 16:51 is the creation date and time.

You can also use our famous ls command to view other folders.  In Windows you would typically type CD first and then DIR. But in Linux you can peek into other folders without leaving your current folder.

For example, you can type pwd to print the working directory.

I’m in /home/vhudson.

Then you can type:

ls -l /

This tells the command interpreter to list all the files and folders in a folder called “/”.  This is just a way of specifying the root folder.  In other words, the forward slash all by it self indicates you want to view the parent folder that contains every other folder on the Linux file system.

ls -l part 2

Let the CAT out of the bag

The second command I want to share is called cat.  Typing this cause Linux to meow and pure like cute cuddly kitten hahaha.

Nah, I’m kidding.  Cat stands for concatenate and it’s the command to view inside files.

ls looks inside folders; cat looks inside files.

To look inside my Todo list file all I need to do is type

cat Todo.txt

Cat in Linux

If I typed:

cat Todo.txt MyInfo

Linux would output the contents of both files.

Echooh Echooh Echooh…

There is another useful command that I want to add to your arsenal.  It’s the humble echo command.

Type:

echo I love fixedByVonnie

and Linux will duitifully spit the phrase “I love fixedByVonnie” back out to you.  Echo prints to the screen the message after the command.  It’s sort of like the cat command but there’s an important difference.

echo treats each word as a distinct argument.  For example if you typed:

echo I                        love fixedByVonnie

it would still print I love fixedByVonnie.  It doesn’t care about all that whitespace.

To preserve the spaces you would quote the string like this:

echo "I                      love fixedByVonnie"

You might say:

Vonnie, who cares about spaces? This is boring.

And I would say, “Hey, hold up buddy.  You need to know this stuff now because when we talk about variables later you’ll see why it’s relevant.”

For example, if we say:

site=fixedByVonnie
echo $site

The command interpreter will print fixedByVonnie.com.  We simply stored the string of text in a variable named site and echoed the contents to the screen.  Also notice I had to include the dollar sign in front of the variable name.  Without the dollar, Linux would print the literal string: “site” to the screen.

Let’s make another variable named tagline:

tagline="I help people fix their Macs, PCs and Smartphones"
echo $tagline

Now we can do a few things here:

If we type:

echo $site $tagline

Linux will treat both variables as two distinct arguments and will print out the combination like so:

fixedByVonnie I help people fix their Macs, PCs and Smartphones

But if you do this:

echo "$site$tagline"

Linux treats it as a single argument and concatenates the result like this:

fixedByVonnieI help people fix their Macs, PCs and Smartphones

Notice there’s no space between Vonnie and I.

Linux Echo Command

Great.

Oh, one more thing, typing a semicolon after commands on a single line is the same thing as typing each command on its own line.

For example typing:

ls; ls;

Prints the directly listing twice: one after the other.

That’s all I’m going to show you today.  Tomorrow we’ll talk about globs. haha.

Oh one more thing, all your variables will go poof when you close the command interpreter (also known as the Shell).  So don’t think you’re going to delete something or blow up your Linux installation with these commands.  You can’t break anything here.

But that’s okay for now.

The Bottom Line

Linux! Oh yeah.

Today we talked about the ls, cat and echo commands.  We also briefly looked at a few arguments.  But that’s just the beginning.  You are in store for a treat because after following this tutorial series you’ll have a new confidence about Linux.  If you find yourself intimidated by Linux then you need to keep reading because I’m about to make you a champion.

Stay tuned.

About

Connect with Vonnie on Twitter

Posted in Linux Tagged with: ,