|
Search |
|
|
|
Classic 2 Guys |
|
10 Random Stories:
|
|
|
|
Don't Be Afraid of Unix - Part 2 |
|
In my last article (Don't Be Afraid of Unix – February 2005) I introduced you to the terminal and some commands to explore what was there and move around the file system a bit. In this article we are going to dig a little deeper and see if we can't do something useful.
First we need to cut through some terminology. Computers need a way to move data around. You can move data from file to file, from the keyboard to files, etc. In Unix speak this is called input and output or I/O.
One of the powerful features of the Unix terminal is that I/O can be redirected. That is, you can force the data to go where you want it. This is made possible by an underlying technology built into the terminal called file descriptors.
There are three basic types of file descriptors in Unix and Mac OS X:
0: Standard In – typically the keyboard
1: Standard Out – typically the terminal window
2: Standard Error – typically the terminal window
If you look carefully, both standard out and standard error are typically both written to the terminal window. Truth be told, if you are looking at the output of a command, you would not see anything different between error messages and data output. We'll get into the reasoning behind why they are treated separate in a minute. Also, pay attention to my numbering scheme, I didn't pick them arbitrarily.
The first concept we are going to explore is redirecting standard output. This can be a very helpful excercise. Say you have a CD with a bunch of photos on it. Your relatives want some copies, but they don't want them all. It would be very convenient to send them a list of the files as a text document so that they could delete the ones they don't want and send the 'order' back to you.
How would you do this? Well, open the terminal and browse to the CD in question (I am assuming that the CD is mounted here).
I want to take a slight detour off topic here to mention one quick thing. In my example, you will notice single quotes around the directory 'My Disc'. The reason for this is that the Unix command line doesn't like spaces in file names. If you tried to type cd /Volumes/My Disc/desktops you would get an error.
When I type ls, you will see a list of all of the files in the desktops directory. How do I get that information into a text file? Very simply it turns out.
The important thing to notice about the above command is the '>' operator. This is what happens when the above command is executed.
The ls command outputs to standard out a list of files and directories within a directory. The above command takes the output of ls and instead of putting it to standard out (the terminal) the '>' operator redirects it to a file called desktops.txt .
Now if you change directories into your home directory and again run the ls command, you will see a file called desktops.txt there. If you open the file in text edit you will see that it is an alphabetical list of the files in the desktops directory on the CD-ROM.
The next problem is if I have more than one directory to look in. If I run the command redirecting the output to the same file the data that was there will be overwritten. Not a problem for Unix. If you want to append the information to the end of a file then use the operator '>>'. Try this in the terminal:
If you examine the file desktops.txt in text editor now, you will see a line that gives the date and time at the top of the file and the data follows.
Now you can probably see why Unix has standard out and standard error separated. It is sometimes convenient to send the error output to one file and the data to a second file. If you wanted to do this, you would use the command structure below:
(command > out_file)>& error_file
*Note that you must include the parentheses.
You can also redirect the input to a command. For example, take our desktops.txt file. Say you wanted all of the names to be in all caps. You could use the tr command to accomplish this task. You would type the command as the following, using the '<' to say that you want to redirect the indicated file to the input of the command.
Now if you look at desktops.txt in TextEdit, everything will be in capitals.
The next thing we are going to investigate are one of the most powerful tools in Unix, Pipes. Pipes are just what the name suggests, conduits. They allow you to hook the output of one command to the input of another command without creating any intermediary files. The structure looks something like this:
command 1 source file | command2 | command3 > out_file
The above statement reads – command one operates on source file. Pipe the standard out of command one to the standard in of command 2. Pipe the standard out of command 2 to the standard in of command 3. Redirect the standard out of command 3 to out_file. It sounds complicated, but it really isn't.
Take this simple example. Many times while in the terminal, when you have many items in a directory, when you type ls the contents scroll off of the page before you can read the results. To help, there is a Unix utility called a pager. It will stop the output one page at a time so that you can see the result. Find a directory on your machine with a lot of elements in it. Type ls and watch the contents scroll up and off the page. Now type the following:
ls | more
The pipe operator is shift-back slash on most keyboards. The results should look something like this:
Simply press the space bar to move to the next 'page'.
Another example would be if you wanted to count the pictures from the first example. You could do this with the following commands:
The command above takes the output of the ls command and pipes it to the input of the wc (word count). The -l command switch to wc tells the program word count to count the lines. You would want to do this in case your file names had spaces. Word count would count them as 2 words. This would not help you if you wanted to know how many files you had. The ls command will only output 1 file per line, so telling wc to count the number of lines will give you the number of files in the directory.
The command line is now starting to strut its stuff. I encourage you to play around with it. Create some dummy files and see what kind of damage you can do. Don't forget to look through the man pages to see what other commands you can chain together with pipes. |
|
March 10 2005, 10:14 AM EDT, by |
Comments:
|
whatsinaname9000 |
3/10/05, 7:52 PM EDT |
Wow, great article. Before, i'd just used terminal's emacs command to play dumb games. :)
|
nhmacusr |
3/10/05, 8:51 PM EDT |
Tetris does rock!
|
the_strategy_freak |
3/12/05, 7:24 PM EDT |
"Wow, great article. Before, i'd just used terminal's emacs command to play dumb games. :)"
I did for a while too but then I learned some commands like ls that you can use to do interesting stuff without the GUI. I wanna do something that actually does something cool though, like how do you 'hack' something. I guess that's mainly changing data files but I dunno...
hint: at the login screen type ">console".
|
un1xid10t |
3/13/05, 8:08 AM EDT |
U mean I can use emacs on my iMacs to play games! Hot damn!! Do eMacs also have emacs so all those school kiddies could play too? ;)
|
This article is archived, so you may not comment on it.
(The good news is there's always the shoutbox, the forums or the contact form if you're socially-inclined at the moment!)
|
|
|
|
Site Links |
|
|
|
Deep Thoughts |
|
I have to laugh when I think of the first cigar, because it was probably just a bunch of rolled-up tobacco leaves.
|
|
Around Da Web |
|
iProng: |
iPhone steals show at CTIA Wireless 2007
|
DLO offers dual cover fashion case for iPod
|
AT&T received 1M inquiries on iPhone
|
MacDailyNews: |
Ars Technica in-depth review: Apple TV ?impressed all those who touched it?
|
Inside Apple?s Mac OS X 10.5 Leopard Server OS
|
The chips inside Apple TV
|
Think Secret: |
Adobe Creative Suite 3 pricing revealed
|
|
|
We Like: |
|
|
|
Side Projects |
|
Jonahan
- JediPoker.net
- Jonahan.com
- iProng
- MacProng
iKen
Jedbeck
J.P.
|
|