Question

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:  

============================================================

  1. Current Directory: /home/tomss
  2. New Directory Created : /home/tomss/pgm01
  3. File information
  1. Total Number of files : 22 files
  2. Directory files:   0 files
  3. Plain text files:   10 files
  4. File have read permissions: 3 files
  5. File have write permissions: 4 files
  6. File have execute permissions : 2files
  7. File have length of zero : 0 files
  8. Others : 0 files
  1. Disk usage : 50 Kb
  2. Biggest 5 files in directory :

14.0 KB ./folder1/folder2/file

12.0 KB ./folder1/folder2/file2 etc

============================================================

                                                                                       Reported by : <user>    

Run C shell :

-bash-4.2$ csh

[tomss@csx8 ~]$ source .login

%DirAnalyze.sh   pgm01

                                                                

Notes.

  1. Change to C shell :   $ csh  

                                    %source .login

  1. Write a C shell script( csh ) named “DirAnalyze.sh”

The script need to start with a line #! /bin/csh

  1. DirAnalyze.sh  
  • put your name, section name and simple description for the assignment
  • Print Title and date at the same line use echo command and cut command

      << CS2351 Directory Analysis >>      Date: Tue Oct 29, 2018

      Eg.   

      Wed Oct 30 17:48:15 CDT 2018 take --> Wed Oct 30, 2018

  1. Display current directory

Use the pwd command to make sure your home directory

  1. Create New Directory and copy files

Create new directory with the mkdir command, directory name should be the first argument $1.

* Command line argument are in special shell variables 0, 1, 2, 3 ,4 ...etc.

   $0 is the name of the script itself, $1 the first, $2 the second ..etc.

DirAnalyze.sh   pgm01  

------------------- ----------

    $0                        $1

* Copy files

  1. copy *.java file in your $HOME directory to pgm01
  2. copy *.class files in your $HOME directory to pgm01

cp $currentDir/*.java   $newDir

cp $currentDir/*.class   $newDir

  1. change directory to pgm01
  1. File information

Use foreach loop and if statement

foreach file (argument list)

  

    if( -d $file) then

        @ x ++

else if( -f $file    ) then

    @ y ++

else if( -r $file   ) then

       

      ……

   else

      ….

   endif

   end

       

# file inquiry Operator

-d File is a Directory

-e File Exists

-f   File is a plain file

-o user owns file

-r user has read permission

-x user has execute permission

-z file has length of Zero

  1. Disk Usage

Use du command

    

  1. Find top 5 big files -print top 5 big file names and their size in decreasing order in human readable format(Hint : use “du” and “sort” and “head” commands together to achieve this)
  1. Run C shell

           bash-4.2$ csh

          [jongyk@csx8 ~]$ source .login

          %DirAnalyze.sh pgm01

Examples

vi DirAnalysis.sh

#! /bin/csh

      2 #Author : Jongyeop Kim

      3 #Section: 401

      4 #Description : put your description

      5

      6 echo -n "<< CS2351 Grade Report >> Date:" ;

      7 echo `date |cut -c1-20` "\n"

      8

      9 ## set variables

     10 set dirCount=0

     11 set plainCount=0

     12 set exePermission=0

     13 set zeroLength=0

     14

     15 set currentDir=`pwd`

     16 set newDir=$1

     17

     18 echo "A. Current Directory :" `pwd`

     19 mkdir $newDir

     20

     21 echo "B. New Directory Created " `pwd`"/"$newDir

     22 cp $currentDir/*.java $newDir

     23 cp $currentDir/*.class $newDir

     24

     25 ## Change directory to pgm02

     26

     27 cd $newDir

     28

     29 ##count number file in your new Directory

     30

     31 foreach file (*)

     32 ## echo $file

     33   if( -x $file) then

     34      echo "directory"

     35      @ dirCount ++

     36   else if( -f $file ) then

     37     echo "plainCount"

     38     @ plainCount ++

     39   else

     40     echo "zeroLength"

     41     @ zeroLength=0

     42   endif

     43 end

     44

     45 echo "File is a directory" $dirCount

     46 echo "File is a plain file" $plainCount

     47 echo "File have length of zero” $zeroLength

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

#! /bin/csh
## set variables
set dirCount=0
set plainCount=0
set exePermission=0
set zeroLength=0
set readPermissions=0
set writePermissions=0
set totalFiles=0
set others=0
set diskUsage=0
set currentDir=`pwd`
set newDir=$1
echo "Current Directory :" `pwd` # Displays current directory
mkdir $newDir #create new directory
echo "New Directory Created " `pwd`"/"$newDir

## copy all .java files to new directory

cp $currentDir/*.java $newDir

## copy all .class files to new directory
cp $currentDir/*.class $newDir
## Change directory to the file name mentioned in command prompt
cd $newDir
echo "File information"
## count number of files in your new Directory
for file in $newDir; do # for every file in the new directory loop until you find 0 files
if( -e $file) then # if file exists then loop to find which file
@ totalFiles++ # increment total files
if( -d $file) then # if the file is directory file then increment dirCount
@ dirCount ++
else if( -f $file ) then # if file is plain text then increment plainCount
@ plainCount ++
else if( -r $file) then # if the current file has read permissions then increment readPermissions
@ readPermissions++
else if( -x $file) then # if the file has execute permissions then increment writePermissions
@ exePermission++
else if( -w $file) then   # if the file has write permissions then increment writePermissions
@ writePermissions++
else if( -z $file) then # if the file has zero length then increment zeroLength
@ zeroLength++
else
@ others++ # if the file is other than the above mentioned then increment others
fi # end if
fi # end if
end for # end for

## print the details
echo "Total number of files: " $totalFiles
echo "Directory files: " $dirCount
echo "Plain text files" $plainCount
echo "File have read permissions: " $readPermissions
echo "File have write permissions: " $writePermissions
echo "File have execute permissions: " $exePermission
echo "File have length of zero : " $zeroLength
echo "Others: " $others

## to get total disk size
echo "Disk Usage:" `du -sh`

## to get the top 5 largest files in storage
echo "Biggest 5 files in directory :" `du -h | sort -n -r | head -n 6`

exit 0

##

du command: Estimate file space usage.

h : Displays all files and folders in human readable format.

sort command : Sort lines of text files.

-n : Compare according to string numerical value.

-r : Reverse the result of comparisons.

head : Output the first part of files.

-n : Print the first ‘n’ lines. (In our case, We displayed first 6 lines, 6 because first line displays the total size then followed by the largest 5 files).

Add a comment
Know the answer?
Add Answer to:
Objective : Write a C Shell script which copies all files(*.java and *.class) from your home dire...
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
  • Open vi or pico and create a script called Information.   Add the contents from the following table to the script: (Hin...

    Open vi or pico and create a script called Information.   Add the contents from the following table to the script: (Hint: use echo to add blank lines to your output and to write out the headings). Headings: Utilities (command to execute) Today’s date is: date The current users logged on to the system are: who My current startup directory is: pwd Today’s identification information is: id The processes running on my system are: ps Add a header to your Information...

  • Cybersecurity Fundamentals Lab #9: Access ControlName: ____________________ If your Kali VM is already up and running,...

    Cybersecurity Fundamentals Lab #9: Access ControlName: ____________________ If your Kali VM is already up and running, login to the root account on the VM via ssh. It does not matter whether you use a command line or GUI sshclient. If your VM is not currently up, use the vSphere web client to start your VM before logging into it via ssh.1: Create an Unprivileged User For some labs, including this one, we will need a non-root user account on the...

  • SHELL SCRIPT: Provide the following questions with the codes and command line Problem 5: Take 5...

    SHELL SCRIPT: Provide the following questions with the codes and command line Problem 5: Take 5 input arguments in a for loop, but only add the even i values to sum and display the result. Problem 6: Write a shell script that accepts path of the directory and returns a count of all the files within the specified directory. (Hint: use alias to perform cd operation) Problem 7: Write a shell script that takes an ‘count’ value as an input...

  • using lubuntu ce the following activities on your Linux virtual machine. Then complete estions. Write a script file which when executed will perform the following: First display today's date in d...

    using lubuntu ce the following activities on your Linux virtual machine. Then complete estions. Write a script file which when executed will perform the following: First display today's date in dd.mm.YYYY format (e.g. 20.04.2019). Ask user to enter a name to create a directory. Create a directory by the given directory name above. . . Ask user to enter a name to create a file. Copy the file /etc/passwd to the given filename above inside the newly created directory Copy...

  • Copy/Paste your script from 5b here. 4. Let's look at a more complicated script executable. a....

    Copy/Paste your script from 5b here. 4. Let's look at a more complicated script executable. a. Write the following as script6.sh and change its permissions to count for num in d do if num eq 100 then count (count+1) done echo The number 10 was found $count times What is the output? Run the script as ./script6.sh 10 20 30 10 40 20 50 10 60 <enter Summarize what the script does r using the read command. The following script...

  • In this assignment, you will demonstrate your ability to write simple shell scripts. This is a...

    In this assignment, you will demonstrate your ability to write simple shell scripts. This is a cumulative assignment that will challenge you to pull together a variety of lessons from throughout the course. 2 The Assignment Every now and then, I find myself with a large number of files that have inappropriate extensions (the set of characters in the file name after the last :') that need to be changed. For example, a complicated C++ program, developed by someone on...

  • 1. Set up an easy way to copy files from /home/distribution/cnguyen/cis18b directory on voyager, without having...

    1. Set up an easy way to copy files from /home/distribution/cnguyen/cis18b directory on voyager, without having to type the long path name every time. Make your set up “permanent” for this quarter (the set up should last from one login session to another). 2. Use echo to write a brief explanation of your set up in step 1. 3. If you are not at your home directory, go to your home directory and don’t change directory for the rest of...

  • UNIX File Permission help, please answer the questions in the green boxes. Thank you Lab 03...

    UNIX File Permission help, please answer the questions in the green boxes. Thank you Lab 03 File Permissions In this lab we will: learn about file permissions learn to create symbolic links and hard links Utilities that will be utilized in this Lab: us, cd, less, cat touch, chmod id umask, mkdir, In, echo and redirection Users and Groups Linux supports several methods of controlling access to files an directories. In this lab we are going to learn the traditional...

  • Step 3: How would you write this script in C? It needs to do the things...

    Step 3: How would you write this script in C? It needs to do the things required in step 3. 1. You have received a new batch of distinguished users; their basic information is located in newusers.tar. Inside of the tar file, there is a file called "newusers.txt" which contains a colon-separated entry for each user: the username, the uid, the GECOS information, and the user's preferred shell. Also in the tar file you will find a public key for...

  • 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...

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