Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 5

MCA 1 st SEMESTER - 2022

Assignment – 5

DOA: 07/03/2022 DOS: 16/03/2022


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

Part (A)- create a file with name Example.txt which contains following content

Welcome to Linux !

Linux is a free and open source Operating system that is mostly used by developers and in
production
servers for hosting crucial components such as web and database servers. Linux has also made a
name
for itself in PCs. Beginners looking to experiment with Linux can get started with friendlier linux
distributions such as Ubuntu, Mint, Fedora and Elementary OS.

Write commands for the following-

Q1- print the lines Which contain the string “linux”?


Sol:- $grep “linux” Example.txt
Output Screenshot

Q2- print total number of occurrences of string “linux”?


Sol:- $grep -c “linux” Example.txt
Output Screenshot

Q3- display the lines that don’t contain the string “Linux”?
Sol:- $grep -v “Linux” Example.txt
Output Screenshot
Q4- search “linux” in same directory and other sub-directories?
Sol:-

$grep -nr “Linux” /home/kalilinux/project


OR
$grep -nr ‘Linux*’ /home/kalilinux/project
OR
$grep -R “Linux” /home/kalilinux/project

• -n Show relative line number in the file


• -r Recursively search subdirectories listed
• -R for "recursive", which means the program searches in all
subfolders,and their subfolders, and their subfolder's
subfolders,etc.
• /home/kalilinux/project Directory for search (current directory)
Output Screenshot

Q5- Number the lines where the string “linux” is matched ?


Sol:- $grep -n “linux” Example.txt
Output Screenshot

Q6- Display the lines start with “Linux”?


Sol :- $grep “^Linux” Example.txt
OR
$grep ^Linux Example.txt
Output Screenshot
Q7- Find the empty lines in Example.txt?
Sol:-
• $grep -ne “^$” Example.txt OR $grep -ne ^$ Example.txt /* Best output */
• $grep -e “^$” Example.txt OR $grep -e ^$ Example.txt
• $grep “$” Example.txt OR $grep $ Example.txt

Output Screenshot

Part (B) Write suitable commands for the following-

Q1- List all the directories in directory?


Sol :-
$ ls -d */*/
Output Screenshot

Q2- List all the text files in a directory ?


Sol :- $ls *.txt
Output Screenshot

Q3- Display all the files which have a word twt,twet, tweet etc in the file name?
Sol :- $ls tw*t.*
Output Screenshot
Q4- display the files which contains any single character between t and t?
Sol:- $ls t*t.*
Output Screenshot

Q5- Display all the files which starts with ‘a’ and wnds with ‘e’?
Sol:- $ls a*e.*
Output Screenshot

Q6- Display all the files which starts with a vowel?


Sol:- $ls [aeiou][AEIOU]*
Output Screenshot

Q7- Display all the files which contains digit at last in its name?
Sol:- $ls *[0-9]*[0-9]*

Output Screenshot

Q8- Display all the files which don’t contain digit in its name?
Sol:- $ls [!0-9]*[!0-9]*[!0-9].*
OR
$ ls [\!0-9]*[\!0-9]*[\!0-9].*
Output Screenshot
Q9- Display all the the files whose name starts with a capital letter?
Sol:- $ls *[A-Z]*

Output Screenshot

Q10- Display all the files whose name contains only three letters?
Sol:- $ls ???.*

Output Screenshot

You might also like