├── README.md └── tip_hints.md /README.md: -------------------------------------------------------------------------------- 1 | # Unix / Linux Interview Questions 2 | > This repo contains questions that have been taken from various sources. Most of these involve the use of two or more commands. 3 | > I have used **GIT Bash** to execute the below scripts. 4 | > 5 | > **Difficulty Level:** Beginner to Intermediate 6 | > 7 | >*All the Best !!!* 8 | 9 | ## Test Commands 10 | 11 | [Tip n Hints](/tip_hints.md) 12 | 13 | | Option | Description | 14 | | ------| -----------:| 15 | | data | path to data files to supply the data that will be passed into templates. | 16 | | engine | engine to be used for processing templates. Handlebars is the default. | 17 | | ext | extension to be used for dest files. | 18 | 19 | 20 | First Header | Second Header | Second Header | centre 21 | ------------ | ------------- | -------------: | :-------------: 22 | Content cell 1 | Content cell 2 | RitghtContent cell 2 | centreContent cell 2 23 | Content column 1 | Content column 2 | engine to be used for processing templates. Handlebars is the default. | engine to be used for processing templates. Handlebars is the default. 24 | 25 | ## References 26 | - http://www.folkstalk.com/ 27 | - http://www.thegeekstuff.com/ 28 | - http://www.theunixschool.com/ 29 | 30 | ## Question List 31 | 32 | - [ ] [1. Delete blank lines in file](#1-delete-blank-lines-in-file)
33 | - [x] [2. Print number of times each word appears in a file](#2-print-number-of-times-each-word-appears-in-a-file)
34 | [3. List all the files in directory except .txt and .pdf](#3-list-all-the-files-in-directory-except-txt-and-pdf)
35 | [4. Rename all .jpg files to .jpeg](#4-rename-all-jpg-files-to-jpeg)
36 | [5. Delete files of size more than 100mb in a folder which are older than 90 days](#5-delete-files-of-size-more-than-100mb-in-a-folder-which-are-older-than-90-days)
37 | [6. Reverse the words in a sentence](#6-reverse-the-words-in-a-sentence)
38 | [7. Given an input file having lines with alphabets and numbers. Print only the distinct alpha-numberic lines.](#7-given-an-input-file-having-lines-with-alphabets-and-numbers-print-only-the-distinct-alpha-numberic-lines)
39 | [8. Given a comma separated input file with items, category and cost. Display the category and its sum.](#8-given-a-comma-separated-input-file-with-items-category-and-cost-display-the-category-and-its-sum)
40 | [9. Output of following command `ps -ef | awk '{ print $1}' | sort | uniq | wc -l`](#9-output-of-following-command-ps--ef--awk--print-1--sort--uniq--wc--l)
41 | [10. Kill process based on user and process.](#10-kill-process-based-on-user-and-process)
42 | [11. Print contents of a file starting from the nth line.](#11-print-contents-of-a-file-starting-from-the-nth-line)
43 | [12. Write a command to calculate the total size of all pdf files in the directory.](#12-write-a-command-to-calculate-the-total-size-of-all-pdf-files-in-the-directory)
44 | [13. Write a command to delete the first and last line in every file in the directory and rename it.](#13-write-a-command-to-delete-the-first-and-last-line-in-every-file-in-the-directory-and-rename-it)
45 | [14. Given a file with records. Print the sum of m where name=aman](#14-given-a-file-with-records-print-the-sum-of-m-where-nameaman)
46 | [15. Given a file with numbers. Sort each line.](#15-given-a-file-with-numbers-sort-each-line)
47 | [16. Bash shell script to reverse a string / word.](#16-bash-shell-script-to-reverse-a-string--word)
48 | [17. Reverse last 4 digits of a string.](#17-reverse-last-4-digits-of-a-string)
49 | 50 | ### 1. Delete blank lines in file 51 | ```bash 52 | $ sed '^\s*$'/d' file.txt 53 | ``` 54 | To save changes back to file 55 | ```bash 56 | $ sed -i '^\s*$'/d' file.txt 57 | ``` 58 | Also 59 | ```bash 60 | $ grep -v '^\s*$' file.txt 61 | ``` 62 | ### 2. Print number of times each word appears in a file 63 | ```bash 64 | $ cat file | tr '[:space:]' '[\n*]' | grep -v '^\s*$' | sort | uniq -c | sort -bnr 65 | ``` 66 | 67 | `tr` just replaces spaces woth newlines 68 |
`grep -v '^\s*$'` trims out empty lines 69 |
`sort` to prepare as input for uniq 70 |
`uniq -c` to count occurrences 71 |
`sort -bnr` sorts in numeric reverse order while ignoring whitespace 72 | ### 3. List all the files in directory except .txt and .pdf 73 | ```bash 74 | $ ls -I "*.txt" -I "*.pdf" 75 | ``` 76 | If you want to iterate across all the subdirectories: 77 | ```bash 78 | $ ls -I "*.txt" -I "*.pdf" -R 79 | ``` 80 | Also 81 | ```bash 82 | $ find -not -iname "*.txt" 83 | $ find . ! '(' -name '*.txt' -o -name '*.pdf' ')' 84 | ``` 85 | Reference: https://unix.stackexchange.com/questions/47151/how-do-i-list-every-file-in-a-directory-except-those-with-specified-extensions 86 |
87 | Back To Top 88 |
89 | 90 | ### 4. Rename all .jpg files to .jpeg 91 | ```bash 92 | $ vi image.sh 93 | 94 | for x in `ls ./images/`; 95 | do 96 | a=`echo $x | cut -d'.' -f1` 97 | mv ./images/$x ./images/$a".jpeg"; 98 | done 99 | 100 | $ ./image.sh 101 | ``` 102 | ### 5. Delete files of size more than 100mb in a folder which are older than 90 days 103 | ```bash 104 | $ find /folder -size +204800 -mtime +90 -exec rm {} \; 105 | ``` 106 | ### 6. Reverse the words in a sentence 107 | ```bash 108 | $ echo "Have a good day" | awk '{ for(i=0;i 112 | Back To Top 113 | 114 | 115 | ### 7. Given an input file having lines with alphabets and numbers. Print only the distinct alpha-numberic lines. 116 | **Input** 117 | ``` 118 | $ cat input.txt 119 | 120 | 1234567890 121 | 0987654321 122 | ABCDEFGH 123 | 123456789X 124 | 1234567890 125 | 123456789X 126 | ``` 127 | **Output** 128 | ``` 129 | 123456789X 130 | ``` 131 | ```bash 132 | $ vi script.sh 133 | 134 | #!/bin/bash 135 | awk ' 136 | BEGIN { 137 | i=0; 138 | } 139 | { 140 | if (($1 ~ /[0-9]/) && ($1 ~ /[A-Z]/)) { 141 | flg=0; 142 | for (x = 0; $x < $i; x=$x+1) { 143 | if($arr[$x] == $1) { 144 | flg=1; 145 | break; 146 | } 147 | } 148 | if(flg==0) { 149 | arr[$i] = $1; i=$i+1; 150 | } 151 | } 152 | } 153 | END { 154 | for (x = 0; x < $i; x++) 155 | print $arr[0] 156 | }' input.txt | uniq 157 | 158 | $ ./script.sh 159 | ``` 160 |
161 | Back To Top 162 |
163 | 164 | ### 8. Given a comma separated input file with items, category and cost. Display the category and its sum. 165 | **Input** 166 | ``` 167 | $ cat input.txt 168 | 169 | item1,category1,200 170 | item2,category2,100 171 | item3,category3,300 172 | item4,category1,400 173 | item5,category2,500 174 | item6,category1,600 175 | ``` 176 | **Output** 177 | ``` 178 | category1 - 1200 179 | category2 - 600 180 | category3 - 300 181 | ``` 182 | ```bash 183 | $ vi script.sh 184 | 185 | awk ' 186 | BEGIN{ 187 | x=0; 188 | flag=0 189 | } 190 | { 191 | split($0,arr,",") # split - arr[1] has item, arr[2] has category, arr[3] has cost 192 | flag=0; 193 | for(i=0;i<=x;i++){ 194 | if(arr[2]==cat_arr[i]){ 195 | flag=1; 196 | sum_arr[i]=sum_arr[i]+arr[3] 197 | } 198 | } 199 | if(flag==0){ 200 | cat_arr[x]=arr[2] 201 | sum_arr[x]=arr[3] 202 | x++; 203 | } 204 | } 205 | END { 206 | for(i=0;i 214 | Back To Top 215 | 216 | 217 | ### 9. Output of following command `ps -ef | awk '{ print $1}' | sort | uniq | wc -l` 218 | **Answer** 2 219 | ```bash 220 | $ ps -ef 221 | UID PID PPID TTY STIME COMMAND 222 | prabakad 20464 1 ? Jul 13 /usr/bin/mintty 223 | prabakad 4940 18944 pty2 Jul 14 /usr/bin/bash 224 | prabakad 18944 1 ? Jul 14 /usr/bin/mintty 225 | prabakad 19944 20464 pty1 Jul 13 /usr/bin/bash 226 | prabakad 11492 4940 pty2 Jul 14 /usr/bin/winpty 227 | prabakad 20708 16868 pty0 20:38:09 /usr/bin/ps 228 | prabakad 16868 13672 pty0 Jul 12 /usr/bin/bash 229 | prabakad 13672 1 ? Jul 12 /usr/bin/mintty 230 | 231 | $ ps -ef | awk '{ print $1}' 232 | UID 233 | prabakad 234 | prabakad 235 | prabakad 236 | prabakad 237 | prabakad 238 | prabakad 239 | prabakad 240 | prabakad 241 | prabakad 242 | 243 | $ ps -ef | awk '{ print $1}' | sort | uniq 244 | prabakad 245 | UID 246 | 247 | $ ps -ef | awk '{ print $1}' | sort | uniq | wc -l 248 | 2 249 | ``` 250 |
251 | Back To Top 252 |
253 | 254 | ### 10. Kill process based on user and process. 255 | - Using grep to search 256 | ```bash 257 | $ ps -ef | grep "bash" | grep "prabakad"| awk 'BEGIN{print $2}{kill -9 $2} 258 | 259 | $ ps -ef | grep "bash" | grep "prabakad"| awk '{print $2}' | xargs kill -9 260 | ``` 261 | - Using ps command with -u and -p 262 | ```bash 263 | $ ps -ef -u prabakad -p 8552 | tail -n +2 | awk '{print $2}' | xargs kill -9 264 | ``` 265 | Explanation 266 | ```bash 267 | $ ps -ef 268 | UID PID PPID TTY STIME COMMAND 269 | prabakad 19208 18800 pty0 21:46:39 /usr/bin/bash 270 | prabakad 11440 19208 pty0 21:46:43 /usr/bin/ps 271 | prabakad 18800 1 ? 21:46:38 /usr/bin/mintty 272 | 273 | $ ps -ef -u prabakad -p 19208 274 | UID PID PPID TTY STIME COMMAND 275 | prabakad 19208 18800 pty0 21:46:39 /usr/bin/bash 276 | 277 | $ ps -ef | grep "bash" | grep "prabakad" 278 | prabakad 19208 18800 pty0 21:46:39 /usr/bin/bash 279 | prabakad 19484 19208 pty0 21:47:52 /usr/bin/bash 280 | ``` 281 | `grep` command does not give header.
282 | `ps` command gives header, so use `tail` command to remove it.
283 | The PIDs are sent as arguments using `xargs` and killed using `kill -9` 284 |
285 | Back To Top 286 |
287 | 288 | ### 11. Print contents of a file starting from the nth line. 289 | For example, print the give file starting from the 4th line 290 | 291 | **Input** 292 | ```bash 293 | $ cat lines.txt 294 | line1 295 | line2 296 | line3 297 | line4 298 | line5 299 | line6 300 | line7 301 | line8 302 | line9 303 | line10 304 | line11 305 | ``` 306 | - Using tail command 307 | ```bash 308 | $ cat lines.txt | tail -n +4 309 | line4 310 | line5 311 | line6 312 | line7 313 | line8 314 | line9 315 | line10 316 | line11 317 | ``` 318 | - Using sed command 319 | ```bash 320 | $ sed '1,3d' lines.txt 321 | line4 322 | line5 323 | line6 324 | line7 325 | line8 326 | line9 327 | line10 328 | line11 329 | ``` 330 | Additional Information 331 | 332 | `head` command displays first n lines.
333 | `tail` command displays last n lines. If `tail` is used with `+`, displays lines starting from nth line as shown above.
334 | The `sed` command above deletes the first 3 lines and prints output starting from 4th line. 335 |
336 | Back To Top 337 |
338 | 339 | ### 12. Write a command to calculate the total size of all pdf files in the directory. 340 | ```bash 341 | $ du -hc *.pdf | tail -1 342 | 25K total 343 | ``` 344 | ### 13. Write a command to delete the first and last line in every file in the directory and rename it. 345 | **Input** 346 | ```bash 347 | $ ls ./sample 348 | abc_20171607.txt rocket_20171503.txt xyz_20171507.txt 349 | mno xyz_20171507.txt sample1_20171607.txt 350 | 351 | $ cat ./sample/abc_20171607.txt 352 | header 353 | line1 354 | line2 355 | line3 356 | footer 357 | ``` 358 | **Output** 359 | ```bash 360 | $ ls sample 361 | renamed_abc_20171607.log renamed_sample1_20171607.log 362 | renamed_mno xyz_20171507.log renamed_xyz_20171507.log 363 | renamed_rocket_20171503.log 364 | 365 | $ cat ./sample/renamed_abc_20171607.log 366 | line1 367 | line2 368 | line3 369 | ``` 370 | ```bash 371 | $ vi sample.sh 372 | 373 | IFS=$'\n' 374 | for filename in `ls ./sample -I '*.sh'`; # ls in sample folder searching file other than .sh 375 | do 376 | newfilename=`echo $filename | cut -d '.' -f1` 377 | sed -i -e '1d' -e '$d' ./sample/$filename # -i to save to file. '1d' and '$d' delete first and last line 378 | mv ./sample/$filename ./sample/"renamed"_$newfilename".log" 379 | done 380 | 381 | $ ./sample.sh 382 | ``` 383 | Additional 384 | 385 | If `IFS=$'\n'` (Internal Field Separator) is missing, file names having space in them are not recognised. 386 | ```bash 387 | $ ./sample.sh 388 | abc_20171607.txt 389 | mno 390 | xyz_20171507.txt 391 | rocket_20171503.txt 392 | sample1_20171607.txt 393 | xyz_20171507.txt 394 | 395 | ``` 396 |
397 | Back To Top 398 |
399 | 400 | ### 14. Given a file with records. Print the sum of m where name=aman 401 | **Input** 402 | ```bash 403 | $ cat input.txt 404 | t=20 405 | m=10 406 | name=aman 407 | --xx-- 408 | t=30 409 | m=20 410 | name=deep 411 | --xx-- 412 | t=40 413 | m=30 414 | name=aman 415 | --xx-- 416 | t=40 417 | m=300 418 | name=aman 419 | --xx-- 420 | t=40 421 | m=10 422 | name=rahul 423 | ``` 424 | **Output** 425 | ``` 426 | sum of aman is 340 427 | ``` 428 | ```bash 429 | $ vi script.awk 430 | 431 | BEGIN { 432 | RS="--xx--\n"; 433 | FS="\n"; 434 | } 435 | { 436 | split($3,data,"=") 437 | if(data[2]=="aman"){ 438 | split($2,mdata,"=") 439 | sum=sum+mdata[2] 440 | } 441 | } 442 | END{ 443 | print ("sum of aman is",sum) 444 | } 445 | 446 | $ awk -f script.awk input.txt 447 | ``` 448 | Reference: http://www.thegeekstuff.com/2010/01/8-powerful-awk-built-in-variables-fs-ofs-rs-ors-nr-nf-filename-fnr/ 449 |
450 | Back To Top 451 |
452 | 453 | ### 15. Given a file with numbers. Sort each line. 454 | **Input** 455 | ```bash 456 | $ cat input.txt 457 | 4 6 8 1 7 458 | 2 12 9 6 10 459 | 6 1 14 5 7 460 | ``` 461 | **Output** 462 | ``` 463 | 1 4 6 7 8 464 | 2 6 9 10 12 465 | 1 5 6 7 14 466 | ``` 467 | ```bash 468 | $ awk ' {split( $0, a, " " ); asort( a ); for( i = 1; i <= length(a); i++ ) printf( "%s ", a[i] ); printf( "\n" ); }' input.txt 469 | ``` 470 | Reference: http://www.unix.com/shell-programming-and-scripting/180835-sort-each-row-horizontally-awk-any.html 471 | ### 16. Bash shell script to reverse a string / word. 472 | ```bash 473 | $ vi reverse.sh 474 | 475 | #!/bin/bash 476 | input="$1" 477 | reverse="" 478 | len=${#input} 479 | for (( i=$len-1; i>=0; i-- )) 480 | do 481 | reverse="$reverse${input:$i:1}" 482 | done 483 | echo "$reverse" 484 | 485 | $ ./reverse.sh Hello 486 | olleH 487 | ``` 488 | Also 489 | ```bash 490 | $ echo "Hello" | rev 491 | olleH 492 | 493 | $ rev<<<"This is a test" 494 | tset a si sihT 495 | ``` 496 | Reference: https://www.cyberciti.biz/faq/how-to-reverse-string-in-unix-shell-script/ 497 |
498 | Back To Top 499 |
500 | 501 | ### 17. Reverse last 4 digits of a string. 502 | **Input** 503 | ``` 504 | 123456****7890 505 | ``` 506 | **Output** 507 | ``` 508 | 123456****0987 509 | ``` 510 | ```bash 511 | $ vi rev.sh 512 | 513 | input="$1" 514 | reverse="" 515 | 516 | len=${#input} 517 | 518 | for((i=0;i<$len-4;i++)) 519 | do 520 | { 521 | reverse="$reverse${input:$i:1}" 522 | } 523 | done 524 | for((i=len-1;i>=len-4;i--)) 525 | do 526 | { 527 | reverse="$reverse${input:$i:1}" 528 | } 529 | done 530 | 531 | echo $reverse 532 | 533 | $ ./rev.sh 123456****7890 534 | ``` 535 | -------------------------------------------------------------------------------- /tip_hints.md: -------------------------------------------------------------------------------- 1 | # Tip & Hints 2 | --------------------------------------------------------------------------------