Unix
Unix
Assignment-1
1. Display the content of current directory.
[06/09/2024 - 10:23.01]~/desktop/unix/demo
[Surali.DESKTOP-5I719O9]$ ls -l
4. Display date and time in format shown in brackets ( Sun June 19,11:40 PM )
[06/09/2024 - 10:25.41]~/desktop/unix/demo
[Surali.DESKTOP-5I719O9]$ date +("%a %B %d , %I : %M%p") (Fri
September 15 , 10 : 26AM)
[06/09/2024 - 10:26.06]~/desktop/unix/demo
[Surali.DESKTOP-5I719O9]$ echo -e " \\ \n \\ \\ \\ \n \\ \\ \\ \\ \\"
\
\\\
\\\\\
6. Execute a command to know the kernel version of operating system you are working on.
[06/09/2024 - 10:26.31]~/desktop/unix/demo
[Surali.DESKTOP-5I719O9]$ uname -r
3.3.5(0.341/5/3)
[06/09/2024 - 10:28.11]~/desktop/unix/demo
[Surali.DESKTOP-5I719O9]$ cat surali.exe how are
you ? good or not ?
2
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335
[06/09/2024 - 10:29.31]~/desktop/unix/demo
[Surali.DESKTOP-5I719O9]$ cat >f2 hyyyyyy
12. display all files from current directory having first and last character as number.
[06/09/2024 - 10:31.41]~/desktop/unix/demo
[Surali.DESKTOP-5I719O9]$ touch 1f0 1sp2
[06/09/2024 - 10:32.07]~/desktop/unix/demo
[Surali.DESKTOP-5I719O9]$ ls [0-9]*[0-9] 1f0
1sp2
13. display the list of all file names that contains only 3 latters.
[06/09/2024 - 10:32.19]~/desktop/unix/demo [surali.DESKTOP-5I719O9]$ ls ???
1f0
3
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335
17. copy those files that must contains 3rd character in the file name as digit to the directory
"xtemp".
[06/09/2024 -
10:34.28]~/desktop/unix/demo
[Surali.DESKTOP5I719O9]$ mkdir xtemp
[06/09/2024 - 10:35.13]~/desktop/unix/demo
[Surali.DESKTOP-5I719O9]$ cp ??[0-9]* ./xtemp
[06/09/2024 - 10:35.21]~/desktop/unix/demo
[Surali.DESKTOP-5I719O9]$ cd xtemp
[06/09/2024 - 10:35.31]~/desktop/unix/demo/xtemp
[Surali.DESKTOP-5I719O9]$ ls 1f0
as1
[06/09/2024 - 10:37.00]~/desktop/unix/demo/xtemp
[Surali.DESKTOP-5I719O9]$ cd apache
[06/09/2024 - 10:37.20]~/desktop/unix/demo/xtemp
[Surali.DESKTOP-5I719O9]$ ls 1f0
as1
5
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335
Assignment – 2
3. remove all files containing digit as the 2nd latter as their names. [28/08/2024 -
14:59.40]~/desktop/unix3 [Surali.DESKTOP-5I719O9]$ rm ?[0-9]* rm: d1: is a directory
[28/08/2024 - 15:02.22]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ ls M
abc d1
[28/08/2024 - 15:09.26]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ cat > c1
[28/08/2024 - 15:10.06]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ cat > c2
6
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335
[28/08/2024 - 15:10.18]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ cat c1 c2 c3> c-
6. display the date in the following format .e.g. "Today's date is : Sat Jul 30 15:25:31 IST 2011"
[28/08/2024 - 15:25.01]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ echo "Today's date is`date`"
[29/08/2024 - 15:39.13]~/desktop/unix3
[Surali.DESKTOP-7GLGQEA]$ cat > sc2
abc def jkl
[29/08/2024 - 15:39.28]~/desktop/unix3
[Surali.DESKTOP-7GLGQEA]$ comm -1 -2 sc1 sc2>result
[29/08/2024 - 15:42.17]~/desktop/unix3
[Surali.DESKTOP-7GLGQEA]$ cat result
abc def
8. find how many number of lines from sc1 and sc2 are common.
[28/08/2024 - 15:42.27]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ comm -1 -2 sc1 sc2 |wc -l 2
9. display only those files containing the more then 5 character as there names ( the file names
having last two characters as digit). [28/08/2024 - 15:45.41]~/desktop/unix3
[Surali.DESKTOP5I719O9]$ cat > abc12 i am abc
[28/08/2024 - 15:48.48]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ cat > psp01 i am psp
7
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335
[28/08/2024 - 15:49.44]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ ls ???[0-9][0-9] abc12 psp01
10. create the directory named "maxx" and copy all files having only capital letters.
[29/08/2024 - 14:14.09]~/desktop/unix3 [Surali.DESKTOP-5I719O9]$
cp `ls [A-Z]` max
[28/08/2024 - 21:43.16]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ ls
M abc12 c- c2 d1 psp01 sc1 abc asd[0-9]* c1 c3 emp* result
sc2
13. display only the last accessed file from current directory.
[29/08/2024 - 13:58.49]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ ls -t |head
-n1 vbe
14. create file named emp_list having empid and date of joining.
8
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335
[29/08/2024 - 14:09.27]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ cut -d"|" -f1,4 emp > emp_list
[29/08/2024 - 14:10.27]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ cp /emp* emp_master2
18. display the path of the directory where all your mail are stored. [29/08/2024 -
14:15.02]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ echo $MAIL
|-- abc.php
|-- dir1
| `-- tmp `--
xyz.java
$ mkdir export
$ mkdir color dir1 m1 m2 m3 dir2
$ cat > fil1.txt
Hello Everyone
$ cd m1
$ mkdir f1 f2
$ cd f1
$ cat > emp.txt Hye
Good Morning
$ cd..
$ cd..
$ cat > menu.txt
Hellloooo $cdm2
$ cd m2 $ cat >
file123 I’m here $
cat > file123 Hello
unix
$ cd
..
$ cat > sps.php Hello php
$ cd dir2
20. copy the content of file emp.lst to file123, make back up of file 'file123' and rename it with
file_bkp
10
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335
21. display the content of "export" directory in a way like question-19 [29/08/2024 -
15:55.57]~/desktop/unix3/m2 [Surali.DESKTOP-7GLGQEA]$ ls
-1d */ -x $ color/:
$ dir1/: tmp $ dir2/: $
m1/:F1F2 $ m2/:
File_bkp $m3/: F1
25. provide the permissions to the file "color" in such a way that only the owner can perform
read and write operation whilegroup members and others can only execute a file.
[29/08/2024 - 16:22.43]~/desktop/unix3
[Surali.DESKTOP-5I719O9]$ chmod 611 color
Assignment-3
11
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335
7. Replace 'hello' with “HELLO” in input file finish and write those lines to output file fout.sh
[29/08/2024 - 14:44.51]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ cat f2 HELLO
8. extract all username and their home directory form /etc/passwd file.
[29/08/2024 - 14:44.58]~/desktop/unix/pract
12
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335
9. Locate lines of file where the second and second last character of the lines are same.
[29/08/2024 - 14:50.08]~/desktop/unix/pract [Surali.DESKTOP-5I719O9]$ grep'^.\(.\).*\1.$' f2
Unix shell programming
10. Display all lines of files that contains “hello” pattern in it.
[29/08/2024 - 14:50.12]~/desktop/unix/pract
11. Display all lines having “g*” patternin it. [29/08/2024 - 14:53.09]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ grep 'g\*' f3
13. List all files of working directory having at least 4 characters in filename. [29/08/2024 -
14:59.57]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ find . -maxdepth 1 -type f -name '????*' -print
[29/08/2024 - 15:23.48]~/desktop/unix/pract
[Surali.DESKTOP-7GLGQEA]$ tr -s ' ' < f1 hii hello
13
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335
18. Write a command to locate lines that begin and end with (.).
[31/08/2024 - 12:09.46]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ grep '^\..*\.$' f2
19. write a command to display all lines that contains 2 or more ^symbol at beginning of line.
[31/08/2024 - 12:12.24]~/desktop/unix/pract [Surali.DESKTOP-
5I719O9]$ grep -E'^\^{2,}' f1
20. Write a command to replace all occurrences of “he” with “she” and “hello” with “hi” in file
f1.
[31/08/2024 - 12:17.15]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ sed 's/he/she/' -e 's/hello/hii/' f1 sed:
s/he/she/: No such file or directory Hi
Hi
[31/08/2024 - 12:18.05]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ sed 's/he/she/' f2
Hii
HELLO
14
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335
unix is subject she is very anoying this is tshe lifeunix sshell programming unix is open source Good
night bin display bin display
21. Display those lines having exactly 10 alphabates from file f1.
[31/08/2024 - 12:18.36]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ grep '^[A-Za-z]\{10\}$' f2
22. Copy file content of f1 to file f2 if f1 exist otherwise write error message to file f2.
[31/08/2024 - 12:19.48]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ cp f1 f2
23. Search those files from current directory which have more than 5 links.
[31/08/2024 - 12:23.30]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ find . -links +5 – print
24. Display lines of file f1 that do not contain digitin it. [31/08/2024 -
12:23.33]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ grep -v '[0-9]' f2 hi hello
25. Replace all occurrences of “linux OS” with “unix OS” in file f1.
[31/08/2024 - 12:26.04]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ sed 's/linux os/unixos/g' f3 surali
hello good morning hyyyy
HELLO unix is subject she is very anoying this is tshe life unix
sshell programming unix is open source Good night bin display
bin display
26. Display all line of file f1 having 3rd word as'user'. [31/08/2024
- 12:36.23]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ grep '^[^ ]* [^ ]* user' f2
27. Display name of all files of working directory having pattern “The”.
[31/08/2024 - 12:27.00]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ grep -l 'The' *
15
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335
28. Display lines of file f1 that begin with any capital letter. [31/08/2024 -
12:35.32]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ grep '^[A-Z]' f3 surali HELLO unix is
subject Good night
29. Write a sed command to extract first word of each line. Assuming that there is no white
space character at beginning of line. [31/08/2024 - 12:35.55]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ sed 's/ .*/ /g' f3 Kevin hello
good byyyy HELLO unix
She this unix unix
Good bin bin
30. What does the following command do?grep f1 f2 f3 It’s return nothing
31. display only those lines of file f1 having length in between 30 to 50 characters.
[31/08/2024 - 12:39.07]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ grep'^.\{10,20\}$' f3
Surali Devani good morning
HELLO unix
is subject she is very smart this is
she life unix is opensource
Goodnight
Bin display
Bin display
33. Replace all occurrences of “hello” with “hi” and “he” with “she”.
[31/08/2024 - 12:17.15]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ sed 's/he/she/' -e's/hello/hii/' f1 sed:
s/he/she/: No such file or directory /hi HI [31/08/2024
- 12:18.05]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ sed 's/he/she/' f2 Hii
16
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335
34. Count number of words and lines of files whose filename begins
with x.
[31/08/2024 - 12:40.29]~/desktop/unix/pract
[Surali.DESKTOP-5I719O9]$ cat `ls x*` | wc -lw
Assignment-4
1. Write a shell script which works like a calculator(enter the values via command line
argument)
Shell Script:
echo -e "enter num1: \c" read num1
echo -e "enter num2: \c" read
num2 echo -e "\t\t menu \n
1. Addition
2. Multiplication
3. subtraction 4. divison
enter your choice: \c" read ch case $ch in 1|+)
ans=`expr $num1 + $num2` ;; 2|*)
ans=`expr $num1 \* $num2` ;; 3|-)
ans=`expr $num1 - $num2` ;;
4|/) ans=`expr $num1 / $num2` ;;
*) echo "invalid option" ;; esac echo
"answer: $ans"
17
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335
OUTPUT:
$ sh textfile1.txt enter
num1: 1 enter
num2: 4 menu
1. Addition
2. Multiplication
3. subtraction 4. divison enter your choice: 1 answer:5
2. Write a shell script to find a file with maximum size in the current directory, also print the no of words,
characters, and lines along with the content of file.
Shell Script:
# Find the file with the maximum size in the current directory max_file=$(find .
-type f -exec ls -lS {} + | head -n 1 | awk '{print $9}')
OUTPUT:
The largest file is:test.txt
Number of lines:6
Number of words:25
Number of characters:120 Content of the
file:
hyy unix hello unix hiii unix hyy
hyy hyy hyy hyy hyy hyy hyy unix
18
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335
3. Write a shell script to display message “good morning” or “good afternoon” or “good evening”, whenever
you login.
Shell Script:
# Get the current hour
hour=$(date +"%H")
19
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335
4. Write a shell script to copy one file into other, display appropriate message if Destination file already exist.
Shell Script:
#check if the correct number of arguments is provided if
[ "$#" -ne 2 ]; then echo "Usage: $0 <source_file>
<destination_file>"
exit 1 fi
OUTPUT:
$ sh copy.sh file1 file3
File 'file1' has been copied to 'file3'.
20
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335
OUTPUT:
$ sh reverse.sh surali
Original String: surali
Reversed String: arahd
6. Write a shell script to input a word in lower case and convert it into upper case.
Shell Script:
# Read input word read -p "Enter a word in
lowercase: " word
OUTPUT:
$ sh convert.sh
Enter a word in lowercase: surali Uppercase word:
SURALI
21
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335
7. Write an awk script to print the fields 2,3,4 and 6 from test.txt file which contains the pattern ‘unix’.
OUTPUT:
$ awk '/unix/ { print $2, $3, $4, $6 }' test.txt unix hello unix
unix
8. Write an awk script that displays first two-words of each line of the file f1.
OUTPUT:
$ awk '{ print $1, $2 }' test.txt
hyy unix hyy hyy
unix php linux wd
ospart java
surali bharodiya
22
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335
AWK Script:
$ awk '{
# Store each line in an array lines[NR]
= $0
}
END {
# Print lines in reverse order
for (i = NR; i > 0; i--) { print
lines[i] } }' test.txt
OUTPUT:
surali bharodiya ospart java nt linux
wd java unix php wd hyy
hyy hyy hyy hyy hyy hyy hyy hyy
unix hello unix hiii unix
23
TYBCA SEM-5 DIV-4 UNIX & SHELL ROLL NO - 335
10. Write an awk script which print frequency of each word in the input file.
AWK Script:
$ awk '
{
# Loop through all words in each line
linux 1 nt 1
wd 2 surali
1 php 1 hiii
1 java 2 hyy
9 ospart 1
bharodiya 1
hello 1 unix
4
24