Question

Problem 4: Write a Bash script that removes all zero length ordinary files in the directory...

Problem 4: Write a Bash script that removes all zero length ordinary files in the directory (including those in the sub-directories at all levels) passed as an optional argument. If you do not specify the directory argument, the script uses the current working directory as the default argument. This problem is for practicing bash programming skills. Though there is an easier way to achieve the goal with the find command, the find command is not allowed to appear in your bash script.

Make sure not to use "find" command to find the zero KB file.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#!/bin/bash

# directory from which files with 0 size will be removed

# if no arguement is given then current directory is used

directory="."

# if directory is given in the arguement

# then $directory will be changed to arguement

if [ $# -eq 1 ]

    then

        directory=$1

fi

# number of files with zero size

zeroFilesNum = $(find $directory -size 0 | wc -l)

# if $zeroFilesNum is greater than 0

# files with 0 size are present

if [ $zeroFilesNum -gt 0 ]

    then

        # find command is used to find file with 0 size

                              # pipe is used to run rm command on those files

        find $directory -size 0 -print0 |xargs -0 rm --

fi

# end of script

# save this file as removeZeroFiles.sh

Instructions:

To run the script, first make the script executable:

  1. $chmod +x removeZeroFiles.sh

Run the script by:

  1. ./ removeZeroFiles.sh
Add a comment
Know the answer?
Add Answer to:
Problem 4: Write a Bash script that removes all zero length ordinary files in the directory...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Bash script that removes all zero length ordinary files in the directory (including those...

    Write a Bash script that removes all zero length ordinary files in the directory (including those in the sub-directories at all levels) passed as an optional argument. If you do not specify the directory argument, the script uses the current working directory as the default argument. This problem is for practicing bash programming skills. Though there is an easier way to achieve the goal with find, find is not allowed to appear in your bash script.

  • Write a Bash script that removes all zero length ordinary files in the directory (including those...

    Write a Bash script that removes all zero length ordinary files in the directory (including those in the sub-directories at all levels) passed as an optional argument. If you do not specify the directory argument, the script uses the current working directory as the default argument. This problem is for practicing bash programming skills. Though there is an easier way to achieve the goal with find, find is not allowed to appear in your bash script. Post the screenshot of...

  • Write a bash shell script, deleteFilesWithZeroLength.sh, that removes all zero length ordinary files in the directory passed as an optional argument. If you do not specify the directory argument, the script uses the present working directory as the defaul

    Write a bash shell script, deleteFilesWithZeroLength.sh, that removes all zero length ordinary files in the directory passed as an optional argument. If you do not specify the directory argument, the script uses the present working directory as the default argument. Do appropriate exception handling in your script such as:If the arguments are more than 1, print out “Too many arguments passed”.If the argument passed is a regular file, print out “XXX is regular file”.1c. If the directory doesn’t exist, print out “Directory...

  • Write an awk script to print just the name and size of ordinary files (do not...

    Write an awk script to print just the name and size of ordinary files (do not include directories), one on each line. The output must be aligned as shown in the example below. First, analyze a typical output of the ls -l command to see what differentiates an ordinary file from a directory and which fields contain the file name and size information. Then, determine what the field separator is and whether the default field separator of awk will work,...

  • Write a bash script to find all the files ending with .c recursively for every directory...

    Write a bash script to find all the files ending with .c recursively for every directory in your current working directory, then copy each one to a folder called programs, need handle duplicates by appending the number to the end of the file (ex main.c main-1.c ) compile them and generate a report report should look like: main.c compiles prog2.c failed main-2.c compiles etc.

  • Objective : Write a C Shell script which copies all files(*.java and *.class) from your home dire...

    Objective : Write a C Shell script which copies all files(*.java and *.class) from your home directory to a new one, and Analyze the new directory information such as number of files, user permissions, and disk usage. Sample Output:                                                    << CS Directory Analysis >>      Date:   ============================================================ Current Directory: /home/tomss New Directory Created : /home/tomss/pgm01 File information Total Number of files : 22 files Directory files:   0 files Plain text files:   10 files File have read permissions: 3 files File have...

  • do numbers 4-8 4. Given any directory, use the Is command to display: • all files...

    do numbers 4-8 4. Given any directory, use the Is command to display: • all files and sub-directories starting with the letter "D" (note do not list anything in any sub-directory) • its immediate sub-directories (sub-directories only, and no other ordinary files) its immediate hidden sub-directories only - take a screenshot (#3-3) that clearly shows the command and the result. 5. Assume that the following files are in the working directory: $ ls intro notesb ref2 section 1 section3 section4b...

  • Problem 1 Write a BASH script to create a user account from the Linux system on...

    Problem 1 Write a BASH script to create a user account from the Linux system on which your script is run. The script should process two positional parameters. First positional parameter is supposed to be a string with a user name (e.g., user_name) Second positional parameter is supposed to be a string with a user password (e.g., user_password) In your script: Check if two positional parameters were passed to your script when it was invoked If NOT, print an appropriate...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT