# Homework 3 Solutions ## Problem 1 1. ``` #!/bin/bash src_dir=$1 if [ ! "$(ls -A $1)" ]; then exit fi declare -A extensions for filepath in $src_dir/*; do filename=${filepath##*/} ext=${filename#*.} ((extensions["$ext"]+=1)) done for key in "${!extensions[@]}"; do echo "$key ${extensions[$key]}" done | sort ``` 2. ``` #!/usr/local/bin/bash src_dir=$1 dst_dir=$2 # create source directory and subfolders mkdir -p $dst_dir/images $dst_dir/videos $dst_dir/documents $dst_dir/other cp $src_dir/*.??* $dst_dir/other mv $dst_dir/other/*.{png,jpg,jpeg,gif} $dst_dir/images mv $dst_dir/other/*.{mp4,mkv,m4v,mov} $dst_dir/videos mv $dst_dir/other/*.{txt,docx,md,csv} $dst_dir/documents echo "Finished copying organized files to $dst_dir" ``` ## Problem 2 ``` roll() { if [ $1 -gt $2 ] then >&2 echo 'The first arg was larger than the second!' exit 1 fi let num="$RANDOM % ($2-$1+1) + $1" echo $num } add() { local sum=0 for x in "$@" do (( sum += x )) done echo $sum } roll-add() { sum=0 for ((run=1; run <= $3; run++)) do sum=$(add $sum $(roll $1 $2)) done echo $sum } ``` ## Problem 3 | URI | scheme | hostname | path | port | |-----|--------|----------|------|------| |hdfs://nn1:8020/user/tux/contetns.xml | hdfs | nn1 |user/tux/contetns.xml| 8020 | |http://ec2-3-92-42-224.compute-1.amazonaws.com:8080/?query=login | http |ec2-3-92-42-224.compute-1.amazonaws.com| |8080| | s3://tuplex/job100/part_0.csv |s3|tuplex|job100/part_0.csv| 80 | | ssh://tux@gitice.com:2222/igloo/penguin.git |ssh|tux@gitice.com|igloo/penguin.git|2222| ## Problem 4 1. Copies `foobar.txt` from the home directory of the user `your_username` on host `remotehost.edu` to the local directory `/some/local/directory`. 2. Copies `foobar.txt` from the current directory to the directory `/some/remote/directory` of user `your_username` on host `remotehost.edu`. 3. Copies `foobar.txt` from the directory `/some/remote/directory` of the user `your_userrname` on host `rh1.edu` to the directory `/some/remote/directory` of the user `your_username` on host `rh2.edu`. 4. Recursively copies the directory `some/directory` from the home directory of user `your_username` on host `remotehost.edu` to the local directory `/some/local/directory`. ## Problem 5 N/A ## Problem 6 1. No. This is a bad practice because your email is not a secure channel of communication, and then anyone who gained access to your email would then be able to access all your machines. 2. No. Essentially it just makes it easier for someone to brute-force their way into your system. See this stackexchange question for a more in-depth answer on why using the default port is not a good idea. https://security.stackexchange.com/questions/32308/should-i-change-the-default-ssh-port-on-linux-servers 3. Password-based login is not recommended because it makes it easier for someone to brute force their way into your system. See this article for a more in-depth answer on the pros/cons of password-based login vs key-based login. https://www.thorntech.com/2018/12/passwords-vs-ssh/ 4. Key-based login is good, actually, because it's more secure. See this article for a more in-depth answer. https://www.thorntech.com/2018/12/passwords-vs-ssh/