1. Suppose we have a file directories.txt where each line contains the name of a directory that we want to create. Our friend proposes that we run cat directories.txt | mkdir into order to do this? Will this command work? If not, how can we change this command to get it to work/could we improve it in any way?
Solutions

One correct way to solve this is the command:

xargs mkdir < dir.txt

Many people might write this as the following. It’s fine, but it is a bit unnecessary to invoke the cat command and using a | when we already have input redirection (<) available.

cat dir.txt | xargs mkdir
  1. Come up with a general statement about when we need to use the xargs command.
Solutions

We use xargs whenever we want to take something that would be coming as input to use them as command-line arguments. So for the last problem, the list of files is coming from a file that we can only redirect as input, but mkdir doesn’t read from stdin, it only takes command-line arguments.

On top of this, people often think of using xargs when you want to run one command over many possible input values.

  1. Suppose we have the intro_survey.csv from the previous homework assignment. Using cut, how many unique answers were there to the question “what is your favorite candy”?
Solutions
tail -n+2 intro_survey.csv | cut -d, -f1 | sort -f | uniq -i | wc -l
  1. Suppose we have the intro survey data below. Let’s try to print the column “Cats or Dogs?” from this data. First, come up with a cut command you might use to do this. Second, predict whether or not this will work for the data below. Third, run the cut command on the data below. Did it work? Why or why not?
What's your favorite candy?,What is your favorite dinner?,Cats or Dogs?,Are you currently enrolled in CSE 351?
Peanut M&Ms,Sandwich,Either,No
Snickers,rice, pork, and cabbage,Dogs,No
Sour skittles,thai,Dogs,No
sour patch, starburst,Dumplings,Cats,No
Solutions

Assuming the data is in short_intro_survey.csv:

cut -d, -f3 short_intro_survey.csv

This will not work for the above data, because some of the responses contain commas. cut cannot detect which comma is “supposed” to be the column separator, so it uses any commas in a line as delimiters.

For example, the line sour patch, starburst,Dumplings,Cats,No will interpret sour patch as the first field, starburst as the second field, and Dumplings as the third field.

This is an important limitation of using cut to parse CSVs!

  1. Write a command that lists all the import statements in any *.java files within the current directory and its subdirectories. For this problem, you can assume that an import statement just has the word “import” in it. Bonus points: also show the line number!
Solutions
find -name "*.java" | xargs grep "import"

To show the line number, use grep’s -n flag!

  1. Write a command that would delete all directories, recursively, starting from the current directory. Be careful about where you run this command!! Don’t run it from your home directory!
Solutions
find -type d | xargs rm
  1. Suppose we were given a zip file called files.zip. Come up with a one-line shell statement that unzips all of its contents and then compiles all of the *.java files inside of it.
Solutions
unzip files.zip && cd files && find -name "*.java" | xargs javac
  1. Suppose we know that Compile.java compiles with no errors. If we were to run javac Compile.java || java Compile what would be the result? Would the program compile? Would it run?
Solutions

When we use the || operator, if the first statement is true, then we know the entire line is true, thus we wouldn’t run the second statement. In other words, Compile.java will compile, but it won’t run.

  1. What would be some security issues with running some of the commands you learned from lecture (think of some of the practical implications of rm, find, and xargs in regards to malicious code injection)?
Solutions

A common example is passing in an argument that “looks like” a flag. For example, you can “trick” rm in deleting files without asking by slipping in a line with the value -rf into standard input, and then making that an argument via xargs.