Tutorail: Linux useful commands

The command 'touch'

Now we're going to talk about a touchy subject. The command 'touch' which is used to change the time and/or date of a file.
You can use 'touch' if your boss yells at you about not having a report ready at lunchtime. You should quickly finish the report, then type:


touch -t 05070915 my_report.txt

and it makes it look like you did it at 9:15. The first four digits stand for May 7 (0507) and the last four (0915) the time, 9:15 in the morning. Make sure your digits match your story. You don't want to have it look like you did it in February. Of course, if you punched in at 9:40, then you're in trouble.

'touch' can be used also to create an empty file. You would just enter.
touch [a file name]
There may be times in the future when you need an empty file that will be filled up later automatically by the workings of some program. We'll deal with the uses of 'touch' in our later courses.

Finding things with the command 'find'
There's so much on a computer's hard drive, nobody could ever know from memory where everything is. Perhaps the smart lad who won the spelling bee by spelling 'prestidigitator' might be able to, but most of us are going to have to find things now and then.
If you use a windows manager like KDE, you can use the find tool. It's very useful because it has a lot of options and you can use them to modify your searches.

How to use the 'find' command
But if you're getting used to using command line stuff, just type in:


find -name *hawaii*

and find out where you put you pictures of you Hawaiian vacation. If you're in your /home directory, it will go through every directory and find every file that has the name 'hawaii' in it. The two asterisks make sure it does that. If they started with 'hawaii' you wouldn't need the first asterisk but you can leave it there if you want.
You may have created some file recently. For example, you may want to find some file that you were working on, let's say from now up to 10 minutes ago, you could type.


find -mmin +0 -mmin -10

This will list the files that you created or modified within the last ten minutes. If you choose to use a higher number for -mmin -? you should probably use a pipe, for example:


find -mmin +0 -mmin -120 | less

will find things that you created or modified up to 2 hours ago and the '| less' part will make it easier to read.

The 'grep' command
In the last section we talked about the 'find' command which finds files. Now we'll talk about the 'grep' command which finds words in files. Your windows manager may have this incorporated into its find tool but then again, the beauty of Linux is having alternatives.
What does 'grep' mean?
'grep' is a Vulcan word that means "find". Actually it isn't, but it sort of looks like it, doesn't it?
Kirk: "Find the solar system L10J, Mr. Spock."
Spock: "Grepping now, Captain." *
* Star Trek stuff copyright Paramount Pictures
Let's have a little practice session with 'grep'. The best way is learning by doing, so let's do it.
A 'grep' mini-tutorial
With 'pico' or any Linux text editor, create a file called 'mary1.txt'


pico mary1.txt

Then type: "Mary had a little lamb"
Press CTRL-X in 'pico' (if you're using that) and it will prompt you to save.
Then create: mary2.txt and enter the text Mary had a little cow.
Save that and create the file: mary3.txt and type: Mary had a little too much to drink. Now we know what Mary was doing when she wasn't watching her lambs! Now save that file.
OK, now we're ready to try out 'grep', so phasers on stun and let's go.
Type the following command:


grep Mary mary*.txt

Let's explain this a little. 'grep' looks for the word "Mary" in any text file that is called "mary(something).txt". You've created three files that start with 'mary', so the asterisk makes sure that 'grep' will look for the word 'Mary' in all three.
You should get this output:
mary1.txt Mary had a little lamb
mary2.txt Mary had a little cow
mary3.txt Mary had a little too much to drink
The word 'Mary' is in all of those files, so you'll get this output.
If you type grep little mary*.txt you'll get the same output because the word "little" is also in each of those files. But if you type the word "cow", you'll get this output:
mary2.txt: Mary had a little cow
because the word "cow" is only in mary2.txt.
Typing grep drink mary*.txt will get us more or less the same, only that mary3.txt will show up instead of mary2.txt.
Well, there's 'grep' in a nutshell. It's been a pleasure 'grepping' with you!
Power user commands
Here is a brief overview of some other commands that you may find interesting at some point as you use Linux. They will help you to get all of the power out of Linux.

'who' is a command to find out who's working on your system. As you now know, Linux is a multi-user system. Even if you're using one computer at your home, you may be working as more than one person. For example, if you logged in as 'root' but are working as 'bob'. You may see something like this:

roottty1May 20 09:48
bobtty2May 20 10:05

This is just Linux's way of saying that 'root' started working on terminal 1 on May 20 at 9:48 in the morning and bob started working on terminal 2 at 10:05. This is mainly used in networked situations so the system administrator knows who's working. It can be used by your boss to find out that you've come in late too. You may use it to find out if you've opened more that one terminal so that you remember to log out.

'tee', '>', '2>'
In the lesson on the pipe command, I mentioned plumbing with Linux. I think I'm going to resist the temptation to make some sort of golf reference here in the lesson on the command 'tee'.

'tee' is used to write out what appears on your screen into a file. You will be using this with the after a pipe '|' .
You might do this:


ls -l | tee directory_listing

to get a file with the listing of a directory. If you've placed files in a directory to be backed up, you could use this command to create a listing of that directory. You could print out the file on a label and stick it to the disk, tape, zip cartridge or whatever you used to make the backups.

If you're using the 'tee' command for the backups I described before, you may want to put a date on the file. You can use this command:


date | tee -a directory_listing

The command 'date' will enter the date and time in the file at the end. Remember to use the -a option if you're going to write to that file a second time. If you don't you will erase everything on the file in favor of whatever the second command was.

The '>' command
The "greater than" symbol '>' will do the same as 'tee'. You don't need the pipe command (|) with this one.


ls -l > directory_listing

will give you the same result. If you want to add the date at the end, use the command:


date >> directory_listing

with two "greater than" symbols (>>)
The two symbols will add to the file without erasing its contents (appending).

The '2>' command
This command, the number two (2) with the "greater than" symbol >, is used for creating a file for an error message that you may get.

You will probably not be using it a lot because we all know how perfect Linux is and how few errors there are when you're using it. But every once and a while you may want to download some software from the Internet You install it and – whoops! – there's some error. You may not have something installed that the program needs to run. You could just do something like this:

[program X that doesn't work] 2> program_X_error

You create a file with the error message. You could show it to someone who might know what's missing or you could send it to the author of the program. He or she would also like to know about it and will probably help you fix it.

whoami is a nice little program that tells you who you are, just in case you didn't know already. You amnesia victims are in luck! Actually it tells you who you are in terms of how Linux understands who you are, that is to say, your user name. So if your user name is bob and you type whoami you'll get: bob This comes in handy if you switch terminals a lot and work as a different user. You may, in terms of computer use anyway, forget who you are!

To show you how to use the 'whatis' command 'whatis' is a command so you can find out what a program does. If you explore your Linux system, you will find a lot of programs and you may not know what they do. You would simply type:


whatis grep

for example, and you would get this:

grep (1) – print lines matching a pattern

Linux is good, but it's not all-knowing, so if you type:


whatis orange juice

You will get this message:

orange: nothing appropriate.

juice: nothing appropriate.

basically telling you that Linux has no idea what orange juice is

whereis is a nice command for finding other commands or programs. If you decide to download any program from the internet, that program may need other programs in order to work. If you want to know whether or not you have it, you can type:
whereis [program name]
and find out.

If you wanted to find out if you have the 'pico' editor and where it is, you would type:


whereis pico

and you may get this:

pico: /usr/bin/pico /usr/man/man1/pico.1.gz

It shows you where the command is as well as the location of its manual file.

whereis isn't designed to find people, so if you type


whereis Harry

Linux is just going to say Harry:

To show you another tool for locating programs 'which' is similar to 'whereis'. It will give you the location of a program. At times, a program may not find another program it needs to make it run. It will need to know its location or "path". For example, a program may need Java to run it but thinks its in another place. You would simply type:


which java

and Linux will inform you of its location


This is a handy command because some locations of programs vary from one version of Linux to the next. A software developer may have designed his/her program to access Java, for example, from a different location. As Open Source software will always let you modify configuration files to get your program working according to your needs, you can get the program to work for your system.

To show you some uses of the 'echo' command 'echo' is a little command that repeats anything you type. For example if you type


echo hello

Linux will display the word 'hello' .

There is really no need to do that under normal conditions. You may find 'echo' useful in the future if you start writing "shell scripts" which are like little programs that you could use to do a few commands at one time. You could use 'echo' in those scripts to tell you what the script is doing at any given time, or to prompt you to do something, like enter text. Shell scripts will be taken up in a later course.

There is a practical use for 'echo' in everyday life. I sometimes use it to write short notes. If we use 'echo' along with 'pipe' (|) and 'tee', you've got a poor-man's post-it-note.

For example:


echo remember to tell Bill Gates he owes me 5 bucks | tee -a bill_gates.note

Will make you a nice reminder note about dear 'ole Bill. Just remember to read your note. You could type:


echo remember to open Gates note | tee -a remember_gates.note

to make yourself a reminder for the other reminder note. Use less bill_gates.note or less remember_gates.note to read your notes

People following this course from Europe may recognize this as the symbol for 'bathroom'. Unfortunately, if you type this in your terminal it will not show you the way to the 'facilities'.

Actually, this command will give you the number of lines, words and letters (characters) in a file and in that order.

Let's go back to the file about the people I owe money. If I type:


wc people_I_owe_money.note

I will get this output:

439 6510 197120 wc people_I_owe.note

As you can see, there are 439 lines, so that means if each line represents one person, then I owe 439 people money. There are 6510 words and a total of 197120 characters.

I might add that this is a good tool for people who write letters professionally and get paid by the word.

The people who traveled down the MS-DOS road will know this one. Actually, dir=ls -l. It will give you the same result. If you do any downloading or uploading of files via FTP by way of a non-GUI FTP program in your terminal, you may find this command useful. I once ran into a case where the remote computer didn't recognize thels -l command. I just typed in dir and that did the trick. Then I fired off a nasty e-mail asking why in the world they weren't using Linux!

The command pwd will show complete information on the directory you're working in. For example, if you type



you may get something like this:


which shows you that you're in the directory 'homework' in your user directory as 'bob', so you know exactly where you are.

Did you forget your wedding aniversary? Your boyfriend or girlfriend's birthday? Tax day? (everyone wants to forget that one!) No need for that to happen anymore with Linux. Just type:



You'll get this: (or something like it, actually. If you get the same thing as I do, then I'd consider buying lottery tickets)

Thu Sep 7 20:34:13 CEST 2000

You probably get everything here. If you're living in central Europe, you will recogize the 'CEST' part. That stands for 'Central European Standard Time'. Linux recognizes world time zones and you set this up when you installed Linux. If you live on Mars, you're out of luck, unfortunately.

There are other uses of the 'date' command. To see just the date, type:


date +%D

To see just the time, type:


date +%T

To see on what day Christmas falls this year (really, I'm not kidding!), type:


date –date 'Dec 25'

and you'll get the day that Christmas falls on this year. Substitute that for any date that you'd like to see.

There are many other options. Consult your manual file ('man date') or ('info date')

Typing cal will give you the calendar of the present month on your screen, in the nice standard calendar format. There are a lot of useful options.

If you type:


cal 2000

You'll get the entire calendar for the year 2000. Substitute any year you like.

If you type:


cal 12 2025

You'll see the calender for December of 2025. Substitute any year or month you like.

If you add the option cal -m, the week will start on Monday, as it is preferred in many countries.

Just for fun, I typed


cal 10 1492

and I found out that Columbus discovered America on a Friday. That was good luck for him because that way he got to relax for the weekend.

As you can guess, you can get out of a terminal with the exit command. If you're working in text mode, typing exitwill prompt you to login again. If you want to work as another user, use logout instead.

If you're in x-windows, exit will close the X-Terminal you're working with exit with the option "stage right" will get you an error message.