0% found this document useful (0 votes)
2 views

lab-sheet-6

This document is a comprehensive guide for learning Linux commands, covering various topics such as file management, process handling, redirection, and encryption. It includes a series of exercises with specific commands to practice, as well as explanations of their functionalities. The document also outlines tasks related to creating and managing files, directories, and using public-private key encryption for secure communication.

Uploaded by

wovigej190
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

lab-sheet-6

This document is a comprehensive guide for learning Linux commands, covering various topics such as file management, process handling, redirection, and encryption. It includes a series of exercises with specific commands to practice, as well as explanations of their functionalities. The document also outlines tasks related to creating and managing files, directories, and using public-private key encryption for secure communication.

Uploaded by

wovigej190
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

lab-sheet-6

Problem-Sheet#6

Objec ve- To learn Linux Commands - PART B

Some more Linux commands included here related to redirec on, filters,process handling,informa on,
transla on,archiving, compression and encryp on etc.

Commands are given inside [.. ] and comments in (.. )

( Refer lab-sheet-6-help for these commands)

Files given in sfolder-6 folder are- infa,in ,infc,fsleep are executable files(In executable files- a infinite loop or
sleep for specific dura on- are implemented)

Q0.

Change password [passwd]

(It prompts to enter current password therea er new password and finally confirm new password)

(create 8 char password as given – first two digits , next one special char, next one lower one upper char, next
one special char and next one char and one digit)

(Assuming files referred in this sheet are in current path. If a file is not in current path then give its
appropriate path. For example path of infa from your UHD is- labsheet/sfolder-6/infa )

Q1.

i. Sort the output of 'ls' in reverse order [ls | sort -r]

ii. Count lines,words and characters of sorted output of 'ls' [ls | sort | wc]

iii. Count number of lines of sorted output of 'ls' [ls | sort | wc -l]

iv. Repeat previous command (executes Q1(iii)) [!!]

v. Sort the output of 'ls' , display it and also save in 'file61' [ls | sort | tee file61 ]

vi. Copy a file to mul ple(three) des na on(files), do not display the output on the screen [tee <file61
file{a..c}>/dev/null].

(ouput of file61 is goes to /dev/null instead screen and copies file1 to filea,fileb and filec )

vii. Create following nested structure of sub-folders -

aa

bb

cc

ee

ffa
ffc

dd

[mkdir -p aa/{bb,cc/ee/ff{a,b,c},dd} creates folders with ffa, and ffc as sibling inside ee. Sub folders bb,cc and
dd are sibling,inside folder aa]

Q2 redirec on

i. Display 'file61' [cat <file61] (same can be displayed using cat -n file61)

ii. Copy 'file61' to 'file62' using cat using both redirec on command [cat <file61 >file62]

iii. Run 'ls', 'tree' and 'date' commands as foreground process in sequence (type these commands separated by
";" a er last command press ENTER) [ls ; tree ; date]

iv. Run two commands with AND condi on- {if first valid command then only run second command} [hate &&
date]

v. Repeat Q2(v) with redirec ng standard error to file err1 [hate 2> err1]

vi. Run two commands with OR condi on- {execute a command if any of these commands valid} [hate || date]

vii. Run 'tree' and 'ls' command in background (send output to /dev/null) and date in foreground
[tree>/dev/null& ls>/dev/null& date;jobs]

Q3. (Give path of executable file 'infa' as labsheet/sfolder-6/infa OR ./infa in case these files copied in your
current directory)

i. Run the programme infa(a infinite loop with no output) [./infa] ,

Put the process running in foreground in suspended mode [ctrl+z] ,

Display processes [jobs] ,display process status using [ps -l] (T flags for stoped process)

Run the immediate previously suspended process [bg]

Bring the immediate background process in foreground [fg]

Terminate foreground process [ctrl+c]

ii. Repeat Q3(i) problem with three programms 'infa', 'in ' and 'infc' (run each in background) [./infa& ./in &
./infc&]

Display processes in current session [jobs]. To display stopped processes [jobs -s]

Bring programme 'infc' in foreground [fg %3] , then suspend it [ctrl+z].

Bring any other of these in foreground then block it

Display process [jobs].

Resume 'infc' in background [bg %3] or [bg 3]

Terminate a blocked process in background [kill -CONT pid-of-the-stopped-process]

Repeat above with other processes


iii. Stop a background nth process [stop job-number]

iv. Terminate a background (stopped or running) process [ kill job-number] or [kill pid-of-the-process-to-be-
terminated]

v. Execute a command using 'exec' and terminate the bash [exec ls]

vi. Execute a command using 'exec' in background (it will not terminate the bash) [exec sleep 15& ps -l]

vii. Terminate a foreground process [ctrl+c]

viii. Details about the host machine [hostnamectl ]

Q4.

i. Display real me(turnaround)taken by following commands- 'date', 'ls' and 'fsleep' [ me date ; me ls ; me
./fsleep]

(repea ng these commands may not give same me values )

ii. Sleep the shell for 10 seconds and display real me [echo "i am sleeping" ; me sleep 10] (CTRL-C will wakeup
the shell)

iii. Display message "my home directory" [echo "my home directory" $HOME]

iv. Append a line in 'file51' using echo [echo "last line added using echo">>file61]

v. Display environmental variable path [echo $PATH]

vi. Add your UHD at the beginning of PATH and verify it[ export PATH=/home/os23/23osxxxx:$PATH]

vii. Add your UHD at the end of PATH [export PATH=$PATH:/home/os23/23osxxxx

viii Display environmental variables [env] // ( 'printenv' command gives same effect)

ix. Display username(yourloginid) as prompt [PS1="\u$"] (use \u-username;\h-hostname;\w-cwd;\$-


normaluser )

x. Change color of prompt with style[PS1="\[\033[01;37m\]\u@h"] ( 30m to 37m gives different color,00 to 07
gives effect)

xi. Display system-calls used to execute 'ls' command [strace -c ls]

xii. Display(dump) binary file 'infa' in hex format [od -x ./infa]

Q5.

i. Create a zero sized file 'file6z' [touch file6z]

ii. Note down me stamp of the 'file62' [ls -l] then update me stamp of the file61 [touch file62]

iii. Display contents of 'lab-sheet-2-v2' [ more lab-sheet-2-v2] , display next page (press space bar , press q to
return to shell)

iv. Display contents of 'lab-sheet-2-v2' [less lab-sheet-2-v2) , display next 7 lines [7j], previous 3 lines [3k], go to
line with "minix "[/minix] , quit less u lity [q ]

v. Display first 15 lines of 'lab-sheet-2-v2' [head -n 15 lab-sheet-2-v2]

vi. Display last 5 lines of 'lab-sheet-2-v2' [tail -n 5 lab-sheet-2-v2]


vii. Sort the file 'file61' and display [sort file61]

viii. Sort the file 'file61' in reverse order and display [sort -r file61]

ix. Sort the file 'file61' without case sensi ve and output to 'file6s' [sort -f file51 -o file6s]

x. Create a file 'file63' having sorted tree output [tree | sort -o file63]

Q6

i. Display first ten lines of 'lab-sheet-2-file' [head lab-sheet-2-file]

ii. Split the file 'lab-sheet-2-file' in small sized(1000 Bytes) files(with filename beginning with 'zoo') [ split -
b1000 qsheet/lab-sheet-2-file zoo ]

iii. Display 'zooaa', 'zooab' and 'zooac' [cat zooaa zooab zooac]

iv. Concatenate files having star ng name 'zoo' to 'file64' [ cat zoo* > file64 ]

v. Display lines and linenumber having word 'Linus' in 'lab-sheet-2-file' [grep -n Linus lab-sheet-2-file]

vi. Display output of 'tree' of parent folder of UHD having character 'c' [tree ../ | grep c]

vii. Display the Linux process [top]; press q to terminate, Note down PR (priority) and NI (nice value)

viii. Repeat Q5(vii) with lower priority [nice -n 10 top]. Compare the output of vii and viii.

Q7.

ia. Display all processes (or first few) with details [ps -el] or [ps -el | head]

ib. Display user processes and its pid [ps]

ii. Display process tree related to you(yourid processes) [pstree -u <yourid>]

iii. Sleep shell for 10 seconds and display process tree [echo "i am sleeping"],[ me sleep 10& pstree]

iv. Dynamically display processes status (press q to return back to shell) [top]

va. Display path of the 'ls' command [which ls]

vb. Display informa on of a command [info ls]

vi. Display path of the 'stdio.h' file [whereis stdio.h]

vii. Display calendar of October 2023 [cal 10 2023]

viii. Display calendar of your birth year [cal birthyear]

ix. Translate(encrypt based on rotate13) keyboard input [cat |tr '[a-m][n-z][A-M][N-Z]' '[n-z][a-m][N-Z][A-M]' ]

(type a word or sentence , press enter. It encrypts input based on rotate13. use CTRL-C to stop )

(simple encryp on-rotate13-each character is shi ed by thirteen posi on in alphabet)

x. Change lower to upper case of characters in 'file61' [tr '[a-z]' '[A-Z]' < file61]

(for keyboard input use: tr '[a-z]' '[A-Z]' )

xi. Generate four random pa ern of eight characters from given pa ern
[cat /dev/urandom| tr -dc 'a-zA-Z0-9' | fold -w 8| head -n 4 ]

xii. Generate four random pa ern of twelve characters using your name, DOB and special chars

Q8

i. Create archive of folder 'draa' [ tar cf draa.tar draa ]

ii. Display files in 'draa.tar' (tar draa.tar)

iii. Create a folder 'uhd' and cd to 'uhd'. Extract the files from 'draa.tar' in folder 'uhd' [tar xf ../draa.tar ]

(return back to UHD)

iv. Append folder 'drba' to 'draa.tar' [tar rf draa.tar drba]

v. Delete 'file4h' from 'draa.tar' [tar f draa.tar –-delete file4h] (use appropriate path for file4h)

vi. Delete contents of folder 'drbc' from 'draa.tar' [tar f draa.tar –-delete drba/drbb/drbc/*]

vii. List the files in 'draa.tar' [tar draa.tar] and confirm Q8(vi)

viii. Create archive of folder 'drba' using gzip compression algo [tar czf drba.tar.gz drba]

ix. Compress files of folder 'drca' recursively with gzip [gzip -r drca] (does not affects symbolic files)

( Verify the compressed files in folder 'drca' using tree)

x. Verify the integrity of the compressed file {gzip -tv file.gz]

xi. Uncompress files in folder 'drca' recursively 'drca.gz' [gzip -r -d drca]

( Verify the uncompressed files in folder 'drca' using tree)

x1i. Repeat Q7(ix) with low compression [gzip -r -1 drca] ;verify with 'ls -l' ; uncompress files

xi1i. Repeat Q7(ix) with high compression [gzip -r -9 drca] ;verify with 'ls -l' ; uncompress files

xiv. Compress folder 'draa' and encrypt it with password (it prompts to enter password) [zip -r -e draa.zip draa]

xv. Unzip encrypted 'draa.zip' [unzip draa.zip]

Q9 Public -private key for secure communica on

Share public key with others. Others can encrypt message using your public key. Only you can decrypt this
message using your private key. You can encrypt a message using your friend(s) public key(s). Only the person
whose public key is used for encryp on can dectrypt the message using his/her private key.

These keys are stored in keyring. The private key is protected by passphrase. Whenever you use private key,it
prompts you to enter passphrase to validate yourself. Never share private key.

i. Create public key and private key pair using gngpg applica on

(provide your emailid [email protected] ,dura on 1 yr of validity of the key and password for protec ng
your private key)

[gpg --gen-key ]

ii. List keys in keyring [gpg --list-keys]


iii. Export public key in a text file (key id is your name/emailid/last-8bytes-of-fingerprint)

[gpg --output yourloginid-pub.asc --armor --export <your_keys_user_ID> ]

(check if te file is created using ls command)

iv. Send your public key to others (put in /tmp) and retrieve others public key in your current directory

[ cp yourloginid-pub.asc /tmp (put your publickey in /tmp)

cp /tmp/someone-pub.asc . (get others publickey from /tmp)

v. Import others public key in keyring

[gpg --import /tmp/someone-pub.asc (put others publickey in keyring)]

vi. List keys in keyring [gpg --list-keys]

vii. Write a message in a text (xxxx1.txt) file and encrypt this for one of classmate whose public key in your
keyring

[ cat >xxx.txt ......... crtl+d (create text file having message for others)

gpg --encrypt --armor --recipient friendmailid xxxx1.txt (friendmailid is iden fies the respec ve public key )

(create encrypted file xxxx1.txt.asc)

viii. Encrypt your text message for more than one classmates whose public key in your keyring

[gpg --encrypt --armor --recipient friendmailid1 --recipient friendmailid2 xxxx1.txt.asc] (repeat --recipient for
as many friends)

ix. Send the encrypted message to others (put in /tmp) and retrieve encrypted message(s) in your current
directory

[cp xxxx1.txt.asc /tmp ]

x. Try to decrypt the others message.(IF it is encrypted for you, you will able to read)

[cp /tmp/someother.asc . ](get the encrypted text file sent by others for others , it may or not may be you)

[gpg --decrypt somexxxx.txt.asc] (if this file is encrypted for you, it prompts for passpharse to use your
privatekey)

[ cat somexxxx.txt ] (read the decrypted file. this message is for you)

xi. Repeat vii -x to exchange confiden al messages with your friends.

You might also like