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

If you read the previous tutorial in this Linux series then you should have no problem opening the terminal and using ls, cat and echo.

ls lists files, cat lists inside files and echo prints whatever you type after it.

Today we’re going to talk about globs and then we’ll finish up with $PATH and history.

globs are pretty cool and it’s the single thing that separates the Linux tyros from the Linux pros.  If you master this little concept you’ll instantly boost your Linux acumen.

Globs of Filters

Check this out.

Let’s say I’ve got two files in my Documents folder.

  • MyInfo
  • Todo.txt

If I type ls I’ll see both files.  No surprises here.

What do you think is going to happen when I type this:

ls *.txt

Linux Globs

I know this isn’t particularly exciting because you can type dir *.txt in Windows and you get the same result.

But watch this.

I’m going to change things up a little bit.  Let’s create two new Todo lists and rename the first one to Todo1.txt.  Here’s what the file structure looks like right now:

Linux File Manager

Now we can use the “?” character to match any single character.

ls Todo?.txt

The Question Mark

The star matches any amount of characters but the question mark only matches any one character.

Using the * and ? filters is known as globbing.  So now you know how to glob haha.

One super special variable: $PATH

In the previous guide I showed you how to define and echo variables to the screen but today I need to show you one super special variable.  It’s called $PATH.

It’s a built into Linux and looks like a bunch of craziness but let’s look at it for a second.

$PATH is a special variable that lists that folders where the shell searches for commands.

You can see it with the echo command:

echo $PATH

Linux $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

Here’s what the command interpreter muses to himself when you enter a command called hahaha.

  • Alright, I see the user wants to run a command named hahaha.  First I’ll check /usr/local/sbin for the entered command.
  • Dang, I don’t see it there.  Okay, so I’ll check /usr/local/bin next.
  • It’s not there either so let me check: /usr/sbin
  • Ooops, where is this thing?  Let me check /usr/bin
  • Ahhh, I’ll check /sbin next
  • And then /bin
  • And then /usr/games
  • And then /usr/local/games
  • Since I don’t see hahaha in any of the folders listed in $PATH I’m giving up with an error.

My point is that every command has to live somewhere.  ls has a home.  cat has a home.  echo has a home.  Where is that home?  That depends on the command, but you can check that out by using the which command.

For example, if I type:

which ls

I’ll see the path where ls lives.

Linux which command

So when the command interpreter sees a crazy command like hahaha it searches all the folders in the path list.  If it doesn’t find the command there it says “No command found”.

The only place Linux looks for commands are the folders listed in $PATH.

Remembering what you typed

I’m going to close out with a really cool feature that most Linux beginners aren’t aware of.  It’s called history.

After typing a bunch of command you may want to go back and see what you typed 10 minutes ago.  One way to do that is to keep pressing the up arrow.

But a better option is to type

history

This will show you all the commands you typed for the given session.

Linux History

If you only want to see the last 10 commands you can type:

history 10

To clear your history type

history -c

The Bottom Line

I hope you’re starting to get the hang of Linux.  If you’re still confused about things it’s because you’ve been reading my tutorials but you haven’t actually typed this commands in a live Linux shell.

You can’t learn Linux by reading about it.  You absolutely must do Linux.  Linux doesn’t respect readers.

That’s why you should read my beginning guide on setting up Linux in Virtualbox so you can follow along.  I’ll only take a few hours to get your Linux box up and running.  And once we set it up you’ll never need to configure it again.

I strongly suggest you go through that guide so you can follow along and really get comfortable with this nimble operating system.

Cheers!

About

Connect with Vonnie on Twitter

Posted in Linux Tagged with: