Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Bash script that removes all zero length ordinary files in the current d

ID: 3720733 • Letter: W

Question

Write a Bash script that removes all zero length ordinary files in the current directory by default and other directory if provided as an optional argument. Ensure that erroneous directories provided are handled with appropriate error handling and/or messages. Show the output for two scenarios.

Write a Bash script that changes the file extension of all the files in a directory having a certain extension, such as .png or .gif. The script takes two command line arguments: old_extension and new_extension.

Write a Bash script takes as an argument a word for which to seek the definition. The script reads dictionary.txt and then displays the requested definition. If you don't still have a copy of dictionary.txt in your ~/data directory, please ask the instructor for a new one. Display the results of running your script, such as: ./word_def.sh byzantine

Using a Bash script, complete class project 6, option I. Your Bash script may contain other tools, such as awk and sed.

Write a Bash script that searches a directory .Trash and removes files and directories older than 30 days. Show how you would have cron run this script daily.

Write a Bash script that updates the SPY.txt file from Yahoo Finance: http://finance.yahoo.com/q/hp?s=SPY+Historical+Prices . Include in your script comments describing how to have cron run the script daily at a 5:30 PM. Test your crontab entry with any time.

Explanation / Answer

NOTE: I have completed the program for your assignment. Please check and let me know if you have any questions. I will acknowledge back with a response within 24 hours. Thanks for your patience.

As per Chegg guidelines we are supposed to answer the first 4 questions when there are multiple sub questions. So i have answered that. Please raise separate questions for your other queries.

Q) Write a Bash script that removes all zero length ordinary files in the current directory by default and other directory if provided as an optional argument. Ensure that erroneous directories provided are handled with appropriate error handling and/or messages. Show the output for two scenarios.

Ans)

Code:

Unix Terminal> cat prog1.sh

#!/bin/bash

# by default it is current directory

dirname=`pwd`

# if directory is passed we will look at that

if [ $# -eq 1 ]

then

dirname=$1

fi

# checking if directory exist

if [ ! -e $dirname ]

then

echo "$dirname does not exist"

exit 1

fi

# iterate through each file in directory

for each_file in `ls $dirname`

do

if [ -f $dirname/$each_file ]

then

# getting length of each file

len=`wc -c $dirname/$each_file|awk '{print $1}'`

# if length of a file is zero bytes delete it

if [ $len -eq 0 ]

then

rm $dirname/$each_file

fi

fi

done

Unix Terminal>

Code output snapshot:

Unix Terminal> ls -tlrh

total 16

-rw-r--r-- 1 bonkv ANTDomain Users 35B May 1 09:08 xyz

-rw-r--r-- 1 bonkv ANTDomain Users 559B May 1 09:12 prog1.sh

drwxr-xr-x 5 bonkv ANTDomain Users 170B May 1 09:13 TEST

-rw-r--r-- 1 bonkv ANTDomain Users 0B May 1 09:13 c

-rw-r--r-- 1 bonkv ANTDomain Users 0B May 1 09:13 b

-rw-r--r-- 1 bonkv ANTDomain Users 0B May 1 09:13 a

Unix Terminal> bash prog1.sh

Unix Terminal> ls -tlrh

total 16

-rw-r--r-- 1 bonkv ANTDomain Users 35B May 1 09:08 xyz

-rw-r--r-- 1 bonkv ANTDomain Users 559B May 1 09:12 prog1.sh

drwxr-xr-x 5 bonkv ANTDomain Users 170B May 1 09:13 TEST

Unix Terminal> ls -tlrh TEST

total 8

-rw-r--r-- 1 bonkv ANTDomain Users 16B May 1 09:09 3

-rw-r--r-- 1 bonkv ANTDomain Users 0B May 1 09:13 2

-rw-r--r-- 1 bonkv ANTDomain Users 0B May 1 09:13 1

Unix Terminal> pwd

/Users/bonkv/chegg/shell/EX2

Unix Terminal> bash prog1.sh /Users/bonkv/chegg/shell/EX2/TEST

Unix Terminal> ls -tlrh TEST

total 8

-rw-r--r-- 1 bonkv ANTDomain Users 16B May 1 09:09 3

Unix Terminal> bash prog1.sh abc

abc does not exist

Unix Terminal>

Q) Write a Bash script that changes the file extension of all the files in a directory having a certain extension, such as .png or .gif. The script takes two command line arguments: old_extension and new_extension.

Ans)

Code:

Unix Terminal> cat prog2.sh

#!/bin/bash

# validating command line arguments

if [ $# -ne 2 ]

then

echo "Must pass two arguments"

echo "Usage: $0 <old extension> <new extension>"

exit 1

fi

# getting old extension and new extension passed into variables

old_ext=$1

new_ext=$2

# iterating through each file with old extension

for each_file in `ls *.${old_ext}`

do

# getting filename only

fname=`echo $each_file|cut -d'.' -f1`

# formatted new filename with new extension

new_fname="$fname.$new_ext"

# renaming file with old extension to new extension

mv $each_file $new_fname

done

Unix Terminal>

Code output snapshot:

Unix Terminal> ls -tlrh

total 24

-rw-r--r-- 1 bonkv ANTDomain Users 35B May 1 09:08 xyz

-rw-r--r-- 1 bonkv ANTDomain Users 559B May 1 09:12 prog1.sh

drwxr-xr-x 3 bonkv ANTDomain Users 102B May 1 09:13 TEST

-rw-r--r-- 1 bonkv ANTDomain Users 0B May 1 09:18 b.ext

-rw-r--r-- 1 bonkv ANTDomain Users 0B May 1 09:18 a.ext

-rw-r--r-- 1 bonkv ANTDomain Users 561B May 1 09:19 prog2.sh

Unix Terminal> bash prog2.sh ext png

Unix Terminal> ls -tlrh

total 24

-rw-r--r-- 1 bonkv ANTDomain Users 35B May 1 09:08 xyz

-rw-r--r-- 1 bonkv ANTDomain Users 559B May 1 09:12 prog1.sh

drwxr-xr-x 3 bonkv ANTDomain Users 102B May 1 09:13 TEST

-rw-r--r-- 1 bonkv ANTDomain Users 0B May 1 09:18 b.png

-rw-r--r-- 1 bonkv ANTDomain Users 0B May 1 09:18 a.png

-rw-r--r-- 1 bonkv ANTDomain Users 561B May 1 09:19 prog2.sh

Unix Terminal>

Q) Write a Bash script takes as an argument a word for which to seek the definition. The script reads dictionary.txt and then displays the requested definition. If you don't still have a copy of dictionary.txt in your ~/data directory, please ask the instructor for a new one. Display the results of running your script, such as: ./word_def.sh byzantine

Ans)

I dont have sample dictionary.txt file which you mentioned in the question. So created a sample file with two entries containing first one as word and the rest as meaning of it. Here is the sample file i have used.

Unix Terminal> cat dictionary.txt

content the subjects or topics covered in a book or document

sample a small part of anything or one of a number, intended to show the quality, style, or nature of the whole; specimen.

Code:

Unix Terminal> cat word_def.sh

#!/bin/bash

dict='dictionary.txt'

# validating command line arguments

if [ $# -ne 1 ]

then

echo "Must pass one argument"

echo "Usage: $0 <word>"

exit 1

fi

word=$1

# identifying the count of word in dictionary

cnt=`grep $word $dict|wc -l`

# if word exists in dictionary

if [ "$cnt" -eq "1" ]

then

word_meaning=`grep $word $dict|cut -d' ' -f2-`

echo "Meaning of the given word "$word" is : $word_meaning"

else

echo "Word is not available in the dictionary"

fi

Unix Terminal>

Code output snapshot:

Unix Terminal> bash word_def.sh sample

Meaning of the given word "sample" is : a small part of anything or one of a number, intended to show the quality, style, or nature of the whole; specimen.

Unix Terminal> bash word_def.sh content

Meaning of the given word "content" is : the subjects or topics covered in a book or document

Unix Terminal>

Unix Terminal> bash word_def.sh contents

Word is not available in the dictionary

Unix Terminal>

Q) Write a Bash script that searches a directory .Trash and removes files and directories older than 30 days. Show how you would have cron run this script daily.

Ans)

Code:

Unix Terminal> cat remove.sh

#!/bin/bash

# storing .Trash and days into variables for easy modification whenever we want to change filename or number of days

dname='.Trash'

days='+30'

# find command to identify the .Trash directory and delete files older than 30 days

find -name "$dname/*" -type d -ctime $days | xargs rm -rf

Unix Terminal>

I dont have .Trash directory and does not have files older than 30 days so not able to show the exact output. Please try the same in your system and let me know if you face any issues.

To set the same in cron file:

0 01 * * * bash $HOME/remove.sh

The above cron file contain the script entry which says to trigger the script every day at 01st hour. Ensure you have given execute permission to the file remove.sh before setting up in cron.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote