224 unix
224 unix
Assignment-1
[06/09/2022 - 10:23.31]~/desktop/unix/demo/d1
[Abcd.DESKTOP-5I719O9]$ cat >> f1
[06/09/2022 - 10:25.08]~/desktop/unix/demo
[Abcd.DESKTOP-5I719O9]$ cal 1 1980
[06/09/2022 - 10:25.19]~/desktop/unix/demo
[Abcd.DESKTOP-5I719O9]$ pwd
4. Display date and time in format shown in brackets ( Sun June 19,11:40 PM )
[06/09/2022 - 10:25.41]~/desktop/unix/demo
[Abcd.DESKTOP-5I719O9]$ date +("%a %B %d , %I : %M%p")
6. Execute a command to know the kernel version of operating system you are working on.
[06/09/2022 - 10:26.31]~/desktop/unix/demo
[Abcd.DESKTOP-5I719O9]$ uname -r
1
TYBCA-DIV3-SEM-5 UNIX ROLL NO-246
[06/09/2022 - 10:26.56]~/desktop/unix/demo
[Abcd.DESKTOP5I719O9]$ tty
[06/09/2022 - 10:28.21]~/desktop/unix/demo
[Abcd.DESKTOP-5I719O9]$ ls *.exe
[06/09/2022 - 10:28.33]~/desktop/unix/demo
[Abcd.DESKTOP-5I719O9]$ ls -ltu
[06/09/2022 - 10:28.51]~/desktop/unix/demo
[Abcd.DESKTOP-5I719O9]$ cat > f1 php
[06/09/2022 - 10:29.31]~/desktop/unix/demo
[Abcd.DESKTOP-5I719O9]$ cat >f2 unix
[06/09/2022 - 10:29.41]~/desktop/unix/demo
[Abcd.DESKTOP-5I719O9]$ cat >f3
Wd-2
[06/09/2022 - 10:29.48]~/desktop/unix/demo
[Abcd.DESKTOP-5I719O9]$ cat >f4
NT
[06/09/2022 - 10:30.06]~/desktop/unix/demo
[Abcd.DESKTOP-5I719O9]$ cat >f5
Asp.net
[06/09/2022 - 10:30.30]~/desktop/unix/demo
2
TYBCA-DIV3-SEM-5 UNIX ROLL NO-246
12. display all files from current directory having first and last character as number.
[06/09/2022 - 10:32.07]~/desktop/unix/demo
[Abcd.DESKTOP-5I719O9]$ ls [0-9]*[0-9]
13. display the list of all file names that contains only 3 latters.
[06/09/2022 - 10:32.19]~/desktop/unix/demo [Abcd.DESKTOP-
5I719O9]$ls ???
[ 09/09/2022 - 10:32.40]~/desktop/unix/demo
[Abcd.DESKTOP-7GLGQEA]$ cat > -abc
hii hello
Abcd
[06/09/2022 - 10:33.10]~/desktop/unix/demo
[Abcd.DESKTOP-5I719O9]$ wc -c -- -abc
[06/09/2022 - 10:34.09]~/desktop/unix/demo
[Abcd.DESKTOP-5I719O9]$ mv ex1 as1
17. copy those files that must contains 3rd character in the file name as digit to the directory
"xtemp".
[06/09/2022 - 10:35.13]~/desktop/unix/demo
[Abcd.DESKTOP-5I719O9]$ cp ??[0-9]* ./xtemp
[12/08/2022 - 12:17.57]/drives/d/tybca316/unix/d1/xtemp
[Abcd.DESKTOP-5I719O9]$ mkdir helix apache
3
TYBCA-DIV3-SEM-5 UNIX ROLL NO-246
[06/09/2022 - 10:36.07]~/desktop/unix/demo/xtemp
[Abcd.DESKTOP-5I719O9]$ cp -R helix tmp
[06/09/2022 - 10:36.07]~/desktop/unix/demo/xtemp
[Abcd.DESKTOP-5I719O9]$ cp -R helix tmp
Assignment-2
[28/08/2022 - 14:42.25]~/desktop/unix3
[Abcd.DESKTOP-5I719O9]$ ls | grep -E '^[0-9]+$'
[28/08/2022 - 14:56.18]~/desktop/unix3
[Abcd.DESKTOP-5I719O9]$ ls | grep -E '^[a-z A-Z]+$'
3. remove all files containing digit as the 2nd latter as their names.
[28/08/2022 - 14:59.40]~/desktop/unix3
[Abcd.DESKTOP-5I719O9]$ rm ?[0-9]*
[28/08/2022 - 15:32.31]~/desktop/unix3
[Abcd.DESKTOP-5I719O9]$ cat > asd[0-9]
Hi Hello Everyone
4
TYBCA-DIV3-SEM-5 UNIX ROLL NO-246
[28/08/2022 - 15:10.18]~/desktop/unix3
[Abcd.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/2022 - 15:25.01]~/desktop/unix3
[Abcd.DESKTOP-5I719O9]$ echo "Today's date is`date`"
7. compare 2 files named sc1, sc2 and store the common content in file result.
[29/08/2022 - 15:39.28]~/desktop/unix3
[Abcd.DESKTOP-7GLGQEA]$ comm -1 -2 sc1 sc2>result
8. find how many number of lines from sc1 and sc2 are common.
[28/08/2022 - 15:42.27]~/desktop/unix3
[Abcd.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/2022 - 15:49.44]~/desktop/unix3
[Abcd.DESKTOP-5I719O9]$ ls ???[0-9][0-9]
10. create the directory named "maxx" and copy all files having only capital letters.
[29/08/2022 - 14:14.09]~/desktop/unix3
[Abcd.DESKTOP-5I719O9]$ cp `ls [A-Z]` max
[28/08/2022 - 21:41.34]~/desktop/unix3
[Abcd.DESKTOP-5I719O9]$ cat > emp\*
5
TYBCA-DIV3-SEM-5 UNIX ROLL NO-246
[29/08/2022 - 13:50.45]~/desktop/unix3
[Abcd.DESKTOP-5I719O9]$ cat emp
empid | empname | post 1| awer |
manager
2| sdfg | employee
3| rtyy | director
4| jbyg | programmer
13. display only the last accessed file from current directory.
[29/08/2022 - 13:58.49]~/desktop/unix3
[Abcd.DESKTOP-5I719O9]$ ls -t
14. create file named emp_list having empid and date of joining.
[29/08/2022 - 14:09.27]~/desktop/unix3
[Abcd.DESKTOP-5I719O9]$ cut -d"|" -f1,4 emp > emp_list
[29/08/2022 - 14:10.27]~/desktop/unix3
[Abcd.DESKTOP-5I719O9]$ cp /emp* emp_master1
[29/08/2022 - 14:10.27]~/desktop/unix3
[Abcd.DESKTOP-5I719O9]$ cp /emp* emp_master2
[29/08/2022 - 14:12.23]~/desktop/unix3
[Abcd.DESKTOP-5I719O9]$ mv emp_master2 backup_emp
6
TYBCA-DIV3-SEM-5 UNIX ROLL NO-246
[29/08/2022 - 14:13.13]~/desktop/unix3
[Abcd.DESKTOP-5I719O9]$ rm /emp*
18. display the path of the directory where all your mail are stored. [29/08/2022
- 14:15.02]~/desktop/unix3
[Abcd.DESKTOP-5I719O9]$ echo $MAIL
$ 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
7
TYBCA-DIV3-SEM-5 UNIX ROLL NO-246
Hellloooo
$cdm2
$ cd m2
$ cat > file123 I’m
here
$ 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
[29/08/2022 - 15:55.45]~/desktop/unix3/m2
[Abcd.DESKTOP-5I719O9]$ cp file123 file_bkp
[29/08/2022 - 15:55.57]~/desktop/unix3/m2
[Abcd.DESKTOP-7GLGQEA]$ ls -1d */ -x $ color/:
[29/08/2022 - 16:01.11]~/desktop/unix3
[Abcd.DESKTOP-5I719O9]$ ls -1t | head -n1 m2
[29/08/2022 - 16:19.25]~/desktop/unix3
[Abcd.DESKTOP-5I719O9]$ find . -name "*.lst"
[29/08/2022 - 16:20.17]~/desktop/unix3
[Abcd.DESKTOP-5I719O9]$ mv '*.lst'
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/2022 - 16:22.43]~/desktop/unix3
[Abcd.DESKTOP-5I719O9]$ chmod 611
Assignment-3
[29/08/2022 - 14:29.19]~/desktop/unix/pract
[Abcd.DESKTOP-5I719O9]$ ls -s | head -n2
[29/08/2022 - 14:33.17]~/desktop/unix/pract
[Abcd.DESKTOP-5I719O9]$ sed -n '1,2p' f1 | wc -w
[29/08/2022 - 14:35.57]~/desktop/unix/pract
[Abcd.DESKTOP-5I719O9]$ ls |grep ^[^.]
[29/08/2022 - 14:36.09]~/desktop/unix/pract
[Abcd.DESKTOP-5I719O9]$ tr -cd '[a-zA-Z0-9\n]' <f1
9
TYBCA-DIV3-SEM-5 UNIX ROLL NO-246
[29/08/2022 - 14:38.43]~/desktop/unix/pract
[Abcd.DESKTOP-5I719O9]$ ls i
[29/08/2022 - 14:40.20]~/desktop/unix/pract
[Abcd.DESKTOP-5I719O9]$ grep -E'^.{20}' f2
7. Replace 'hello' with “HELLO” in input file finish and write those lines to output file fout.sh
[29/08/2022 - 14:44.51]~/desktop/unix/pract
[Abcd.DESKTOP-5I719O9]$ cat f2
8. extract all username and their home directory form /etc/passwd file.
[29/08/2022 - 14:44.58]~/desktop/unix/pract
[Abcd.DESKTOP-5I719O9]$ cat /etc/passwd | cut -d ':' -f 1,6
9. Locate lines of file where the second and second last character of the lines are same.
[29/08/2022 - 14:50.08]~/desktop/unix/pract
[Abcd.DESKTOP-5I719O9]$ grep'^.\(.\).*\1.$' f2
10. Display all lines of files that contains “hello” pattern in it.
[29/08/2022 - 14:50.12]~/desktop/unix/pract
[Abcd.DESKTOP-5I719O9]$ grep 'hello' `grep -l'hello' *` f1:hello f3:hello
[29/08/2022 - 14:53.09]~/desktop/unix/pract
[Abcd.DESKTOP-5I719O9]$ grep 'g\*' f3
[29/08/2022 - 14:54.47]~/desktop/unix/pract
[Abcd.DESKTOP-5I719O9]$ touch -t 08261957f1
13. List all files of working directory having at least 4 characters in filename.
[29/08/2022 - 14:59.57]~/desktop/unix/pract
[Abcd.DESKTOP-5I719O9]$ find . -maxdepth 1 -type f -name '????*' -print
[29/08/2022 - 14:59.57]~/desktop/unix/pract
[Abcd.DESKTOP-5I719O9]$ echo “./hello.sh” | at teatime
[29/08/2022 - 15:23.48]~/desktop/unix/pract
[Abcd.DESKTOP-7GLGQEA]$ tr -s ' ' < f1 hii hello
[29/08/2022 - 15:24.15]~/desktop/unix/pract
[Abcd.DESKTOP-5I719O9]$ awk 'BEGIN {print 4*3.14+6}'18.56
[31/08/2022 - 12:09.29]~/desktop/unix/pract
[Abcd.DESKTOP-5I719O9]$ tr ' ' '\n' <f1 | sort | uniq -u
18. Write a command to locate lines that begin and end with (.).
[31/08/2022 - 12:09.46]~/desktop/unix/pract
[Abcd.DESKTOP-5I719O9]$ grep '^\..*\.$' f2
11
TYBCA-DIV3-SEM-5 UNIX ROLL NO-246
19. write a command to display all lines that contains 2 or more ^symbol at beginning of line.
[31/08/2022 - 12:12.24]~/desktop/unix/pract
[Abcd.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/2022 - 12:17.15]~/desktop/unix/pract
[Abcd.DESKTOP-5I719O9]$ sed 's/he/she/' -e 's/hello/hii/' f1
21. Display those lines having exactly 10 alphabates from file f1.
[31/08/2022 - 12:18.36]~/desktop/unix/pract
[Abcd.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/2022 - 12:19.48]~/desktop/unix/pract
[Abcd.DESKTOP-5I719O9]$ cp f1 f2
23. Search those files from current directory which have more than 5 links.
[31/08/2022 - 12:23.30]~/desktop/unix/pract
[Abcd.DESKTOP-5I719O9]$ find . -links +5 –
[31/08/2022 - 12:23.33]~/desktop/unix/pract
[Abcd.DESKTOP-5I719O9]$ grep -v '[0-9]' f2
25. Replace all occurrences of “linux OS” with “unix OS” in file f1.
[31/08/2022 - 12:26.04]~/desktop/unix/pract
12
TYBCA-DIV3-SEM-5 UNIX ROLL NO-246
[31/08/2022 - 12:36.23]~/desktop/unix/pract
[Abcd.DESKTOP-5I719O9]$ grep '^[^ ]* [^ ]* user'
27. Display name of all files of working directory having pattern “The”.
[31/08/2022 - 12:27.00]~/desktop/unix/pract
[Abcd.DESKTOP-5I719O9]$ grep -l 'The' *
28. Display lines of file f1 that begin with any capital letter.
[31/08/2022 - 12:35.32]~/desktop/unix/pract
[Abcd.DESKTOP-5I719O9]$ grep '^[A-Z]' f3
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/2022 - 12:35.55]~/desktop/unix/pract
31. display only those lines of file f1 having length in between 30 to 50 characters.
[31/08/2022 - 12:39.07]~/desktop/unix/pract
[Abcd.DESKTOP-5I719O9]$ grep'^.\{10,20\}$' f3
13
TYBCA-DIV3-SEM-5 UNIX ROLL NO-246
33. Replace all occurrences of “hello” with “hi” and “he” with “she”.
[31/08/2022 - 12:17.15]~/desktop/unix/pract
[Abcd.DESKTOP-5I719O9]$ sed 's/he/she/' -e's/hello/hii/' f1
34. Count number of words and lines of files whose filename begins with x.
[31/08/2022 - 12:40.29]~/desktop/unix/pract
[Abcd.DESKTOP-5I719O9]$ find . -maxdepth 1 -type f -name 't*' -exec wc -wl {} \;
[31/08/2022 - 12:41.19]~/desktop/unix/pract
[Abcd.DESKTOP-5I719O9]$ sed -n '1,5!p' f3
ASSIGNMENT-1
1 Write a shell script which works like a calculator(enter the values via
command line argument)
14
TYBCA-DIV3-SEM-5 UNIX ROLL NO-246
case $ch in
1|+ )
ans=$(expr $num1 + $num2)
;;
2|\* )
ans=$(expr $num1 \* $num2)
;;
3|- )
ans=$(expr $num1 - $num2)
;;
4|/ )
if [ $num2 -eq 0 ]; then
echo "Error: Division by zero!"
exit 1
fi
ans=$(expr $num1 / $num2)
;;
*)
echo "Invalid option"
exit 1
;;
esac
echo "Answer: $ans"
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.
max_size=0
max_file=""
for file in *; do
if [ -f "$file" ] && [ "$file" != "$0" ]; then
file_size=$(stat -c%s "$file")
if [ $file_size -gt $max_size ]; then
max_size=$file_size
15
TYBCA-DIV3-SEM-5 UNIX ROLL NO-246
max_file=$file
fi
fi
done
if [ -n "$max_file" ]; then
echo "Largest file: $max_file"
echo "Size: $(du -h $max_file | awk '{print $1}')"
echo "Words: $(wc -w < $max_file)"
echo "Characters: $(wc -c < $max_file)"
echo "Lines: $(wc -l < $max_file)"
echo "Content:"
cat $max_file
else
echo "No files found!"
fi
hour=$(date +%H)
16
TYBCA-DIV3-SEM-5 UNIX ROLL NO-246
4 Write a shell script to copy one file into other, display appropriate
message if Destination file already exist.
if [ $# -ne 2 ]; then
echo "Usage: $0 source destination"
exit 1
fi
source=$1
destination=$2
if [ -f "$destination" ]; then
read -p "Destination file already exists. Overwrite? (y/n) " response
if [ "$response" = "y" ]; then
cp $source $destination
echo "File copied successfully!"
else
echo "Copy operation cancelled!"
fi
else
cp $source $destination
echo "File copied successfully!"
fi
17
TYBCA-DIV3-SEM-5 UNIX ROLL NO-246
6 Write a shell script to input a word in lower case and convert it into
upper case.
7 Write an awk script to print the fields 2,3,4 and 6 from test.txt file
which contains the pattern ‘unix’
test.txt
Linux unix 10 20 30 40 50
Windows 5 10 15 20 25
Unix linux 25 30 35 40 45
8 Write an awk script that displays first two-words of each line of the file
f1
18
TYBCA-DIV3-SEM-5 UNIX ROLL NO-246
10 Write an awk script which print frequency of each word in the input
file
19
TYBCA-DIV3-SEM-5 UNIX ROLL NO-246
20