-------------------------------- Practice Unix/Linux Questions #1 -------------------------------- -IAN! idallen@idallen.ca This is a set of questions of various difficulties that reviews Chapter 4 and the related Notes. -- 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. -- What is the difference to a process between receiving Interrupt (usually ^C) and receiving EOF (usually ^D)? -- 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! -- 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. 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.) -- On ACADUNIX, which man page has more lines, the one for bash or the one for tcsh? What is the difference in length (in lines)? -- 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.) -- 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? -- The Unix "last" command shows the recent logins to the machine. (Warning: on ACADUNIX, this output is thousands of lines long!) On ACADUNIX, select the most recent 100 login records to the machine. (Hint: pipe the "last" command output into a command that shows only the first 100 lines.) Use commands to extract and count the number of times the string "aip" ocurred in the last 100 logins. -- On ACADUNIX, use a pipeline to extract the last 3 of your logins (search for logins with your userid as the pattern). -- On ACADUNIX, go to this directory: /usr/share/man/info/en_US/a_doc_lib/cmds/aixcmds2/figures Type "pwd" to show the current directory. Now type this: cd ./../.././../../. What directory are you in now? Use "pwd" to verify this. From where you are now, give a relative pathname (no leading slash allowed) for the Unix password file. -- What command and option removes a directory and everything under it? -- How do you sort something in reverse? -- What is the fastest way to create ten empty files named 0 through 9? (Hint: Doing "date >1; date >2; etc." is not the right answer.) -- On ACADUNIX, 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 ; cat foo >foo $ 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 Hint: The answer is the same for all the command lines. Some versions of the "cat" command will warn you if you use an output file that is the same as any of the input files. The version on ACADUNIX does not warn you. Will the version on Linux warn you? -- Why is the file "myfile" not empty after this command sequence? $ date >myfile ; wc myfile >myfile -- Will the output of "wc" include the name of the file in both the following command lines? Why or why not? $ date >foo ; wc foo $ date >foo ; cat foo | wc Hint: how many arguments are passed to "wc" in each case? -- What does this do? $ mkdir my dir -- What does this do? $ mkdir "my dir" Is it the same as this?: mkdir my dir -- How do you append to a file, so that you can put the output of several commands into the same file without losing what was there before? -- What is wrong with this command for copying abcd.txt into file foo? $ cat abcd.txt | foo As seen by the Unix shell, list the exact command names and arguments for all the commands being used in the above pipeline. Give the correct command line. -- 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"? $ cp foo >bar (Hint: How many arguments are passed to cp? How many are required? RTFM) -- 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) -- 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) -- What is the output 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)? -- 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 will not permit the above command line: cshell% date >bar | mv bar foo Ambiguous output redirect. -- 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" program from the Notes that displays and counts all the command line arguments: $ ./argv a b c >d e f g $ ./argv alleni >out /etc/passwd The "argv" program is available for download under the Notes buttons, written in shell script, C language, and C++ language. 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. -- The "file" command tells you what kind of date is in one or more pathname arguments. On ACADUNIX, use the "file" and "ls -l" commands to identify these pathnames: /etc/passwd /etc /dev/null /dev/hd1 /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) -- On ACADUNIX, 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 136 lines.) Pipe that output in to a selection command that selects only lines containing 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 109 lines. -- Alter the above to find types in /dev/ that do *not* contain the string (pattern) "special". (You will need to supply an option to the selection command so that it only shows lines not matching the specified pattern. RTFM) What are the four lines that result? -- Prepare one-line descriptions in your own words of what the following Unix commands do, and where they are described (either a Linux text page number, a page in your course notes, or a man page). I've done the first command as a one-line example. You do the rest: apropos - locate manual pages by title keyword (Linux p.99) bash - cal - cat - cd - chmod - cp - date - diff - du - echo - file - grep - head - last - less - ls - man - mesg - mkdir - more - mv - passwd - pwd - rm - rmdir - sort - stty - tail - touch - vi - vim - wc - who -