-------------------------------------------- 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 pathnames to correctly place your output. 1. Create a file here 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) Use absolute paths for the input and output file names. (2) 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 -- What is the output of each of these command lines, and what is in the file "foo" when the command finishes? $ cat foo >foo $ sort foo >foo $ more foo >foo $ grep "pattern" foo >foo $ head foo >foo $ tail foo >foo $ tr a b foo Hint: The answer is the same for all the command lines. -- Note that the following command line sometimes works for small files: $ cat foo | tr 'a' 'b' | grep "lala" | sort | head >foo There is a critical race between the first "cat" command trying to read the data out of "foo" before the shell truncates it to zero when launching the "head" command at the end of the pipeline. Depending on the system load and the size of the file, "cat" may or may not get out all the data before the file is truncated or altered by the shell. Don't depend on pipelines saving you from bad redirection! Never redirect output into a file that is being used as input. -- Does this work to give a file a link count of four? $ date >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 the directory? What information is stored in the inode of the file? What command shows the permissions of the CURRENT DIRECTORY ONLY? 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, and remove the unnecessary "cat". -- What is wrong with this command for copying abcd.txt into file foo? $ cat abcd.txt | foo 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.