Write UNIX commands to perform the following tasks. Each task must use exactly one line of command. Unless stated otherwise, all files are assumed to be at your current working directory.
a) (10 points) Print your current working directory into a file named file1
b) (10 points) Assume that your current working directory is NOT the same as your home directory. Print all file and subdirectory names of your home directory into a file named file2.
c) (15 points) Copy the text from line 11 to line 20 of a given file input.txt and append the copied text at the end of the file output.txt. (Assume that the file input.txt contains more than 20 lines of text contents)
d) (10 points) Count and display the total number of words in ALL files in the current working directory whose name end with “.txt”
e) (10 points) Count the number of lines where the word “foo” appears in a given file bar.txt
a)$ pwd >file1
// output of the command pwd(present working directory) is redirected to file1 using ouput redirection operator(>)
$ cat file1
//cat command displays contents of file1
b)
ls -R /home/user >file2
//ls -R(recursively find all directories of home directory(/home/user) and the output is redirected to file2)
c)
head -n 20 input.txt | tail -10 >> output.txt
head command select first 20 lines from input.txt ,this is passed to next command(tail -10) which select last 10 lines and it is appended to output.txt using >> operator d)
e)grep -c "foo" bar.txt
grep command searches for a pattern "foo" in the file bar.txt and print the lines containing it
option -c returns the count ie no of lines containing the pattern "foo"
Get Answers For Free
Most questions answered within 1 hours.