Yet another Mac OS X command line introduction (Part 3)
By Mark Dalrymple, August 5, 2002
No matter where you go, there you are
Feedback Farm
Have something to say about this article? Let us know below and your post might be the Post of the Month!
Okay, getting back to finding our disk pigs. We now know that ClipArt and downloads are the big consumers in my home directory. I don’t think I’ll do anything about ClipArt (gotta have my clip art collection), but I think I can trim out stuff from downloads.
The shell has a concept of “current working directory,” that is,
which directory (folder) that commands will currently work upon. For
example, if I
run the ls
(list files) in my Home,
I’ll get something like:
% ls ClipArt Documents Movies Pictures Sites Desktop Library Music Public downloads
You can decorate the command also:
% ls -F ClipArt/ Documents/ Movies/ Pictures/ Sites/ Desktop/ Library/ Music/ Public/ downloads/
... which shows the types of special kinds of files, such as
/
for directories, and @
for
symbolic links (similar to aliases in the Finder). Notice the slashes
at the end of the file names – that means my home directory is nothing
but directories.
So, now we want to run the du -sk
command on the contents
of downloads. A way to do that is to change your current
working directory to downloads. Here I’ll show the full prompt since it
will change when you run the command:
[localhost:~] bork% cd downloads
No real output. But the prompt has changed:
[localhost:~/downloads] bork%
Notice that ’downloads’ is now in the prompt. Exciting. If you want
to find out what your current working directory is, you can use the
pwd
(print working directory)
% pwd /Users/bork/downloads
So, you can change directories and re-run the disk space pipeline. Assuming we’re starting from the home directory:
% cd downloads
% du -sk * | sort -n | tail -5
23460 coldstone_demo.sit
37020 postgresql-7.2.1.tar
47880 aolserver-src-ad13
79460 emacs-41
193544 postgresql-7.2.1
So, it looks like the biggest culprit (weighing in at 193MB) is the build directory for PostgreSQL (a pretty neat open source relational database system), the build directory for emacs, the build directory for a Web server, the source tarball for Postgres, and a demo of the Ambrosia ColdStone game building system. I can now clean out stuff I don’t need or compress things for later use. Just for fun, I’m going to go into postgresql-7.2.1 and see what the real pig is there:
% cd postgresql-7.2.1 % du -sk * | sort -n | tail -5 156 config 252 configure 3216 contrib 8388 doc 181236 src
You can use the cd
command without any arguments to take
you directly to your home directory:
% cd % pwd /Users/bork
The point of the whole exercise? I now know to look into src for stuff to remove.
Note that the shell offers completion – you type in part of a path name,
press tab
, and the shell will expand the name as
far as it can. For instance, if I wanted to cd
into Library, I would do:
% cd L(tab)
and the shell would automatically fill it out to be Library. This really cuts down on the amount of typing necessary. If the shell can’t unambiguously complete a name it’ll show you the possible completions:
% cd M(tab) Movies/ Music/
Dealing with a lot of output
Sometimes you’re going to execute some command that will generate a lot of output, scrolling by faster than you can possibly read it (even after taking the Evelyn Wood speed reading course). For example, try doing a single-column directory listing of /usr/bin, where many of the command-line programs live:
% ls -1 /usr/bin CFInfoPlistConverter a2p addftinfo addr aexml ... zip zmore znew zprint
... for a total of 449 lines of output. Note that I didn’t have to count
that myself, I used the wc -l
(word
ccount, but
count lines) in a pipeline with the above ls
command,
thusly:
% ls -1 /usr/bin | wc -l 449
There’s a number of things you can do about that. You can use the
previously mentioned tail
to see the last couple of
lines, or its companion command head
(which returns
just the beginning lines). You can also go backwards in the
terminal window using the scroll bar.
If you’re looking for something
specific in the middle of all your output, those won’t save you much time.
If you want to slow down the output so you can read
every line or page, there is more
. Put that in a
pipeline and if the output exceeds a windowful of text,
more
will prompt you “more”. Pressing
return
will move you forward a line, and pressing
space
will move you forward a windowful. So, try:
% ls -1 /usr/bin | more
... and press the space bar and the Return key. When you’ve had enough,
pressing
q
will exit from more
.
Now wouldn’t it be cool if you could go backwards through the
pipeline’s output, and also search for stuff? more
is a pretty ancient program without too many features. Given the Unix
programmer’s penchant for wordplay, a new improved command was created
called less
. Doing something like:
% ls -1 /usr/bin | less
... will show you a windowful of text and give you a :
prompt. You can press Return
and
space
just like with more
, but
you can also do B
to page backwards. To
search, type a forward slash (/
) and the text you
want to search for, and type return
.
less
will scroll you to the first occurrence of
the text and will highlight your search term. Typing
n
will take you to the next occurence.
Run the above ls/less
pipeline, type the
/
, and enter mail
. The listing
should scroll down to fetchmail
, and highlight the
tail end of formail
. Press n
a
couple of time to see mail
,
mailq
, and mailstat
, and a
couple of others.