Category Archives: Files

Linux® Scratch Paper

Sometimes in algebra when I’d be working out a problem, I’d find myself grabbing a small piece of paper and working out a side problem.   Like ‘gee, what IS 9×19’?  I need that to solve the problem, but my teacher hated it when I made what she called messy marks on my test.  So, I had to use scratch paper.

I even find myself using scratch paper today, like when I’m on my phone and someone says, ‘write down this number!’.  Normally I’d save that in my phone, but my phone doesn’t actually let me both listen to the number and save it in the phone.

Linux® needs scratch paper sometimes too!  Sometimes a process needs to write to a file to say, ‘I did this’ or ‘I did that’.  It doesn’t really do that.  It writes state to a file so that if it has to stop it can pick up where it left off.   state means that ‘Hey, this is what I was going now, let me know if I need it later.’

This can get complicated.  Every process would need its own directory to write its own files, so imagine keeping track of all of them.

Linux® likes to keep things simple and straightforward.  Unix is the same way, and Linux® picked up this trick from them.  Instead of each process having its own directory, we have one central directory that everyone has write permission to.  It’s called the /tmp/ directory.  tmp stands for temporary, so it’s meant for files that aren’t supposed to stick around.  In fact, every time you reboot your computer, the /tmp/ directory is emptied.

/tmp/ can have lots of files in it or it can be empty.  In fact, when I look at /tmp/ on Alpha, I see:

Alpha:~ computerlamp$ ls /tmp/
Alpha:~ computerlamp$

That means in my case, the directory is empty.  What does it look like on your system?

The bashrc file

In this post we made aliases… now we want to save them in a file; bash has such a file called the .bashrc file.  It’s also known as the bash configuration file.

That period before the name is very important, without it it isn’t the right file.  It also means that the filename is hidden from view when you do a ls.  You have to use the ls -a flag in order to see it.

But this post isn’t about ls, it’s about the .bashrc file.  We’re going to use nano, that useful editor we talked about in the last post, to edit the file.

I’ll start with opening the file in nano:

Editing .bashrc with nano

Editing .bashrc with nano

I did this with the command:

Alpha:~ computerlamp$ nano .bashrc

Now I’ll type those two alias commands that I used before.  I aliased more to less and history to whatIdid:

Adding Aliases to the .bashrc file

Adding Aliases to the .bashrc file

And now we save the file:

Saving .bashrc with nano

Saving .bashrc with nano

Well, so now what?  I edited the file, I added those two lines, and saved it… just don’t forget to exit nano when you’re done with control-X!

Once we created the file, we need to use it.  Otherwise, we just saved our aliases in a file to do nothing.

It’s called sourcing  the file.  The command to do it is:

Alpha:~ computerlamp$ . .bashrc

See that dot before the .bashrc?  Be sure to type it.  That’s how bash knows to use its configuration file.  Also, every time you open up a new terminal, you’ll have those aliases.  And you’ll be able to see them with the alias command.

You can also look at what you’ve got aliased by using:

Alpha:~ computerlamp$ cat .bashrc

The output is exactly the same, no matter which one you use.  It looks like:

alias less='more'
alias whatIdid='history'

The .bashrc has other uses aside from aliases, we’ll talk about more in the future.

Nano! Creating, Changing and Saving Files

In this post we created files using the echo command, now we want something more complex.  The answer is to use an editor.  Now this isn’t an editor like the guy who works for a newspaper, this is a program that lets us create, save, change, and otherwise fiddle with a file.  We’re going to use one called nano.

In the Linux© community, a good way to start an argument is to ask a group of people what the best editor is.  It’s kind of like asking who the best superhero is (I’m told Deadpool. I personally like Iron Man… or even Hawkeye… Black Widow is cool… see what I mean?) or which is better, Marvel or DC?  So we’re not going to talk about best editor, we’re going to start with an easy one.

Alpha:~ computerlamp$ nano

This is what it looks like:

The nano editor

The nano editor

I can type whatever I want into this screen, like this:

Editing with nano

Editing with nano

I can save it with the key combination control-O:

Saving with nano

Saving with nano

I just have to tell it what filename I want.  I’m going to save it as nano.txt.

Then, when I’m done, I’ll exit nano:

Exiting nano

Exiting nano

And now I can create, edit and save a file.  What if I want to edit the file again?

Alpha:~ computerlamp$ nano nano.txt

That brings up that file I just created.

nano has a man page, and it even explains why it’s called nano:

NANO(1)                                                                                NANO(1)

NAME
       nano - Nano's ANOther editor, an enhanced free Pico clone

SYNOPSIS
       nano [OPTIONS] [[+LINE,COLUMN] FILE]...

There’s also help available within nano, if you use the key combination control-G you’ll get a help screen that looks like:

Help within nano

Help within nano

nano is one of the easiest editors to use, which is awesome because we no longer have to rely on echo!  It also creates what are called text files, which means that what  you type in is what you get.

 

 

Finding More Things

find has lots and lots and LOTS of flags. Lots. So we’re going to see what other cool things we can do with it.

So last post, we talked about finding Sewers. Suppose I forgot if it’s Sewers or sewers or SEWERS or even sEwErs. I know the name, I just forgot what case I used. Luckily, there’s a flag for that.

Alpha:~ computerlamp$ find . -iname sewers

See how I used iname as my flag rather than name? That means ignore the case of what I’m looking for. If the name is Sewers or SeWeRs or sewerS… or any other combination, this command will find it. I can also look for anything with an ‘S’ in it either upper case or lower case by doing:

Alpha:~ computerlamp$ find . -iname '*s*'

Suppose I want to know all the files in my home directory, no matter where I’ve put them. I want to find them, in other words. Well, guess what… find has a flag for that.

Alpha:~ computerlamp$ find . -type f

The flag -type has a flag. A flag with a flag. Or a flag with a smaller flag. The first flag means ‘find everything with a certain type’. The second flag means ‘and that type is a file’. If you said:

Alpha:~ computerlamp$ find . -type d

Guess what? You’re looking for all directories. And there’s even a subflag for symlinks. That’s:

Alpha:~ computerlamp$ find . -type l

You can combine these flags. I want all directories that have an s in their name:

Alpha:~ computerlamp$ find . -type d -name '*s*'

Or I can look for all symlinks that have the name word in them.  find has approximately a gazillion flags, just check out the man page for it if you don’t believe me.   For example, there’s even a flag for finding any files that are empty.  empty is that flag.  In other words:

Alpha:~ computerlamp$ find . -empty

In summary:  find has a flag for that.

Symlinks … or how to be lazy.

We’ve been using the file /usr/share/dict/words in the past posts to look up words. We can use this to figure out if we’ve spelled things correctly, for example, looking for freind only to discover that it isn’t in the file. Oops, it’s spelled friend.

Every time we look up a word, we have to type /usr/share/dict/words. Over and over and… What we want is a shortcut to that file, so we don’t have to type the whole thing repeatedly. The shortcuts in Linux® are called symlinks, also known as symbolic links. The command to create them is ln and we create the symlnks as:

Alpha:~ computerlamp$ ln -s target destination

Where target is the file (or directory) we want to create a shortcut to and destination is our shortcut. So for example:

Alpha:~ computerlamp$ ln -s /usr/share/dict/words words

Will create a shortcut to /usr/share/dict/words as words in the current directory. You can more (or less) words, grep words, and generally treat it like any other file. If we want to remove it when we’re done or no longer need it, we can just use the command rm.

One other thing to note about it: If you use the command ls -F, you’ll see words@. The @at the end of the filename is your only sign that it’s a symlink. Otherwise, if you do just a plan ls, you’ll see words without any sign that it is a symlink. It’s just another file.

Back to creating the link. There’s no rule that says I had to link the file named words to a new file named words. I could have called it something else. Like:

Alpha:~ computerlamp$ ln -s /usr/share/dict/words heapbigwordlist

Or even:

Alpha:~ computerlamp$ ln -s /usr/share/dict/words thematrix

It’s nice how Linux® lets us be lazy.  I don’t want to type the entire path to the dictionary file out, I’ll just make a symlink.  And the symlink is a file, so I can treat it like one.

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