The shell Flashcards
What is Bash?
“Bourne Again Shell”
other common shells include sh, csh, tcsh, will also be referenced in this tutorial, and they sometime differ from bash
How can shell scripting be accomplished?
by directly executing shell commands at the shell prompt or by storing them in the order of execution, in a text file, called a shell script, and then executing the shell script. To execute, simply write the shell script file name, once the file has execute permission (chmod +x filename)
What is “the shell”?
Simply put, the shell is a program that takes commands from the keyboard and gives them to the operating system to perform. In the old days, it was the only user interface available on a Unix-like system such as Linux. Nowadays, we have graphical user interfaces (GUIs) in addition to command line interfaces (CLIs) such as the shell.
What is a “terminal”?
It’s a program called a terminal emulator. This is a program that opens a window and lets you interact with the shell. There are a bunch of different terminal emulators you can use. Most Linux distributions supply several, such as: gnome-terminal, konsole, xterm, rxvt, kvt, nxterm, and eterm.
separate commands in one line
;
Command history
Up/down arrows
If you’re not logged in as root, what are you?
If the last character of your shell prompt is # rather than $, you are operating as the superuser. This means that you have administrative privileges. This can be potentially dangerous, since you are able to delete or overwrite any file on the system. Unless you absolutely need administrative privileges, do not operate as the superuser.
Why do you need to learn the command line anyway?
Graphical user interfaces (GUIs) are helpful for many tasks, but they are not good for all tasks. I have long felt that most computers today are not powered by electricity. They instead seem to be powered by the “pumping” motion of the mouse! Computers were supposed to free us from manual labor, but how many times have you performed some task you felt sure the computer should be able to do but you ended up doing the work yourself by tediously working the mouse? Pointing and clicking, pointing and clicking.
pwd
The directory you are standing in is called the working directory. To find the name of the working directory, use the pwd command.
Stands for “print working directory”
ls
list the files in the working directory
Stands for “list”
cd
Change the working directory (cd followed by the pathname)
Absolute vs. relative path names
Where an absolute pathname starts from the root directory and leads to its destination, a relative pathname starts from the working directory. To do this, it uses a couple of special notations to represent relative positions in the file system tree. These special notations are “.” (dot) and “..” (dot dot).
The “.” notation refers to the working directory itself and the “..” notation refers to the working directory’s parent directory
Root directory
/
User directory (or home directory/where you start out normally)
~
cd followed by nothing
Shortcut to change working directory to your home directory
cd ~user_name
Change to home directory of specified user
Hidden files
File names that begin with a period character are hidden. This only means that ls will not list them unless you say ls -a. When your account was created, several hidden files were placed in your home directory to configure things for your account. Later on we will take a closer look at some of these files to see how you can customize your environment. In addition, some applications will place their configuration and settings files in your home directory as hidden files.
File names in Linux, like Unix, are…
case sensitive. The file names “File1” and “file1” refer to different files.
Linux has no concept of…
a “file extension” like legacy operating systems. You may name files any way you like. However, while Linux itself does not care about file extensions, many application programs do.
Punctuation in file names
Though Linux supports long file names which may contain embedded spaces and punctuation characters, limit the punctuation characters to period, dash, and underscore. Most importantly, do not embed spaces in file names. If you want to represent spaces between words in a file name, use underscore characters. You will thank yourself later.
Most commonly used command
probably ls
List files in a directory
ls /bin (/bin is just a directory for this example)
List the files in the working directory in long format
ls -l
long-form shows a lot more info, like:
File Name
-The name of the file or directory.
Modification Time
-The last time the file was modified. If the last modification occurred more than six months in the past, the date and year are displayed. Otherwise, the time of day is shown.
Size
-The size of the file in bytes.
Group
-The name of the group that has file permissions in addition to the file’s owner.
Owner
-The name of the user who owns the file.
File Permissions
-A representation of the file’s access permissions. The first character is the type of file. A “-“ indicates a regular (ordinary) file. A “d” indicates a directory. The second set of three characters represent the read, write, and execution rights of the file’s owner. The next three represent the rights of the file’s group, and the final three represent the rights granted to everybody else. I’ll discuss this in more detail in a later lesson.
List files in long format in multiple directories
ls -l dir_1 dir_2
List all files (even ones with names beginning with a period character, which are normally hidden) in the parent of the working directory in long format
ls -la ..
The format for most commands
command -options arguments
less
a program that lets you view text files. This is very handy since many of the files used to control and configure Linux are human readable.
What is “text”?
There are many ways to represent information on a computer. All methods involve defining a relationship between the information and some numbers that will be used to represent it. Computers, after all, only understand numbers and all data is converted to numeric representation.
Some of these representation systems are very complex (such as compressed multimedia files), while others are rather simple. One of the earliest and simplest is called ASCII text. ASCII (pronounced “As-Key”) is short for American Standard Code for Information Interchange. This is a simple encoding scheme that was first used on Teletype machines to map keyboard characters to numbers.
Text is a simple one-to-one mapping of characters to numbers. It is very compact. Fifty characters of text translates to fifty bytes of data. Throughout a Linux system, many files are stored in text format and there are many Linux tools that work with text files. Even the legacy operating systems recognize the importance of this format. The well-known NOTEPAD.EXE program is an editor for plain ASCII text files.
How to display a text file?
less text_file
Once started, less will display the text file one page at a time. You may use the Page Up and Page Down keys to move through the text file. To exit less, type “q”.
less commands
Page Up or b: Scroll back one page
Page Down or space: Scroll forward one page
G: Go to the end of the text file
1G: Go to the beginning of the text file
/characters: Search forward in the text file for an occurrence of the specified characters
n: Repeat the previous search
h: Display a complete list less commands and options
q: Quit
file
examines a file and tell you what kind of file it is
While it may seem that most files cannot be viewed as text, you will be surprised how many can. This is especially true of the important configuration files. You will also notice during our adventure that many features of the operating system are controlled by shell scripts. In Linux, there are no secrets!
File types recognizable by ‘file’
File Type / Description / Viewable as text?
ASCII text/The name says it all/yes
Bourne-Again shell script text/A bash script/yes
ELF 32-bit LSB core file/A core dump file (a program will create this when it crashes)/no
ELF 32-bit LSB executable/An executable binary program/no
ELF 32-bit LSB shared object/A shared library/no
GNU tar archive/A tape archive file. A common way of storing groups of files./no, use tar tvf to view listing.
gzip compressed data/An archive compressed with gzip/no
HTML document text/A web page/yes
JPEG image data/A compressed JPEG image/no
PostScript document text/A PostScript file/yes
RPM/A Red Hat Package Manager archive/no, use rpm -q to examine contents.
Zip archive data/An archive compressed with zip/no
/
The root directory where the file system begins. In most cases the root directory only contains subdirectories.
/boot
This is where the Linux kernel and boot loader files are kept. The kernel is a file called vmlinuz
/etc
The /etc directory contains the configuration files for the system. All of the files in /etc should be text files. Points of interest:
/etc/passwd
The passwd file contains the essential information for each user. It is here that users are defined.
/etc/fstab
The fstab file contains a table of devices that get mounted when your system boots. This file defines your disk drives.
/etc/hosts
This file lists the network host names and IP addresses that are intrinsically known to the system.
/etc/init.d
This directory contains the scripts that start various system services typically at boot time.
/bin, /usr/bin
These two directories contain most of the programs for the system. The /bin directory has the essential programs that the system requires to operate, while /usr/bin contains applications for the system’s users.
/sbin, /usr/sbin
The sbin directories contain programs for system administration, mostly for use by the superuser.
/usr
The /usr directory contains a variety of things that support user applications. Some highlights:
/usr/share/X11
Support files for the X Window system
/usr/share/dict
Dictionaries for the spelling checker. Bet you didn’t know that Linux had a spelling checker. See look and aspell.
/usr/share/doc
Various documentation files in a variety of formats.
/usr/share/man
The man pages are kept here.
/usr/src
Source code files. If you installed the kernel source code package, you will find the entire Linux kernel source code here.
/usr/local
/usr/local and its subdirectories are used for the installation of software and other files for use on the local machine. What this really means is that software that is not part of the official distribution (which usually goes in /usr/bin) goes here.
When you find interesting programs to install on your system, they should be installed in one of the /usr/local directories. Most often, the directory of choice is /usr/local/bin.
/var
The /var directory contains files that change as the system is running. This includes:
/var/log
Directory that contains log files. These are updated as the system runs. You should view the files in this directory from time to time, to monitor the health of your system.
/var/spool
This directory is used to hold files that are queued for some process, such as mail messages and print jobs. When a user’s mail first arrives on the local system (assuming you have local mail), the messages are first stored in /var/spool/mail
/lib
The shared libraries (similar to DLLs in that other operating system) are kept here.
/home
/home is where users keep their personal work. In general, this is the only place users are allowed to write files. This keeps things nice and clean :-)
/root
This is the superuser’s home directory.
/temp
/tmp is a directory in which programs can write their temporary files.
/dev
The /dev directory is a special directory, since it does not really contain files in the usual sense. Rather, it contains devices that are available to the system. In Linux (like Unix), devices are treated like files. You can read and write devices as though they were files. For example /dev/fd0 is the first floppy disk drive, /dev/sda (/dev/hda on older systems) is the first hard drive. All the devices that the kernel understands are represented here.
/proc
The /proc directory is also special. This directory does not contain files. In fact, this directory does not really exist at all. It is entirely virtual. The /proc directory contains little peep holes into the kernel itself. There are a group of numbered entries in this directory that correspond to all the processes running on the system. In addition, there are a number of named entries that permit access to the current configuration of the system. Many of these entries can be viewed. Try viewing /proc/cpuinfo. This entry will tell you what the kernel thinks of your CPU.
/media,/mnt
Finally, we come to /media, a normal directory which is used in a special way. The /media directory is used for mount points. As we learned in the second lesson, the different physical storage devices (like hard disk drives) are attached to the file system tree in various places. This process of attaching a device to the tree is called mounting. For a device to be available, it must first be mounted.
When your system boots, it reads a list of mounting instructions in the file /etc/fstab, which describes which device is mounted at which mount point in the directory tree. This takes care of the hard drives, but you may also have devices that are considered temporary, such as CD-ROMs, thumb drives, and floppy disks. Since these are removable, they do not stay mounted all the time. The /media directory is used by the automatic device mounting mechanisms found in modern desktop oriented Linux distributions. On systems that require manual mounting of removable devices, the /mnt directory provides a convenient place for mounting these temporary devices. You will often see the directories /mnt/floppy and /mnt/cdrom. To see what devices and mount points are used, type mount.
symbolic links
System.map, module-info, and vmlinuz for example
Symbolic links are a special type of file that points to another file. With symbolic links, it is possible for a single file to have multiple names. Here’s how it works: Whenever the system is given a file name that is a symbolic link, it transparently maps it to the file it is pointing to
Just what is this good for? This is a very handy feature. Let’s consider the directory listing above (which is the /boot directory of an old Red Hat 5.2 system). This system has had multiple versions of the Linux kernel installed. We can see this from the files vmlinuz-2.0.36-0.7 and vmlinuz-2.0.36-3. These file names suggest that both version 2.0.36-0.7 and 2.0.36-3 are installed. Because the file names contain the version it is easy to see the differences in the directory listing. However, this would be confusing to programs that rely on a fixed name for the kernel file. These programs might expect the kernel to simply be called “vmlinuz”. Here is where the beauty of the symbolic link comes in. By creating a symbolic link called vmlinuz that points to vmlinuz-2.0.36-3, we have solved the problem.
Example of a symbolic link in the output of a “ls -l” command:
lrwxrwxrwx 25 Jul 3 16:42 System.map -> /boot/System.map-2.0.36-3
How to create a symbolic link
ln command
Copy files and directories
cp