Category Archives: Linux

ps and flags

Last post we introduced ps, this post we’ll talk about ps and flags. So, brief recap:

Alpha:~ computerlamp$ ps
  PID TTY          TIME CMD
17245 pts/2    00:00:00 bash
17248 pts/2    00:00:00 ps

Tada! ps with no flags.

Let’s start with a simple flag, x. In the first example, we saw every command that has a tty, or a direct connection to a terminal. That’s the default action of ps. What if we want to see every thing even if it doesn’t have a terminal?

Alpha:~ computerlamp$ ps x
12779 pts/2    R+     0:00 ps x
17245 pts/2    Ss     0:00 -bash

Well, that isn’t any different from the first time around, except now I see that I ran ps with a flag.

But wait, something is weird here. Since we started talking about flags, they’ve always started with a aka a dash or a hyphen. That time, I didn’t use one. ps is an interesting command, it has three kinds of flags you can use. From the man page:

      This version of ps accepts several kinds of options:
       1   UNIX options, which may be grouped and must be preceded by a dash.
       2   BSD options, which may be grouped and must not be used with a dash.
       3   GNU long options, which are preceded by two dashes.

This means that there are flags that begin with a dash, flag that don’t begin with a dash and flags that have two dashes. If you use the kind without a dash then you can’t use the kind with a dash. It also means that ps -a and ps a are two different commands. See?

Alpha:~ computerlamp$ ps -a
  PID TTY          TIME CMD
12825 pts/2    00:00:00 ps
Alpha:~ computerlamp$ ps a
  PID TTY      STAT   TIME COMMAND
12832 pts/2    R+     0:00 ps a
13451 pts/2    Ss     0:00 -bash

And isn’t that confusing? It feels like Dr. Strange wandered in and the world went weird.

Next post, we’ll talk about processes again.  Understanding processes is key to understanding ps.

Processes and Running Things

In one of my first posts I talked about this thing called a process. It happens when you run any program on a Linux® system, you create a process. If these things are running, how do I see them? Well, Linux&reg keeps them a table called a process table. We can see them with the command ps. You run it like:

Alpha:~ computerlamp$ ps

The output is kind of boring. It looks like:

  PID TTY          TIME CMD
17245 pts/2    00:00:00 bash
17248 pts/2    00:00:00 ps

It is just the processes that I’m currently running. bash is a shell and that is a program that gives me my command lines. We’ve been using it all along. There’s many kinds of shells, bash is one of the most common. It stands for Bourne Again Shell and was called that because it was a written as a replacement for a Bourne Shell. Wikipedia says it was first released in 1989. It’s been around a long time and it’s the standard command line interface on most Linux® systems.

The second command is the ps that we ran to get that command. If we had used any flags with ps, we would see those too.

The first column is the process id. Remember, Linux® understands numbers better than strings, so every process gets a number. It uses that id to refer to the process.

The second column is the tty. That stands for teletypewriter, a very very old device. According to Wikipedia it’s a typewriter that can be used to send messages to another typewriter. How’s that for very old school? Linux® took that history and made a tty mean terminal or output device. It’s the output device that that command is hooked up to.

ps is a command with many flags. Many MANY flags. We’ll talk about those in more posts.

Users and Groups

You’re using the computer, so you are a user. Computers understand numbers better than names, so you have a number associated to you called a user id.

If you want to see your user id, the command id with the flag -u will show you.

Alpha:~ computerlamp$ id -u

Let’s pretend that you have a friend named Nick who also uses your computer sometimes. The two of you are working on a project together on the computer so you want to share your files so that bot of you can read them or write to them. You and Nick want your project to be secret too, so that only the two of you can access the files. For fun, we’ll call this project insight. It’s a super secret project to design helicarriers to be used by S.H.I.E.L.D. and you don’t want Hydra to see them.

But I digress. Back to the original problem. How do you and Nick make it so only the two of you can access your project?

Linux® has groups to solve this problem. This means you create a group of you and Nick (let’s pretend his username is nickfury) and this will let you and Nick make it so only the two of you can access the project insight files.

You actually already belong to groups that are added by default when your account was added to the system. The command groups returns a list.

Alpha:~ computerlamp$ groups
computerlamp staff admin netusers

That’s just an example. It isn’t every group I’m a member of and your results are certainly different from mine.

To add the new group, let’s call it shield, you need to talk to the system administrator for the computer system you’re using. She’s the one that set the whole thing up to begin with and can add the group. Once you do that, if you run the groups command you get a new list.

Alpha:~ computerlamp$ groups
computerlamp staff admin netusers shield

Now that we have the new group, in the next post we’ll talk about how to share your files with Nick and only Nick.

Touch, A Silly Linux Command

In a break from common Linux command line tools, I’m going to show you a silly command that exists in Linux® called touch. It’s silly in that it looks ridiculous to begin with but someone created it for a reason.

touch means you’re going to touch a file or directory.   It is often used to create empty new files, like this:

Alpha:~ computerlamp$ touch Avengers

If you ls, you’ll see a file named Avengers in the current directory. Now try the command cat on Avengers.

Alpha:~ computerlamp$ cat Avengers

Hey, nothing there! You created a file with nothing in it. Not a single Avenger to be found in the Avengers file. Not even a Hawkeye or a Black Widow.  This means that if you need a new file, you cause this command to create it just like you can use echo.  Unlike touchecho actually puts words into the file.

There’s nothing stopping you from touching a file that already exists.

Alpha:~ computerlamp$ touch Avengers

Run ls again and nothing has changed. You can also try find and you won’t see anything different.  it doesn’t look like my command did anything.

So why did I touch it? I touched it for some reason, something must have changed, I just need to find out what.  It turns out there’s another flag to ls that will let me see the changes I made

Try this, run ls with the -l flag. It looks like:

Alpha:~ computerlamp$  ls -l
total 0
-rw-r--r--  1 computerlamp  staff  0 Nov 12 11:33 Avengers

See that date in there? That’s the date the of the last time the file was modified. Every time you touch a file, that gets updated. Try it a few times and see. touch was made so it’s easy to change that time. The 0 before the Nov in the date is the size of the file in bytes. The other things we’ll discuss in the next post.

Fix Those Pipes

The pipe command in this post didn’t work with every command, so now we’re going to fix those pipes. Remember that the pipe takes the standard out from one command and turns it into standard in for another and how that doesn’t always work with every command. One command it doesn’t work with is rm. Let’s start by making a file.

Alpha:~ computerlamp$ echo "Mario Rocks!" > Nintendo

Now we’re going to use find to remove it. I know that’s kind of silly, but hey, we need to be able to try these things out. Here’s the command to use find and rmove it:

Alpha:~ computerlamp$ find . -name 'Nintendo' | rm

What we get back is an error telling us how rm works:

Alpha:~ computerlamp$ find Nintendo | rm
usage: rm [-f | -i] [-dPRrvW] file ...
       unlink file

This looks like I can’t use find with rm and a pipe.

Wait, this is Linux, there’s another way around this, I can fix those pipes! It’s the command xargs. It takes the output from one command and turns it into the command line string for another. This is for those commands that don’t read input from standard in, like ls does.

It works like:

Alpha:~ computerlamp$ find . -name 'Nintendo' | xargs rm

See where I stuck that xargs? Try it and the error we saw before has gone away! I fixed those pipes!

But wait, isn’t this a different way of doing what the -exec flag to find did that we saw in this post? Well, yes it is. It’s also a more efficient way of doing things.

The -exec flag works on each individual file. When it finds something, it carefully removes it then goes back to looking. Using xargs lets us group the files together and remove them all at once. This is actually must faster than using the -exec flag with find.

Pipe Like Mario

So when Mario jumped into a pipe he’d come out in a different area. Well, technically it’s a sewer pipe, but close enough. He’d go out one area and into the other.

Linux® has pipes too. They take the results out from one command and put them in to another. It’s not a sewer pipe, so you don’t have to worry about figuring how to make a pipe on the command line. It’s the | character. It kind of looks like a pipe… ish. It isn’t hollow, but it’ll do.

So what can we do with it? Well, let’s start with something simple. Suppose when I run ls in a directory I get waaaaaay too many files to look at.  It’s just pages and pages of results.  Rather than trying to read all those lines scrolling by, I can pipe the results from ls into more

Alpha:~ computerlamp$ ls | more

I could also pipe the results through less:

Alpha:~ computerlamp$ ls | less

For silliness, I can pipe the command echo through less:

Alpha:~ computerlamp$ echo "Mario Rocks" | less

Now if you don’t give a command after the pipe, things just hang around.

Alpha:~ computerlamp$ echo "Mario Rocks" |

The command line is waiting for me to give another command to pipe the results of that echo in to. I can either type another command or hit control-C to exit that. That’s holding down the control key and then hitting C. It’s a common way to interrupt things on Linux®.

If you remember way back from post on Linux® and Files, we talked about standard file types. What technically is happening with a | is that the first command writes to standard out and that is written to the standard in of the second command through the pipe. This means that in order for it to work, the second command has to take input from standard in. Not every command does this. We’ll talk about how to fix that next time.

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.

More on more (and less)

In the Linux® system, there is a fle that contains a list of dictionary words. This file is normally in /usr/share/dict/words. Each line on that file contains a single word. If you want to know how many words are in the file, try:

Alpha:~ computerlamp$  wc -l /usr/share/dict/words

Wow, isn’t that a lot of words in a single file?  The one on my system has 235,886 words.  That’s a lot.

Now imagine using more (or less) on the file and looking for a single word. This could take forever. Especially if we’re looking for vulcan because we want to make sure we spelled it right. There has to be an easier way to find this… And of course there is. If we are using more (or less) to look through the file, then we can use the . Try

Alpha:~ computerlamp$ more /usr/share/dict/words

and hit the key. Then type the word you’re looking for and it will hopefully take you to it.  Try that with turtle.  Now pretend we tried to page through the file looking for turtle.  We’d still be looking.

Now suppose we’re looking for the word way. If we type that, the command takes is to the word airway. Which is almost, but not quite, what we need. I could keep looking through the file, but that would give me every word that had way in it.  Like alway, or away or backway or cartway.  There’s an awful lot of words with way in it.

We need to tell more that we’re looking for the word that begins with way. We can do this by typing ^way (see the ^) which tells more that the word should begin with way. Now if the word isn’t found in the file, then… either you spelled it wrong (vulcan begins with a v not a w!) or it doesn’t exist in the file.  For example, google isn’t in my file!

And that’s how you find words in your files.

Finding Commands

Suppose you made a directory that you thought you needed called penguin. As it turns out, you don’t need penguin at all, but due to a brief brain fart you’ve forgotten how to remove directories. Unfortunately, we haven’t yet progressed to the point where you can tell your computer ‘remove that directory’. We have to know what command to type.

Luckily for us, Linux® has a command that will help us find the command we need. It is called apropros. If we try:

Alpha:~ computerlamp$  apropros directory

That gives us a list of every man page on the system that has something to do with directory. Luckily for us, the command uses the more or less command for the output of apropros so we can look through the results and find the command that will remove the directory. Which, as we know from a previous post, is rmdir.

As Linux® always has another way to do things, there is another way to search through man pages. In this case, it is a flag for man, man -k. It will give the same results as apropros.

Alpha:~ computerlamp$  man -k directory

Sometimes apropos finds nothing. Try

Alpha:~ computerlamp$ apropos penguin