Monthly Archives: August 2016

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

More… and Less…. and MORE and less…

 

So before we start this post, we need to download a file to our system. Go to this link blog1.txt in your web browser on your Linux® system and save the web page in your home directory. If you’re using firefox or Chrome, look under the File Menu for Save. Make sure you save the file as text and it will be named blog1.txt. So now we have a file on our system that we created from the web. We could look at the link to find out what’s in the file, but we want to use the command line. We could use cat but the file is a little big. If you try it, the command looks like:

Alpha:~ computerlamp$ cat blog1.txt

So the contents of the file just go vroom past and all you see is the end of the file. So there has to be a better way… …and there is! The command is called more. more allows us to look through a file, one screen at a time. So rather than the vroom of cat we can look at a file much slower. The command looks like:

Alpha:~ computerlamp$ more blog.txt

The output looks like:

Introduction

This blog is going to cover the ins and outs of using Linux® from the
command line.

So before we talk about Linux®, we have to first ask...

...what is Linux®?

Linux® is an operating system.  An operating system is the thing that
makes a computer workable.  Without it, the computer would be unable to
do anything.  No internet access, no games, no nothing.  The operating
system is the framework that allows the computer to actually do things.
Without one, you've really got an expensive machine that can turn on the
fan and make beeps, but do nothing else.

Unix is an operating system that was created back in the mid 1960s to
run on a new type of computer.  It has a long and convoluted
history, but it boils down to 'new computer system fast and spiffy'.
It was so spiffy various people made it so that it could run on other
types of machines.
blog1.txt

Which looks like the first part of the blog post. If you want to see the next part, hit the space bar. Or, if you think “gee, I’ve read this before, I don’t want to read it again”, hit the q key. So let’s try something different. The program /usr/bin/more is the program that’s executed when you type more. Let’s more more. What we get is:

"/usr/bin/more" may be a binary file.  See it anyway?

So more is smart enough to have us read only text files and not binary files. Another command that works instead of more is less. You can more a file or you can less a file.

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