If you have not installed a graphical user interface (GUI), the system will boot you directly into a command shell. Otherwise, you will have to find your systems terminal program, such as Terminal.
Using File Commands
The commands you type into the command line are programs, and what they do can be controlled based on options you give. The general format of a BASH command is command -option(s) parameter(s).
Crunch TimePay careful attention to capitalization as Linux distinguishes between upper and lower case, in commands, options, and file names. For example: ls -s shows file sizes, where ls -S sorts by size. Likewise, FILE1, File1, and file1 are three different files, and ls is a valid command, but LS is not.
The list of options that can be used with
ls is large, with the most common ones shown in
Table 5.1.
Table 5.1. Common Options with the ls Command
|
Option | Description |
---|
-R | List subdirectories recursively |
-l | Display detail listing of a directory |
-a | Do not ignore hidden files beginning with. |
An example line from the output of
ls -l is shown below and interpreted in
Table 5.2.
Table 5.2. Explanation of the ls –l Output
|
Field | Description |
---|
drwxr-xr-x | Type and permission. The first character in the type field is the file type and the next nine characters show the access rights or mode of the file or directory. These are described later. |
7 | Number of links |
chappel | File owner |
users | Group the file belongs to |
4096 | Size in bytes |
2009-06-23 21:36 | Last modification date |
Documents | Name of the file |
drwxr-xr-x 7 chappel users 4096 2009-06-23 21:36 Documents
The type and permissions field is 10 characters long; the left most character is the type of the file, and the next nine bits read, write, or execute permissions for the user, group, and others, respectively, as shown in
Table 5.3.
Table 5.3. Explanation of the Type and Permission Fields
|
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
---|
File type | User Permissions | Group Permissions | Other Permissions |
d | r | w | x | r | w | x | r | w | x |
The first character in the type and permissions field is the file type and can be
■ c (character device special file)
■ b (block device special file)
The user, group, and other permissions will have one of the following five characters in it:
■ s (setuid; only found in the execute field)
■ - (in a field means that permission is not set)
The parameters of a command can also alter the results it will give. The most common parameter variations are the wildcards “*” and “+.” These are symbols that can represent multiple patterns; “*” for any number of any character and “?” for just one of any character.
There are a number of commands that can help you to look for files. find is very flexible, requiring a place to start, and what to look for, for example find /home -name file 1 looks for a file named “file1” in the /home directory, then continuing to look in each subdirectory.
locate creates a database of file names that it searches through making it faster. It can only find information in its database, so changes since the last automatically scheduled database update would not be found, and it can only search on file names. The slocate flavor of locate works the same way, but it adds additional security to prevent users from searching through files they do not have access to.
whereis is intended for searching for commands and files related to them – source, binary, configurations, and help files. It does not look in user directories at all. which looks through your path and tells you what program you will run if you type a given command.
File Types
A proper understanding of linked files requires a little technical background information. Directories contain the names of the files that reside in them, and pointers to an inode for each file. The inode stores the metadata for each file – the owner, size, access rights, time of last modification, and where the data contained in each file is physically located. The extra layer of abstraction provided by the inode means that it is possible to have two different names that point to exactly the same file information. This is called a “hardlink” and is created as follows: ln file_name hard_link_name
The hardlink name is just as valid of a name as the original file name, and all hard links must be deleted before the space occupied by the file is freed. The nature of hard links does not permit hard links to directories or to files residing on separate drive volumes.
The soft link is an actual file, with its own inode number and contains the path to the object the file links to. Soft links are allowed to point to directories and to files on other file systems, but because they are independent files it is possible to delete the target of the soft link and leave the link file “broken,” pointing to a nonexistent file. The ls -l command shows symbolic links with an “l” at the very first position in the row and with an arrow (→) pointing to the target file. Symbolic links are created as follows: ln -s file_name soft_link_name
Device Files
Anything capable of moving information in or out of your Linux system has a device file in the /dev subdirectory. This way of thinking about devices makes it easy for user programs to send or receive information; they only need to write (or read) from the appropriate file.
Block special device files move data in and out of hardware connected to the system in large chunks, and use buffers to improve efficiency, such as hard drives, USB, and tape drives. Character device files move data a single character at a time and are unbuffered. Note that not every file in the /dev directory corresponds to a currently connected device; most are simply placeholders waiting for a new device to point to.
Named pipes FIFO (first-in first-out) are buffers that are used for communications between programs running within the computer. One program opens the pipe to write, and the other to read, and data gets transferred between them.
Special files are created using the mknod command, shown for character, block, and pipe files, respectively:
mknod new_file_name c [major_device_number] [minor_device_number]
mknod new_file_name b [major_device_number] [minor_device_number]
Normally, special files are created manually only when device drivers are installed manually, which should include detailed instructions with additional information.
Testing Files
Files frequently have an extension to help indicate what they contain, which may not be so obvious, and
file can sort through things with a syntax
file [filename]. An example output is shown in
Figure 5.1.
test evaluates expressions, as in test 1 -gt 2 and the answer is returned as an exit value that can be checked to make decisions in a script. The power of the test command really shows some more interesting options, such as test -e [filename] to check if a file exists to be sure not to overwrite it, and test -d [directory_name] to see if a directory exists.
The ls -F command lists files with various file types tagged for easier recognition. While the file and test commands are more useful within scripts, ls -F is more helpful for humans and classifies the file with a special character denoting what it is, such as / for directory, @ for a link file, and * for an executable file.
Editing Files Using vi
A good text editor is called vi and the enhanced version is called vim. The key to dealing with vi is understanding that it has three modes: Command, Ex, and Edit. When first starting vi, you will be in Ex mode, which allows you to move around in the file you are editing and to perform copy and paste commands and other advanced features like recording and replaying macros. To actually enter text, you will need to shift into Edit mode by typing one of the edit keys, and then go back to Ex mode by pressing the Esc key. Command mode is entered from Ex mode by typing colon (:) and is used to enter save files, enter a filename or text to be searched for. You can go back to Ex mode by pressing Esc again.
To transition to Edit mode, use the i key to insert text in front of the current cursor position, or o to insert text onto a new line below the line the cursor is on. Once in Edit mode you can type text as you normally would. Press the Esc key to get back to Ex mode.
When you are finished moving and editing you can press the colon key (:) to go to command mode. From command mode you can search and replace text, save the file, and exit from vi.
Managing Processes
Every program, utility, or bit of code waiting in the background on your Linux machine, is called a process, and it is automatically handled by the kernel. Users can monitor and manage their own processes; system administrators have access to nearly all processes.
PS
ps will show you what programs are being run by your user in the terminal window it is executed in, which is usually just bash and the ps command itself. ps -A will show all the processes currently recognized by the machine. The output from the ps command is formatted into four columns. The first column is the process identification number or PID, which the system uses to uniquely identify each process. The next column is where the process is running – it's “control terminal,” which determines where the program's input and output should go. Next is the amount of time the processor has spent on the process. The last is the “human readable” name of the process.
Some other useful options include the following:
■ -u or --user (username or user ID) will show processes associated with the given user.
■ -C (command_name) will show processes that have the given command name.
■ p, -p, or -pid (process_id) will show processes with the listed process ID. If ps is only given a number as an argument, it assumes the number is a process ID.
■ -t, -tty (ttylist) will show processes on a given tty interface port. A plain “-” will show processes not associated with a port, and a -t without a tty number will assume you want the processes associated with the current port you are using.
KILL
kill will forcefully end a process using the process id, and killall will terminate a process by name instead of PID.
The
kill command works by sending a
signal to the process, by default the TERM signal. The syntax for signals is
kill -s [signal] PID and common signals are shown in
Table 5.4.
Table 5.4. Common Signals
|
Signal Name | Number | Description |
---|
Kill | 9 | Forcibly kill the process |
INT | 2 | Interrupt, like typing control-c |
TERM | 15 | Gracefully terminate a program |
HUP | 1 | Often used to make the program reread its configuration |
QUIT | 3 | Like TERM but can cause a program to copy the memory it was using to a diagnostic file |
Signals can also be used to communicate between processes, or by the kernel to report an event or problem back to a process.
TOP
A more versatile utility is
top, which dynamically shows running processes in real time, sorted by the amount of CPU time they are using shown in
Figure 5.2.
The upper section of the top display shows a summary of the system, including uptime, current users (each bash session counts as a user), and various load and memory usage statistics. The lower part of the display shows the process ID, owner, amounts of various memory, and CPU resources being consumed by each process. One handy feature is a built-in kill option; to terminate a process, just press k and type in the process ID.
PSTREE
pstree shows the relationship between parent and child processes in a hierarchical tree view and pstree -p will also show process IDs. The init process is the parent of all the other processes and always has a PID of 1. Each child process knows its parent process ID (PPID), which is what pstree uses to trace the relationship between processes. PPID numbers can be viewed with the ps -Al command.
To look at not just CPU but also physical and network drive usage you can use the
iostat command as shown in
Figure 5.3.
The various CPU-related statistics shown at the top of the report are as follows:
■ %user Percentage of processor time spent on user applications
■ %nice Percentage of processor time spent on user applications running at a lowered priority
■ %system Portion of processor time used by the system
■ %iowait Percentage of time the processor has been idle when the IO system has been busy
■ %steal Related to virtualized systems
The next section of the report shows statistics for drives attached to the system, and include the following:
■ tps (transfers per second) A transfer is considered a request for data
■ Blk_read/s, Blk_wrtn/s Blocks read and written per second
■ Blk_read, Blk_wrtn Total blocks read and written
Nice
A process has a priority value between –20 and +19 called a niceness value. The lower the value, the faster it runs, and niceness is inherited from the parent process. The ps -Al will show niceness in the NI column. The owner of a process can make it run slower, freeing up resources, and only a system administrator can make it run faster. There are two ways to adjust the niceness of a file: nice and renice. To start a program with an adjusted nice value, type the following:
nice -n 10 find/-name my_file.doc
Once the process is running it can be reset to a specific value with root access and renice like this:
Leveraging I/O Redirection
There are a number of small useful tools that are meant to work together and I/O redirection is the glue that sticks these handy little programs together. These console programs make use of three
streams for communicating, as shown in
Table 5.5.
Table 5.5. The Three Communications Streams
|
Name | Number | Symbol | Normal Connection |
---|
STDIN | 0 | < | Keyboard |
STDOUT | 1 | > | Console |
STDERR | 2 | > | Console |
You can use file
redirection that allows you to use the output from one program as the input for another, or send it to a file. You can use the contents of a file as the input for a program, or use a succession of programs to create and manipulate information. Common uses of redirection are shown in
Table 5.6.
Table 5.6. Common Uses of Redirection
|
Command | Result |
---|
some_program > not_the_console | Output is directed to not_the_console, which can be a file or a device |
some_program < something_to_send_to_a_program | Input for some_program or file or device |
some_program > data_output.txt 2> error_output.txt | Output sent to data_output.txt and errors sent to error_output.txt |
some_program 2> error_output.txt | Errors sent to error_output.txt |
some_program > data_and_errors.txt 2>&1 or some_program &> data_and_errors.txt | Data and error streams are combined |
some_command | another_command | Sends output from one program into the input of another |
ls -al | tee directory.txt | tee duplicates a stream and sends one copy to the display and another to a file |
Exam WarningBe careful when using >, if you redirect your output stream to an existing file it will be overwritten. Use >> to append to the end of an existing file.
Some commands will only accept a certain amount of input at a time, and the solution to this is to use xargs, which will accept the input stream and split it into chunks to pass along a bit at a time and repeatedly running the downstream command until the input stream ends. The following example will move all of Bob's music files into his music directory:
find . -name “*.mp3” -u bob | xargs -i mv {} ./bobs_mp3s
If you put a “;” between two commands, they will run consecutively:
do_this_first; then_do_this; and_finally_this
The “= =” is used to compare the equivalence of two variables, often used in scripting.