Frequently Asked Questions about Unix - with Answers. [Monthly posting]
Steve Hayman
sahayman at iuvax.cs.indiana.edu
Sun Jul 2 04:47:50 AEST 1989
This article contains the answers to some Frequently Asked Questions
often seen in comp.unix.questions and comp.unix.wizards.. Please don't
ask these questions again, they've been answered plenty of times
already - and please don't flame someone just because they may not have
read this particular posting. Thank you.
This article includes answers to:
How do I remove a file whose name begins with a "-" ?
How do I remove a file with funny characters in the filename ?
How do I get a recursive directory listing?
How do I get the current directory into my prompt?
How do I read characters from a terminal without requiring the user
to hit RETURN?
How do I read characters from the terminal in a shell script?
How do I check to see if there are characters to be read without
actually reading?
How do I find the name of an open file?
How do I rename "*.foo" to "*.bar"?
Why do I get [some strange error message] when I "rsh host command" ?
How do I find out the creation time of a file?
How do I use "rsh" without having the rsh hang around
until the remote command has completed?
What does {awk,grep,fgrep,egrep,biff,cat} stand for?
While these are all legitimate questions, they seem to crop up in
comp.unix.questions on an annual basis, usually followed by plenty
of replies (only some of which are correct) and then a period of
griping about how the same questions keep coming up. You may also like
to read the monthly article "Answers to Frequently Asked Questions"
in the newsgroup "news.announce.newusers", which will tell you what
"UNIX" stands for.
With the variety of Unix systems in the world, it's hard to guarantee
that these answers will work everywhere. Read your local manual pages
before trying anything suggested here. If you have suggestions or
corrections for any of these answers, please send them to to
sahayman at iuvax.cs.indiana.edu or iuvax!sahayman.
1) How do I remove a file whose name begins with a "-" ?
Figure out some way to name the file so that it doesn't
begin with a dash. The simplest answer is to use
rm ./-filename
(assuming "-filename" is in the current directory, of course.)
This method of avoiding the interpretation of the "-" works
with other commands too.
Many commands, particularly those that have been written to use
the "getopt(3)" argument parsing routine, accept a "--" argument
which means "this is the last option, anything after this is not
an option", so your version of rm might handle "rm -- -filename".
Some versions of rm that don't use getopt() treat a single "-"
in the same way, so you can also try "rm - -filename".
2) How do I remove a file with funny characters in the filename ?
The classic answers are
rm -i some*pattern*that*matches*only*the*file*you*want
which asks you whether you want to remove each file matching
the indicated pattern; depending on your shell, this may
not work if the filename has a character with the 8th bit set
(the shell may strip that off);
and
rm -ri .
which asks you whether to remove each file in the directory,
answer "y" to the problem file and "n" to everything else.,
and which, unfortunately, doesn't work with many versions of rm;
(always take a deep breath and think about what you're doing
and double check what you typed when you use rm's "-r" flag)
and
find . -type f ... -ok rm '{}' \;
where "..." is a group of predicates that uniquely identify the
file. One possibility is to figure out the inode number
of the problem file (use "ls -i .") and then use
find . -inum 12345 -ok rm '{}' \;
or
find . -inum 12345 -ok mv '{}' new-file-name \;
"-ok" is a safety check - it will prompt you for confirmation of the
command it's about to execute. You can use "-exec" instead to avoid
the prompting, if you want to live dangerously, or if you suspect
that the filename may contain a funny character sequence that will mess
up your screen when printed.
If none of these work, find your system manager.
3) How do I get a recursive directory listing?
One of the following may do what you want:
ls -R (not all versions of "ls" have -R)
find . -print (should work everywhere)
If you're looking for a wildcard pattern that will match
all ".c" files in this directory and below, you won't find one,
but you can use
% some-command `find . -name '*.c' -print`
"find" is a powerful program. Learn about it.
4) How do I get the current directory into my prompt?
It depends which shell you are using. It's easy with some shells,
hard or impossible with others.
C Shell (csh):
Put this in your .cshrc - customize the prompt variable
the way you want.
alias cd 'chdir \!* && set prompt="${cwd}% "'
If you use pushd and popd, you'll also need
alias pushd 'pushd \!* && set prompt="${cwd}% "'
alias popd 'popd \!* && set prompt="${cwd}% "'
Some C shells don't keep a $cwd variable - you can use
`pwd` instead.
If you just want the last component of the current directory
in your prompt ("mail% " instead of "/usr/spool/mail% ")
you can do
alias cd 'chdir \!* && set prompt="$cwd:t% "'
Bourne Shell (sh):
If you have a newer version of the Bourne Shell (SVR2 or newer)
you can use a shell function to make your own command, "xcd" say:
xcd { cd $* ; PS1="`pwd` $ "; }
If you have an older Bourne shell, it's complicated but not impossible.
Here's one way. Add this to your .profile file:
LOGIN_SHELL=$$ export LOGIN_SHELL
CMDFILE=/tmp/cd.$$ export CMDFILE
PROMPTSIG=16 export PROMPTSIG
trap '. $CMDFILE' $PROMPTSIG
and then put this executable script (without the indentation!),
let's call it "xcd", somewhere in your PATH
: xcd directory - change directory and set prompt
: by signalling the login shell to read a command file
cat >${CMDFILE?"not set"} <<EOF
cd $1
PS1="\`pwd\`$ "
EOF
kill -$PROMPTSIG ${LOGIN_SHELL?"not set"}
Now change directories with "xcd /some/dir".
Korn Shell (ksh):
Put this in your .profile file:
PS1='$PWD $ '
If you just want the last component of the directory, use
PS1='${PWD##*/} $ '
5) How do I read characters from a terminal without requiring the user
to hit RETURN?
Check out cbreak mode in BSD, ~ICANON mode in SysV.
If you don't want to tackle setting the terminal parameters
yourself (using the "ioctl(2)" system call) you can
let the stty program do the work - but this is inefficient,
and you should change the code to do it right some time:
main()
{
int c;
printf("Hit any character to continue\n");
system("/bin/stty cbreak");
c = getchar();
system("/bin/stty -cbreak");
printf("Thank you for typing %c.\n", c);
exit(0);
}
6) How do I read characters from the terminal in a shell script?
In sh, use read. It is most common to use a loop like
while read line
do
...
done
In csh, use $<.
7) How do I check to see if there are characters to be read without
actually reading?
Certain versions of UNIX provide ways to check whether characters
are currently available to be read from a file descriptor. In BSD,
you can use select(2). You can also use the FIONREAD ioctl (see
tty(4)), which returns the number of characters waiting to be read,
but only works on terminals and pipes. In System V Release 3, you
can use poll(2), but that only works on streams.
There is no way to check whether characters are available to be
read from a FILE pointer. (Well, there is no *good* way. You could
poke around inside stdio data structures to see if the input buffer
is nonempty but this is a bad idea, forget about it.)
Sometimes people ask this question with the intention of writing
if (characters available from fd)
read(fd, buf, sizeof buf);
in order to get the effect of a nonblocking read. This is not the
best way to do this, because it is possible that characters will
be available when you test for availability, but will no longer
be available when you call read. Instead, set the O_NDELAY flag
(which is also called FNDELAY under BSD) using the F_SETFL option
of fcntl(2). Older systems (Version 7, 4.1 BSD) don't have O_NDELAY;
on these systems the closest you can get to a nonblocking read is
to use alarm(2) to time out the read.
8) How do I find the name of an open file?
In general, this is too difficult. The file descriptor may
be attached to a pipe or pty, in which case it has no name.
It may be attached to a file that has been removed. It may
have multiple names, due to either hard or symbolic links.
If you really need to do this, and be sure you think long
and hard about it and have decided that you have no choice,
you can use find with the -inum and possibly -xdev option,
or you can use ncheck, or you can recreate the functionality
of one of these within your program. Just realize that
searching a 600 megabyte filesystem for a file that may not
even exist is going to take some time.
9) How do I rename "*.foo" to "*.bar"?
Why doesn't "mv *.foo *.bar" work? Well, the shell expands
*.foo and *.bar before the mv command ever sees the arguments,
so since there probably aren't any *.bar files already, mv will
be invoked as "mv a.foo b.foo c.foo" which doesn't do what you want..
Depending on your shell, you can do it with a loop to "mv" each
file individually. If your system has "basename", you can use:
C Shell:
foreach f ( *.foo )
set base=`basename $f .foo`
mv $f $base.bar
end
Bourne Shell:
for f in *.foo; do
base=`basename $f .foo`
mv $f $base.bar
done
Some shells have their own variable substitution features, so instead
of using "basename", you can use simpler loops like:
C Shell:
foreach f ( *.foo )
mv $f $f:r.bar
end
Korn Shell:
for f in *.foo; do
mv $f ${f%foo}.bar
done
If you don't have "basename" or want to do something like
renaming foo.* to bar.*, you can use something like "sed" to
strip apart the original file name in other ways, but
the general looping idea is the same.
10) Why do I get [some strange error message] when I "rsh host command" ?
If your remote account uses the C shell, the remote host will
fire up a C shell to execute 'command' for you, and that shell
will read your remote .cshrc file. Perhaps your .cshrc contains
a "stty", "biff" or some other command that isn't appropriate
for a non-interactive shell. The unexpected output or error
message from these commands can screw up your rsh in odd ways.
Fortunately, the fix is simple. There are, quite possibly, a whole
*bunch* of operations in your ".cshrc" (e.g., "set history=N") that are
simply not worth doing except in interactive shells. What you do is
surround them in your ".cshrc" with:
if ( $?prompt ) then
operations....
endif
and, since in a non-interactive shell "prompt" won't be set, the
operations in question will only be done in interactive shells.
You may also wish to move some commands to your .login file; if
those commands only need to be done when a login session starts up
(checking for new mail, unread news and so on) it's better
to have them in the .login file.
11) How do I find out the creation time of a file?
You can't - it isn't stored anywhere. Files have a last-modified
time (shown by "ls -l"), a last-accessed time (shown by "ls -lu")
and an inode change time (shown by "ls -lc"). The latter is often
referred to as the "creation time" - even in some man pages - but
that's wrong; it's the time the file's status was last changed,
either by writing or changing the inode (via mv or chmod, etc...).
The man page for "stat(2)" discusses this.
12) How do I use "rsh" without having the rsh hang around until the
remote command has completed?
The obvious answers fail:
rsh machine command &
or rsh machine 'command &'
The solution - if you use csh on the remote machine:
rsh machine -n 'command >&/dev/null </dev/null &' &
If you use sh on the remote machine:
rsh machine -n 'command >/dev/null 2>&1 </dev/null &' &
Why? "-n" attaches rsh's stdin to /dev/null so you can run it
in the background. Also, the input/output redirections ensure
that rsh thinks the session can be terminated (there's no
data flow any more.)
Various parts of these complicated commands aren't necessary in all cases.
13) What does {awk,grep,fgrep,egrep,biff,cat} stand for?
awk = "Aho Weinberger and Kernighan"
This language was named by its authors, Al Aho, Peter Weinberger and
Brian Kernighan.
grep = "Global Regular Expression Print"
grep comes from the ed command to print all lines matching a
certain pattern
g/re/p
where "re" is a "regular expression".
fgrep = "Fixed Grep".
fgrep searches for fixed strings only. The "f" does not
stand for "fast".
egrep = "Extended Grep"
egrep uses fancier regular expressions than grep.
cat = "catenate"
catenate is an obscure word meaning "to connect in a series".
Yes, "catenate" is a real word.
biff = "biff"
This command, which turns on asynchronous mail notification,
was allegedly named after someone's dog that barked whenever
the postman arrived. Or so the story goes.
--
Steve Hayman Workstation Manager Computer Science Department Indiana U.
sahayman at iuvax.cs.indiana.edu iuvax!sahayman (812) 855-6984
More information about the Comp.unix.questions
mailing list