2a
#!/bin/sh
echo "10 levels of folders are created for the departments and 10 levels of files created for student details"
i=1
while [ $i -le 10 ]
do
mkdir MSRITDept$i
cd MSRITDept$i
j=1
while [ $j -le 10 ]
do
touch MSRITStudentDetails$j
j=$(($j+1))
done
cd ..
i=$(($i+1))
done
2b
#! /bin/bash
if [ $# -eq 0 ]
then
echo "Display does not exist"
else
ls -l $1 > t1
x=`cut -d ' ' -f 1,6,7,8,9 t1`
echo $x
fi
3a
#!/bin/bash
for i in $*
do
if [ -d $i ]
then
echo "Large filename size is"
echo `ls -lR $1 | grep "^." | tr -s ' ' | cut -d ' ' -f 5 | sort -n | tail -l`
else
echo "Not a directory"
fi
done
3b
#!/bin/bash
#cd /home/exam/
filename="rit.txt"
if [ -e $filename ]
then
echo "Moving the contents of rit.txt to rit.txt_old"
mv -f $filename $filename"_old"
touch $filename
elif [ ! -e $filename ]
then
echo "File does not exist.. so creating an empty file"
touch $filename
fi
3d
#!/bin/bash
echo " '\$*' output is $*"
echo " '\$#' output is $#"
echo " '\$1 and \$2' output is $1 and $2"
echo " '\$@' output is $@"
echo " '\$?' output is $?"
echo " '\$!' output is $!"
echo " '\$0' your current program name is $0"
4a
#!/bin/bash
threshold=50
for path in `/bin/df -h | grep -vE 'Filesystem/tmpfs' | awk '{print $5}' | sed 's/%//g'`
do
echo "$path"
if [ $path -ge $threshold ];
then
df -h | grep $path% >> t.txt
find . -type f -size +500M > w.txt
fi
done
value=`cat w.txt | wc -l`
if [ $value -ge 1 ];
then
echo "Yes, the directory contains a file size more than 500M or near to 1GB"
fi
4b
#!/bin/bash
echo "read a string"
read string
words=$(echo -n "$string" | wc -w)
chars=$(echo -n "$string" | wc -c)
space=$(expr length "$string" - length `echo "$string" | sed "s/ //g"`)
specialsymbols=$(echo $string|grep -o [^A-Za-z0-9_[:space:]] | wc -l)
echo "The number of words = $words"
echo "The number of characters = $chars"
echo "The number of white spaces = $space"
echo "The number of special symbols = $specialsymbols"
5a
{split ( $0, arr, "-" )
if ((arr[1] < 1) || (arr[1] > 31) || (arr[2] < 1) || (arr[2] > 12))
{
print "invalid date"
exit 0
}
else
{
if (arr[2] == 1)
print "January"
else if (arr[2] == 2)
print "February"
else if (arr[2] == 3)
print "March"
else if (arr[2] == 4)
print "April"
else if (arr[2] == 5)
print "May"
else if (arr[2] == 6)
print "June"
else if (arr[2] == 7)
print "July"
else if (arr[2] == 8)
print "August"
else if (arr[2] == 9)
print "September"
else if (arr[2] == 10)
print "October"
else if (arr[2] == 11)
print "November"
else if (arr[2] == 12)
print "December"
print arr[1]
print arr[3]
}
}
5b
BEGIN {
print "Removing Duplicated lines"
}
{
line [++no] = $0
}
END {
for (i = 1 ; i <= no; i++)
{
flag = 1
for (j = 1; j < i; j++)
if (line[i] == line[j])
flag = 0
if(flag == 1)
print line[i] >>"out.txt"
}
}
7a
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
void listFiles(char *path) {
DIR *dir;
struct dirent *entry;
struct stat fileStat;
// Open the directory
if ((dir = opendir(path)) == NULL) {
perror("Error opening directory");
exit(EXIT_FAILURE);
}
// Print header
printf("%-10s %-8s %-8s %-8s %-12s %s\n", "Permissions", "Links", "Owner", "Group", "Size", "Last Modified");
// Read each entry in the directory
while ((entry = readdir(dir)) != NULL) {
// Construct the full path
char filePath[1024];
snprintf(filePath, sizeof(filePath), "%s/%s", path, entry->d_name);
// Get file status
if (stat(filePath, &fileStat) < 0) {
perror("Error getting file status");
exit(EXIT_FAILURE);
}
// Get owner and group names
struct passwd *owner = getpwuid(fileStat.st_uid);
struct group *group = getgrgid(fileStat.st_gid);
// Print file attributes
printf("%s %2lu %-8s %-8s %8ld %s %s\n",
(S_ISDIR(fileStat.st_mode)) ? "d" : "-",
fileStat.st_nlink,
owner->pw_name,
group->gr_name,
fileStat.st_size,
ctime(&fileStat.st_mtime),
entry->d_name);
}
// Close the directory
closedir(dir);
}
int main(int argc, char *argv[]) {
// Check if the correct number of arguments is provided
if (argc != 2) {
fprintf(stderr, "Usage: %s <directory>\n", argv[0]);
exit(EXIT_FAILURE);
}
// Call the function to list files
listFiles(argv[1]);
return 0;
}
7b
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
void removeEmptyFiles(char *path) {
DIR *dir;
struct dirent *entry;
struct stat fileStat;
// Open the directory
if ((dir = opendir(path)) == NULL) {
perror("Error opening directory");
exit(EXIT_FAILURE);
}
// Read each entry in the directory
while ((entry = readdir(dir)) != NULL) {
// Construct the full path
char filePath[1024];
snprintf(filePath, sizeof(filePath), "%s/%s", path, entry->d_name);
// Get file status
if (stat(filePath, &fileStat) < 0) {
perror("Error getting file status");
exit(EXIT_FAILURE);
}
// Check if the file is empty and remove it
if (S_ISREG(fileStat.st_mode) && fileStat.st_size == 0) {
if (unlink(filePath) == 0) {
printf("Removed empty file: %s\n", entry->d_name);
} else {
perror("Error removing file");
}
}
}
// Close the directory
closedir(dir);
}
int main(int argc, char *argv[]) {
// Check if the correct number of arguments is provided
if (argc != 2) {
fprintf(stderr, "Usage: %s <directory>\n", argv[0]);
exit(EXIT_FAILURE);
}
// Call the function to remove empty files
removeEmptyFiles(argv[1]);
return 0;
}