Linux record (2)
Linux record (2)
1.Basic Linux Commands File handling utilities, Security by file permissions, Process utilities, Disk
utilities, sed, awk, grep.
File handling Commands: : cat ,touch ,rm, mv, vi, chmod, cp, mkdir, cd, ls , find ,chown ,chgrp
Disk Utilities: df , du
Grep command
Sed command
Awk
2. Process Utilities
Usage: `who`
Usage: `who am i`
Usage: `top`
3. Disk Utilities
4. Filters
5. Grep Command
6. Sed Command
7. Awk Command
2. Write a shell script that accepts a file name, starting and ending line numbers as arguments and
displays all the lines between the given line numbers.
Program
OUTPUT:
3.Write a shell script that deletes all lines containing a specified word in one or more files supplied
as arguments to it.
echo 'enter a word to be deleted'
read word
echo 'enter file name'
read fname
echo 'lines in' $fname 'after deleting the word' $word ':'
sed "/$word/d" $fname
(or)
if [ $# -eq 0 ]
then
echo "Please enter one or more filenames as argument"
exit
fi
echo "Enter the word to be searched in files"
read word
for file in $*
do
sed "/$word/d" $file | tee tmp
mv tmp $file
done
4.Write a shell script that displays a list of all the files in the current directory to which the user has
read, write and execute permissions.
for i in *
do
if [ -r $i -a -w $i -a -x $i ]
then
echo $i
fi
done
6.Write a shell script that receives any number of file names as arguments checks if every
argument supplied is a file or directory and reports accordingly. Whenever the argument is a file,
the number of lines on it is also reported.
for fname in $*
do
if [ -f $fname ]
then
echo $fname 'is a file'
echo 'no.of lines in' $fname ':'
wc -l $fname
elif [ -d $fname ]
then
echo $fname 'is a directory'
else
echo 'Does not exist'
fi
done
shell.sh
if [ $# -lt 2 ]; then
echo "Usage: $0 source_file target_file1 [target_file2 ...]"
exit 1
fi
source_file=$1
shift
if [ ! -f "$source_file" ]; then
echo "Source file '$source_file' does not exist."
exit 1
fi
for word in $(tr -s '[:space:]' '\n' < "$source_file"); do
total_count=0
for target_file in "$@"; do
if [ ! -f "$target_file" ]; then
echo "Target file '$target_file' does not exist."
continue
fi
count=$(grep -oiw "$word" "$target_file" | wc -l)
total_count=$((total_count + count))
done
if [ $total_count -gt 0 ]; then
echo "Word '$word' found $total_count times in the target files."
else
echo "Word '$word' not found in any target files."
fi
done
file1.txt
apple
banana
orange
file2.txt
file3.txt
8.a write an awk script to count number of lines in a file that does not contain vowels
file1.txt
hi
how r u
this
is
hello
bajj
dfdf
hhhh
rrrr
ttt
word.awk
!/[aeiouAEIOU]/ {
count++
}
END {
print count
}
output:
8.b write an awk script to find the no of characters ,words and lines in a file
file2.txt
hi how r u
this is hw
word2.awk
{
char_count += length($0)
word_count += NF
line_count++
}
END {
print "Lines:", line_count
print "Words:", word_count
print "Characters:", char_count
}