Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Unix Assessment Question

1) How to display the 10th line of a file?

Cat filename |head -10|tail-1 (or)

Sed –n 10p filename

2) How to remove the header from a file?

Sed 1d filename

3) How to display the first 20 lines of a file?

Cat filename |head -20 (or)

Sed –n 1,20p filename

4) Write a command to print the fields from 10 to 20 from each line of a file?

Cut –c 10-20 filename

5) Write a command to replace the word "apple" with "(apple)" in a file?

Sed ‘s/”apple”/”(apple)”/q’ filename

6) How to display zero byte size files?

Find . –size 0

7) How to remove blank lines in a file ?

Grep –v ‘^s’ filename (or)

Sed ‘/^s/d’ filename

8) Display all the files in current directory sorted by size?

ls -Sl

9) Write a command to redirect the output of date command to multiple files?


10) How do you display from the 5th character to the end of the line from a file?

Cut –c 5- filename

11) Write a Shell Script that adds two numbers if provided as the command Line
Argument and if the two numbers are not entered, it outputs an Error Message

#!/bin/sh

A=10

B=20

If(A!null and B!null)

Then

Echo “sum is”a+b

Else

Echo “error message”

12) Write a shell Script to do the below operation,

1. Pass a file path and filename as argument

2. Check the file exist or not. If not, display an error message

FILE=(file path)

if [ -f "$FILE" ]; then

echo "$FILE exists."

else

echo "$FILE does not exist."

fi

3. If file path exists, create the file with file name passed as parameter
4. Write the content "Hello World" to the file

#!/bin/sh

Echo”Hello World”

13) Write the shell script that renames all files in the current directory that end in
“.jpg” to begin with today’s date in the following format: YYYY-MM-DD. For
example, if a picture of my cat was in the current directory and today was October
31,2016 it would change name from “mycat.jpg” to “2016–10–31-mycat.jpg”.

#!/bin/bash

DAY=$(date +%F)

cd /home/

for FILE in *.txt

do

mv $FILE ${DAY}-${FILE}

done

14) Write a shell script that displays “man”,”bear”,”pig”,”dog”,”cat”,and “sheep” on


the screen with each appearing on a separate line. Try to do this in as few lines as
possible

#!/bin/sh

Echo”man”

Echo”bear”

Echo”pig”

Echo”dog”

Echo”cat”

Echo”sheep”

You might also like