Google

Monday, January 14, 2008

Unix Question And Answers

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) du -a . (shows you both the name and size) 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.

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 setprompt 'set prompt="${cwd}% "' setprompt # to set the initial prompt alias cd 'chdir \!* && setprompt' If you use pushd and popd, you'll also need alias pushd 'pushd \!* && setprompt' alias popd 'popd \!* && setprompt' 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 use alias setprompt '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 # 16 is SIGURG, pick a signal that's not likely to be used 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"} <

Unix Question And Answers

How do I remove a file with funny characters in the filename ?
If the 'funny character' is a '/', skip to the last part of this answer. If the funny character is something else, such as a ' ' or control character or character with the 8th bit set, keep reading. 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. Unfortunately this doesn't work with many versions of rm. Also unfortunately, this will walk through every subdirectory of ".", so you might want to "chmod a-x" those directories temporarily to make them unsearchable. 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 or a wildcard on the command line; 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. What if the filename has a '/' in it? These files really are special cases, and can only be created by buggy kernel code (typically by implementations of NFS that don't filter out illegal characters in file names from remote machines.) The first thing to do is to try to understand exactly why this problem is so strange. Recall that Unix directories are simply pairs of filenames and inode numbers. A directory essentially contains information like this: filename inode file1 12345 file2.c 12349 file3 12347 Theoretically, '/' and '\0' are the only two characters that cannot appear in a filename - '/' because it's used to separate directories and files, and '\0' because it terminates a filename. Unfortunately some implementations of NFS will blithely create filenames with embedded slashes in response to requests from remote machines. For instance, this could happen when someone on a Mac or other non-Unix machine decides to create a remote NFS file on your Unix machine with the date in the filename. Your Unix directory then has this in it: filename inode 91/02/07 12357 No amount of messing around with 'find' or 'rm' as described above will delete this file, since those utilities and all other Unix programs, are forced to interpret the '/' in the normal way. Any ordinary program will eventually try to do unlink("91/02/07"), which as far as the kernel is concerned means "unlink the file 07 in the subdirectory 02 of directory 91", but that's not what we have - we have a *FILE* named "91/02/07" in the current directory. This is a subtle but crucial distinction. What can you do in this case? The first thing to try is to return to the Mac that created this crummy entry, and see if you can convince it and your local NFS daemon to rename the file to something without slashes. If that doesn't work or isn't possible, you'll need help from your system manager, who will have to try the one of the following. Use "ls -i" to find the inode number of this bogus file, then unmount the file system and use "clri" to clear the inode, and "fsck" the file system with your fingers crossed. This destroys the information in the file. If you want to keep it, you can try: create a new directory in the same parent directory as the one containing the bad file name; move everything you can (i.e. everything but the file with the bad name) from the old directory to the new one; do "ls -id" on the directory containing the file with the bad name to get its inumber; umount the file system; "clri" the directory containing the file with the bad name; "fsck" the file system. Then, to find the file, remount the file system; rename the directory you created to have the name of the old directory (since the old directory should have been blown away by "fsck") move the file out of "lost+found" into the directory with a better name. Alternatively, you can patch the directory the hard way by crawling around in the raw file system. Use "fsdb", if you have it.

Unix Question And Answers

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".

Unix Question And Answers

What does {some strange unix command name} 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" - in fact, "fgrep foobar *.c" is usually slower than "egrep foobar *.c" (Yes, this is kind of surprising. Try it.) Fgrep still has its uses though, and may be useful when searching a file for a larger number of strings than egrep can handle. egrep = "Extended GREP" egrep uses fancier regular expressions than grep. Many people use egrep all the time, since it has some more sophisticated internal algorithms than grep or fgrep, and is usually the fastest of the three programs. cat = "CATenate" catenate is an obscure word meaning "to connect in a series", which is what the "cat" command does to one or more files. Not to be confused with C/A/T, the Computer Aided Typesetter. gecos = "General Electric Comprehensive Operating Supervisor" When GE's large systems division was sold to Honeywell, Honeywell dropped the "E" from "GECOS". Unix's password file has a "pw_gecos" field. The name is a real holdover from the early days. Dennis Ritchie has reported: "Sometimes we sent printer output or batch jobs to the GCOS machine. The gcos field in the password file was a place to stash the information for the $IDENT card. Not elegant." nroff = "New ROFF" troff = "Typesetter new ROFF" These are descendants of "roff", which was a re-implementation of the Multics "runoff" program (a program that you'd use to "run off" a good copy of a document). tee = T From plumbing terminology for a T-shaped pipe splitter. bss = "Block Started by Symbol" Dennis Ritchie says: Actually the acronym (in the sense we took it up; it may have other credible etymologies) is "Block Started by Symbol." It was a pseudo-op in FAP (Fortran Assembly [-er?] Program), an assembler for the IBM 704-709-7090-7094 machines. It defined its label and set aside space for a given number of words. There was another pseudo-op, BES, "Block Ended by Symbol" that did the same except that the label was defined by the last assigned word + 1. (On these machines Fortran arrays were stored backwards in storage and were 1-origin.) The usage is reasonably appropriate, because just as with standard Unix loaders, the space assigned didn't have to be punched literally into the object deck but was represented by a count somewhere. biff = "BIFF" This command, which turns on asynchronous mail notification, was actually named after a dog at Berkeley. I can confirm the origin of biff, if you're interested. Biff was Heidi Stettner's dog, back when Heidi (and I, and Bill Joy) were all grad students at U.C. Berkeley and the early versions of BSD were being developed. Biff was popular among the residents of Evans Hall, and was known for barking at the mailman, hence the name of the command. Confirmation courtesy of Eric Cooper, Carnegie Mellon University rc (as in ".cshrc" or "/etc/rc") = "RunCom" "rc" derives from "runcom", from the MIT CTSS system, ca. 1965. 'There was a facility that would execute a bunch of commands stored in a file; it was called "runcom" for "run commands", and the file began to be called "a runcom." "rc" in Unix is a fossil from that usage.' Brian Kernighan & Dennis Ritchie, as told to Vicki Brown "rc" is also the name of the shell from the new Plan 9 operating system. Perl = "Practical Extraction and Report Language" Perl = "Pathologically Eclectic Rubbish Lister" The Perl language is Larry Wall's highly popular freely-available completely portable text, process, and file manipulation tool that bridges the gap between shell and C programming (or between doing it on the command line and pulling your hair out). Don Libes' book "Life with Unix" contains lots more of these tidbits.

Welcome To My Blog

Welcome To My Blog