{"id":639,"date":"2019-09-27T16:16:35","date_gmt":"2019-09-27T15:16:35","guid":{"rendered":"https:\/\/blogs.gre.ac.uk\/cmssupport\/?page_id=639"},"modified":"2019-09-27T16:16:35","modified_gmt":"2019-09-27T15:16:35","slug":"unixviewing","status":"publish","type":"page","link":"https:\/\/blogs.gre.ac.uk\/cmssupport\/systems\/unix\/unix-commands\/unixviewing\/","title":{"rendered":"Viewing and Searching Files"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Note: These tutorials assume you have a copy of the&nbsp;tutorial.txt&nbsp;file, which can be downloaded from the link below and saved in your Unix home area on the U: Drive. (Right-click &gt; Save As).<a href=\"http:\/\/cms-support.gre.ac.uk\/old\/unix\/tutorial.txt\">tutorial.txt<br>Text Document \u2022 1.07 KB<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Viewing filescat<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cat&nbsp;(concatenate)<br><br>The&nbsp;cat&nbsp;command can be used to display the contents of a file on the screen. Type the following:<br><br>% cat tutorial.txt<br><br>As you can see, the file is longer than than the size of the window, so it scrolls past making it unreadable.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>less<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;less&nbsp;command writes the contents of a file onto the screen a page at a time. Type the following:<br><br>% less tutorial.txt<br><br>Press the space bar if you want to see another page, type&nbsp;q&nbsp;if you want to quit reading. As you can see,&nbsp;less&nbsp;is used in preference to&nbsp;cat&nbsp;for long files.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>head<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;head&nbsp;command writes the first 10 lines of a file to the screen. Clear the screen first, then type the following:<br><br>% head tutorial.txt<br><br>Then type the following to write the first 5 lines out:<br><br>% head -5 tutorial.txt<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>tail<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;tail&nbsp;command writes the last 10 lines of a file to the screen. Clear the screen first, then type the following:<br><br>% tail tutorial.txt<br><br>How can you view the last 15 lines of the file?<br><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Searching through filesSimple searching using\u00a0less<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Using the less command, allows you to search though a text file for a keyword (pattern). For example, to search through tutorial.txt for the word tutorial, type the following:<br><br>% less tutorial.txt<br><br>then, still in&nbsp;less&nbsp;(i.e. don&#8217;t press&nbsp;q&nbsp;to quit), type a forward slash followed by the word to search:<br><br>\/tutorial<br><br><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As you can see,&nbsp;\/tutorial&nbsp;finds and highlights the keyword. Type&nbsp;n&nbsp;to search for the next occurrence of the word.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>grep<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;grep&nbsp;command is one of many standard Unix utilities. It searches files for specified words or patterns. Clear the screen first, then type the following:&nbsp;% grep tutorial tutorial.txt<br><br>As you can see,&nbsp;grep&nbsp;has printed out each line containing the word tutorial. Try typing the following:<br><br>% grep tutorial tutorial.txt<br><br>The&nbsp;grep&nbsp;command is case sensitive. Therefore it distinguishes between &#8216;tutorial&#8217; and &#8216;Tutorial&#8217;.<br><br>To ignore the upper\/lower case distinctions, use the&nbsp;-i&nbsp;option, i.e. type the following:<br><br>% grep -i tutorial tutorial.txt<br><br>To search for a phrase or pattern, you must enclose it in single quotes (the apostrophe symbol). For example to search for spinning top, type the following:<br><br>% grep -i &#8216;spinning top&#8217; tutorial.txt<br><br>Some of the other options of grep are as follows:<br>-v&nbsp;display those lines that do NOT match<br><br>-n&nbsp;precede each matching line with the line number<br><br>-c&nbsp;print only the total count of matched lines<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>wc<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">wc&nbsp;(word count)<br><br>The&nbsp;wc&nbsp;command is a handy little utility, which is short for word count. To do a word count on tutorial.txt, type the following:<br><br>% wc -w tutorial.txt<br><br>To find out how many lines the file has, type the following:<br><br>% wc -l tutorial.txt<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Summary<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">man command&nbsp;&#8211; view the man pages (help files)<br><br>checkquota&nbsp;&#8211; shows how much disk space you have left.<br><br>clear&nbsp;&#8211; clears the screen<br><br>uname&nbsp;&#8211; prints name of current system<br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Note: These tutorials assume you have a copy of the&nbsp;tutorial.txt&nbsp;file, which can be downloaded from the link below and saved in your Unix home area on the U: Drive. (Right-click &gt; Save As).tutorial.txtText Document \u2022 &hellip;<\/p>\n","protected":false},"author":53,"featured_media":0,"parent":584,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"footnotes":""},"class_list":["post-639","page","type-page","status-publish","hentry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blogs.gre.ac.uk\/cmssupport\/wp-json\/wp\/v2\/pages\/639","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.gre.ac.uk\/cmssupport\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.gre.ac.uk\/cmssupport\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.gre.ac.uk\/cmssupport\/wp-json\/wp\/v2\/users\/53"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.gre.ac.uk\/cmssupport\/wp-json\/wp\/v2\/comments?post=639"}],"version-history":[{"count":0,"href":"https:\/\/blogs.gre.ac.uk\/cmssupport\/wp-json\/wp\/v2\/pages\/639\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.gre.ac.uk\/cmssupport\/wp-json\/wp\/v2\/pages\/584"}],"wp:attachment":[{"href":"https:\/\/blogs.gre.ac.uk\/cmssupport\/wp-json\/wp\/v2\/media?parent=639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}