Category Archives: Directories

The /etc/ directory

In the last post we looked at /etc/passwd.  That file is in the /etc/ directory, which contains all sorts of configuration files.  This time, we’ll talk about that directory and some of the interesting files in it.

Like I said, it’s a configuration directory.  For example, your computer has an IP address.  If that’s a static IP address, then it’s going to be stored in a file in the /etc/ directory.

Every time you start your computer, it starts a lot of programs by default.  For example, it starts init, the mail program, the program that runs DNS, the printing system, cron, and more.  All of this is controlled by the files in /etc/.

There’s also some interesting files there.  For example, /etc/hostname.

Remember cron?  There’s a system file that runs all of the cron jobs for the system.  It’s in /etc/ too, in /etc/crontab.

There’s also our friend NTP, it has a configuration file there too.

The /etc/ directory was designed so that there wouldn’t be configuration files all over the system, instead, they’d be kept in one place.  This makes it much easier to manage the system, you don’t have to go searching in multiple directories to find the files.

Alpha:~ computerlamp$ cat /etc/hostname
Alpha

Yup, there’s the hostname of my computer.  Alpha.

Another file is /etc/motd, it contains the message of the day.   When you first open a terminal, you see the contents of this file. Sometimes it’s empty, sometimes it will have important messages, and sometimes it just has silly things:

Alpha:~ computerlamp$ cat /etc/motd
 _______
< Alpha >
 -------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Yes, that is cowsay in my /etc/motd file.  I was feeling whimsical.

The point of the /etc/ directory is to have the configuration files in one place, not scattered all over the computer.  It makes the system a lot easier to manage.

Plus, cowsay is fun!  All motd files should use cowsay!

Flags

These aren’t the flags you wave from a flag pole.  Ever heard of semaphores?  Those are the flags that sailors used to use to send signals between ships in the days before radio and cell phones and all those other ways we have to communicate now.   So these are flags that can be used to send a signal to a command that you want it to operate differently.

In our previous posts, we’ve used several commands like file, cat, more, echo, and ls. Let’s look at ls some more. If we execute:

Alpha:~ computerlamp$ ls /usr

then we see the directory contents of the usr directory. Now suppose we want to know which of these is a directory and which isn’t. We could cd into that directory and then try each one at a time to find out which one is a directory, but that’s the long way around. As luck would have it, there’s something that will allow us to see what is a directory and what isn’t. We can run:

Alpha:~ computerlamp$ ls -F /usr

Each directory in that listing ends with a ‘/’. Try that command on other directories and see what happens. For example, we made a directory called Sith in a previous post. What happens if you do that on the Sith? The -F is called a command flag or just a flag.  It’s a semaphore that doesn’t involve picking up and waving flags to that command.  It lets us change the output of ls so we can see different things.

Try that command on other directories and see what happens. For example, we made a directory called Sith in a previous post. What happens if you do that on the Sith?

With ls we can change what we see in the output of the command. It’s part of what makes the command line so powerful. With the mouse (remember Fred the mouse?) you can’t add flags to commands and change how it operates.  You can just click on icons.  I mean, I suppose you could add actual flags to the icons, but that won’t change how it operates.

And most commands have flags, so you can customize them to do exactly what you want.

Copying Things

 

In the last post we talked about moving files using mv. What if instead of moving a file from place to place, you’d like to copy the file. Maybe you want to share it with someone, or maybe you want to make a copy of a file so you can change the second file a bit but still keep around the first. So first we’ll make another file. Let’s do this:

Alpha:~ computerlamp$ echo "Storm Trooper" >Clones

So now we can copy this file to a new file, let’s call it StarWars The command we’re going to use is cp which is short for, of all things, copy.

Alpha:~ computerlamp$ cp Clones StarWars

Now if we do a ls, we’ll see:

Alpha:~ computerlamp$ ls
Clones StarWars

If we cat the file StarWars, we’ll see:

Alpha:~ computerlamp$ cat StarWars
Storm Trooper

We can copy files into different directories. If we have a directory named Sith, we can do:

Alpha:~ computerlamp$ cp Clones Sith/

The / means that Sith is a directory, so the cp command expects to copy that file into a directory. We can also copy the file Clones from the Sith directory back into the current directory by:

Alpha:~ computerlamp$ cp Sith/Clones .

Or you can cd into the Sith directory and copy it like:

Alpha:Sith computerlamp$ cp Clones ..

Back to Files!

Now that we know a bit about directories, let’s go back to looking at files. Remember, Linux® treats everything as files, so being able to manipulate them is a useful skill. Let’s begin by creating a file in our home directory:

Alpha:~ computerlamp$ echo "Teenage Mutant Ninja Turtles" >Turrtles

Well, oops. I misspelled Turtles. I should fix that. The command mv will let me do that.

Alpha:~ computerlamp$ mv Turrtles Turtles

The mv command moves files. It can move from one place to another or, in our case from one name to another. We can make a directory and move the file Turtles into it.

Alpha:~ computerlamp$ mkdir Sewer
Alpha:~ computerlamp$ mv Turtles Sewer

There, now the Turtles file resides in the Sewer just like they’re supposed to. We can either

Alpha:~ computerlamp$ cd Sewer
Alpha:Sewer computerlamp$ ls

To see our Turtles or just:

Alpha:~ computerlamp$ ls Sewer
Turtles

Now if we want our Turtles back out of the sewer, there’s two ways to do that. If we’re in the home directory, the command is:

Alpha:~ computerlamp$ mv Sewer/Turtles .

That’s a period after the file name Sewer/Turtles. That actually means a special directory, the one we’re currently in. We could also have said:

Alpha:~ computerlamp$ mv Sewer/Turtles ~

Which means move it to the home directory. On the other hand, if we cd into the Sewer directory, the command is:

Alpha:Sewer computerlamp$ mv Turtles ..

That’s two dots. That means the parent directory of the one we’re currently in. To summarize: mv Move files . Current directory .. Parent directory

Making Directories

We’ve talked about moving around in directories and what they’re called, but we haven’t talked about ma king directories yet. So let’s do that. To make a directory, the command is mkdir. The command looks like:

Alpha:~ computerlamp$ mkdir deathstar

Now that we made it, if we do a ls we’ll see it

Alpha:~ computerlamp$ ls
deathstar

You can look in it by doing

Alpha:~ computerlamp$ ls deathstar

And since it’s empty, then that command will show nothing. We can remove the directory with the command rmdir. The command looks like:

Alpha:~ computerlamp$ rmdir deathstar

What happens if I try to remove a directory that doesn’t exist?

Alpha:~ computerlamp$ rmdir notreal
rmdir: notreal: No such file or directory

The command rmdir only works if the directory is empty. If it isn’t, this happens:

Alpha:~ computerlamp$ rmdir deathstar
rmdir: deathstar/: Directory not empty

So now we know how to create directories. We can also change directory into the new directory with:

Alpha:~ computerlamp$ cd deathstar

And cd back to where we started with:

Alpha:~ computerlamp$ cd ..

Another trick to return to the directory we started with is:

Alpha:~ computerlamp$ cd -

You can think of this as ‘change directory back to where I started with, no matter how weird my new directory is.’

More About Directories!

So in the last post we talked about moving up and down in directories, and that there’s such a thing as levels in the directories. Today we’ll talk about some more commands useful for directories and y et more terms used.

The first thing to talk about is, well, where are you at any time? What’s your current directory? This is actually called the current working directory and you can find out what your current wo rking directory is by doing the command pwd.

Alpha:~ computerlamp$ pwd

/home/computerlamp

When you first start your terminal session, you’re in your home directory. This has a special symbol of ~. That means when you do:

Alpha:~ computerlamp$ cd ~

You’ll go to your home directory. To make things easier, you don’t even have to use the ~ to go to your home directory. The command:

Alpha:~ computerlamp$ cd

Will take you there. So let’s change gears slightly and talk about that prompt:

Alpha:~ computerlamp$

The first word Alpha is the name of the computer. Mine is named Alpha for Alpha 5 from the Power Rangers.  The ~ in the prompt refers to the directory you’re in. In this case, we’re in the home directory.  After the ~ is a space and then your username. My username is computerlamp.

Now let’s talk about the string we saw before when we typed pwd. We saw: /home/computerlamp The / is the directory separator. The first one means the root directory, or the very top directory that’s possible on the system. The home is the subdirectory of the root and it’s the directory where all home directories are kept. And the computerlamp is my home directory. So my home directory has two names, ~ and /home/computerlamp 

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 ..