02- Bash Shell Basics
In these and subsequent sections, we will be following closely the book The Linux Command Line, by William E. Shotts, Jr. Personally, I recommend that you purchase the book (I did!); however the author has kindly made a .pdf version available at no cost. I have found that the e-book format is better suited to browsing than the full .pdf, and the cost is quite reasonable.
If you followed the 01 notes, you now have a fully functional CentOS 6.2 virtual machine; you can log on to the system with your password and you are presented with a desktop like the following:
We are going to start learning about Linux by learning more about how the command line functions- but to do that, we need to find the command prompt. Navigate the Applications Menu, then System Tools, and select Terminal.
Once you do so, you will be presented with a command prompt like the following
Chapter 1
At this point, you are ready to start working through the book. Pull up The Linux Command Line (TLCL), and begin with Chapter 1, working through the text as you go. For example, be sure to run the date
and cal
commands. If you followed my instructions in the 01 notes, then the output of your df
and free
commands should match the following:
[adent@localhost ~]$ date Mon May 27 14:16:58 PDT 2013 [adent@localhost ~]$ cal May 2013 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 [adent@localhost ~]$ df Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda2 18339256 2403972 15003700 14% / tmpfs 506544 548 505996 1% /dev/shm /dev/sda1 297485 32685 249440 12% /boot [adent@localhost ~]$ free total used free shared buffers cached Mem: 1013088 527972 485116 0 105344 173352 -/+ buffers/cache: 249276 763812 Swap: 2031608 0 2031608
One note as you read Chapter one. There is a section marked "The Console Behind the Curtain". Everything in that section is correct, but you may have difficulty seeing so. Why? On VMWare, the key sequence CTRL-ALT is used by default to switch between the host and the guest. Thus, if you try CTRL-ALT-F1 as recommended, you may end up simply switching out of the virtual machine, and moving to the host. That said, I have seen different behavior with different VMWare products over the years. Try it on your system; if it works, then great. If it doesn’t, then at least you have an idea why it does not work.
Chapter 2
TLCL starts by teaching you about the file system; it even has a picture from a graphical file manager. If you want to see the graphical file manager that comes standard on our CentOS 6.2 system, navigate Applications → System Tools → File Browser.
When the tool is run, you will be presented with a window like the following
This is different than the view in TLCL, but it presents much of the same information.
When you run the ls
command from the command prompt, you should compare the results to what is being shown by the file browser; they will agree.
As you work through the examples using cd
and pwd
in Chapter 2, be sure to follow along in the file browser to be sure that they agree. For example, suppose you change your working directory to /var
and perform an ls
:
[adent@localhost ~]$ cd /var [adent@localhost var]$ ls account crash empty gdm local log nis preserve spool www cache db games lib lock mail opt run tmp yp [adent@localhost var]$
If you change directories in the file browser to /var
you will get the same information:
Chapter 3
Try the different options for the ls
command. If you run ls -al
from your home directory ~
, then you should see something like the following
[adent@localhost var]$ cd ~ [adent@localhost ~]$ ls -al total 160 drwx------. 28 adent adent 4096 May 27 14:44 . drwxr-xr-x. 3 root root 4096 May 27 04:59 .. -rw-------. 1 adent adent 834 May 27 14:22 .bash_history -rw-r--r--. 1 adent adent 18 Dec 2 2011 .bash_logout -rw-r--r--. 1 adent adent 176 Dec 2 2011 .bash_profile -rw-r--r--. 1 adent adent 124 Dec 2 2011 .bashrc drwxr-xr-x. 3 adent adent 4096 May 27 14:22 .cache drwxr-xr-x. 5 adent adent 4096 May 27 14:22 .config drwx------. 3 adent adent 4096 May 27 09:04 .dbus drwxr-xr-x. 2 adent adent 4096 May 27 14:44 Desktop -rw-r--r--. 1 adent adent 31 May 27 14:23 .dmrc drwxr-xr-x. 2 adent adent 4096 May 27 09:04 Documents drwxr-xr-x. 2 adent adent 4096 May 27 09:04 Downloads -rw-------. 1 adent adent 16 May 27 09:04 .esd_auth drwxr-xr-x. 2 adent adent 4096 May 27 09:04 .fontconfig drwx------. 4 adent adent 4096 May 27 14:23 .gconf drwx------. 2 adent adent 4096 May 27 14:45 .gconfd drwxr-xr-x. 6 adent adent 4096 May 27 14:22 .gnome2 drwxrwxr-x. 3 adent adent 4096 May 27 09:04 .gnote drwx------. 2 adent adent 4096 May 27 14:23 .gnupg drwxrwxr-x. 2 adent adent 4096 May 27 09:04 .gstreamer-0.10 -rw-rw-r--. 1 adent adent 137 May 27 14:23 .gtk-bookmarks drwx------. 2 adent adent 4096 May 27 09:04 .gvfs -rw-------. 1 adent adent 934 May 27 14:23 .ICEauthority drwxr-xr-x. 3 adent adent 4096 May 27 09:04 .local drwxr-xr-x. 4 adent adent 4096 May 27 04:54 .mozilla drwxr-xr-x. 2 adent adent 4096 May 27 09:04 Music drwxr-xr-x. 2 adent adent 4096 May 27 09:04 .nautilus drwxr-xr-x. 2 adent adent 4096 May 27 09:04 Pictures drwxr-xr-x. 2 adent adent 4096 May 27 09:04 Public drwx------. 2 adent adent 4096 May 27 09:04 .pulse -rw-------. 1 adent adent 256 May 27 09:04 .pulse-cookie -rw-------. 1 adent adent 1985 May 27 14:44 .recently-used.xbel drwxr-xr-x. 2 adent adent 4096 May 27 09:04 .spice-vdagent drwx------. 2 adent adent 4096 May 27 09:04 .ssh drwxr-xr-x. 2 adent adent 4096 May 27 09:04 Templates drwx------. 3 adent adent 4096 May 27 14:39 .thumbnails drwxr-xr-x. 2 adent adent 4096 May 27 09:04 Videos -rw-------. 1 adent adent 3107 May 27 14:39 .xsession-errors -rw-------. 1 adent adent 2437 May 27 14:22 .xsession-errors.old [adent@localhost ~]$
which is not likely what you expected to see, especially if you compare it to the output of a simple ls
[adent@localhost ~]$ cd ~ [adent@localhost ~]$ ls Desktop Documents Downloads Music Pictures Public Templates Videos [adent@localhost ~]$
Have no fear at this point, we still have a lot of explaining to do.
Next in the chapter, TLCL discusses the file
command. To get a couple of examples of files, start the web browser, and download a few. One of my favorite programs on Windows is the text editor Notepad++. From their download site, you can grab a copy of the installer, the zip package, and the 7z package. Do so. By default, Firefox will save the result in the directory ~/Downloads
, so change to that directory and check out the file types for each file:
[adent@localhost ~]$ cd ~/Downloads/ [adent@localhost Downloads]$ ls npp.6.3.3.bin.7z npp.6.3.3.bin.zip npp.6.3.3.Installer.exe [adent@localhost Downloads]$ file npp.6.3.3.bin.7z npp.6.3.3.bin.7z: 7-zip archive data, version 0.3 [adent@localhost Downloads]$ file npp.6.3.3.bin.zip npp.6.3.3.bin.zip: Zip archive data, at least v2.0 to extract [adent@localhost Downloads]$ file npp.6.3.3.Installer.exe npp.6.3.3.Installer.exe: PE32 executable for MS Windows (GUI) Intel 80386 32-bit [adent@localhost Downloads]$
One comment- the file tool does not use the name of the file to determine its type!
Next up in Chapter 3 is the less
command, which is used to read the contents of a text file. The text starts by asking you to use the command on the file /etc/passwd
. From our (security) point of view, this will be an important file. For now though, all we will do is run the less
command to see its contents:
We can clearly see the user we created in notes 01; in my case it was Arthur Dent. The rest is unclear for now, but as we go on we will learn quite a bit more about the structure of this file and what is all means.
Finally, be sure to take the guided tour. Relax, provide you don’t delete or modify any files, there is no damage that you can do to the system. Since we haven’t even discussed the command to to these things, have no worries. Even if you did change something, you need root permissions to really mess things up- and we haven’t yet learned those either. At this point, feel free to explore, and play around with the command you have learned so far.