Getting Started
If you’re running Mac OS X, or your favourite flavour Linux, you’re all set. Just fire up the terminal, and keep going.
1. Cat and Less
there’s a good change you’ll want to see the contents of a file from the terminal sooner or later. There’s a few commands that will do this for you. First is
cat
; cat
is short for “concatenate”, and this command does more than output file contents; however, that’s what we’ll look at here. It’s as simple as passing the command a file:
1
| $ cat shoppingList.txt |
However, if the file is large, the contents will all scroll past you and you’ll be left at the bottom. Granted, you can scroll back up, but that’s lame. How about using
less
?
1
| $ less shoppingList.txt |
Less is a much better way to inspect large files on the command line. You’ll get a screen-full of text at a time, but no more. You can move a line up or a line down with the
k
and j
respectively, and move a window up or down with b
and f
. You can search for a pattern by typing /pattern
. When you’re done, hit q
to exit the less
viewer.2. Man
Most of the commands you’ll use in a bash shell are pretty flexible, and have a lot of hidden talents. If you suspect a command might do what you want, or you just want to see some general instruction on using a command, it’s time to hit the manuals, or man pages, as they’re called. Just type
man
followed by the command you’re curious about.
1
| $ man ln |
You’ll notice that the man pages are opened in
less
.3. History, ,!"number", !!, and !$
Since the command line is all about efficiency, it’s supposed to be easy to repeat commands. There are a few ways to do this. First, you can use the history
command to get a numbered list of many of your recent commands. Then, to execute one of them, just type an exclamation mark and the history number.
Granted, this is a terrible example, because I’m typing more characters to use the history than it would take to re-type the command. But once you’re combining commands to create long strings, this will be faster.
It’s even quicker to access the last command and last argument you used. For the latest command, use !!
; the usual use case given for this is adding sudo to the front of a command. For the latest argument, use !$
; with this, moving into a new folder is probably the common example. In both these cases, the shell will print out the full command so you can see what you're really executing.
history
command to get a numbered list of many of your recent commands. Then, to execute one of them, just type an exclamation mark and the history number.!!
; the usual use case given for this is adding sudo to the front of a command. For the latest argument, use !$
; with this, moving into a new folder is probably the common example. In both these cases, the shell will print out the full command so you can see what you're really executing.
No comments:
Post a Comment