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

Oh Linux.  You’re so fine.

In this guide, I’m going to show you how to get around Linux.  You’re going to learn how to leap through folder,  list file contents and get going.  After following this tutorial you’ll have everything you need to move around the operating system.  And the really cool part is the next time your co-worker tries to grandstand his knowledge of Linux you can say, “Well, actually Bob… the easiest way to get to the home directory is to just type CD”

Whoa!

Let’s do this.

Getting around Linux.

CD just like Windows

So here’s the scoop on the CD command: it works exactly like Windows.

cd /etc

Changes to a directory named etc which lives inside the / directory (root).

Nothing exciting here but here’s where things get interesting.

Your Linux box has a home folder just like you had in Windows.  But instead of C:\Users\Vonnie\Documents I can go to /home/vonnie/Documents

The forward slash represents the top-level parent directory.  It’s the grandpappy of all your directories.  Every directory sprouts out of the root directory.  Next there a directory called home.  Linux graciously gives every user account on the system his or her very own home.

How sweet.

And then inside home you can find the accounts that reside on your computer.  Mine is called vonnie. Inside that you’ll see all your folders

  • Desktop
  • Documents
  • Downloads
  • Music
  • Pictures
  • Public
  • Templates
  • Videos

Look familiar?  Yup.  You can think of these like your Windows 7/8/8.1 Libraries.

Here are a few facts you need to know about CD.

  1. cd . is code for “Change to the current directory”.  The dot (.) represents the directory you’re in right now.  You might say:

    Why the heck would I ever change to the directory I’m in right now?

    Typically, you wouldn’t CD to (.) but rwould rather use it to run programs.  For example, if you had a script called install_firefox.sh in /home/Vonnie/Downloads you could cd to Downloads and then type ./install_firefox.sh (Notice the dot prefix)  This tells the Linux shell that you want to run the file in the current directory.  I think this is a security feature to prevent malware from executing in arbitrary directories.

  2. cd .. shoots you up one folder just like it does in Windows
  3. ~ (tilde, typically in the upper left corner of your keyboard) is an alias for your home directory.  So If I typed cd ~/Documents it would take me to /home/vonnie/Documents

And that’s it!  That’s all you need to know about cd.

Incidentally, if you ever get lost and need to figure out what directory you’re in just type:

pwd

This stands for “print working directory” and get’s the job done quite nicely.

Finally, I want to show you a little more about ls.

ls is the best

The sheer beauty of ls stems from its simplicity.

typing ls by itself lists the files and folders but doesn’t display hidden files (usually configuration files that have a . (dot) prefix).

If you type:

ls -a

You’ll see all your files; including the hidden files and the . (dot) and .. (dot dot) files we talked about earlier.  You probably didn’t realize this but in Linux everything, I mean everything, is a file.  So even when you type cd . (dot) or cd .. (dot dot) you are actually running a file called dot or dot dot.

Amazing.

So what else can we do with ls?

Let’s say you want to get as much data as possible from the files.  You want to see the timestamps, file sizes, and permissions for all files in a folder.  You can use the long argument (dash L) to list the long format.

ls -l

You can even combine arguments like this to create a super view that shows all hidden files and displays everything in the long format:

ls -la

Here’s a power user tip: The -R lets you recursively list folders.  In other words, it not only lists all the files but also all files inside all folders too.

ls -R

You can also do things like:

ls *.log

to list all files that end in .log or

ls bieber*

to list all files that start with Bieber.

I won’t ask why you’re searching for that.

The Bottom Line

ls and cd are two good friends that will serve you well.  In fact, these are two of my favorites.  These commands aren’t fancy or anything – they just get the job done and that’s why you should use them.

Do you have any favorite Linux commands?  Share the love in the comments below!

About

Connect with Vonnie on Twitter

Posted in Linux Tagged with: