{"id":1077,"date":"2017-12-26T04:00:40","date_gmt":"2017-12-26T04:00:40","guid":{"rendered":"http:\/\/blog.cloudxlab.com\/?p=1077"},"modified":"2017-12-26T04:00:40","modified_gmt":"2017-12-26T04:00:40","slug":"linux-tutorial-part-2","status":"publish","type":"post","link":"https:\/\/cloudxlab.com\/blog\/linux-tutorial-part-2\/","title":{"rendered":"A Simple Tutorial on Linux \u2013 Part-2"},"content":{"rendered":"<p>This post is the continuation of\u00a0<a href=\"https:\/\/cloudxlab.com\/blog\/linux-tutorial-part-1\/\">A Simple Tutorial on Linux &#8211; Part-1<\/a><\/p>\n<p>In the Part-1 we learned the following topics on Linux.<\/p>\n<ul class=\"ili-indent\">\n<li>Linux Operating System<\/li>\n<li>Linux Files &amp; Process<\/li>\n<li>The Directory Structure<\/li>\n<li>Permissions<\/li>\n<li>Process<\/li>\n<\/ul>\n<p>Keeping up the same pace, we will learn the following topics in the 2nd part of the Linux series.<\/p>\n<ul class=\"ili-indent\">\n<li>Shell Scripting<\/li>\n<li>Networking<\/li>\n<li>Files &amp; Directories<\/li>\n<li>Chaining Unix Commands<\/li>\n<li>Pipes<\/li>\n<li>Filters<\/li>\n<li>Word Count Exercise<\/li>\n<li>Special System commands<\/li>\n<li>Environment variables<\/li>\n<\/ul>\n<h2>Writing first shell script<\/h2>\n<p><span style=\"font-weight: 400;\">A shell script is a file containing a list of commands. Let&#8217;s create a simple command that prints two words:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">1. Open a text editor to create a file myfirstscript.sh:<\/span><\/p>\n<pre class=\"lang:default decode:true\">nano myfirstscript.sh<\/pre>\n<p><span style=\"font-weight: 400;\">2. Write the following into the editor:<\/span><\/p>\n<pre class=\"lang:default decode:true\">#!\/bin\/bash\r\nname=linux\r\necho \"hello $name world\"<\/pre>\n<p><b>Note:<\/b><span style=\"font-weight: 400;\"> In Unix, the extension doesn&#8217;t dictate the program to be used while executing a script. It is the first line of the script that would dictate which program to use. In the example above, the program is &#8220;\/bin\/bash&#8221; which is a Unix shell.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">1. Press Ctrl +x to save and then &#8220;y&#8221; to exit<\/span><\/p>\n<p><span style=\"font-weight: 400;\">2. Now, by default, it would not have executable permission. You can make it executable like this:<\/span><\/p>\n<pre class=\"lang:default decode:true \">chmod +x myfirstscript.sh<\/pre>\n<p><span style=\"font-weight: 400;\">3. To run the script, use:<\/span><\/p>\n<pre class=\"lang:default decode:true\">.\/myfirstscript.sh<\/pre>\n<p><!--more--><\/p>\n<h3>Program returns value and arguments<\/h3>\n<p><span style=\"font-weight: 400;\">Every program returns a value to the operating system. It is also referred as the exit status. In Linux, a program returns 0 if successful. Otherwise a non-zero error number.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">To check the return value of the previous command you can check the value of a special variable &#8220;?&#8221;<\/span><\/p>\n<pre class=\"lang:default decode:true\">\u00a0echo $?<\/pre>\n<p><a href=\"https:\/\/cloudxlab.com\/assessment\/slide\/linux-basics\/68\/value-of-echo\" target=\"_blank\" rel=\"noopener\">Attend the quiz on echo command<\/a><\/p>\n<h2>Networking<\/h2>\n<h3>Sockets and ports<\/h3>\n<p><span style=\"font-weight: 400;\">There are some programs that need to keep running all the time. Such programs are called services.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">You can communicate with such programs even from another computer. These programs or processes bind themselves to a port. No two programs can listen on the same port.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">For example, usually, A web server listens on 80 port and an email server listens on 25 port.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">You can connect and talk to any service using nc command in the following way:<\/span><\/p>\n<pre class=\"lang:default decode:true \">\u00a0nc computer_name(or ip_address) port_number<\/pre>\n<p><span style=\"font-weight: 400;\">The way a person refers to himself as &#8220;I&#8221;, a computer refers itself as localhost or IP Address 127.0.0.1.<\/span><\/p>\n<h2>Files &amp; Directories<\/h2>\n<h3>Linking<\/h3>\n<p><span style=\"font-weight: 400;\">If you want to give multiple names to a single file without copying the content, you can create the link.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">1. Create a file:<\/span><\/p>\n<pre class=\"lang:default decode:true\">nano orig_text<\/pre>\n<p><span style=\"font-weight: 400;\">2. Put the following text in it:<\/span><\/p>\n<pre class=\"lang:default decode:true \">This is my valuable data<\/pre>\n<p><span style=\"font-weight: 400;\">3. Save and Exit: Press CTRL+x and y<\/span><\/p>\n<p><span style=\"font-weight: 400;\">4. Check if the file has right data:<\/span><\/p>\n<pre class=\"lang:default decode:true \">cat orig_text<\/pre>\n<p><span style=\"font-weight: 400;\">5. Create Link:<\/span><\/p>\n<pre class=\"lang:default decode:true\">ln orig_text mylink<\/pre>\n<p><span style=\"font-weight: 400;\">6. Check if it is there:<\/span><\/p>\n<pre class=\"lang:default decode:true\">ls -l mylink<\/pre>\n<h3>Hard Links and Softlink<\/h3>\n<p><span style=\"font-weight: 400;\">The link that we created previously were known as hard link. If you delete the original file, the hard link would not be impacted.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">1. Delete original content created in the previous section:<\/span><\/p>\n<pre class=\"lang:default decode:true\" style=\"padding-left: 270px;\">rm orig_text<\/pre>\n<p><span style=\"font-weight: 400;\">2. Check if it still has the content:<\/span><\/p>\n<pre class=\"lang:default decode:true\">cat mylink<\/pre>\n<h3>Symbolic links<\/h3>\n<p><span style=\"font-weight: 400;\">A symbolic link points to a file. In case, the original file is deleted, the symbolic link would be pointing to the non-existing file.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">You can create a symbolic link to a directory too.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">1. Create a file:<\/span><\/p>\n<pre class=\"lang:default decode:true\">nano orig_text1<\/pre>\n<p><span style=\"font-weight: 400;\">2. Put the following text in it:<\/span><\/p>\n<pre class=\"lang:default decode:true \">This is my not-so-valuable data<\/pre>\n<p><span style=\"font-weight: 400;\">3. Save and Exit: Press Ctrl+x and y<\/span><\/p>\n<p><span style=\"font-weight: 400;\">4. Check if the file has right data:<\/span><\/p>\n<pre class=\"lang:default decode:true\">cat orig_text1<\/pre>\n<p><span style=\"font-weight: 400;\">5. Create Link:<\/span><\/p>\n<pre class=\"lang:default decode:true \">ln -s orig_text1 myslink<\/pre>\n<p><span style=\"font-weight: 400;\">6. Check if it is there:<\/span><\/p>\n<pre class=\"lang:default decode:true\">ls -l myslink<\/pre>\n<p><span style=\"font-weight: 400;\">You should see something like this<\/span><\/p>\n<pre class=\"lang:default decode:true \">lrwxrwxrwx 1 sandeepgiri9034 sandeepgiri9034 10 Dec 14 11:45 myslink -&gt; orig_text1<\/pre>\n<p><span style=\"font-weight: 400;\">7. Check the contents of myslink using:<\/span><\/p>\n<pre class=\"lang:default decode:true\">cat myslink<\/pre>\n<h2>Chaining Unix Commands<\/h2>\n<p><span style=\"font-weight: 400;\">Say, you want to execute a command only if another command is successful then you use &amp;&amp;.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">And if you want a command to be executed if another command has failed, you use ||.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">1. Create a file tea_ready:<\/span><\/p>\n<pre class=\"lang:default decode:true \">touch tea_ready<\/pre>\n<p><span style=\"font-weight: 400;\">2. The following command would print &#8220;Tea is ready&#8221; if the tea_ready file exists:<\/span><\/p>\n<pre class=\"lang:default decode:true \">ls -l tea_ready &amp;&amp; echo \"Tea is ready\"<\/pre>\n<p><span style=\"font-weight: 400;\">3. Delete the file tea_ready:<\/span><\/p>\n<pre class=\"lang:default decode:true\">rm tea_ready<\/pre>\n<p><span style=\"font-weight: 400;\">4. Check the command from #2 again:<\/span><\/p>\n<pre class=\"lang:default decode:true\">ls -l tea_ready &amp;&amp; echo \"Tea is ready\"<\/pre>\n<p><span style=\"font-weight: 400;\">5. The following will print &#8220;Tea is not ready&#8221;:<\/span><\/p>\n<pre class=\"lang:default decode:true\">ls -l tea_ready || echo \"Tea is not ready\"<\/pre>\n<p><span style=\"font-weight: 400;\">6. You can use brackets to avoid confusion:<\/span><\/p>\n<pre class=\"lang:default decode:true\">(ls -l tea_ready &amp;&amp; echo \"Tea is ready\") || echo \"Tea is not ready\"<\/pre>\n<h2>Redirecting the output of a program<\/h2>\n<p><span style=\"font-weight: 400;\">The output of a program can be saved to a file:<\/span><\/p>\n<pre class=\"lang:default decode:true\">\u00a0myprogram &gt; myfile<\/pre>\n<p><span style=\"font-weight: 400;\">If &#8220;myfile&#8221; does not exist, it will be created. If it exists it will be overwritten.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Please follow these steps:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">1. Run the following command to save the output of echo to a file:<\/span><\/p>\n<pre class=\"lang:default decode:true\">echo \"hello\" &gt; hello.out<\/pre>\n<p><span style=\"font-weight: 400;\">2. Append world to it:<\/span><\/p>\n<pre class=\"lang:default decode:true\">echo \"world\" &gt;&gt; hello.out<\/pre>\n<p><span style=\"font-weight: 400;\">3. Check by typing:<\/span><\/p>\n<pre class=\"lang:default decode:true\">cat hello.out<\/pre>\n<h2>Pipes &#8211; Introduction<\/h2>\n<p><span style=\"font-weight: 400;\">If we want to send the output of one program to another, we can use pipe. A pipe is denoted by &#8220;|&#8221;.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8220;echo&#8221; command prints on the standard output whatever argument is passed to it.<\/span><\/p>\n<pre class=\"lang:default decode:true\">echo \"Hi\"<\/pre>\n<p><span style=\"font-weight: 400;\">&#8220;wc&#8221; command prints the number of characters, words, and lines out of whatever you type on standard input. Start &#8220;wc&#8221; command, type some text and press Ctrl+d to end the input:<\/span><\/p>\n<pre class=\"lang:default decode:true \">wc\r\nhi\r\nhow are you\r\n[CTRL + d]<\/pre>\n<p><b>Output-<\/b><\/p>\n<pre class=\"lang:default decode:true\">\u00a0\u00a0\u00a0\u00a0\u00a02 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a04 \u00a0\u00a0\u00a0\u00a0\u00a015<\/pre>\n<p><span style=\"font-weight: 400;\">Now, if we want to count the number of words, characters in the output of any program, we can pipe the output in the following way:<\/span><\/p>\n<pre class=\"lang:default decode:true\">\u00a0echo \"Hello, World\" | wc<\/pre>\n<p><span style=\"font-weight: 400;\">You can also save the results using redirection, in the following way:<\/span><\/p>\n<pre class=\"lang:default decode:true\">echo \"Hello, World\" | wc &gt; wc_results<\/pre>\n<h1>Filters<\/h1>\n<p><span style=\"font-weight: 400;\">Pipes are very powerful. They can be chained to solve many problems. So, most of the commands are built in a way that they can be used with pipes as long as they read from standard input (keyboard or pipe) and write to standard output (screen or pipe )<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Such programs that can be used with pipes are generally called filters. Command examples of filters are:<\/span><\/p>\n<p><b>wc &#8211;<\/b><span style=\"font-weight: 400;\"> for counting the letters, words, and lines in the input<\/span><\/p>\n<p><b>grep &#8211;<\/b><span style=\"font-weight: 400;\"> displays only the lines from the input in which keyword (which is passed as argument) is found.<\/span><\/p>\n<p><b>sort &#8211;<\/b><span style=\"font-weight: 400;\"> sorts\/orders the input lines lexically (alphabetically) by default but can be changed<\/span><\/p>\n<p><b>more &#8211;<\/b><span style=\"font-weight: 400;\"> displays the input in a page-wise manner<\/span><\/p>\n<p><b>cat &#8211;<\/b><span style=\"font-weight: 400;\"> displays the content of the file passed as an argument<\/span><\/p>\n<p><b>sed &#8211;<\/b><span style=\"font-weight: 400;\"> substitute a word with another word: sed &#8216;s\/word\/another_word\/&#8217;<\/span><\/p>\n<p><b>tr &#8211;<\/b><span style=\"font-weight: 400;\"> translate character ranges. For example to lowercase characters in input you can use:<\/span><\/p>\n<pre class=\"lang:default decode:true \">tr 'A-Z' 'a-z'<\/pre>\n<p><b>uniq &#8211;<\/b><span style=\"font-weight: 400;\"> Display the uniq input lines. The input lines need to be sorted. If you want to display frequency, use:<\/span><\/p>\n<pre class=\"lang:default decode:true \">uniq -c<\/pre>\n<h3>Word Count Exercise<\/h3>\n<p><b>Step 1:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Check the Data using cat command. Since the file is big, you can use &#8220;more&#8221; to see pagewise<\/span><\/p>\n<pre class=\"lang:default decode:true \">\u00a0cat \/cxldata\/big.txt | more<\/pre>\n<p><b>Step 2:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Replace space with newline such that every line in output contains the only single word:<\/span><\/p>\n<pre class=\"lang:default decode:true\">\u00a0cat \/cxldata\/big.txt | sed 's\/ \/\\n\/g' |more<\/pre>\n<p><span style=\"font-weight: 400;\">For example, after replacing space with a new line in &#8220;I am ok&#8221; we should get:<\/span><\/p>\n<pre class=\"lang:default decode:true\">I\r\nam\r\nok<\/pre>\n<p><span style=\"font-weight: 400;\">The &#8220;\/g&#8221; is an option of sed which makes replace all occurrences of space instead of only one.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Also, note this command has three programs connected by two pipes. The output of cat is going to sed and output of sed is going to more to see the pagewise.<\/span><\/p>\n<p><b>Step 3:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">We can sort the words using sort command in the following way<\/span><\/p>\n<pre class=\"lang:default decode:true \">cat \/cxldata\/big.txt | sed 's\/ \/\\n\/g' | sort|more<\/pre>\n<p><span style=\"font-weight: 400;\">Note that we are using &#8220;more&#8221; command just to avoid screen-blindness (too much text scrolling).<\/span><\/p>\n<p><b>Step 4:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">We can now, count the words using uniq command<\/span><span style=\"font-weight: 400;\">\u00a0<\/span><\/p>\n<pre class=\"lang:default decode:true\">cat \/cxldata\/big.txt | sed 's\/ \/\\n\/g' | sort|uniq -c|more<\/pre>\n<p><span style=\"font-weight: 400;\">Please save the result of the command to a file &#8220;word_count_results&#8221; in your home directory<\/span><span style=\"font-weight: 400;\">\u00a0<\/span><\/p>\n<pre class=\"lang:default decode:true \">cat \/cxldata\/big.txt | sed 's\/ \/\\n\/g' | sort|uniq -c &gt; word_count_results<\/pre>\n<h3>Improved Word Count Using Unix Commands<\/h3>\n<p><span style=\"font-weight: 400;\">We can further improve the word frequency count by using more filters.<\/span><\/p>\n<p><b>Improvement 1:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Translate to lower case using<\/span><span style=\"font-weight: 400;\">\u00a0<\/span><\/p>\n<pre class=\"lang:default decode:true \">tr 'A-Z' 'a-z'<\/pre>\n<p><b>Improvement 2:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Remove non-alphanumeric characters using sed with the regular expression:<\/span><\/p>\n<pre class=\"lang:default decode:true \">sed 's\/[^0-9a-z]\/\/g'<\/pre>\n<p><b>Improvement 3:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Replace all whitespace (multiple tabs and spaces):<\/span><\/p>\n<pre class=\"lang:default decode:true \">\u00a0sed -E 's\/[ \\t]+\/\\n\/g'<\/pre>\n<p><span style=\"font-weight: 400;\">Please note that since we are using regular expressions, we need to specify &#8220;-E&#8221;<\/span><\/p>\n<p><b>Improvement 4:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Display most frequent at the top or display the results in reverse numeric sorting:<\/span><\/p>\n<pre class=\"lang:default decode:true\">sort -nr<\/pre>\n<p><b>Improvement 5:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">If the input file is big, the sort command might use too much of memory. So, you can force sort command to use less memory say 100 MB:<\/span><\/p>\n<pre class=\"lang:default decode:true\">\u00a0sort -S 50M<\/pre>\n<p><span style=\"font-weight: 400;\">After all of these improvements, please save the results<\/span><\/p>\n<pre class=\"lang:default decode:true \">cat \/cxldata\/big.txt |tr 'A-Z' 'a-z'| sed -E 's\/[ \\t]+\/\\n\/g'|sed 's\/[^0-9a-z]\/\/g' | sort|uniq -c|sort -nr -S 50M &gt; word_count_results_nice<\/pre>\n<h3>Shell script for WordCount<\/h3>\n<p><span style=\"font-weight: 400;\">A shell script is a file which contains the commands separated by a newline.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Let&#8217;s create a script to do the sorting of the data:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">1. Create a file using nano text editor:<\/span><\/p>\n<pre class=\"lang:default decode:true \">nano wordcountscript.sh<\/pre>\n<p><span style=\"font-weight: 400;\">2. The first line of a script should have &#8220;#!&#8221; followed by the name of the program to execute the script with. Since we are creating a shell script, we want it to be executed using bash. So, the first line of the program should be:<\/span><\/p>\n<pre class=\"lang:default decode:true\">#!\/bin\/bash<\/pre>\n<p><span style=\"font-weight: 400;\">3. Add the command in the editor:<\/span><\/p>\n<pre class=\"lang:default decode:true\">tr 'A-Z' 'a-z'| sed -E 's\/[ \\t]+\/\\n\/g'|sed 's\/[^0-9a-z]\/\/g' | sort|uniq -c|sort -nr -S 50M<\/pre>\n<p><span style=\"font-weight: 400;\">4. Save the file by pressing Ctrl+x and &#8220;y&#8221;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">5. Now, make this file executable:<\/span><\/p>\n<pre class=\"lang:default decode:true\">chmod +x wordcountscript.sh<\/pre>\n<p><span style=\"font-weight: 400;\">6. Check if it is running:<\/span><\/p>\n<pre class=\"lang:default decode:true\">cat \/cxldata\/big.txt | .\/wordcountscript.sh | more<\/pre>\n<p><span style=\"font-weight: 400;\">Please recall &#8220;.\/&#8221; means current directory and &#8221; | more&#8221; will show the result pagewise instead of causing too much scrolling.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Also, note that whatever is the input to the script is also passed to the programs executed in the script.<\/span><\/p>\n<h2>Permissions of Processes &#8211; setuid<\/h2>\n<p><span style=\"font-weight: 400;\">In Unix, there is a file \/etc\/shadow that contains (one-way) encrypted passwords for every user. The user can not see the contents of the file. This is to defend the password cracking programs.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">To change the password, a user needs to use the command: <\/span><b>passwd<\/b><span style=\"font-weight: 400;\">. This passwd command first asks you for your old password and encrypts your input and compares it against the value in the file \/etc\/shadow. If it matches then it updates the password file \/etc\/shadow with new content.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">When you are not allowed to view the \/etc\/shadow file, how can a program (passwd) do the same when run by you?<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This is where the idea of a special permission called setuid come into the picture. A program file can be given setuid permission such that program becomes the user who owns the program file instead of the user who is running it.<\/span><\/p>\n<p>Usually, a program runs with the same permissions as the user who is running it. The program can read or modify only the files which user is allowed to.<\/p>\n<p>A process or program is something that is running. A process is started by the user. The question is what is a process allowed to do? Can a process do something that the user is not allowed to do? If so, then the user will create programs which can do anything. Therefore, the process is allowed to do only the things an owner of the process can do. And who is the owner? The user that started the process.<\/p>\n<h3>Setting setuid<\/h3>\n<p><span style=\"font-weight: 400;\">You can make a program setuid by giving &#8216;s&#8217; instead of &#8216;x&#8217; permission. If you have written a script x.sh, an example would be:<\/span><\/p>\n<pre class=\"lang:default decode:true\">chmod +s x.sh<\/pre>\n<p><span style=\"font-weight: 400;\">1. Start creating a file with the name whoownsit_username.sh in \/tmp directory:<\/span><\/p>\n<pre class=\"lang:default decode:true\">nano \/tmp\/whoownsit_$USER.sh<\/pre>\n<p><span style=\"font-weight: 400;\">Note: If your username is sandeep1234, the filename would be whoownsit_sandeep1234.sh<\/span><\/p>\n<p><span style=\"font-weight: 400;\">2. Put the following content in the editor in the previous step:<\/span><\/p>\n<pre class=\"lang:default decode:true\">whoami<\/pre>\n<p><span style=\"font-weight: 400;\">3. Save it by pressing Ctrl+x and press &#8220;y&#8221;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">4. Give it setuid permission:<\/span><\/p>\n<pre class=\"lang:default decode:true\">chmod +sx \/tmp\/whoownsit_$USER.sh<\/pre>\n<p><span style=\"font-weight: 400;\">5. Check if you have given correct permission by<\/span><\/p>\n<pre class=\"lang:default decode:true \">ls -l \/tmp\/whoownsit_$USER.sh<\/pre>\n<p><span style=\"font-weight: 400;\">It should display something like this in permissions: rwsrwsr_x<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Note: Setuid doesn&#8217;t work in shell scripts. Please see<br \/>\n<a href=\"http:\/\/www.faqs.org\/faqs\/unix-faq\/faq\/part4\/section-7.html\">How can I get setuid shell scripts to work?<\/a><br \/>\n<\/span><\/p>\n<p><a href=\"https:\/\/cloudxlab.com\/assessment\/slide\/linux-basics\/88\/permissions-of-passwd-file\">Attend the quiz on permissions<\/a><\/p>\n<h2>Special System commands<\/h2>\n<h3><b>sudo<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">This command makes a program run as root (the system administrator). This command is only allowed to be used by few users. Such users are called sudoers. You can modify the sudoers using<\/span><\/p>\n<pre class=\"lang:default decode:true\">sudo visudo<\/pre>\n<h3><b>shutdown<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">This command makes the system shutdown. You can use the following command to shutdown the system immediately<\/span><\/p>\n<pre class=\"lang:default decode:true \">shutdown -h now<\/pre>\n<p><span style=\"font-weight: 400;\">The alternative command to shutdown immediately is: &#8220;halt&#8221;<\/span><\/p>\n<h3><b>Restart the system<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">To restart the system, you can use &#8220;reboot&#8221; command.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Please note that above commands (shutdown, halt, reboot) can only be run as root.<\/span><\/p>\n<h3>Where is my program?<\/h3>\n<p><span style=\"font-weight: 400;\">To find where is your program located, you can use &#8220;which&#8221; command.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">For example,<\/span><\/p>\n<pre class=\"lang:default decode:true\">which java<\/pre>\n<p><span style=\"font-weight: 400;\">would print &#8220;\/usr\/bin\/java&#8221; which means java is a command in the directory &#8220;\/usr\/bin&#8221;.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">To further find out, you can use:<\/span><\/p>\n<pre class=\"lang:default decode:true\">ls -l \/usr\/bin\/java<\/pre>\n<p><span style=\"font-weight: 400;\">This would display:<\/span><\/p>\n<pre class=\"lang:default decode:true \">lrwxrwxrwx 1 root root 22 May 18 \u00a02016 \/usr\/bin\/java -&gt; \/etc\/alternatives\/java<\/pre>\n<p><span style=\"font-weight: 400;\">It means that \/usr\/bin\/java is actually a link to \/etc\/alternatives\/java<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Let us try:<\/span><\/p>\n<pre class=\"lang:default decode:true\">ls -l \/etc\/alternatives\/java<\/pre>\n<p><span style=\"font-weight: 400;\">It should display something like:<\/span><\/p>\n<pre class=\"lang:default decode:true\">lrwxrwxrwx 1 root root 72 May 18 \u00a02016 \/etc\/alternatives\/java -&gt; \/usr\/lib\/jvm\/java-1.8.0-openjdk-1.8.0.91-0.b14.el7_2.x86_64\/jre\/bin\/java<\/pre>\n<p><span style=\"font-weight: 400;\">Further, to find out about the content of a file, you can use &#8220;file&#8221; command:<\/span><\/p>\n<pre class=\"lang:default decode:true\">file \/usr\/lib\/jvm\/java-1.8.0-openjdk-1.8.0.91-0.b14.el7_2.x86_64\/jre\/bin\/java<\/pre>\n<p><span style=\"font-weight: 400;\">This should display something like<\/span><\/p>\n<pre class=\"lang:default decode:true\">\/usr\/lib\/jvm\/java-1.8.0-openjdk-1.8.0.91-0.b14.el7_2.x86_64\/jre\/bin\/java: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU\/Linux 2.6.32, BuildID[sha1]=bb856d3fd5d21b9d8c9dd63b5c781bd8f9eccb87, stripped<\/pre>\n<p><span style=\"font-weight: 400;\">This means it is Linux binary.<\/span><\/p>\n<h2>Environment variables<\/h2>\n<p><span style=\"font-weight: 400;\">Unix shell provides environment variables. To see the entire list of environment variables, use:<\/span><\/p>\n<pre class=\"lang:default decode:true\">set<\/pre>\n<p><span style=\"font-weight: 400;\">These environment variables can be used by the shell, programs or commands.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">For example, the $PS1 variable is used by the shell to display the prompt. To change your prompt, you can try:<\/span><\/p>\n<pre class=\"lang:default decode:true\">PS1='xxx&gt;&gt;'<\/pre>\n<p><span style=\"font-weight: 400;\">The environment variable $PATH is a list of directories separated by a colon. It is used by the shell to find the file corresponding to a command.<\/span><\/p>\n<h3>Setting Environment variables<\/h3>\n<p><span style=\"font-weight: 400;\">You can set the environment variables simply by assignment: MYVAR=VAL<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The following list of commands:<\/span><\/p>\n<pre class=\"lang:default decode:true \">MYV=myfile\r\n\r\nls $MYV<\/pre>\n<p><span style=\"font-weight: 400;\">are equivalent to the single command:<\/span><\/p>\n<pre class=\"lang:default decode:true \">ls myfile<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This post is the continuation of\u00a0A Simple Tutorial on Linux &#8211; Part-1 In the Part-1 we learned the following topics on Linux. Linux Operating System Linux Files &amp; Process The Directory Structure Permissions Process Keeping up the same pace, we will learn the following topics in the 2nd part of the Linux series. Shell Scripting &hellip; <a href=\"https:\/\/cloudxlab.com\/blog\/linux-tutorial-part-2\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;A Simple Tutorial on Linux \u2013 Part-2&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1083,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[14],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>A Simple Tutorial on Linux \u2013 Part-2 | CloudxLab Blog<\/title>\n<meta name=\"description\" content=\"In the second part of the Linux Tutorial series, we will learn more commands with important concepts and see their practical examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cloudxlab.com\/blog\/linux-tutorial-part-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"A Simple Tutorial on Linux \u2013 Part-2 | CloudxLab Blog\" \/>\n<meta property=\"og:description\" content=\"In the second part of the Linux Tutorial series, we will learn more commands with important concepts and see their practical examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cloudxlab.com\/blog\/linux-tutorial-part-2\/\" \/>\n<meta property=\"og:site_name\" content=\"CloudxLab Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/cloudxlab\" \/>\n<meta property=\"article:published_time\" content=\"2017-12-26T04:00:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cloudxlab.com\/blog\/wp-content\/uploads\/2017\/12\/learn-linux-2.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"512\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CloudxLab\" \/>\n<meta name=\"twitter:site\" content=\"@CloudxLab\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"12 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/#website\",\"url\":\"https:\/\/cloudxlab.com\/blog\/\",\"name\":\"CloudxLab Blog\",\"description\":\"Learn AI, Machine Learning, Deep Learning, Devops &amp; Big Data\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/cloudxlab.com\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/linux-tutorial-part-2\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/cloudxlab.com\/blog\/wp-content\/uploads\/2017\/12\/learn-linux-2.png\",\"contentUrl\":\"https:\/\/cloudxlab.com\/blog\/wp-content\/uploads\/2017\/12\/learn-linux-2.png\",\"width\":1024,\"height\":512,\"caption\":\"linux tutorial pat 2\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/linux-tutorial-part-2\/#webpage\",\"url\":\"https:\/\/cloudxlab.com\/blog\/linux-tutorial-part-2\/\",\"name\":\"A Simple Tutorial on Linux \\u2013 Part-2 | CloudxLab Blog\",\"isPartOf\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/linux-tutorial-part-2\/#primaryimage\"},\"datePublished\":\"2017-12-26T04:00:40+00:00\",\"dateModified\":\"2017-12-26T04:00:40+00:00\",\"author\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/#\/schema\/person\/0efa3c54df68406de820ea466f002d3c\"},\"description\":\"In the second part of the Linux Tutorial series, we will learn more commands with important concepts and see their practical examples.\",\"breadcrumb\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/linux-tutorial-part-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cloudxlab.com\/blog\/linux-tutorial-part-2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/linux-tutorial-part-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/\",\"url\":\"https:\/\/cloudxlab.com\/blog\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/linux-tutorial-part-2\/#webpage\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/#\/schema\/person\/0efa3c54df68406de820ea466f002d3c\",\"name\":\"Abhinav Singh\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/fc74fe31169bf872f6ab11bbab621d53?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/fc74fe31169bf872f6ab11bbab621d53?s=96&d=mm&r=g\",\"caption\":\"Abhinav Singh\"},\"sameAs\":[\"https:\/\/cloudxlab.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/posts\/1077"}],"collection":[{"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/comments?post=1077"}],"version-history":[{"count":8,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/posts\/1077\/revisions"}],"predecessor-version":[{"id":1088,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/posts\/1077\/revisions\/1088"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/media\/1083"}],"wp:attachment":[{"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/media?parent=1077"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/categories?post=1077"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/tags?post=1077"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}