Xref: utzoo comp.unix.questions:15318 comp.unix.wizards:17446 Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!iuvax!sahayman From: sahayman@iuvax.cs.indiana.edu (Steve Hayman) Newsgroups: comp.unix.questions,comp.unix.wizards Subject: Frequently Asked Questions about Unix - with Answers [Monthly posting] Message-ID: Date: 1 Aug 89 19:45:30 GMT Expires: 1 Sep 89 05:00:00 GMT Followup-To: comp.unix.questions Organization: Computer Science Department, Indiana University Lines: 478 Supersedes: 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? How do I truncate a file? What does {awk,grep,fgrep,egrep,biff,cat,gecos,nroff,troff,tee,bss} 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@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% "' Some older csh's get the meaning of && and || reversed. Try doing: false && echo bug If it prints "bug", you need to switch && and || (and get a better version of csh.) 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"} <&/dev/null /dev/null 2>&1