Note: You are currently viewing my old web site. There is a new version with most of this content at OJB.NZ.
The new site is being updated, uses modern techniques, has higher quality media, and has a mobile-friendly version.
This old site will stay on-line for a while, but maybe not indefinitely. Please update your bookmarks. Thanks.


[Previous] [Next] [Index] [Menu] [Up] 2[Header]
Unix Tips

Next   Previous   (Up to OJB's Tips Page)


Doing Something Useful

Now that we know how to do basic stuff in the terminal, let's use it for something useful. Sometimes we want a list of what items are in a folder for reference later or for printing. We will list our documents folder into a file for later use. First, open a new terminal window. Choose New from the Shell menu in the Terminal program (a shell is an environment that accepts and executes your commands, you can have many shells running at once). Remember to switch back to the terminal before typing a command, to do every instruction exactly as listed below, type everything exactly (including upper/lower case) and to press return after each command...

Before you start, use the Finder to check that you don't have a file called List.txt in your Documents folder. If you do, please rename it or it will be deleted! Your working directory will be Home. First we will change to our Documents folder, so type cd Documents. Now we will list our files. Type ls -1 > List.txt. Now return to the Finder and look in the Documents folder. There will be a file called "List.txt" Double click on it to open it, then close it again. The "ls -1" part of the command creates a list one column wide of the working directory. The "> List.txt" part takes the output which would normally appear on the screen and sends it to the file List.txt.

Maybe we wanted to list the contents of the folders in the Documents folder as well. Type ls -1R > List.txt. This might take a while. On my machine it took 3 seconds to generate a file 22,000 lines long showing every file in my Documents folder! Note that the old List.txt was over-written without warning. Unix can be dangerous! The R option means do a recursive list which here has the effect of showing all the files in every folder.

Finally, let's be more specific. This time we will only look for files with "mac" in their name. Type ls -1R | grep mac > List.txt. Now type open List.txt. You should see the file displayed by the TextEdit program. Notice that the open command opens a file using a Mac (graphical) program. The original command now uses two powerful Unix concepts: re-direction and piping. First the ls command generates a list of items in the folders, the "|" pipes the output to the grep command which looks for the text "mac" and re-directs its output to the file we specified. When we use a pipe, the output from one program is fed to another program concurrently. You can use many pipes in one command - this is a very powerful function!

We're finished with our List.txt now so let's delete it. Close the file if its open in TextEdit and type rm -P List.txt. The "rm" means remove, the "-P" is an option that overwrites the file so it can never be recovered. If you have a confidential document you want really deleted you could delete it this way - but be careful, there's no second chance!


[Next] [Up]

[Contact][Server Blog][AntiMS Apple][Served on Mac]