-->
Page 365
-r or --real | Prints the real ID instead of the effective ID |
-u or --user | Prints only the user ID |
Thus, groups is really just id -Gn and whoami is just id -un.
A related command is logname, used to determine the username that was used to log in. The logname command becomes useful if a user (usually the sysadmin) logs into the system as different people and uses su extensively.
Everyone should know how to check what system they are running. The command to use is uname, and it comes with a whole host of options. Usually, the most useful option is -a, which prints all the information uname knows about. On my computer, uname -a prints
Linux kanchi 2.0.29 #4 Sat Jul 19 10:36:15 PDT 1997 i586
This is a complete summary of my system and even includes the version of the kernel I am running and when I last complied it, along with my current architecture. Some of the other options for uname are
-m or --machine | Prints the machine (hardware) type |
-n or --nodename | Prints the machine's network node hostname (usually the same as hostname) |
-r or --release | Prints the operating system release |
-s or --sysname | Prints the operating system name |
-v | Prints the operating system version |
The environment is a list of variables and shell functions that the shell passes to every process that is started. It is important to know about the environment and manage it properly because it affects the execution of many programs.
The two main commands for getting information and controlling the environment are printenv and env. If invoked without arguments, both commands will print a list of all the current environment variables and shell functions.
The difference between the two is that env is used to manipulate the environment given to a process, and printenv is used to simply list information about the environment.
The primary use of env is to start a subshell or process in a clean environment when the current environment needs to be preserved. For example,
env -i $0
Page 366
starts another shell in a clean environment. The env command can also be used to set the environment:
env DISPLAY=kanchi:0.1 netscape &
The preceding line starts up Netscape on my second display without affecting my environment. The options that env understands are as follows:
-u or --unset=NAME | Removes named variable from the environment |
-i or --ignore-environment | Starts with an empty environment |
The GNU text utilities contain programs that enable the easy manipulation of large amounts of text. The programs in this distribution are as follows:
cat | od |
cksum | paste |
comm | pr |
csplit | sort |
cut | split |
expand | sum |
fmt | tac |
fold | tail |
head | tr |
join | unexpand |
md5sum | uniq |
nl | wc |
What's a cat without a head and a tail? A very bad deal. Every cat should come with a head and a tail. Every GNU cat comes with them, so if you have one of those old system V cats, upgrade (it's easier than bathing your cat).
Seriously, there are certain times when it is nice to be able to output entire files at once, or to squeeze multiple blank lines in a file into one line, but most often more control over which lines are displayed is required. The head and tail commands provide this control.
The head command shows the first 10 (or n if -n is given) lines of its standard input. This is useful for viewing the tops of large README files, but its real power is in daily applications.
Take the following problem. Every day a list of the five largest mail spool files needs to be generated. What is the easiest solution? It's easier to see if the problem is broken down. First, to generate a list of the mail spool files and their sizes, use
Page 367
ls -1 /var/spool/mail
Next, to sort the list by size, give ls the -S (sort by size) option:
ls -1S /var/spool/mail
To get a list of the five largest mail spool files, pipe the output of ls into head -5:
ls -1S | head -5
On my system, I get this list:
root ranga vathsa amma anna
Say you also want the five oldest mail spool files. Start with ls -1 again, but this time give the -t (sort by last accessed time) option instead of the -S option:
ls -1t /var/spool/mail
To get the bottom five, use tail instead of head:
ls -1t /var/spool/mail | tail -5
On my system, I get (there are only five users on my system) this list:
anna root amma vathsa ranga
As a quick check with ls -l reveals, in this list the files are listed newest to oldest; to reverse the order, use tac (the reverse of cat):
ls -1t /var/spool/mail | tail -5 | tac
On my system, I get this list:
ranga vathsa amma root anna
There is one other handy feature of tail: the -f option, which allows for examining files while they are growing. Often I have to look at the log files generated by programs that I am debugging, but I don't want to wait for the program to finish, so I start the program and then tail -f the log file. Another common use for tail -f is
tail -f /var/log/httpd/access_log
which can be used to watch the HTTP requests made for their system.