Saturday 21 January 2012

Linux Commands, Emacs killed data recover Buffer,rm * command deletes all files


1. Write command to display following text in a shell prompt. Explain your command.

Hi $2, I’m number * (guess)! 20 Marks


The text to be displayed contains special characters, which are generally used in a shell to

perform specific functions. In this case it is required that shell interpret them as characters and

thus it is required that these characters be “escaped”, i.e., preventing shell to try executing

them. Special characters can be escaped by two ways:

i) By placing \ before the special character. In this case we can give the following

command to generate the desired output:

echoHi\$2,I\’m number\*\(gues\)\!

ii) By placing the statement within single quotes, since single quotes can escape any

enclosed character. However, single quotes can not escape itself. Since one single quote

is present in the desired statement, the whole statement can not just be enclosed in

single quotes. Therefore, we can enclose single quote in double quote or escape it using

\.

echo‘Hi$2,I’\’m number*(gues)!’ {Between I and m, first there is

a single quote to close single quote started before Hi, then \ to escape next

single quote and then an opening single quote which is closed after !}

echo‘Hi$2,I’”’”’m number*(gues)!’ {Between I and m, first there is

a single quote to close single quote started before Hi, then a opening double

quote which enclose a single quote and then a closing double quote. Then there

is an opening single quote which is closed after !}

2. In emacs, killed data is stored in a buffer and yanking is used to recover this data when required.
What is this buffer called? How can we look at all the contents of this buffer? How can we
empty this buffer? 15 Marks


The buffer is called kill-ring.

The contents of buffer can be seen by writing kill-ring in a new line in the editor and while cursor

at its right end, press C-x C-e. C-x C-e evaluates LISt Processing (LISP) expression from start of

the line to the current cursor position. The echo area displays the output of the expression.

Evaluating a buffer name in LISP outputs its contents. Hence, in this case evaluating kill-ring

displays its contents in the echo area. M-: is also used to evaluate LISP expression, which means

M-: kill-ring can be used to list its contents. Contents can also be displayed by C-h v kill-ring.

Contents of this buffer can be cleared by writing appropriate expression in LISP and evaluating

it. Expression to clear kill ring is (set ‘kill-ring ‘nil) OR (setq kill-ring ‘nil). Hence, write either one

of them in the editor and while cursor at its right end, press C-x C-e. Kill-ring can also be cleared

by M-: (set ‘kill-ring ‘nil) OR M-: (setq kill-ring ‘nil).

3. Use touch command to create a new file named –i. How different this command is from usual usage of touch to create files? How can you remove the file named –i? 15 Marks

touch command is used to create new blank files or “touch” existing files, i.e., just updating

modification time without modifying file contents. Usual format for touch is:

touch

filename

However, in this case file name has a special character -. Shell expects character(s) implying

option(s) after -. -- can be added before –i filename, since -- implies end of options and shell

expects a filename after that. Thus, command to create file named –i is given as:

touch -- -i

Similarly, this file can be removed by:

rm -- -i

4.
rm *
command deletes all files in the current directory. Does it delete directories and hidden files? If not, what changes in the command are required to delete all contents of working
directory including directories and hidden files. What happens if there are write protected files in the directory? How can we make sure that we delete all write protected files as well, without being asked for confirmation? 20 Marks


rm * does not delete directories and hidden files. In order to delete directories –r option is used

and in order to delete hidden files .* is required in addition to *. Hence command to delete all

directories, files and hidden files is:

rm –r * .*

However, this will also try deleting . and .. entries in the directory. Since, these two can not be

deleted, shell will give error messages that they can not be deleted. Nevertheless, all other files

and directories will be deleted. In order to avoid getting these error messages, following

command can be used:

rm –r .[^.]* *

This will delete everything except . and .. entries. In case there are write protected files, shell

will ask for confirmation for each file before deleting it. In order to avoid getting this

confirmation, -f option can be used which force shell to delete write protected files without

asking for confirmation.

Including –f option, the rm command can be given as:

rm –rf .[^.]* *

5. Explain output of following commands:

echo ‘testing’ > test

grep t test > test

cat test 15 Marks


testing is saved in the file named test. grep command redirects its output to test file as well.

When output redirection is used, a file is created or overwritten before evaluating the

command. Thus, in this case first a test file is overwritten and then t is searched in its contents.

Since, it is now an empty file grep can not find t in it and hence test remains an empty file. Last

command confirms that test is empty by giving no output.

6. Explain the output of following command:

wc –m *|sort –nr|head -2|tail -1

What will be the difference if we use –r instead of –nr. 15 Marks


Multiple pipes are used in the command. First one wc –m * lists character count for each file

and total number of characters in all the files present in the working directory. sort –nr reverse

numerically sorts them, so that total number of characters in all files come at the top as total

has to be higher than any individual entry. The second line would be the file containing highest

number of characters. The combination of head and tail displays the second line, i.e., the file

containing highest number of characters.

0 comments:

Post a Comment

 
Free Host | lasik surgery new york