Monthly Archives: January 2017

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.

 

 

Index of ComputerLamp Posts

You will find 83 posts in the category Linux on this blog.


Jump to B, C, D, E, F, H, I, K, L, M, N, P, R, S, T, U, V, W

B (4)

C (4)

D (8)

E (3)

F (6)

H (3)

I (5)

K (3)

L (9)

M (9)

N (2)

P (7)

R (2)

S (8)

T (6)

U (1)

V (1)

W (2)

Alias … Not the Television Show

Alias was a television show where Sydney Bristow took on various aliases to do spy things and to kick butt while she was at it. But the aliases were important. She had names like Julia Thorne, Kate Jones, Sharleen…all kinds of names she hid behind.
bash has aliases too, but it’s not names to hide behind because you’re spying, it’s more names to hide that long command behind so you don’t have to remember it. Remember in the post about bash history where I said it should be named thingsIdid or whatIdid? Now we can make that happen!

Alpha:~ computerlamp$ alias whatIdid="history"

 

Now when you type whatIdid, you get the results of the history command.

There’s the more command and the  less command. I don’t like the more command but I love the less command. So what do I do to keep from typing more by accident? Well, I don’t stop myself from typing it, I just make it run the less command:

Alpha:~ computerlamp$ alias more="less"

The bash shell has history, so there’s got to be a way for me to see what commands I’ve aliased aside from using that history command (which I could, if I combined it with grep!). And there is… just type:

Alpha:~ computerlamp$ alias
alias whatIdid="history"
alias more="less"

Since I can add them and I can list them, there’s got to be a way to remove them… and there is! It’s the imaginatively named unalias command.

Alpha:~ computerlamp$ unalias more

That removes the alias for more from my list of aliases.

Alpha:~ computerlamp$ alias
alias whatIdid="history"

What happens if I type the wrong thing?

Alpha:~ computerlamp$ unalias jedi
-bash: unalias: jedi: not found

I guess the alias command doesn’t know where Luke Skywalker is hiding at either.  Which is good for him, right?  He can hide from Snoke and Kylo Ren a little longer.

 

History

Remember that teacher who never forgot a single thing in history? Who could not only tell you what General Custer was wearing at his last stand but the grade you got on your last test?

Well, bash, the shell you’re using in your command line has the same thing. It’s called history. How original! You use the command history to see your history. Okay, maybe not original. Maybe it should be called thingsIdid or whatIdid. Anyway, it looks like:

Alpha:~ computerlamp$ history
    1  ls
    2  pwd
    3  cd
    4  history

See?  Those are the last four commands I happened to type.  Bash is like that teacher, it doesn’t forget.

It’s also useful to combine history with a grep:

Alpha:~ computerlamp$ history | grep ls
    1  ls
    5  history | grep ls

So if you forget ‘exactly how did I do that find command?’ you can, well, find it!

Another way in bash to see your history is to hit the up arrow at the command line.  You’ll see previous commands that you executed and can hit enter and run them again.  If I hit the up arrow ↑ I get:

Alpha:~ computerlamp$ history | grep ls

Which, guess what? It’s the command I just typed.

Your history can contain 500 (or more) commands… and that’s a lot to see. To see the last 10 commands you typed, try this:

Alpha:~ computerlamp$ history 10
    4  ls
    5  echo "Stop Hydra" >plans
    6  ls
    7  history
    8  fortune
    9  yes
   10  find . -type f
   11  ps auxw
   12  ps
   13  history 10

See those numbers before each command? They’re useful too. You can either hit that up-arrow until you find the command you’re looking for (boy, that’s boring) or you can do:

Alpha:~ computerlamp$ !8
fortune
You learn to write as if to someone else because NEXT YEAR YOU WILL BE
"SOMEONE ELSE."

Now you don’t have to remember every command flag you’ve ever used, bash does it for you.

 

Finding Processes

Now that we know that processes run, how can we go about finding processes?  There must be some way to find a particular process… like if I want to find every process that has the letter a in the name. Or if I want to find every process that has a name like ‘insight’ because I’m sure Hydra is running something on my system and I want to know what it is.  Or if I want to know every process that’s being run by a user with the name hydra.

You get the idea.  I want to find processes!

To do this, we’re going to combine two commands.  The first is ps, that useful command that lists the processes.  The second is that useful command grep that we used to find things in files.

ps auxw shows you every single process that’s currently running on a system.  On a system that’s very very busy, this can be a lot.  There’s 65536 processes allowed at any time, so combing through those looking for hydra (or Hydra or hYdRa) would take too long.  I’m lazy!  I want the system to do it for me!

From this post we know that grep has an option where it ignores case.  This means it can find hydra, Hydra, HYdra, HyDrA, or… I’m stopping that now.  It means you can find any combination of the 5 letters hydra no matter what capitals you use.

Now that we have those two commands, we’re going to combine them.  Remember this post on pipes?  We can take the output from one command, ps and pipe it through to the other command, grep.

Alpha:~ computerlamp$ ps auwx | grep -i hydra

Nothing was found from that command.  Hurray!  No Hydra user on the system!

…unless they’re hiding themselves.  Alexander Pierce didn’t have a username of Hydra.  He was hiding and probably had a user name of apierce or alexander or alexander.pierce or even supersecretary.  But not something we could actually look for in ps.

Remember ps a from this post? It showed what processes you were running in a single terminal.  If you want to see every single process you were running, no matter what terminal, then this combination of ps and grep  would work for you!

Alpha:~ computerlamp$ ps auwx | grep -i computerlamp

Finding processes is easy with that combination of ps and grep!

Silly Commands

In honor of New Year’s Day, today we’re going to talk about more silly commands.

The first one is called yes

Alpha:~ computerlamp$ yes
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y
y

 

And it keeps going and going and going! It’s the Energizer Bunny of commands!

To stop it, hit control-C.  That’ll stop the Bunny, er, Y from streaming on your terminal.

So what’s this command for? Other than to fill your screen up with more y’s than should be legal. Well, remember from this post the xargs command? Some commands insist you sit there and type y over and over and over before they’ll finish. This command lets you do:

Alpha:~ computerlamp$ yes | xargs annoying_command

So you can be lazy and not type it over and over and… did I mention over? Then when that command finishes, your yes command will exit.

Linux® has lots of silly commands.  Another one has no redeeming feature at all. Aside from pure amusement. It’s called fortune.

Alpha:~ computerlamp$ fortune
Long life is in store for you.
Alpha:~ computerlamp$ fortune
Q:    What do you say to a New Yorker with a job?
A:    Big Mac, fries and a Coke, please!
Alpha:~ computerlamp$ fortune
So she went into the garden to cut a cabbage leaf to make an apple pie;
and at the same time a great she-bear, coming up the street pops its head
into the shop. "What! no soap?" So he died, and she very imprudently
married the barber; and there were present the Picninnies, and the Grand
Panjandrum himself, with the little round button at top, and they all
fell to playing the game of catch as catch can, till the gunpowder ran
out at the heels of their boots.
        -- Samuel Foote
Alpha:~ computerlamp$ fortune
You may worry about your hair-do today, but tomorrow much peanut butter will
be sold.

It even has a man page. You can type man fortune and see the flags it has too.

For example, if you only want short fortunes, then you could do:

Alpha:~ computerlamp$  fortune -s
How apt the poor are to be proud.
		-- William Shakespeare, "Twelfth-Night"

If you’re only interested in long fortunes, then you would do:

Alpha:~ computerlamp$  fortune -l
At once it struck me what quality went to form a man of achievement,
especially in literature, and which Shakespeare possessed so enormously
-- I mean negative capability, that is, when a man is capable of being
in uncertainties, mysteries, doubts, without any irritable reaching
after fact and reason.
		-- John Keats

The fortune command pulls it’s sayings from many different files. If you want to know what file the fortune came from, you would do:

Alpha:~ computerlamp$  fortune -c
(riddles)
%
Q:	How do you play religious roulette?
A:	You stand around in a circle and blaspheme and see who gets
	struck by lightning first.

As you can see, not all of the fortunes are polite.

So there you have it. Two silly commands found in Linux®.