-------------------------------- Practice Unix/Linux Questions #1 -------------------------------- -IAN! idallen@ncf.ca This set of questions reviews Chapters 1-4, early parts of Chapter 5, selected files under the Notes buttons, and the related Floppix Labs on www.floppix.com. -- 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 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. -- On Floppix, you are in the "/home" directory (owned by root). 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: the -r option to "ls" may be useful.) -- Count the number of words in the output of the "cal" command when used for the month of September, 1752. How can you restrict the output to show only the number of words, and not the number of lines and bytes also? -- On ACADUNIX, put these two lines into a file named "short.pl": #!/usr/bin/perl -w print "Hello World.\n"; (If you use echo commands to do this, make sure you single-quote the lines to hide all special characters from the shell.) Make the file executable: chmod +x short.pl Execute the file: ./short.pl Expected output: Hello World. -- On ACADUNIX, use the "diff" command and count the number of lines of difference output between the file /etc/group and the same file sorted. (The answer should be about 40 lines.) -- On ACADUNIX, use the "file" and "ls -l" commands to identify these pathnames: /dev/null /dev/hd1 /dev/log /dev/xti What letter does "ls" use to identify each of the above different kinds of Unix "files"? What option to "ls" causes it to show you the directory itself, instead of the contents of the directory? -- 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. Pipe that output in to a command that selects only lines containing the string "character". 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 "special". What are the four lines that result? -- On ACADUNIX, select the most recent 100 login records to the machine. (Hint: Floppix lab 13.) 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 (your userid). Pipe that output to the mail program and send it to your Algonquin email account. -- On ACADUNIX, which man page has more lines, the one for bash or the one for tcsh? -- On ACADUNIX, issue a command to show all the processes that are running. (Floppix Lab 13.) Use a pipeline to count how many processes are being run by "root". (Hint: Select lines that start with "root".) Add an option and count how many processes are *not* being run by "root". -- 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 files named 0 through 9? (Hint: Doing "date >1; date >2; etc." is not the right answer.) -- To print only the first field (first column) of any file or input stream, use this little "awk" command fragment: awk '{print $1}' (Note the use of single quotes to protect the string from the shell!) Sample usage: who | awk '{print $1}' - prints just the userids (first column) of the logged-in users On ACADUNIX, extract just the userids of all the users who logged in recently and have a login record. (Hint: Floppix Lab 13.) Pipe this into sort, then into a command that removes duplicate adjacent lines. Count how many unique lines (unique userids) remain. (The answer should be something over 270.) -- What is the difference between "whereis perl" and "which perl"? -- Which program issues the error message in each case below? $ date >/etc/passwd permission denied $ cp foo /etc/passwd permission denied $ cat no-such-file cannot open (or no such file) $ sort foo $ cat foo $ sort foo >foo $ sort foo $ more foo >foo $ more foo $ grep "pattern" foo >foo $ grep "pattern" foo $ head foo >foo $ head foo $ tail foo >foo $ tail foo $ tr a b 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 Floppix warn you? -- Why is the file "myfile" not empty after this command sequence? $ date >myfile ; wc myfile -- What is the difference between the outputs of the two "wc" commands? $ date >foo $ wc foo $ wc foo $ ln foo one $ ln foo two $ ln foo three -- Does this work to give a file a link count of four? $ date >foo $ ln foo one $ ln one two $ ln two three -- Does this work to give a file a link count of four? $ date >foo $ ln foo one $ ln one two $ ln one three -- Does this work to give a file a link count of four? $ date >foo $ ln foo new $ ln foo new $ ln foo new -- Create a file, then do some commands to give it a link count of five. (Give the file four other names, for a total of five names.) Now, remove all (ALL) permissions from the above file. How many Unix commands are necessary to remove all permissions from a file that has five names? After removing all permissions, try to copy one of the above file names to another file. Which program issues the error message, and why? -- Compare these two command sequences and explain how they differ, if they do differ: $ date >a $ date >a $ ln a b $ ln a b $ ln a c $ ln b c $ ln a d $ ln c d -- What information is stored with a file name in a Unix directory? What information is stored in the inode of the file? What command shows the permissions of the CURRENT DIRECTORY ONLY? (Do not show the *contents* of the current directory!) What command shows the inode numbers associated with names in a directory? What permissions do you need on a directory to see the names it contains? What permissions do you need to find out the owner of a file in the current directory? What permissions to you need to find out if the current directory contains an entry named "secret"? What permissions do you need on a file to make a link to it? What permissions do you need on a file to remove its name from a directory? What permissions do you need on a sub-directory to remove its name from a directory? What must be true about the contents of the sub-directory? What (minimal) permissions do you absolutely need on a directory to remove a name from the directory? -- 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 to mail the contents of existing file abcd.txt to user alleni? $ cat abcd.txt > mail alleni@algonquincollege.com What error message will you see? What does the above command actually do? *Exactly* how many arguments are passed to the "cat" command? Rewrite the above command to work, having the "mail" command read the file text from standard input and removing the unnecessary "cat". -- 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? $ 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 actually works (in the Bourne-style shells), but the programmer who wrote it didn't really understand how piping operates, given the two commands involved: $ tr a b 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? 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% tr a b bar | mv bar foo Ambiguous output redirect. -- Consider the identical output from these pairs of command lines: $ cat /etc/passwd | grep $USER $ grep $USER /etc/passwd $ cat /etc/passwd | tr 'a-z' 'A-Z' $ tr 'a-z' 'A-Z' bar $ sort >bar foo $ bar $ tail d e f g $ wc a b c g Hint: To check your answers you can replace the command name by the "argv" program that displays and counts all the command line arguments: $ ./argv a b bar The "argv" program is available for download under the Notes buttons, written in shell script, C language, and C++ language. The shell script version is the easiest to use under Unix - just copy it to your directory and make it executable ("chmod +x argv"). -- Prepare one-line descriptions in your own words of what these Unix commands do, and where they are described (either a Linux text page number or a Floppix lab number). A few commands may not be found in Chapters 1-4 or the Floppix labs - you'll have to look them up in man pages. (The Floppix site has a TofC index that may prove helpful.) I've done the first command as a one-line example. You do the rest: apropos - locate manual pages by title keyword (p.50) cal - cat - cd - chmod - compress - cp - date - diff - echo - file - finger - ftp - grep - gunzip - gzip - head - last - less - links - ln - ls - mail - man - mesg - mkdir - more - mv - passwd - ps - pwd - rm - rmdir - sort - su - tail - talk - tar - telnet - touch - uniq - w - wc - whereis - which - who - write - zcat -