Useful UNIX commands

Back To Mike's WebToys Page!

UNIX is a neat OS. Very powerful, with great shells and superb editors. And it's built on the idea that everything can read something from a file, and write it's results out to another file. This 'file' interface is then exploited by the OS to connect one program to another. While one program thinks it's writing it's output to a file, the OS actually reroutes that output to be the input of another program. Very nice indeed.

In geek-speak, each program can reads from "standard in" (stdin) and writes to "standard out" (stdout). Thus, the output of one command can be piped to another, to another, etc. Note that not all programs choose to read/write stdin/stdout, though. Those that can are marked with "I/O", those that can only read input are marked I, those taht can only print output are marked O.

The difficulty with this is that it's hard to find programs that do what you want. Even if you browse the directories containing these programs, their cryptic names (and lack of good documentation) make it hard to find what you're looking for. The following is a concise list of commands that I've found to be useful


Commands By Category

Programatic/Shell Scripting
set * foreach
Environment
setenv

Commands Alphabetically

O apropos subject
Looks for entries in the system's manual pages that contain subject. Pretty much the same as man -k subject
I/O cat inFilename
I/O cat > outFilename
cat reads the contents of inFilename, and prints the output onto standard out. If inFilename is ommitted, it reads from standard in and prints to standard out. Can be used with file redirection to print something to file outFilename
- cd
Change directory
- complete
Used to set tabcompletions for your shell
I/O cut -fN -d':'
Suppose you have a file that that data in columns. There are 5 columns per line, with colon characters inbetween each column. Suppose you want to pipe each value in the third column to another file. Use cut to grab column N, where the the columns are separated (delimited) by the character ':'.
- df
Disk space free: reports free disk space. df . reports free space for this partition, while sometimes you need to use the -K option to get the size in K, rather than blocks
- dos2unix <filename>
Converts a text file from Windows (CR-LF line endings) to Unix (LF only).  CR is often displayed as "^M" in Unix, which is the tip-off that a text file (including shell scripts) need to be converted
- du DirOrFileName
Reports Ddisk Usage. Might be in blocks (not K), so watch out.
- foreach
foreach VarName VarName ( <list&qt; )
...
end
Set VarName to the first value in the list, execute the ..., then set VarName to the second item in the list, execute ..., until all items in the list have been executed. Note that this is useful for doing stuff like
foreach file ( `ls *` ) .... end
I/O grep pattern files
Grep looks for, and prints out, all lines that contain pattern, in the files listed in files. E.G. grep money ~/*
I/O gzip
Compress a file
I/O gunzip[-c] inFilename.Z
Replaces the compressed file inFilename.gz with the compressed file inFilename. The -c option leaves file inFilename.gz untouched, and prints the uncompressed file onto standard out. See also zcat
I/O head -n N inFilename
Prints the first N lines of inFilename to standard out. See also tail
O id
Prints your uid (user id), gid (group id), and groups to standard out. See also whoami
I less
A text-file viewer with a vi-type syntax. Can scroll forwards and backwards, unlike, say, man
O man topic [-k keyword]
Looks in the manual for a topic. Optionally, looks in the systems manual pages for anything that contains keyword. -k is pretty much the same as the apropos command
- more
Same idea as less
I/O set VarName = value
Sets the value of a variable. This variable isn't visible to any programs that are spawned by the shell. Thus, any variable created can be used by the shell (for example, in a shell script), but any programs you launch won't be able to use it.
I/O setenv VarName Value
Sets the value of an environment variable, creating it if it didn't exist before. Note that environment variables can be 'seen' by any programs/scripts/etc that you launch, unlike variables that you've merely set. Example:
setenv DISPLAY mpanitz.bell-labs.com:0.0
I/O sort
Sorts incoming lines lexicographically (alphabetically), and prints the sorted output to standard out
I/O tail -n N inFilename
Prints the last N lines of inFilename to standard out. See also head
I/O tee outFilename
Prints the text from standard in to standard out, and "tee's" off a copy into oufFilename. Good for debugging a pipeline
- time
Executes a command and then prints out the time used executing it. This is tricky, because it's a built-in c-shell (csh, tcsh) command. It's better to use /usr/bin/time, which is a regular commmand.
I/O uniq
Eliminates adjacent, identical lines in a file.
I/O wc
The word count command counts newline characters (end-of-line characters), as well as words and bytes.
- where command
Prints the fully qualified path to the first instance of command that's found. Good for making sure you're using the command you think you are. Not found on all systems. See also which
- whoami
Prints your uid (user id) to standard out. See also id
- which command
Prints the fully qualified path to the first instance of command that's found. Good for making sure you're using the command you think you are. Found more often than the where command, above
I/O zcat inFilename.gz
Reads the file inFilename.gz, uncompresses it, and prints the uncompressed text to standard out. Doesn't affect inFilename.gz. Identical to gunzip -c inFilename.gz

This page was put together by Mike Panitz