Let's try this, say you want a directory listing of everything in your home folder. Well in Windows you would open up your My Documents . But that doesn't save it in file, it just shows it to you. What about the command prompt, Start->Run->cmd enter in "dir" and there it is. But that still isn't in a file; okay copy, open notepad, paste it in. Now you have your home folder listing in a file. Bash or csh or ksh is much easier!
cd ; ls -l >> myhome.txt
That's it. cd changes directory to your home, ls -l gives a directory listing in long format, >> is the redirect command. Redirect takes the output from a command and sends it somewhere else. Usually it's used to create log files, here it takes the output and saves it in a file. Sometimes output is sent to the null device /dev/null; this will just send it nowhere. Redirect can also work the other way as in sending an input file to a command for processing.
Here is a common command I use:
ps -ef | grep db2
Here I use the pipe. Pipes are used usually in a chain of commands, the output of one command is used as the input for the next command. In the example I take the output of the ps command, process, and send to grep, pattern searching, looking for anything containing db2. You can add any number of commands to chain as long as it makes sense:
tail -f `ls -tr *backup* | tail -1`
This chain will get the most recently modified file that has backup in the name. The back ticks will return the commands between them. In effect tail -f will wait for ls -tr *backup* | tail -1 to execute and use the results in the command. tail -1 returns the last line of ls -tr *backup*. Adding it up the command will continously display the contents of the latest backup file.
Normally ps returns process sorted by their id, suppose we want the only db2 process sorted by name saved in a file:
ps -ef | grep db2 | sort >> db2process.txt
Let's find out who has an account
cat /etc/passwd | cut -f 1 -d : | sort
cat just spills out the contents of a file, sends the output to cut field 1 delimited by ":" and then sends it sort.
Command chains can become quite cryptic and long:
cd dba ; tail `ls -tr *backup* | tail -1` | (grep Finished && ls -ltr *backup*) || (tail -f `ls -tr *backup* | tail -1`)
My favorite, because I wrote it, change to the dba directory and tail the most recent backup file looking for "Finished" then show the listing otherwise keep tailing the file.
More to follow...