![]() |
| |||||||
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Warning - It's Goodbye, not Au Revoir Unix assumes you know what you are doing. For example, if you remove a file, the file will be removed. It won't be placed in the trash, and you won't be asked for confirmation. The file will be removed just as you ordered. The file will never been seen again. Goodbye. (This assumes you have the necessary permissions to delete the file.) If you copy file-one to file-two, and file-two already exists, the copy of file-one will overwrite the old file-two. Gone. Well, why not? You did know what you were doing. You did know that file-two already existed. Didn't you? The same goes for moving. Moving file-one to file-two will overwrite any existing file-two. The old file-two will be gone. If this behaviour makes you a little nervous, the option '-i' may help calm you. Create two files with: % touch x y Now, try to rename 'x' to 'y', with and without option '-i' % mv -i x y The first version will prompt you asking if you really want to overwrite y. The second won't. (The second will fail if you have allowed the first to move x.) And again for copy: % touch x y Again, the first version will prompt for confirmation. And for a safe removal: % rm -i x y In all the above examples, option '-i' specifies interactive mode. Viewing Files How does one examine a file? Using a text editor such as 'pico', 'emacs', or 'vi' is one option. Listing to the Terminal window is a second. I will cover text editors in a later tutorial, and concentrate on the latter option now. Firstly, create 'file-one' and display it. The simplest command is 'cat' which lists the contents of a file to the Terminal window: % man tcsh > file-one The file will whiz by, scrolling out of the window. You can view the file by pressing page up and page down (the keys below F15). Select 'Control' from the Terminal menu to see other keys for moving around. Here we are making use of the Terminal's scroll-back buffer. A more elegant method is to use a better viewer. % more file-one Will display the first screen-full of 'file-one'. On the bottom line is some inverse text giving the file name and percentage of the file displayed so far. 'more' understands many single-key commands to browse the file. return to display the next line 'more' does more than I have described, but let's concentrate on 'less', which actually does more than 'more'. At this point it's obligatory to say 'less is more'. 'less' allows one to scroll back and forth through a file, as well as providing a nicer search facility. % less file-one Try the following keys: return spacebar To search for some text within the file, for example 'history', type: /history (return) and hit 'n' to search for the next occurrence. Notice how each occurrence of 'history' is highlighted. This will remain so as you view more of the file. ?history will search backwards, with 'n' continuing the backward direction. Finally, hit 'q' to quit. 'less' does so much more than I have described above. As ever: % man less |
Tell Me More...
|
|
A Unix Trash If you wish to use the Finder's trash when deleting files you can simply move them to the trash using 'mv'. % mv file ~/.Trash/ The (hidden) trash directory is in your home directory, and is called '.Trash' 'file' will be deleted when the trash is emptied in the normal manner. Home Grown Trasher Issue this command (all on one line): % alias trash mv \!\* ~/.Trash/ Now, use your new trash command: % touch xxx and look in the trash using Unix or the Finder. 'alias' is a form of text substitution. When you type 'trash' on the command line it will be substituted with mv parameters ~/.Trash where 'parameters' is whatever you typed after 'trash'. I will not explain aliases in depth until a later tutorial. Note Make sure you never accidentally create a file called '.Trash'. It must always be a directory. Cat and Mouse 'cat' is not a Unix command to scare the mouse. It is short for concatenate and will join two or more files, writing the results to the Terminal. % cat file-one file-two 'man' to 'more' The 'man' command displays its output using 'more'. So all the commands that work with 'more' can be applied to 'man'. I prefer 'man' to use 'less' instead. This can be achieved in two ways. As a temporary solution: % man tcsh | less To make 'less' the default viewer for the duration of the Terminal session: % setenv PAGER less Now use the key-strokes you would for 'less'. 'setenv' is a command that affects your environment, in this case by setting 'PAGER' to 'less'. 'man' looks at 'PAGER' to see if a different viewer has been specified. vi If you are familiar with the 'vi' editor, you will already know most of the commands that 'less' will accept. There is much common ground between the two, and in fact many other Unix tools too. |
As I mentioned previously, every command you type into the Terminal is saved in a history file called '~/.tcsh_history'. To view the file type:
% less ~/.tcsh_history
then 'q' to quit out of less.
Note that the first time you use the Terminal '.tcsh_history' will not exist. Exiting from the Terminal will cause the file to be created as the command line history is saved.
The history can also be displayed using the 'history' command:
% history
You can make great use of the history by recalling a previous command and editing it. Hit the up-arrow key, and the command line will show your last command - probably 'history'. Each up-arrow press recalls the previous command, moving back through the command history. Down-arrow will move forward through the command history. A command can be edited using the left-arrow, right-arrow, and back-delete keys. Play around with this to edit and re-issue previous commands.
This is going to save you so much typing!
You may have noticed that each command in the history is numbered. To repeat a previous command (say number 123) simply issue the command:
% !123
You will not see the numbers when viewing with 'less', only by issuing the 'history' command.
Customising Your Command Line Environment
This section is rather advanced, and you may not understand exactly what is going on. Later tutorials will cover such topics in more depth. For now, I present it merely as an exercise for you to try, and not as a full tutorial. You may skip it if you wish.
There is a hidden file in your home directory called '.tcshrc'. This is a configuration file used by the command line and is read each time a new Terminal is started up.
% cat ~/.tcshrc
will view it. You probably don't have one yet. We can make a new one, or add to the end of an existing one, to customise your command line experience.
If you would like the '-i' option permanently applied to cp, rm, and mv, type the following:
% echo "alias cp cp -i \\!\*" >> ~/.tcshrc
% echo "alias mv mv -i \\!\*" >> ~/.tcshrc
% echo "alias rm rm -i \\!\*" >> ~/.tcshrc
'echo' simply echoes what appears between the quotes. '>> ~/.tcshrc' says to add it to the end of the given file instead of echoing to the Terminal. We are adding lines to the '.tcshrc' file. Each is an alias that tells the command line to substitute 'cp', 'mv', and 'rm' with the text that follows.
Start a new Terminal by pressing cmd-N. When a new Terminal starts it reads the commands in '~/.tcshrc', in this case the three aliases. Now all three commands will be replaced with the '-i' versions given in the aliases, and thus always ask for confirmation before a file is overwritten or deleted.
% touch x y z
% rm x y z
If you wish to temporarily override '-i' when issuing a command, use option '-f'.
% rm -f x y z
If you would like the 'trasher' command described above, type:
% echo "alias trash mv \\!\* ~/.Trash" >> ~/.tcshrc
And if you would like 'less' to be the viewer 'man' uses, type:
% echo "setenv PAGER less" >> ~/.tcshrc
Now start a new Terminal window with cmd-N.
Alternatively, you can edit the '.tcshrc' file with 'pico' if you are familiar with it. In this case, add the following lines to the (new) file:
alias cp cp -i \!\*
alias mv mv -i \!\*
alias rm rm -i \!\*
setenv PAGER less
alias trash mv \!\* ~/.Trash
You can reverse this by removing the lines from the file with an editor, or deleting the file if you never had it in the first palace.
In Part 3
In the next part to this column I will cover Unix commands to create and delete directories, wildcards, and recursion.
In the meantime, try this:
% cd ~
% pushd /
% pwd
% pushd /Library
% pwd
% dirs
% popd
% pwd
% dirs
% popd
% pwd
% dirs
When you wish to finish working at the command line close the Terminal window by typing:
% logout
Enjoy :-)
Discuss this article in the Learning Center forum
|
|
Part 2 - Playing With Files (page 2 of 2) |
|
| Copyright © 2000-2010 Inside Mac Media, Inc. All rights reserved. | ||
| Apple assumes no responsibility with regard to the selection, performance, or use of the products or services. All understandings, agreements, or warranties, if any, take place directly between the vendors and prospective users. | ||
| Apple, the Apple logo, Mac, PowerMac G4, PowerMac G5, Xserve, Xserve RAID, PowerBook, iBook, Airport, AirPort Extreme, iMac, eMac, iLife, iMovie, iCal, iPhoto, iTunes, QuickTime, FireWire, iPod, iSight, AppleWorks, Macintosh, Jaguar, Panther, Mac OS, Mac OS X and Mac OS X Server are trademarks of Apple Computer, Inc. |