Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
152 views

Unix Assignment 1

The document contains 7 questions and answers about UNIX commands. Question 1 asks how to list all files in subfolders and the answer is "ls -R". Question 2 asks how to search files for a string and the answer is "grep" inside a for loop. Question 3 asks how to rename all files with a prefix and the answer uses "mv" inside a for loop to add "Unix_" to each file name.

Uploaded by

Aarthi Asohkumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
152 views

Unix Assignment 1

The document contains 7 questions and answers about UNIX commands. Question 1 asks how to list all files in subfolders and the answer is "ls -R". Question 2 asks how to search files for a string and the answer is "grep" inside a for loop. Question 3 asks how to rename all files with a prefix and the answer uses "mv" inside a for loop to add "Unix_" to each file name.

Uploaded by

Aarthi Asohkumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

UNIX ASSIGNMENT Question 1: Write a command to list all the files inside a folder i.e.

if there is a folder inside a folder then it should list all files inside the sub-folder which is inside the folder to be listed. Answer 1: ls -R Question 2:Search all the files which contains a particular string, say thin a folder. Answer 2: for i in *.*; do grep -l include $i; done include wi

Question 3: Rename all the files within a folder with suffix Unix_ i.e. suppose a folder has two files a.txt and b.pdf than they both should be renamed from a sin gle command to Unix_a.txt and Unix_b.pdf Answer 3: for i in *.*; do mv $i Unix_$i; done ;

Question 4: Rename all files within a folder with the first word of their conten t(remember all the files should be text files. For example if a.txt contains Unix is an OS in its first line then a.txt should be renamed to Unix.txt Answer 4 : for i in *.txt; do j= "$(head -1 $i "; done cut -f1-d" ").txt"; mv "$i" "$j

Question 5 : Suppose you have a C project in a folder called project , it contains .c and .h files, it also contains some other .txt files and .pdf files. Write a Linux command that will count the number of lines of your text files. That means total line count of every file. (remember you have to count the lines in .txt f iles only) Answer 5 : wc -l *.txt

Question 6 : Rename all files which contain the sub-string 'foo', replacing it w ith 'bar' within a given folder. Answer 6 :for i in ./*foo*; do mv "$i" "${i//foo/bar}";done

Question 7 : Show the most commonly used commands from history . [hint: remember th e history command, use cut, and sort it. Answer 7 : history cut -f6- -d" " sort

You might also like