The Linux Shell

January 25, 2010 by: Allen Sanford
Knoppix Linux desktop
Image via Wikipedia

Today I am going to step back a little bit and cover a subject that seems to be a pivotal make it or break it moment in most would be Linus users. For those of us who are long past the transition days between M$ and Linux, can still remember the day we found out that the Linux shell (a.k.a The Terminal) is where we would spend most of our time trying to customize our Linux distro, or in some cases trying to get our favorite distro working. Anyway I understand that in this day and time Linux is getting better about meeting the needs or the graphical point and click user, but still by far the shell is the most powerful tool in any Linux users arsenal.

So lets get started!

Shells

The Shell is also known as the command line for those new to Linux, and the shell is also used to execute your commands (sometimes without you even knowing it). Common examples include tcsh, bash, zsh, ksh, ash,…. However, there are two main styles: Korn Shells (bash is the main one) and C Shells (tcsh is the main one). A note about Windows shells; they suck, do not work properly, and do very little expect execute commands. Now on the contrast *nix shells are full programming environments with your imagination as the limit. Shells read your commands, interpret them and execute system process.
If the command is a built-in (eg cd skoleLinux), your shell does it internally. Otherwise, it searches the PATH environment variable for a program matching with that name. Here’s my PATH (and how to display it):

The difference between one shell and another lies mostly in the details of the interpretation and what built-in commands are available. The shells are the main owners/maintainers of Environment variables (eg $PATH, $CVS_RSH, $UID). One major difference between bash and tcsh is the built-in command used to set/modify your environment variables.

In bash, to set an environment variable you do things like:

export PATH=/usr/local/bin:/usr/bin
export PATH=$PATH:/usr/bin/X11:/usr/games:/<a class="zem_slink" href="http://en.wikipedia.org/wiki/Home_directory" title="Home directory" rel="wikipedia">home</a>/asanford/bin
echo $PATH
        /usr/local/bin:/bin:/usr/bin/X11:/usr/games:/home/asanford/bin

In tcsh, you would do:

setenv PATH /usr/local/bin:/usr/bin
setenv PATH $PATH:/usr/bin/X11:/usr/games:/home/asanford/bin
echo $PATH
        /usr/local/bin:/bin:/usr/bin/X11:/usr/games:/home/asanford/bin

I recommend using bash. It’s the standard shell in almost any modern day Linux installation, and it’s the one most people will generally assume you are using when trying to help you out.

BASH

A few useful facts about bash:

1. ~/.bash_profile (ie the file .bash_profile in your home dir)

All commands in this file are executed when you start bash. So you can put customizations in there, such as setting environment variables. You might like to create the file with:

              export CVS_RSH=ssh
              export CVS_ROOT=":ext:asanford@cvs.example.com:/var/lib/cvs"

the above commands would be executed every time you login. Then you wouldn’t need to type the commands every time you want to use CVS, they’d be set every time in advance.
2. alias

If you type a command a lot (eg ssh -X -C asanford@example.com): you can create a shortcut by:

      alias forty="ssh -t -X -C asanford@example.com "

so, from then on, every time you type “forty” on the command line, when bash interprets it, it will substitute the above longer string delaying your inevitable arthritis. This is something you would put in your ~/.bash_profile
3. It’s a full programming environment.

Without going into gory details, it will suffice to say that you can write if, else, while, for, … etc. directly into bash and it will interpret it. For example, the following command will loop over a bunch of infidel usernames, deleting their home directories.

         for i in asanford ksanford john
         do
              rm -r ~$i/
         done

4. Wildcards and other special characters

If you do

              ls foo_*.txt

you’ll get a listing of all files beginning with “foo_” and ending “.txt” . In DOS, you can similarly do

      dir *

. However, in DOS, the dir program reads the * and evaluates it. Here, BASH evaluates the * and ls sees the list of files as its options. `ls` itself does not understand wildcards. When it interpets the command it looks for certain special characters to interpret:

* means a sequence of one or more characters (a wildcard)
? means a single character (a wildcard)
$ means the following characters make up an env variable (eg $PATH)
[acde] means a single character matching any of a,c,d or e
[0-9] means any digit
~ shorthand for home directory

As a case in point, if you are ever in a directory with a huge number of files (say 100,000) and you want to delete or list the files, you might try:

          asanford@shual$ rm *
          bash: /bin/rm: Argument list too long

It won’t work. This is because bash evaluates the *, all 100,000 filenames end up as arguments (options) to the command and `rm` can’t handle this. One solution is as follows explaining this in detail is outside the scope of this document but trust me it works:

          asanford@shual$ ls . | gawk '{print "rm -f "$1}' | bash

So bash launches the rm command once for each file * matches instead of once for all files.

The Basics

You may have noticed that each time you start Xwindows, a shell window, similar to a DOS window in Win95/98, appears. This is the powerhouse of the Linux OS, and from here you can execute copy, compile, move, find and edit commands just as easily as in dos. Unfortunately, there are different commands used in the Linux shell than in DOS, so I’ll explain a few of them. Remember that commands are case sensitive in Linux, so LS is not the same command as ls, and will return an error.

Changing Directories – You’ll need this more often than anything.

Just as in DOS, the cd (Change Directory) command is used. When the command shell opens, it opens in your own (Most of the time there are some exceptions), home directory. If you ever need to return to your own home directory, just enter cd and press return. As in DOS, to move out one level in a directory tree, type cd .. You can go to the root directory of your Linux OS and all it’s drives by typing cd /. Note how in Linux, there is a backslash instead of a forward slash. Another thing is that Hard Disks are not mounted as drive c, drive d and so on. They are placed as folders, and to all intents and purposes look just like one big hard disk. To find out what disks are mounted, where, and what capacity they are and have left, type df (I remember this using, disk free, relating to free space). If you want to go to one of the user accounts on your machine, you can cd ~username, and that’ll take you to where their account resides (Assuming you have permission to enter their directory).

Need to know what is in a directory?

DOS has that crappy dir command, but where is the Linux version? It’s replaced by ls. ls, can be remembered as a list (Acronym) of what is in that directory. It is quite confusing to look at, and its harder to separate what is a folder, what is a file, and what can be run. In DOS, there are a vast number of switched, or options, that can be added to the basic command dir to give more information. Linux is not left behind, and adds even more commands to confuse yourself with *grin*. ls -m (remember this as more), is similar to dir /w, giving horizontal lines of filenames and directory names. ls -l is a switch that will give the full names of the files, and then give the permissions of the files (Who is entitiled to execute them), and whether or not they are executable. If an X is present in the attributes, that file is a program, and can be run by typing ./nameoffile. Files can only be run if they have the ./ prefix before them. The other attributes you will learn as time progresses and you become more familiar with the Operating System. The best usage of ls, is ls –color. This gives a wide listing, with colors assigned to various file types. When using this option, directories are in blue, and executables in green. This option is by far the best. Typing ls –color can get annoying as I found after a while, so its possible to create a text file with ‘ls –color’ contained, and then allow that file to be executed. By placing this file in the path, and changing its name to lss, you could use it quite frequently! I will show how to do this at a later date.

File name completion

Ok now the nicest features about a Linux shell, as opposed to the DOS shell, is that it can (if configured to do so and most distros do by default) complete file names and directory names for you. The TAB button, located above Caps-Lock, is the completion key. Once you have typed in the beginning of a file, or directory, pressing TAB will search for a list of possible file or directory names. If more than one option is presented, nothing will appear, you will most likely get some sort ot audible bell or screen flicker, and pressing TAB a second time will present a complete listing of all the available completion candidates, enter a few more characters of the option you want, and press TAB again.

Copying files

This is an interesting one. For copying internally between mounted drives, it’s very similar to the DOS syntax. to copy blah.xxx from the current directory to /root/, the syntax is cp blah.xxx /root/, just remember that if something is going into a lower level directory to give the full path, including the forward slash.
Moving or Renaming Files

Again, ths syntax is very similar to the DOS syntax. If you want to rename blah.xxx to xxx.blah, the command is mv blah.xxx xxx.blah, fairly simple. If you are just relocating the file, remember that two files with the same name cannot reside in the same directory, so you will have to give a directory name, just like the description in the Copying section above.
Working with MS-DOS floppy disks

Now one of the most useful things to now is how to wiping the screen clean and have the first line at the top of the screen or window. To clear a screen full of text, just type in `clear` from the command line

Any way I hope this has been a helpful introduction for someone out there.

Enjoy and Have a Good’n!

Comments

One Response to “The Linux Shell”
  1. Wayne Gimse says:

    Cheers for this post, I’ve learned a lot more now about mp3′s. My personal place is a directory I found somwere on google. It has storaged hundreds of music files. I have added the link in that website url option. I hope to see a lot more blogposts from you!

Leave a Reply