{"id":975,"date":"2018-06-17T19:13:26","date_gmt":"2018-06-17T19:13:26","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=975"},"modified":"2018-06-17T19:13:26","modified_gmt":"2018-06-17T19:13:26","slug":"list-of-linux-commands-every-developer-should-know","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=975","title":{"rendered":"List of Linux Commands Every Developer Should Know"},"content":{"rendered":"<p>At some point in you developer career , you will have to work with Linux Systems and you will be looking for information regarding Linux commands. In this post I will explain the use of 10 Linux commands you should know as a developer.<\/p>\n<h2 id=\"10-linux-commands-for-developers\">10 Linux Commands For Developers<\/h2>\n<p>Following are the list of ten commands you will learn about in this post.<\/p>\n<pre><code> man\n touch, cat and less\n sort and grep\n cut\n sed\n tar\n find\n diff\n uniq\n chmod<\/code><\/pre>\n<p>Let&#8217;s get started.<\/p>\n<h3 id=\"1-man\">1. man<\/h3>\n<p>The first command you should learn in Linux is &#8220;man&#8221;. Using this command you can get the usage and description of all  Linux commands. For example, if you want to know about &#8220;ls&#8221; command and its options, just execute &#8220;man ls&#8221; command in the terminal to list its usage and description. <\/p>\n<pre><code>Syntax: man \nman ls<\/code><\/pre>\n<pre><code>root@devopscube:~# man ls\nLS(1)                            User Commands                           LS(1)\nNAME\n       ls - list directory contents\nSYNOPSIS\n       ls [OPTION]... [FILE]...\nDESCRIPTION\n       List  information  about  the FILEs (the current directory by default).\n       Sort entries alphabetically if none of -cftuvSUX nor --sort  is  speci\u00e2\n       fied.\n       Mandatory  arguments  to  long  options are mandatory for short options\n       too.\n       -a, --all\n              do not ignore entries starting with .<\/code><\/pre>\n<p><strong>Recommended:<\/strong><a href=\"http:\/\/crunchadeal.com\/coupon\/93-off-coupon-learn-linux-in-5-days-and-level-up-your-career\/?ref=devopscube.com\" rel=\"noopener\">Learn Linux in 5 Days and Level Up Your Career Udemy Course<\/a><\/p>\n<h2 id=\"2-touch-cat-and-less\">2. touch, cat and less<\/h2>\n<p>Touch command is used to create any type of file in Linux systems with &#8220;0&#8221; size. As a developer , when working with Linux you might want to create files in the server. You can make use of touch command to do that. <\/p>\n<pre><code>Syntax: touch \ntouch demo.txt\n\nroot@devopscube:~# touch demo.txt\nroot@devopscube:~# ls\ndemo.txt<\/code><\/pre>\n<p>Cat command is used to view the contents of a file. You cannot edit the contents of the file using cat. It just gives a view of the file. cat doesn&#8217;t support scrolling using keyboard.<\/p>\n<p><em>Note: To work the commands given below, copy any content to the demo.txt file using your favorite vi or nano editor. For our examples, i have copied the boot.log file contents on to demo.txt file using &#8221; cat \/var\/log\/boot.log &gt;&gt; demo.txt&#8221; command. You can also do the same.<\/em><\/p>\n<pre><code>Syntax: cat \ncat demo.txt<\/code><\/pre>\n<p>Less command also gives the view of a file. less is very fast and you can use the arrow keys to scroll up and down to know the start and end of the file. There is also &#8220;more&#8221; command, which is used to view the file but it allows only forward scrolling using &#8220;enter&#8221; key. It doesn&#8217;t support backward scrolling.<\/p>\n<pre><code>Syntax: less \n        more<\/code><\/pre>\n<pre><code>less demo.txt\nmore demo.txt<\/code><\/pre>\n<h2 id=\"3-sort-and-grep\">3. sort and grep<\/h2>\n<p>Sort is used to sort the contents of a file. Create a file named test.txt and copy the following contents onto the file to test the sort command. <\/p>\n<pre><code>1 mike level intermediate jan\n10 lucy level beginer mar\n45 Dave level expert dec\n4 dennis start beginner jul\n7 Megan employee trainee feb\n58 Mathew Head CEO nov<\/code><\/pre>\n<p>In the above example, the second column has the names. So if you want to sort the names alphabetically use &#8220;-k&#8221; flag with the column location. It would be &#8220;-k2&#8221;. <\/p>\n<pre><code>Syntax: sort\nsort -k2 test.txt<\/code><\/pre>\n<pre><code>root@devopscube:~# sort -k2 test.txt\n45 Dave level expert dec\n4 dennis start beginner jul\n10 lucy level beginer mar\n58 Mathew Head CEO nov\n7 Megan employee trainee feb\n1 mike level intermediate jan<\/code><\/pre>\n<p>The first column has numbers. If you want to sort numbers, use&nbsp; \u201c-h\u201d flag. If the numbers are in different column, you can use the \u201c-k\u201d flag along with \u201c-h\u201d flag.<\/p>\n<pre><code>root@devopscube:~# sort -h test.txt  \n1 mike level intermediate jan\n4 dennis start beginner jul\n7 Megan employee trainee feb\n10 lucy level beginer mar\n45 Dave level expert dec\n58 Mathew Head CEO nov<\/code><\/pre>\n<p>The last column has months. You can sort a file based on month using \u201c-M\u201d flag.<\/p>\n<pre><code>root@devopscube:~# sort -k5 -M test.txt\n1 mike level intermediate jan\n7 Megan employee trainee feb\n10 lucy level beginer mar\n4 dennis start beginner jul\n58 Mathew Head CEO nov\n45 Dave level expert dec<\/code><\/pre>\n<blockquote><p>Note: If you want to eliminate the duplicate lines , you can use \u201c-u\u201d flag along with the sort command.<\/p><\/blockquote>\n<p>To sort the file in descending order, use \u201c-r\u201d flag.<\/p>\n<pre><code>root@devopscube:~# sort -h -r test.txt\n58 Mathew Head CEO nov\n45 Dave level expert dec\n10 lucy level beginer mar\n7 Megan employee trainee feb\n4 dennis start beginner jul\n1 mike level intermediate jan<\/code><\/pre>\n<h3 id=\"4-grep\">4. Grep:<\/h3>\n<p>Grep is a powerful command and will be used by sysadmins quite often. Grep command is used for searching specific string patterns in a file as well as the standard output (STDIN). We will look into few file based operations . Other uses of grep is out of scope of this post.<\/p>\n<pre><code>Syntax: grep \"\"  \n        grep \"Mathew\" test.txt<\/code><\/pre>\n<pre><code>root@devopscube:~# grep \"dennis\" test.txt\n4 dennis start beginner jul<\/code><\/pre>\n<p>The above command gives the output including the sub-string. If you want to search for individual words, you need to add \u201c-i\u201d flag to the grep command. Also you can search for a string or a pattern in multiple files using a single grep command. For example,<\/p>\n<pre><code>grep \"dennis\" test1.txt test2.txt test3.txt<\/code><\/pre>\n<p>You can also use regular expressions for matching the string.<\/p>\n<h2 id=\"4-cut\">4. cut<\/h2>\n<p>Cut command is used for extracting a portion of a file using columns and delimiters. If you want to list everything in a selected column, use the &#8220;-c&#8221; flag with cut command. For example, lets select the first two columns from our test.txt file. <\/p>\n<pre><code>cut -c1-2 test.txt<\/code><\/pre>\n<pre><code>root@devopscube:~# cut -c1-2 test.txt\n1\n10\n45\n4\n7\n58<\/code><\/pre>\n<p>If you want to extract specific strings from a file, you can used the delimiter \u201c-d\u201d flag and \u201c-f\u201d flag to select the field. For example, if you wan to extract all the names from our test.txt file you can use the following command.<\/p>\n<pre><code>cut -d' ' -f2 test.txt<\/code><\/pre>\n<pre><code>root@devopscube:~# cut -d' ' -f2 test.txt\nmike\nlucy\nDave\ndennis\nMegan\nMathew<\/code><\/pre>\n<p>The following example extracts the users from \/etc\/passd file using \u2018:\u201d delimiter.<\/p>\n<pre><code>cut -d':' -f1 \/etc\/passwd<\/code><\/pre>\n<h2 id=\"5-sed\">5. sed<\/h2>\n<p>sed is a text-editor which can perform editing operations in a non-interactive way. Sed command gets its input from a standard input or a file to perform the editing operation on a file. Sed is a very powerful utility and you can do a lot of file manipulations using sed. I will explain the important operation you might want to do with text file.<\/p>\n<p>If you want to replace a text in a file by searching it in a file, you can use the  sed  command with substitute &#8220;s&#8221; flag to search for the specific pattern and change it.<\/p>\n<pre><code>Syntax: sed 's\/\/\/' test.txt<\/code><\/pre>\n<p>For example, lets replace&nbsp; \u201cmike\u201d in test.txt file to \u201cmichael<\/p>\n<pre><code>sed 's\/mike\/michael\/' test.txt<\/code><\/pre>\n<pre><code>root@devopscube:~# sed 's\/mike\/michael\/' test.txt\n1 michael level intermediate jan\n10 lucy level beginer mar\n45 Dave level expert dec\n4 dennis start beginner jul\n7 Megan employee trainee feb\n58 Mathew Head CEO nov<\/code><\/pre>\n<p>In the above example we used &#8220;\/&#8221; as a delimiter for string substitution. You can use any character as a delimiter for substitution. For example, if you want to make changes to a url, you need to have a different delimiter because the url already have slashes. So you can substitute like the following.<\/p>\n<pre><code>echo \"http:\/\/www.example.uk\/main.html\" | sed 's_uk\/main_com\/index_'<\/code><\/pre>\n<pre><code>root@devopscube:~# echo \"http:\/\/www.example.uk\/main.html\" | sed 's_uk\/main_com\/index_'\nhttp:\/\/www.example.com\/index.html<\/code><\/pre>\n<p>You can also replace a line by matching a string pattern in the line. &#8220;-c&#8221; flag is used for replacing text using sed. Lets replace the first line in our test.txt file using the following command.<\/p>\n<pre><code> sed '\/1 mike\/c 1 michael start beginner mar' test.txt<\/code><\/pre>\n<pre><code>root@devopscube:~# sed '\/1 mike\/c 1 michael start beginner mar' test.txt\n1 michael start beginner mar\n10 lucy level beginer mar\n45 Dave level expert dec\n4 dennis start beginner jul\n7 Megan employee trainee feb\n58 Mathew Head CEO nov<\/code><\/pre>\n<h2 id=\"6-tar\">6. tar<\/h2>\n<p>tar command is used to create and extract archive files. &#8220;-cf&#8221; and &#8220;-xf&#8221; flags are used for creating and extracting archives. <\/p>\n<pre><code>Syntax: tar  &lt;file\/folder name&gt;<\/code><\/pre>\n<p>Let&#8217;s create a tar archive out of test.txt file<\/p>\n<pre><code>tar -cf test.tar test.txt<\/code><\/pre>\n<pre><code>root@devopscube:~# tar -cf test.tar test.txt\nroot@devopscube:~# ls\ntest.tar  test.txt<\/code><\/pre>\n<p>Lets extract the test.tar archive to the destination folder \u201cdemo\u201d using \u201c-C\u201d flag.<\/p>\n<pre><code>tar -xf test.tar -C \/root\/demo\/<\/code><\/pre>\n<pre><code>root@devopscube:~# tar -xf test.tar -C \/root\/demo\/\nroot@devopscube:~# cd demo\/\nroot@devopscube:~\/demo# ls\ntest.txt<\/code><\/pre>\n<h2 id=\"7-find\">7. find<\/h2>\n<p>find command is used for finding files. You can find the files using its name with &#8220;-name&#8221; flag. <\/p>\n<pre><code>find -name  find -name test.txt<\/code><\/pre>\n<pre><code>root@devopscube:\/home\/ubuntu# cd ~\nroot@devopscube:~# find -name test.txt\n.\/demo\/test.txt\n.\/test.txt<\/code><\/pre>\n<p>You can also find folder using its name by using\u201d\/ -name\u201d flag.<\/p>\n<pre><code>find \/ -name passwd<\/code><\/pre>\n<pre><code>root@devopscube:~# find \/ -name passwd\n\/etc\/cron.daily\/passwd\n\/etc\/pam.d\/passwd\n\/etc\/passwd\n\/usr\/share\/lintian\/overrides\/passwd<\/code><\/pre>\n<h2 id=\"8-diff\">8. diff<\/h2>\n<p>diff command is used to find the difference between two files. Diff command analyses the files and prints the lines which are different. Lets say we have two files test and test1. you can find the difference between the two files using the following command. <\/p>\n<pre><code>Syntax: diff  \n        diff test.txt test1.txt<\/code><\/pre>\n<pre><code>root@devopscube:~# diff test.txt test1.txt\n7c7\n&lt; 59 sdfsd --- &gt; 59 sdfsd  CTO dec<\/code><\/pre>\n<h2 id=\"9-uniq\">9. Uniq<\/h2>\n<p>uniq command is used for filtering out the duplicate line in a file. <\/p>\n<pre><code>Syntax: uniq \nuniq test.txt<\/code><\/pre>\n<pre><code>root@devopscube:~# uniq test.txt\n1 mike level intermediate jan\n10 lucy level beginer mar\n45 Dave level expert dec\n4 dennis start beginner jul\n7 Megan employee trainee feb\n58 Mathew Head CEO nov<\/code><\/pre>\n<h2 id=\"10-chmod\">10. chmod<\/h2>\n<p>chmod command is used for changing the read\/write\/execute permissions of a file. Permissions are represented in numbers as follows. <\/p>\n<pre><code>4 - read permission<\/code><\/pre>\n<pre><code>2 - write permission<\/code><\/pre>\n<pre><code>1 - execute permission<\/code><\/pre>\n<pre><code>0 - no permission<\/code><\/pre>\n<p>To give all permissions on test.txt file, you can use the following chmod command.<\/p>\n<pre><code>chmod 755 test.txt<\/code><\/pre>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/list-of-linux-commands-every-developer-should-know\/\" target=\"_blank\" rel=\"noopener noreferrer\">List of Linux Commands Every Developer Should Know \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/list-of-linux-commands-every-developer-should-know\/<\/p>\n","protected":false},"author":1,"featured_media":976,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-975","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/975","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=975"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/975\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/976"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=975"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=975"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=975"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}