Using the Shell The shell is your most powerful window into the world of a Unix system. Whether you're running it in an X Window System window or through a text-based dumb terminal, the shell gives you full access to a Unix system's many powerful command-line tools, configuration files, and devices.Code listing 1.1. Using the pwd command and PWD environment variable to find your current directory.
bender:~ chrish$ pwd /Users/chrish bender:~ chrish$ echo $PWD /Users/chrish
Moving around and seeing what's there When you first log in to a Unix system, whether it's via GUI, Telnet, or SSH connection, your current directory will be your account's home directory.To find your current directory Do either of the following: From your current directory, you can get back to your home directory.To get back home
Code listing 1.2. Getting back to your home directory using the HOME environment variable or the ~ shortcut.
bender:/tmp chrish$ pwd /tmp bender:/tmp chrish$ cd $HOME bender:~ chrish$ pwd /Users/chrish bender:~ chrish$ cd /etc bender:/etc chrish$ pwd /etc bender:/etc chrish$ cd ~ bender:~ chrish$ pwd /Users/chrish
Do either of the following:- cd $HOMEJust as the PWD environment variable always contains your current directory, the HOME environment variable always has your home directory inside.
- cd ~Want to get home with the least amount of typing? The ~ (tilde) shortcut also refers to your home directory. Unix developers love shortcuts (Code Listing 1.2)!
TipsSo now that we know where we are, what files are there? The ls command holds the answer. You have several options.To list the files in a directory Do any of the following:- lsThe ls command will show you which files are in your current directory (Code Listing 1.3):
bender:~ chrish$ ls Default Documents Movies Pictures Sites emacs-lisp Desktop Library Music Public daily-url108
- ls -FThe -F option adds / to the end of directory names, * to executables, and @ to symbolic links. This makes it easier to find specific things in the file system.
bender:~ chrish$ ls -F Default Documents/ Movies/ Pictures/ Sites/ emacs-lisp/ Desktop/ Library/ Music/ Public/ daily-url108
- ls -aUnix hides configuration files and directories by starting them with a . (period) character. These dotfiles are hidden by convention; Unix applications ignore them and usually only show them if you specifically ask them to. The ls command's -a option tells it to show all files, including the "hidden" dotfiles.
Code listing 1.3. Listing files with the ls command and its -F (add file-type decorations) and -a (show all files) options.
bender:~ chrish$ ls Default Documents Movies Pictures Sites emacs-lisp Desktop Library Music Public daily-url108 bender:~ chrish$ ls -F Default Documents/ Movies/ Pictures/ Sites/ emacs-lisp/ Desktop/ Library/ Music/ Public/ daily-url108 bender:~ chrish$ ls -a . .bash_history .lpoptions Desktop Public .. .cvspass .ncftp Documents Sites .CFUserTextEncoding .emacs .profile Library daily-url108 .DS_Store .emacs.d .ssh Movies emacs-lisp .MacOSX .irssi .viminfo Music .Trash .lftp Default Pictures bender:~ chrish$ ls -aF ./ .bash_history .lpoptions Desktop/ Public/ ../ .cvspass .ncftp/ Documents/ Sites/ .CFUserTextEncoding .emacs .profile Library/ daily-url108 .DS_Store .emacs.d/ .ssh/ Movies/ emacs-lisp/ .MacOSX/ .irssi/ .viminfo Music/ .Trash/ .lftp/ Default Pictures/
Now that we've found some files, we probably want to see what's in them. Again, there are several options.To look at a file's contents Do any of the following:- cat filenameNow that you've used your cd and ls expertise to find a file you're interested in, cat will print the contents of a file to the terminal (Code Listing 1.4).
- head filenameOnly want to see the beginning of the file? Use the head command instead (see Code Listing 1.4).
- tail filenameYou can also use the tail command (see Code Listing 1.4) to see just the end of a file.
- less filenameUsually you'll want to use the less command (Figure 1.1) so that you can interactively page through or search the file.
Figure 1.1. The less command lets you view a file one screen at a time.
![](/image/library/english/13776_01fig01.gif;380139) - more filenameIf less isn't available for some reason, its older, utilitarian brother more can help you out.
Tip- Sometimes if you cat a binary file (such as a program, an image, or a sound file), it'll screw up your terminal's settings. This could result in strange keyboard behavior, invisible text, and so on. To fix it, carefully type this command and press Enter:
stty sane
Code listing 1.4. Looking at a file's contents with cat, head, and tail.
chrish@taffer [508]: cat unix-advanced-outline.txt UNIX Advanced Visual QuickPro Guide - Outline Introduction - who this is for - what's in this book - how to use this book - requirements . . . - Apache forums, etc. - MySQL forums, etc. - PHP forums, etc. chrish@taffer [509]: head unix-advanced-outline.txt UNIX Advanced Visual QuickPro Guide - Outline Introduction - who this is for - what's in this book - how to use this book - requirements Chapter 1 - Installing UNIX - Which "UNIX"? - Fedora Core, FreeBSD, cygwin, MacOS X chrish@taffer [510]: tail unix-advanced-outline.txt Appendix A - Finding out more - how to find out more - man, info (and pinfo), Google, mailing lists, RFCs - Fedora Core forums, mailing lists, etc. - FreeBSD forums, mailing lists, etc. - cygwin forums, mailing lists, etc. - fink forums, mailing lists, etc. - system administration mailing lists (risks-l, CERT, etc.) - Apache forums, etc. - MySQL forums, etc. - PHP forums, etc.
Redirecting input and output Because so many Unix utilities operate on text files, it can be very useful to redirect a program's output to a file for later processing by another application. You can even use a file as input to a program.Code listing 1.5. Redirecting output to a file with > and input from a file with <.
chrish@taffer [506]: ls chrish@taffer [507]: ps > ps.txt chrish@taffer [508]: ls ps.txt chrish@taffer [509]: sort < ps.txt
> ps-sorted.txt chrish@taffer [510]: ls ps-sorted.txt ps.txt
To send a command's output to a file
Code listing 1.6. Using >> to append to an existing file.
chrish@taffer [513]: echo "hello"
> hello-there.txt chrish@taffer [514]: ls hello-there.txt chrish@taffer [515]: echo "there"
> hello-there.txt chrish@taffer [516]: cat
hello-there.txt there chrish@taffer [517]: echo "hello"
> hello-there.txt chrish@taffer [518]: echo "there"
>> hello-there.txt chrish@taffer [519]: cat
hello-there.txt hello there
To append a command's output to a file
To get a command's input from a file
Streams Unix has three standard text streams automatically associated with every program: stdin (standard input), stdout (standard output), and stderr (standard error).While programmers can refer to these streams by name, shell users have to refer to them by number: As we've seen, < redirects from stdin and > redirects the stdout stream, but what about stderr? That's an output stream for errors, but we can't redirect it with > because it's already taken by stdout.By specifying the stream with the redirection, we can send stderr to a file as well: chrish@taffer [511]: sort <
ps.txt
> ps-sorted.txt
2> sort-errors.txt
This sorts the contents of ps.txt and saves the output to ps-sorted.txt. Any error messages will go to sort-errors.txt.What if you wanted stdout and stderr in the same file? There's another special redirection for just such an occasion: chrish@taffer [523]: sort
< ps.txt
> ps-sorted.txt 2>&1
You can think of the & as tying the error stream to the output stream for this command. |
Plumbing
Code listing 1.7. Piping the output of one command into another lets you create your own powerful chain of tools.
bender:~ chrish$ ps PID TT STAT TIME COMMAND 1853 p1 S 0:00.42 -bash 2168 p1 S+ 0:00.65 ssh taffer 1875 std S 0:00.51 -bash bender:~ chrish$ ps | sort PID TT STAT TIME COMMAND 1853 p1 S 0:00.42 -bash 1875 std S 0:00.51 -bash 2168 p1 S+ 0:00.65 ssh taffer 2175 std R+ 0:00.00 sort
Being able to chain together a series of commands without having to use intermediate files is a very powerful feature. It goes hand-in-hand with another Unix design philosophy: providing a set of small, well-defined tools that work together.To use a program's output as input
tee for Two If you're nosy like me, you'd probably prefer that commands show progress while redirecting their output, especially when they take a long time to finish.Luckily, the tee command takes its input, saves it to a file, and then prints it to the terminal.For example, the following command, chrish@taffer [530]: ps |
tee ps.txt
will write the output of ps to ps.txt and also display it onscreen.This gets really handy when you're using tee to watch something that runs forever (like a Web server) or that might take a long time (like a program compile). |
|