0% found this document useful (0 votes)
112 views

Unix Assignments 1

The document contains a series of questions and answers related to Linux commands. It demonstrates how to: 1) List all files recursively in a folder and subfolders using ls -R; 2) Search for files containing a particular string like "include" using grep; 3) Rename all files in a folder prefixing with "Unix_" using a for loop.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views

Unix Assignments 1

The document contains a series of questions and answers related to Linux commands. It demonstrates how to: 1) List all files recursively in a folder and subfolders using ls -R; 2) Search for files containing a particular string like "include" using grep; 3) Rename all files in a folder prefixing with "Unix_" using a for loop.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 6

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. ANS:alien@CommandCenter:~/unix$ ls -R Output: .: copyu incl ./copyu: family familysort names.txt Question 2: Search all the files which contains a particular string, say 'include' within a folder. ANS: alien@CommandCenter:~/copyu$ grep -i 'include' ./* output: ./incl.txt:includes various imput for the text fiels ./incl.txt:include() ./incl.txt:The include() statement includes and evaluates the specified file. ./incl.txt:Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include() will finally check in the calling script's own directory and the current working directory before failing. The include() construct will emit a warning if it cannot find a file; this is different behavior from require(), which will emit a fatal error. ./incl.txt:If a path is defined whether absolute (starting with a drive letter or \ on Windows, or / on Unix/Linux systems) or relative to the current directory (starting with . or ..) the include_path will be ignored altogether. For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file. ./incl.txt:For more information on how PHP handles including files and the include path, see the documentation for include_path. 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 single command to Unix_a.txt and Unix_b.pdf final incl.txt list1 list1.txt list2

ANS: alien@CommandCenter:~/copyu$ for file in *; do mv "$file" unix_"$file" ; done OUTPUT:alien@CommandCenter:~/copyu$ ls Unix_family Unix_final Unix_list1 Unix_list2 Unix_familysort Unix_incl.txt Unix_list1.txt Unix_names.txt Question 4: Rename all files within a folder with the first word of their content(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 ANS: for i in *; do if [ ! -f $i ]; then echo "skipping $i"; else newname=`head -1 $i | sed 's/^\s*\([a-zA-Z0-9]\+\).*$/\1/'`; [ -n "$newname" ] && mv -i $i $newname.txt || echo "error at: $i"; fi; done OUTPUT: alien@CommandCenter:~/copyu$ ls alien.txt ilakkia.txt includes.txt movies.txt 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 files only) ANS:laien@CommandCenter:~/copyu$ wc l *.txt OUTPUT: wc: l: No such file or directory 2 10 90 alien.txt 12 9 71 ilakkia.txt 155 2497 15082 includes.txt 7 7 44 movies.txt 176 2523 15287 total Question 6 : Rename all files which contain the sub-string 'foo', replacing it with 'bar' within a given folder. ANS:alien@CommandCenter:~/copyu$ for file in ./*foo*;do mv -"$file" "${file/foo/bar}";done OUTPUT:alien@CommandCenter:~/copyu$ ls bar.txt ilakkia.txt includes.txt movies.txt Question 7: Show the most commonly used commands from history. [hint: remember the history command, use cut, and sort it]

ANS:lien@CommandCenter:~/copyu$ history | sort | cut -b1-72 -n OUTPUT: 10 ~ 100 for f in *.txt; do d="$(head -1 "$f") ;x=$(cut -d " " -f1 $f) 101 Read more: http://vineshbalan.blogspot.com/2011/05/tcsunix-assig 102 q 103 q 104 unix_Unix_xit 105 exit 106 clear 107 ls 108 cat 109 dir 110 ls 111 asdkjfhaskjdf 112 qfz 113 for f in *.txt; do d="$(head -1 "$f") ;x=$(cut -d " " -f1 $f) 114 Read more: http://vineshbalan.blogspot.com/2011/05/tcsunix-assig 115 for f in *.txt; do d="$(head -1 "$f") ;x=$(cut -d " " -f1 $f) 116 zskjfkldsfgndsf 117 gsdfg 118 sdfgksd;fg;sdkfjgsd 119 fdasf nsdf nsdf 11 ld 120 sdfgnl;sdfjgsdf 121 cd copyu/ 122 for f in *.txt; do d="$(head -1 "$f") ;x=$(cut -d " " -f1 $f) 123 cd copyu/ 124 for file in *.txt ; do d="$(head -1 "$file") ; x = " $ ( cut -d " 125 ls 126 done 127 . 128 .. 129 ./ 12 ls 130 ` 131 ~ 132 cd 133 dir 134 quit 135 esc 136 claear 137 clear 138 head -1 "$f" | awk '{print $1}' to <"$f" awk {print $1; exit}

139 for i in *; do if [ ! -f $i ]; then echo "skipping $i"; else newn 13 cd unix 14 ~ 140 cd copyu 141 for i in *; do if [ ! -f $i ]; then echo "skipping $i"; else newn 142 y 143 ls 144 for i in *; do if [ ! -f $i ]; then echo "skipping $i"; else newn 145 ls 146 wc l *.txt 147 for i in ./*foo*;do mv -- "$i" "${i//foo/bar}";done 148 Read more: http://vineshbalan.blogspot.com/2011/05/tcsunix-assig 149 ls 150 for file in ./*bar*;do mv -- "$file" "$ {file/bar/foo}";done 151 ls 152 for i in ./*foo*;do mv -- "$i" "${i//foo/bar}";done 153 ls 154 history|sort|cut d-10 -n 155 history|sort|cut - d-10 -n 156 history|sort|cut -d 10 -n 157 history|sort|cut -d 10 158 cut --help 159 history|sort|cut d-10 -n 15 cd ~ 160 history | sort | cut d-10 -n 161 history | sort | cut -b1-72 d-10 -n 162 history | sort | cut -b1-72 -n 16 cd .. 17 cd 18 cd ~/unix 19 ls ~ 1 ls 20 cd ' 21 exit 22 clear 23 cd 24 ls ~/.. 25 ls ~ 26 ls -a 27 mkdir copyu 28 cd copyu/ 29 dir 2 mkdir unix 30 ls 31 ls -a 32 cd ~/unix 33 ls

34 35 36 37 38 39 3 40 41 42 43 44 45 46 47 48 49 4 50 51 52 53 54 55 56 57 58 59 5 60 61 62 63 64 65 66 67 68 69 6 70 71 72 73 74 75 76 77 78 79 7 80

cd /home/alien/copyu/ cp /home/alien/unix/incl . ls cat incl less incl incl cd unix cd incl ls incl.txt cd incl.txt less incl less incl.txt head incl.txt tail incl.txt help tail cd . less incl.txt -n 15 clear less incl.txt -n 15 less incl.txt less incl.txt +n15 less incl.txt -n 15 wc -w incl.txt grep include incl.txt grep -c include incl.txt cat cd .. cat > list1.txt list1 less list1.txt cat list1 cat list1.txt cat > list2 cat list1.txt list2 > final cat final sort cat > family cd . sort < family cat < family > familysort cat familysort who who >names.txt sort < names.txt who | sort who | wc -l cat list1 cat list1.txt cd unix cp list1.txt list1

81 82 83 84 85 86 87 88 89 8 90 91 92 93 94 95 96 97 98 99 9

cat list1 list2 | grep i | sort cat list1 list2 | grep 'ilakkia' | sort cat list1 family | grep 'ilakkia' | sort cd .. cd unix/ cd .. ls -R cd unix ls -R pwd ls cd copyu/ ls who grep include ./* grep -i 'include' ./* for f in * ; do mv "$f" Unix_"$f" ; done ls for file in *; do mv "$file" unix_"$file" ; done ls cd

You might also like