Terminal.app or Linux Command Shell 101

Posted: May 31st, 2010 | Author: | Filed under: Insight, Linux | No Comments »

The Folks

Someone the other day asked me about how I got into the computer industry, and I guess it was because of my father. I still remember the day he brought home our first Commodore 64. The excitement I felt as the disk drive began to makes noises that are reminiscent to some sort of steam punk robot still resonates within me today. I was roughly eight but from that day on I was hooked, especially when I figured how to use a modem. If my parents read my blog they’re about to read a story regarding a strange incident from the past.

We had a 300 Baud modem that my father thought didn’t work and tossed aside, it came with a program called Quantum Link which eventually turned into AOL. I figured out how this modem worked by reading our subscription to the Computing Monthly Magazine and eventually got connected to a BBS in Toronto. Toronto, the city.

Now if you’ve been around long enough you’ll remember that most BBS were crowded services that were hard to get onto as they were all modem based, and if run out of house only had one line dedicated to it. In addition, since modem operate on phone lines you had to physically call the other end, so if the BBS was in Toronto you were basically making a long distance phone call.

My parents could NOT figure out what this number was that racked up so much long distance, and why it would just squeal when they called it. They called the phone company and I think got the charges reversed but I didn’t want this to happen again. I told some friends of mine who also had a Commodore 64 and they showed me that if you use a touch tone telephone to navigate through an automated phone system at BC Gas you could get to an open relay and make free long distance calls.

Through the years I’ve learned more and more and eventually got to the point where it’s now my profession. With this comes the need to have servers and there’s just something gratifying in having that “server under the bed.” My parents just so happen to have a spare bedroom in their house and thus the perfect place for my server to live. However every so often it needs some maintenance and thus my Dad is the one who intervenes. Recently though the requests I’ve been making of him have been getting more and more complicated and so I wrote a Linux Command Shell 101, to which I now share with you world. For all you people learning the shell for the first time.

Hi Dad,

So I’ve decided that it’s time you learn the command shell in Linux and how to use it. Yes, you have to read this :)

Chapter 1 – The Shell

The shell is actually not that complicated, but if you don’t understand what you’re looking at it can be very confusing. First what is a command shell? Well it’s actually no different than a programming language. It’s a way of giving the computer commands and interacting with it in either one off commands or in a script. There are many types of shells as well, the most common is the Bourne Again SHell or BASH for short, for the most part this is the defacto standard shell. Some other shells are:

ash
kash
tsh
tcsh

Most shells are all the same thing, the only difference between them is small grammatical differences. For example, and don’t worry that you have no idea what this is about.

bash ${variable}
ash $variable
kash $|variable|

see? small differences. So in summary the shell is a place where you can input commands to the computer and the computer executes them for you.

Next is a look at the shell. The following is what my shell looks like on my mac.

bart:~ jordan$

This is a command prompt. When you see something like this it means that computer is ready to accept commands.

So there are a few pieces of information here.
The first word tells us the name of the computer. In this case, my computer’s name is bart. Another word for the computers name is hostname
“:” indicates the end of the hostname,
“~” tells us the path that we are currently located (more about path later)
then username that we’re currently logged in as.
The $ tells us its the end of the prompt.

Now some prompts may look a little different from this but they’ll be the same more or less. Here’s what it looks like when I’m logged in as root

bart:~ root#

notice the only difference is the end of the prompt has a pound symbol instead of a dollar sign. This is because we’re root. All users prompts end in a dollar sign but only the root user ends in a pound. So if you ever encounter a prompt with nothing but just a dollar sign OR a pound you’ll still know that its a prompt.

Chapter 2 – Command Structure

Now it’s time to give the computer a command. You already know what a command is, its basically a program that runs in text mode. Commands can be anything from something simple like “cd” (change directory) to something complicated like “firefox-text” (text mode on firefox) When you give a shell a command there are a few things that you need to be aware of. First is a switch.

Switchs
A switch is a modifier that you give to the command to change what the command does. For example the command “ls” gives a listing of the files in the current directory. So here’s an example of the ls command listing the files in my home directory.


bart:~ jordan$ ls
Applications Downloads Movies Public bin
Desktop Library Music Sites test
Documents LimeWire Pictures bash.test zimbra
bart:~ jordan$

Now what I’m going to do is give the same command but with the -l switch. (that’s a lower case L)


bart:~ jordan$ ls -l
total 16
drwxr-xr-x 3 jordan staff 102 12 May 23:40 Applications
drwx------ 20 jordan staff 680 22 May 02:41 Desktop
drwxr--r--@ 39 jordan staff 1326 12 May 23:31 Documents
drwx------ 40 jordan staff 1360 18 May 09:22 Downloads
drwx------ 47 jordan staff 1598 23 May 20:01 Library
drwxr-xr-x 4 jordan staff 136 12 Feb 10:09 LimeWire
drwx------ 12 jordan staff 408 16 May 23:17 Movies
drwxr--r-- 21 jordan staff 714 24 Mar 19:41 Music
drwxr--r-- 23 jordan staff 782 7 May 23:33 Pictures
drwxr-xr-x 5 jordan staff 170 16 Sep 2009 Public
drwxr-xr-x@ 7 jordan staff 238 2 Nov 2009 Sites
-rwxr-xr-x 1 jordan staff 73 23 May 15:03 bash.test
drwxr-xr-x 6 jordan staff 204 7 May 23:33 bin
-rw-r--r-- 1 jordan staff 19 18 May 12:07 test
drwxr-xr-x 3 jordan staff 102 9 May 17:40 zimbra
bart:~ jordan$

see how command changes? It’s the same command, it does the same thing in that it lists all the files and folders but it also gives me LOTS of other options such as owner, size, date, etc.

Arguments
Next is an argument, an argument usually goes with a switch. So say you have a command like a text editor. One example of a text editor is “nano” if you just type edit the computer will not do anything, you have to give it a file to edit as well. So for example:


nano example.txt

We see here that “nano” (which is used as an example only) is the command and “example.txt” is the argument.

Final Chapter – Commands

Finally I’m just going to touch a few basic and extremely common commands as well as a way of searching for commands and their manuals. First let’s introduce you to a utility called “apropos” To use apropos you simply type it in followed by an argument in quotations. ie: apropos “move files” What this will do is search all the commands available on your computer for something that matches “move files” Think of it as a search engine for commands, a really dumb search engine. It’s dumb because if you don’t pick the right words to search for it won’t find anything. Take this for example. apropos “make folder” will find nothing but apropos “create folder” will have many search results.

So let’s use apropos to find a command to move files


bart:~ jordan$ apropos "move files"
mv(1) - move files
removefile(3), removefile_state_alloc(3), removefile_state_free(3), removefile_state_get(3), removefile_state_set(3) - remove files or directories
srm(1) - securely remove files or directories
bart:~ jordan$

So in the search results here there are the actual commands on the left (ignore the number in the brackets) and then explanations on the right. The first line looks like the one the that we want, the second (and third) line looks like garbage and the last line is some command to remove files. Apropos found the last line because we searched for move files and reMOVE FILES matches that. See how it’s dumb?

Ok so the command we want is the first line, mv. Next we need to learn how to use this command, what switches and arguments it can take so what we do is use another command called “man” man is short for manual. Simple. We type man and then the command name as an argument. for example


bart:~ jordan$ man mv
MV(1) BSD General Commands Manual MV(1)

NAME
mv -- move files

SYNOPSIS
mv [-f | -i | -n] [-v] source target
mv [-f | -i | -n] [-v] source ... directory

DESCRIPTION
In its first form, the mv utility renames the file named by the source
operand to the destination path named by the target operand. This form
is assumed when the last operand does not name an already existing direc-
tory.

In its second form, mv moves each file named by a source op...... ETC ETC ETC

To navigate this man page you can use the up and down arrows as well as the page up and page down keys. To exit push “q”

There you have it between apropos and man you can search and learn all commands linux. Here are a few commands that very important for you to know.

ls: list files and folders in current directory
cd: change directory
mkdir: make directory
rm: remove file
rm -rf: remove files and folders, WITHOUT VERFICATION
mv: move files and folders
pwd: display current directory
whoami: display your username
nano: an easy to use text editor, to use it just type “nano ”
exit: exits the shell

Ok, so this is pretty short and brief but hopefully was a good introduction to the Linux command shell. If you have questions or if something I wrote doesn’t make sense email me back and let me know.



Leave a Reply

  • Current month ye@r day *