[Solved] Objective Write C Shell Script Copies Files Java Class Home Directory New One Analyze New Q37155881
Objective : Write a C Shellscript which copies all files(*.java and *.class) from your homedirectory to a new one, and Analyze thenew directory information such as number of files, userpermissions, 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 write permissions: 4 files
- File have execute permissions : 2files
- File have length of zero : 0 files
- Others : 0 files
- Disk usage : 50 Kb
- Biggest 5 files in directory :
14.0 KB ./folder1/folder2/file
12.0 KB ./folder1/folder2/file2etc
============================================================
Reported by : <user>
Run C shell :
-bash-4.2$ csh
[tomss@csx8 ~]$ source .login
%DirAnalyze.sh pgm01
Notes.
- Change to C shell : $csh
%source .login
- Write a C shell script( csh ) named“DirAnalyze.sh”
The script need to start with a line#! /bin/csh
- DirAnalyze.sh
- put your name, section name and simple description for theassignment
- Print Title and date at thesame line use echo command andcut command
<< CS2351 Directory Analysis>> Date: Tue Oct 29, 2018
Eg.
Wed Oct30 17:48:15 CDT 2018 take –> Wed Oct 30, 2018
- Display current directory
Use the pwdcommand to make sure your home directory
- Create New Directory and copyfiles
Create new directory with themkdir command, directory name should bethe first argument $1.
* Command lineargument are in special shell variables 0, 1, 2, 3 ,4…etc.
$0 is the name of thescript itself, $1 the first, $2 the second ..etc.
DirAnalyze.sh pgm01
——————- ———-
$0 $1
* Copyfiles
- copy *.java file in your $HOME directory to pgm01
- copy *.class files in your $HOME directory to pgm01
cp $currentDir/*.java $newDir
cp $currentDir/*.class $newDir
- change directory to pgm01
- 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 plainfile
-o user owns file
-r user has read permission
-x user has execute permission
-z file has length of Zero
- Disk Usage
Usedu command
- Find top 5 big files -print top 5 big file namesand their size in decreasing order in human readable format(Hint :use “du” and “sort” and “head” commands together to achievethis)
- 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 yourdescription
5
6 echo -n “<< CS2351 GradeReport >> 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 newDirectory
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
Expert Answer
Answer to Objective : Write a C Shell script which copies all files(*.java and *.class) from your home directory to a new one, an… . . .
OR

