-------------------------------------------- Practice Unix/Linux Questions - Chapters 1-5 -------------------------------------------- -IAN! idallen@ncf.ca -- You are in your home directory. Create Unix 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.) -- 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.) -- 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 bar $ sort >bar foo $ bar $ tail d e f g $ wc a b c g -- On ACADAIX, what is the output on your screen of each of these command lines, and what is in the file "foo" when the command finishes? $ cat foo >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 ACADAIX does not 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. -- Rewrite the following command line (from page 172 of the textbook) to get rid of the useless "cat" command: kudos% cat memos.new | rsh bravo diff - memos/memo.draft Let programs open their own files, or let the shell do it. Don't use "cat" unnecessarily.