-------------------------------- Practice Unix/Linux Questions #1 -------------------------------- -IAN! idallen@idallen.ca This is a set of questions of various difficulties that reviews basic Unix command line usage: 1. You get stuck in a Unix program and can't get out. List five different things you might try typing to exit or get help. 2. What is the difference to a process between receiving Interrupt (usually ^C) and receiving EOF (usually ^D)? 3. If a file "foo" contains the numbers 1, 22, 111, and 2222 on four different lines, what will be the output of this command: $ sort -r foo foo Try it! 4. You are in your home directory. Use Unix commands or pipelines (combinations of commands) to perform the following actions. Do not use any change directory commands; use relative or absolute pathnames to place your output in the correct directories. Minimize your typing - use the shortest pathnames you can. 1. Create a file in the current directory named "out" that contains the last 15 lines of the Unix password file, sorted in reverse. (Only the 15 lines are sorted in reverse; not the whole file.) 2. Create a file in the /tmp directory named "out" that contains only line 15 from the Unix passwd file. (Hint: use "tail -1" as part of the pipeline.) 3. Make a new directory (in your home directory) named "foo". Next, under that directory, make a sub-directory named "bar". (Remember that no cd commands are allowed - use appropriate pathnames only!) Move the file from the previous question to under the "bar" directory. Move the "bar" directory to under your current (home) directory. Remove the empty "foo" directory. (No "cd" commands allowed! Use short relative pathnames.) 5. Which man page has more lines, the one for bash or the one for tcsh? 6. Go to the the "/home" directory (owned by root) on a Unix machine. Without changing directories, create a file named "recent" in *your* home directory that contains the names of the five most recently modified files in the "/bin" directory. (Hint: the -t option to "ls" may be useful. See the man page.) (1) Answer the question using absolute paths for the input and output file names. (2) Now use the shortest possible relative paths for both the input and output file names. (3) Produce a long listing of the 15 oldest files, instead of the newest files. (Hint: adding the -r option to "ls" may be useful.) 7. Read the man page for the Unix "cal" command to figure out how to produce a calendar for September 2003. Now try September 1752. Count the number of words in the output of the "cal" command when used for the month of September, 1752. (Answer: 28 words.) How can you restrict the output to show only the number of words, and not the number of lines and bytes also? (RTFM) 8. The Unix "last" command shows the recent logins to the machine. (Warning: this can be thousands of lines of output on some machines!) Use a pipeline to extract the last 3 of your logins (search for logins with your userid as the pattern; show only the first three lines). 9. This filter extracts the first blank-delimited field on every input line: cut -f 1 -d ' ' # note the single blank inside the quotes Produce a sorted list of the last userids (just the userids) to log in to this machine. Produce a sorted count of the number of times each userid logged in, with the most frequent login first. 10. Go to this directory: /home/alleni/public_html/teaching/cst8129/05f/notes/ Type "pwd" to show the current directory. Now type this: cd ./../.././../../. Without looking, what directory are you in now? Use "pwd" to verify this. Return to the original directory and type the shortest cd command that will return you to the same place (get rid of the redundant items in the cd pathname argument). From where you are now, give a relative pathname (no leading slash allowed) for the Unix password file. 11. What command and option removes a directory and everything under it? 12. How do you sort something in reverse? 13. What is the fastest way to create ten empty files named 0 through 9? (Hint: Doing "echo >1; echo >2; etc." is not the right answer.) 14. What is the output on your screen of each of these command lines, and what is in the file "foo" when the command finishes? $ date >foo ; sort foo >foo $ date >foo ; more foo >foo $ date >foo ; grep "e" foo >foo $ date >foo ; head foo >foo $ date >foo ; tail foo >foo $ date >foo ; cat foo >foo Hint: The answer is the same for all the command lines. Some versions of the "cat" command will tell you if you have used an output file that is the same as any of the input files. Will the version on Linux tell you? Does the warning message prevent the output file from being erased? 15. Why is the file "myfile" not empty after this command sequence? $ date >myfile ; wc myfile >myfile ; cat myfile 16. Will the output of "wc" include the name of the file in all the following command lines? Why or why not? $ date >foo ; wc foo $ date >foo ; cat foo | wc $ date >foo ; wc bar (Hint: How many arguments are passed to cp? How many are required? RTFM) 22. What is wrong with the following command for creating file bar? What will be the output on the screen? What will be in file "bar"? $ echo "Some Text" | cp bar (Hint: How many arguments are passed to cp? How many are required? RTFM) 23. What is wrong with the following command for copying file foo to file bar? What will be the output on the screen? What will be in file "bar"? $ cat foo | cp >bar (Hint: How many arguments are passed to cp? How many are required? RTFM) 24. What is the output on your screen of this command line? $ date >foo ; echo "Hello There" | mv foo bar What does the "mv" command do with information coming in on standard input (after the pipe symbol) (RTFM)? 25. The following command line sometimes works (in the Bourne-style shells), but the programmer who wrote it didn't really understand how piping operates, given the two commands involved: $ date >bar | mv bar foo What is the actual output (on standard output) of the part of the command line before (to the left of) the pipe? (Run the command on its own, without the pipe and what follows, and see the output.) What does the "mv" command do with information coming in on standard input (after the pipe symbol) (RTFM)? Some shells (e.g. C Shells) will not permit the above command line: cshell% date >bar | mv bar foo Ambiguous output redirect. 26. The "argv" program is available for download under the Notes buttons, written in shell script, C language, and C++ language. It counts and displays arguments, with output going to standard error. The shell script version argv.sh.txt is the easiest to use under Unix - just copy it to your directory, rename it, and make it executable ("chmod +x argv.sh"). Use it whenever you are trying to see how the shell passes command line arguments. Try the argv.sh command in current dir: $ ./argv.sh a >b c # output appears on stderr, not stdout Argument 0 is [./argv] Argument 1 is [a] Argument 2 is [c] # redirection is never an argument How many arguments are passed by the shell to the commands given here: $ sort >bar foo $ head >bar $ tail bar $ wc a b c e f >g $ cat a b c >d e f g $ grep alleni >out /etc/passwd Hint: To check your answers you can replace the command name by the "argv.sh" program from the Notes that displays and counts all the command line arguments: $ ./argv.sh a b c >d e f g ... $ ./argv.sh alleni >out /etc/passwd ... The argv.sh program will tell you how many arguments the shell created. 27. The "file" command tells you what kind of data is in one or more pathname arguments. Use the "file" and "ls -l" commands to identify these pathnames: /etc/passwd /etc /dev/null /dev/hda /dev/log What letter does "ls" use to identify each of the above different kinds of Unix "files"? One of the above pathnames is a directory. The "ls" command normally shows you the *contents* of a directory pathname argument, not the status or permissions of the directory itself. What option to "ls" causes it to show you the status of the directory itself, instead of the contents of the directory? (RTFM) 28. Shell GLOB patterns can be used to supply file name arguments to programs, e.g. "ls -l /bin/*sh" lists all non-hidden files ending in "sh" under the /bin directory. Use the "file" command with a shell glob pattern to list the file types of all the non-hidden names under the /dev/ directory. (Output: about 351 lines.) Pipe that output in to a selection command that selects only lines that contain the string "character". (Hint: the string is also called a "pattern".) Pipe that into a comand that will count those lines. The answer should be about 226 lines. 29. Use the "file" command with a shell glob pattern to list the file types of all the non-hidden names under the /dev/ directory. (Output: about 351 lines.) Pipe that output in to a selection command that selects only lines that contain the string "block". Pipe that into a comand that will count those lines. The answer should be about 99 lines. 30. Use the "file" command with a shell glob pattern to list the file types of all the non-hidden names under the /dev/ directory. (Output: about 351 lines.) Pipe that output in to a selection command that selects only lines that do *not* contain the string "special". You should see about 26 lines of output. Now pipe that output (the 26 lines) into yet another selection command that selects lines that do *not* contain the word "symbolic". About 11 lines should display.