Monthly Archives: July 2016

Directories!

A phone directory is a collection of people and their phone numbers. A directory in Linux®
is a collection of files and information about the files. Directories can contain other directo
ries which can contain other directories… and on and on.

So, files are contained in directories and directories are contained in directories. We usually
draw this out for directories like:

topdir
   `-- middir
        `-- bottomdir

If we add a file in bottomdir it’ll look like:

topdir
   `-- middir
        `-- bottomdir
            `-- turtles

(For the record, the command tree was used to create these pictures. If it isn’t installed on your system, ask the person who owns it to install it.)

First question: How do we use these things?

Well, the first command is cd. Short for ‘change directory’. You can cd into different direc tories, like:

Alpha:~ computerlamp$ cd topdir

Which will move you down into the directory topdir. Now, for the contents of this directory, try ls:

Alpha:topdir computerlamp$ ls

The output is now:

middir

What if we want to go back to where we were? Well, that means moving up in the directory structure. The directory above us is called the parent, and to move up we do:

Alpha:topdir computerlamp$ cd ..

Writing, Reading, and Listing Files

So in the last post we talked about the command echo. Now we’re going to use it to create a file. At your terminal prompt, do:

Alpha:~ computerlamp$ echo "Hi there" >myfile

This took the output from the command echo “Hi there” and put it in a file called myfile. To see the contents of myfile, do:

Alpha:~ computerlamp$ cat myfile

On your terminal screen will appear:

Alpha:~ computerlamp$ cat myfile
Hi there

So the command cat can show you the contents of a file. But how do we know what files we have? That’s where the command ls comes in. If we execute ls, we see:

Alpha:~ computerlamp$ ls
myfile

The ls command lists the files you have. In the next blog post we’ll talk about directories, which are the containers of the files.

Linux® and Files

 

Everything in Linux® is treated as if it were a file. Directories, hardware, memory, network, you name it, Linux at the core, treats it like a file. Even software that runs is treated like a file. Why a file? Well, files can be written to, can be read from, can be appended to, can be removed, can be created, can be modified… in other words, you can do a lot of things to a file. You can also enforce some standardization and rules about the files. So, files. Files files files.

That being said, there are three important files that Linux® has. They’re called:

  • standard in (stdin)
  • standard out (stdout)
  • standard error (stderr)

These are all created for you by Linux®, you don’t have to do a thing to make them work. You just have to use them.

  •  Standard in
    This is the file that is set up for all input into processes by default. In other words, it’s read directly from the keyboard. If you start a process and it wants you to type, it’s reading from the standard in file.
  • Standard out
    This is the default file for output from a process. If you start a process from the command line, it’s going to print output into the terminal that you started it from. Now, not everything writes output of any sort, but by default, they put it to standard out.
  • Standard error
    Not everything goes as planned… which means not every process runs as planned. Whether by user error or computer error… errors happen. The errors are sent to a different stream than standard out. But to make things confusing, on a basic terminal that means they’re printed to the same place standard out goes to.

So you want to run some commands…

Well, we’ve talked about what the command line is…

…so what can you do with it?
Well, given the name, you can do ‘commands’.

There’s lots of built in commands, but we’ll start with a simple one. Open a terminal session (I’ll leave it up to you to find the one particular to your version of Linux®. It generally has the word ‘term’ in the name.)

The terminal has a ‘prompt’, which is a place to type your commands. They usually combine the name of the computer with your username. So it could look like:

  
 Alpha:~ computerlamp$_

So for our first command, we’re going to try echo. This is a fun command, it repeats any words or sentences you give it to the terminal.

Alpha:~ computerlamp$ echo Hi There
Hi There

Alpha:~ computerlamp$ echo How now brown cow.
How now brown cow.

Alpha:~ computerlamp$ echo Star Wars Rocks!
Star Wars Rocks!

Another fun command is rev. If I type it at the terminal, it looks like:

Alpha:~ computerlamp$ rev

Type any string you want and hit enter. You’ll see the reverse of that string show up. For example:

Alpha:~ computerlamp$ rev
abcdef
fedcba

So now it seems you’re stuck in a loop. You need to get out the rev and back to the prompt. You can do this one of two ways, you can hold down control and hit C or D.