<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: laxman</title>
    <description>The latest articles on DEV Community by laxman (@laxmanvijay).</description>
    <link>https://dev.to/laxmanvijay</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F244176%2Ff61a7f83-fdb0-4781-9860-d9dc2eedf281.png</url>
      <title>DEV Community: laxman</title>
      <link>https://dev.to/laxmanvijay</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/laxmanvijay"/>
    <language>en</language>
    <item>
      <title>3 tips to find the value of 'this' in javascript</title>
      <dc:creator>laxman</dc:creator>
      <pubDate>Thu, 11 Nov 2021 18:49:14 +0000</pubDate>
      <link>https://dev.to/laxmanvijay/3-tips-to-find-the-value-of-this-in-javascript-5fo7</link>
      <guid>https://dev.to/laxmanvijay/3-tips-to-find-the-value-of-this-in-javascript-5fo7</guid>
      <description>&lt;p&gt;This article is to help you deduce the value of 'this' in javascript. It is not as simple as in Java where this refers to the current object. In javascript, "this" is dynamic. To calculate the value of this, the following 3 tips are enough and it is not difficult at all. I will present the tips and give some sample snippets in the end to help you understand the idea better. &lt;/p&gt;

&lt;p&gt;(Basic knowledge of JS is assumed)&lt;/p&gt;

&lt;h3&gt;
  
  
  The first tip:
&lt;/h3&gt;

&lt;p&gt;Javascript is a function-scoped language.&lt;/p&gt;

&lt;p&gt;It does not create new lexical scopes for every bracket pair. This is a common misunderstanding.&lt;/p&gt;

&lt;p&gt;The following syntax does not create a new scope:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (true) {
    // statements
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same applies for loops, switch statements as well.&lt;/p&gt;

&lt;p&gt;However the following statement does create a new scope:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getName() {
    return "Jake";
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note the use of function keyword here. &lt;strong&gt;Arrow functions do not create a new scope.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The second tip:
&lt;/h3&gt;

&lt;p&gt;Starting from ES6, there are two variants for creating functions in JS:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using function keyword&lt;/li&gt;
&lt;li&gt;Using arrow syntax&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The important difference between them is arrow function is very lightweight - it does not support prototype keyword; bind, call and apply don't work, arrow functions are not constructible and arrow functions do not create a scope. &lt;/p&gt;

&lt;p&gt;However the most important distinction lies in how they both handle this keyword.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;this keyword inside a normal function is &lt;strong&gt;bound to the object&lt;/strong&gt; where the reference of the function is &lt;strong&gt;invoked&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note: If there is no outer scope, the default scope is used, which is the global object (Window in case of browser and global in case of Node.js)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getName() {
    return this.name
}

// will return Window.name because getName is called globally.
getName();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;One catch is "this" of global scope will be set to undefined in strict mode.&lt;/strong&gt; (but not really the point of focus here)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;this keyword inside an arrow function is &lt;strong&gt;bound to the object&lt;/strong&gt; where the function is &lt;strong&gt;defined&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note the difference between defined and invoked. More examples on this after the third one.&lt;/p&gt;

&lt;h3&gt;
  
  
  The third tip:
&lt;/h3&gt;

&lt;p&gt;The function keyword is special. It sets its scope to the object even if it is defined using the object literal or function is defined using prototype property. And more special property of normal functions: nesting normal functions does not alter how this is resolved. Every nested function is simply treated as a top level function.&lt;/p&gt;

&lt;h4&gt;
  
  
  object literal syntax:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let obj = {
    fn: function() {
        // prints obj
        console.log(this)
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, the function using arrow syntax does not comply with the above rule (because remember js is function-scoped and not bracket-scoped).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let obj = {
    fn: () =&amp;gt; {
        // prints Window (outer scope in this case)
        console.log(this)
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  functional literal syntax:
&lt;/h4&gt;

&lt;p&gt;The extension to this rule is when you define object using the function literal.&lt;/p&gt;

&lt;p&gt;Consider a car class&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Car() {
    this.name = "BMW";
}

Car.prototype.getName = () =&amp;gt; this.name;

const c = new Car();

// Will return Window.name
c.getName();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the getName is defined using arrow syntax and thus does not obey the prototype declaration and prints out Window.name&lt;/p&gt;

&lt;p&gt;However,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Car.prototype.getName = function () {
  return this.name;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will return "BMW". This is because of the nature of function keyword is to obey prototype declaration or object literal.&lt;/p&gt;

&lt;p&gt;What happens in ES6 class syntax?&lt;/p&gt;

&lt;p&gt;You might know this already, ES6 classes are just a sugarcoat over the function literal for defining objects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Car {
    name = "BMW";

    getName() {
        return this.name;
    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above getName will return BMW because function keyword obeys function literal objects.&lt;/p&gt;

&lt;p&gt;Arrow syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Car {
    name = "BMW";

    getName = () =&amp;gt; {
        return this.name;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The arrow syntax also prints BMW because of a different and an interesting reason - &lt;strong&gt;since class keyword just abstracts function literal and function literal creates a scope, the getName arrow function is always bound to Car object. This is different to object literal arrow syntax case - where it was bound to the outer scope and not the object itself.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And that's it!&lt;/p&gt;

&lt;p&gt;These are the three tips which you can follow to always deduce the exact value of this keyword.&lt;/p&gt;

&lt;p&gt;Building up on the above idea, let's consider the below examples:&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 1: Indirect invocation
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Car {
    name = "BMW";

    getNameFn() {
       return this;
    }

    getNameArrow = () =&amp;gt; {
       return this;
    }
}

function printUtil(obj) {
    const util = (fn) =&amp;gt; {
      console.log(fn());
    }

    util(obj);
}


let c = new Car();
printUtil(c.getNameFn); // prints undefined
printUtil(c.getNameArrow); // prints BMW
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we pass a function defined inside a class to another function, the scope will change as in the example above, the first print statement produces undefined.&lt;/p&gt;

&lt;p&gt;However, if we define an arrow function, it is always bound to where it is defined (respecting the function-scope) and thus it prints BMW.&lt;/p&gt;

&lt;p&gt;In order to overcome this situation, js has methods like bind, apply and call which indirectly invoke the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;printUtil(c.getNameFn.bind(c)); // prints BMW
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;bind, call and apply are simple. They just call the function with the scope of any given object (This prevents function from having dynamic "this" value). Here, c.getNameFn is passed to printUtil and is bound to object "c" (It can be bound to any object for that matter). Therefore it prints BMW.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 2: Function Invocation
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function print() {
    console.log(this)
}

print()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since print function is invoked directly, it will print its outer scope object which is Window object.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 3: IIFE
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(function () {
    console.log(this)
})()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This syntax is called Immediately Invoked function expressions (IIFE). Quite a mouthful but nothing special about them. Consider them as normal functions that get invoked. &lt;/p&gt;

&lt;p&gt;Thus the value of this will be its outer scope object. Same as above.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 4: Nested Function
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let obj = {
    name = "car";

    function print() {

        function util() {
            console.log(this); // prints Window
        }

        util();
    }
}

obj.print()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The one caveat here is nesting normal functions does not modify how this resolves. (In other words, closure scope does modify this). Every nested function is simply treated as a top level function. Therefore util is still treated as a separate function and will print Window.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;But as you may have guessed, this is still dynamic. You can bind it to print function using the two methods as discussed earlier:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;use bind/call/apply keyword:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let obj = {
    name = "car";

    function print() {

        function util() {
            console.log(this); // prints obj
        }

        util.call(this);
    }
}

obj.print()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Or use the arrow syntax:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let obj = {
    name = "car";

    function print() {

        const util = () =&amp;gt; {
            console.log(this); // prints obj
        }

        util.call(this);
    }
}

obj.print()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The reason: You guessed it! JS is function-scoped and print function created a scope and since the scope of util arrow function is based on where it is defined (respecting function-scope).&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Always remember JS is function-scoped. It will solve a lot of this keyword confusions.&lt;/li&gt;
&lt;li&gt;The function keyword obeys function literal and object literal.&lt;/li&gt;
&lt;li&gt;The arrow syntax obeys only function-scope nature of js.&lt;/li&gt;
&lt;li&gt;Nesting normal functions do not modify how this is resolved.&lt;/li&gt;
&lt;li&gt;If the value of this inside a normal function has to be bound to a specific object consider bind/call/apply.&lt;/li&gt;
&lt;li&gt;Always prefer ES6 classes and Arrow syntax. Go for normal functions only when you have to extend a prototype.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;More resources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://dmitripavlutin.com/gentle-explanation-of-this-in-javascript/"&gt;https://dmitripavlutin.com/gentle-explanation-of-this-in-javascript&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Feedbacks, questions and constructive criticisms are always welcome!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>Compression and Archival in Linux</title>
      <dc:creator>laxman</dc:creator>
      <pubDate>Thu, 03 Dec 2020 10:25:02 +0000</pubDate>
      <link>https://dev.to/laxmanvijay/compression-and-archival-in-linux-53jl</link>
      <guid>https://dev.to/laxmanvijay/compression-and-archival-in-linux-53jl</guid>
      <description>&lt;p&gt;File archiving is used when one or more files need to be transmitted or stored as efficiently as possible. Linux supports lots of file archival mechanisms. This article describes the most popular ones.&lt;/p&gt;

&lt;p&gt;Before looking into those here's a quick definition of compression and archival&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Compression:&lt;/strong&gt; Makes the files smaller by removing redundant information.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Archival:&lt;/strong&gt; Combines multiple files into one, which eliminates the overhead in individual files and makes the files easier to transmit.&lt;/p&gt;

&lt;p&gt;Simply compression reduces size and archival combines files.&lt;/p&gt;

&lt;h2&gt;
  
  
  Compression algorithms:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;gzip&lt;/li&gt;
&lt;li&gt;bzip2&lt;/li&gt;
&lt;li&gt;xz&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  gzip:
&lt;/h3&gt;

&lt;p&gt;gzip (GNU zip) is a compression algorithm. It uses the &lt;strong&gt;Lempel-Ziv-Markov (LZMA) chain&lt;/strong&gt; algorithm. It is quite fast but the file size may be larger.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gzip {filename}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The original file is deleted and replaced by the compressed file.&lt;/p&gt;

&lt;h4&gt;
  
  
  Decompression of gzipped files:
&lt;/h4&gt;

&lt;p&gt;Decompression is done using &lt;code&gt;gunzip&lt;/code&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gunzip {gzipped filename}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Including &lt;code&gt;-l&lt;/code&gt; flag will show the compression information without actually compressing/decompressing.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  bzip2:
&lt;/h3&gt;

&lt;p&gt;bzip2 uses a different compression algorithm called &lt;strong&gt;Burrows-Wheeler block sorting&lt;/strong&gt;, which can compress files smaller than gzip at the expense of more CPU time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bzip2 {filename}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Decompression of bzip2'ed files:
&lt;/h4&gt;

&lt;p&gt;Decompression is done using &lt;code&gt;bunzip2&lt;/code&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bunzip2 {bunzip2'ed filename}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  xz:
&lt;/h3&gt;

&lt;p&gt;xz also uses the LZMA algorithm. It has the benefits of both gzip and bzip2. It compresses quickly and also results in smaller file sizes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;xz {filename}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Decompression of xzed files:
&lt;/h4&gt;

&lt;p&gt;Decompression is done using &lt;code&gt;unxz&lt;/code&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;unxz {xzed filename}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Archival:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;tar&lt;/li&gt;
&lt;li&gt;zip&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  tar:
&lt;/h3&gt;

&lt;p&gt;Tar is a short form of &lt;strong&gt;TApe Archive.&lt;/strong&gt; The tar command takes in several files and creates a single output file that can be split up again into the original files. The tar archived file is often called a tarball.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tar -f {filename} {options} {files to archive}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The tar command has three modes (pass the appropriate flag to mention the mode):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create: Make a new archive out of a series of files. (-c)&lt;/li&gt;
&lt;li&gt;Extract: Extract files out of an archive. (-x)&lt;/li&gt;
&lt;li&gt;List: Show the contents without extracting. (-t)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tar can also compress the resulting archive using the above compression algorithms.&lt;/p&gt;

&lt;p&gt;Provide any of the following flags to mention the compression algorithm.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;gzip (-z)&lt;/li&gt;
&lt;li&gt;bzip2 (-j)&lt;/li&gt;
&lt;li&gt;xz (-J)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;-v&lt;/code&gt; flag can be provided for a verbose result.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tar -cvJf backup.tar.xz projects/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a tarball of the &lt;code&gt;projects&lt;/code&gt; folder that is compressed using &lt;strong&gt;xz&lt;/strong&gt; with a filename of backup.tar.xz&lt;/p&gt;

&lt;p&gt;The extension can be anything but it is generally preferred to name this way.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;for gzip, it is &lt;code&gt;.tar.gz&lt;/code&gt; or &lt;code&gt;.tgz&lt;/code&gt;, &lt;code&gt;.taz&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;for bzip2, it is &lt;code&gt;tar.bz2&lt;/code&gt; or &lt;code&gt;.tb2&lt;/code&gt;, &lt;code&gt;.tbz&lt;/code&gt;, &lt;code&gt;.tbz2&lt;/code&gt;, &lt;code&gt;.tz2&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;for xz, it is &lt;code&gt;tar.xz&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Listing:
&lt;/h4&gt;

&lt;p&gt;You can list the contents inside the archive without actually extracting using the &lt;code&gt;-t&lt;/code&gt; flag.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tar -tvf backup.tar.xz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command lists the contents of &lt;code&gt;backup.tar.xz&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Unarchival:
&lt;/h4&gt;

&lt;p&gt;Unarchival is done by passing &lt;code&gt;-x&lt;/code&gt; flag.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tar -xvJf backup.tar.xz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command extracts the file in the same folder. If you wish to change it, pass the &lt;code&gt;-C&lt;/code&gt; flag. (which will change directory to the specified one, therefore the directory should be present.)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tar -C backups -xvJf backup.tar.xz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will extract in backups folder.&lt;/p&gt;

&lt;p&gt;In order to extract a specific file/folder from the archive, provide the relative path of the file/folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tar -xvJf backup.tar.xz projects/project1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will extract only project/project1 from the archive.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It is important to note that &lt;code&gt;-f&lt;/code&gt; flag should always precede the filename&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Zip:
&lt;/h3&gt;

&lt;p&gt;Zip is an archival and compression mechanism. It does both. The compression is lossless. The default compression algorithm is &lt;a href="https://en.wikipedia.org/wiki/DEFLATE"&gt;DEFLATE&lt;/a&gt;. It is more common than tarballs. It has builtin support in Windows and Mac. Therefore, it is more preferred for archival than tar.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;zip {options} {output filename} {files to compress}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default, zip will not compress recursively. Therefore in order to compress files/subfolders inside a folder, use &lt;code&gt;-r&lt;/code&gt; flag.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;zip files.zip temp*
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will compress all files starting with &lt;em&gt;temp&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;zip -r projects.zip projects
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will recursively compress all files inside the projects folder.&lt;/p&gt;

&lt;h4&gt;
  
  
  Unzip:
&lt;/h4&gt;

&lt;p&gt;the &lt;code&gt;unzip&lt;/code&gt; command is used to extract the contents of the zipped archive.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;unzip projects.zip
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To just list without extracting,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;unzip -l projects.zip
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To extract specific files/folders inside the zip,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;unzip projects.zip projects/project1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is similar to tar.&lt;/p&gt;

&lt;p&gt;Thanks for reading :)&lt;/p&gt;

</description>
      <category>linux</category>
      <category>computerscience</category>
      <category>devops</category>
    </item>
    <item>
      <title>Useful Linux commands </title>
      <dc:creator>laxman</dc:creator>
      <pubDate>Sun, 15 Nov 2020 09:38:46 +0000</pubDate>
      <link>https://dev.to/laxmanvijay/useful-linux-commands-3g9m</link>
      <guid>https://dev.to/laxmanvijay/useful-linux-commands-3g9m</guid>
      <description>&lt;p&gt;This is a list of useful Linux commands. Most of them are basic and they can be used as a reference for developers.&lt;/p&gt;

&lt;h4&gt;
  
  
  files and directories
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;ls  =&amp;gt; list all files&lt;/li&gt;
&lt;li&gt;ls -a =&amp;gt; list all files along with hidden ones&lt;/li&gt;
&lt;li&gt;ls -R =&amp;gt; list all files and folders recursively&lt;/li&gt;
&lt;li&gt;&lt;p&gt;ls -l =&amp;gt; list in long format (shows more info)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;rm {filename} =&amp;gt; deletes a file&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;mv {filename} {directory path} =&amp;gt; moves a file&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;mv {srcFileName} {destFileName} =&amp;gt; renames a file&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;cp {srcFileName} {destFileName} =&amp;gt; copies file&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;cp -R {srcDir} {destDir} =&amp;gt; copies directory&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;cat {filename} =&amp;gt; read contents of a file&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;cat &amp;gt; {filename} =&amp;gt; writes to file&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;touch {filename} =&amp;gt; creates a file, touch is basically used to change file timestamps. Refer &lt;a href="https://www.howtoforge.com/tutorial/linux-touch-command/"&gt;here&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;mkdir {directory name} =&amp;gt; makes a directory&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;rmdir {directory name} =&amp;gt; removes a directory&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;rm -rf {directory name} =&amp;gt; removes a directory recursively along with the files&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;mv {dirname1} {path or dirname} =&amp;gt; moves or renames directory&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;history =&amp;gt; list all commands used in the current session&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;clear =&amp;gt; clears the terminal screen&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;pwd =&amp;gt; displays the path of the current working directory.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  file permissions
&lt;/h4&gt;

&lt;p&gt;File permissions will be provided to users, groups and others (in the same order). They can be provided as numbers or letters.&lt;/p&gt;

&lt;p&gt;r - read, w - write, x - execute&lt;br&gt;
u - user, g - group, o - other, a - all&lt;/p&gt;

&lt;p&gt;0 - no permission&lt;br&gt;
1 - execute&lt;br&gt;
2 - write&lt;br&gt;
3 - execute + write&lt;br&gt;
4 - read&lt;br&gt;
5 - read + execute&lt;br&gt;
6 - read + write&lt;br&gt;
7 - all&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;chmod {number} {filename} =&amp;gt; gives permissions to user,group and others.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Eg: &lt;code&gt;chmod 777 ex.txt&lt;/code&gt; =&amp;gt; gives all permissions to all users, groups and others.&lt;/p&gt;

&lt;p&gt;'+' - gives permission&lt;br&gt;
'-' - removes permission&lt;br&gt;
'=' - assigns the given permission &lt;/p&gt;

&lt;p&gt;Eg: &lt;code&gt;chmod u=rw ex.txt&lt;/code&gt; =&amp;gt; gives read/write to user&lt;br&gt;
Eg: &lt;code&gt;chmod a= ex.txt&lt;/code&gt; =&amp;gt; removes all permission to all users.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;chgrp {group name} {filename} =&amp;gt; changes group for a file&lt;/li&gt;
&lt;li&gt;chown {user} {filename} =&amp;gt; changes owner of a file&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  input/ output directions
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;'&amp;gt;' write (stdout)&lt;br&gt;
Eg: ls -al &amp;gt; {filename} =&amp;gt; writes the output of "ls -al" to the file. If the file already exists, it is &lt;strong&gt;overwritten&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;'&amp;gt;&amp;gt;' append (stdout)&lt;br&gt;
Eg: &lt;code&gt;echo "hi" &amp;gt;&amp;gt; {filename}&lt;/code&gt; =&amp;gt; appends to the file&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;'&amp;lt;' input&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;File descriptors : 0 - stdin, 1 - stdout, 2 - stderr&lt;/p&gt;

&lt;h5&gt;
  
  
  Error redirection:
&lt;/h5&gt;

&lt;p&gt;This is used to redirect errors to a log file, rather than cluttering the terminal.&lt;/p&gt;

&lt;p&gt;Eg: &lt;code&gt;cat ex.txt 2&amp;gt; error.log&lt;/code&gt; =&amp;gt; tries to read the file, if there is any error, it is written to error.log&lt;/p&gt;

&lt;p&gt;Here, '2' is the file descriptor for stderr.&lt;/p&gt;

&lt;h4&gt;
  
  
  working with pipes
&lt;/h4&gt;

&lt;h5&gt;
  
  
  pg, more and less
&lt;/h5&gt;

&lt;p&gt;Show large text in the terminal as scrollable chunks by piping it to the pg, more or less commands.&lt;/p&gt;

&lt;p&gt;Eg: &lt;code&gt;cat large_file.txt | less&lt;/code&gt; =&amp;gt; gives a scrollable viewer.&lt;/p&gt;

&lt;h5&gt;
  
  
  grep
&lt;/h5&gt;

&lt;p&gt;Search for a string in the output by piping to grep.&lt;/p&gt;

&lt;p&gt;Eg: &lt;code&gt;ls | grep key&lt;/code&gt; =&amp;gt; shows files that have a string "key".&lt;/p&gt;

&lt;p&gt;Flags:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;-i =&amp;gt; case insensitive&lt;/li&gt;
&lt;li&gt;-c =&amp;gt; displays only count&lt;/li&gt;
&lt;li&gt;-n =&amp;gt; displays matching line and number&lt;/li&gt;
&lt;li&gt;-v =&amp;gt; displays output that didn't match&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Eg: &lt;code&gt;ls | grep -cv key&lt;/code&gt; =&amp;gt; displays count to output that didn't match&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;grep can also accept regular expressions. Refer &lt;a href="https://www.guru99.com/linux-regular-expressions.html"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  sort
&lt;/h5&gt;

&lt;p&gt;Sort the contents&lt;/p&gt;

&lt;p&gt;Eg: &lt;code&gt;ls | sort&lt;/code&gt; =&amp;gt; displays in alphabetic order&lt;/p&gt;

&lt;p&gt;Flags:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;-r =&amp;gt; reverse&lt;/li&gt;
&lt;li&gt;-n =&amp;gt; numerical&lt;/li&gt;
&lt;li&gt;-f =&amp;gt; case insensitive&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Eg: &lt;code&gt;ls -l | sort -nk2&lt;/code&gt; =&amp;gt; displays content numerically sorted on the second column. (number of hard links in this case)&lt;/p&gt;

&lt;h4&gt;
  
  
  variables
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;VARIABLE=value =&amp;gt; Set variables in the session.&lt;/li&gt;
&lt;li&gt;$VARIABLE =&amp;gt; gets the value of a variable.&lt;/li&gt;
&lt;li&gt;unset VARIABLE =&amp;gt; unsets the variable.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  networking commands
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;ping {ip} =&amp;gt; checks the connection&lt;/li&gt;
&lt;li&gt;dig {hostname} =&amp;gt; performs a dns lookup&lt;/li&gt;
&lt;li&gt;ssh -i {key} {username@ip} =&amp;gt; connects to a remote computer securely. key is the private key file.&lt;/li&gt;
&lt;li&gt;scp -i {key.pem} {source} {dest} =&amp;gt; copies a file. remote location should be &lt;code&gt;host:file&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;lsof -i {protocol:port} =&amp;gt; list all processes listening on a port.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Eg: &lt;code&gt;lsof -i tcp:3000&lt;/code&gt; lists all processes listening to port 3000&lt;/p&gt;

&lt;h4&gt;
  
  
  process management
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;top =&amp;gt; lists all running process&lt;/li&gt;
&lt;li&gt;kill {pid} =&amp;gt; kills a process with the given process id&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  mount device
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;lsblk =&amp;gt; list all block devices&lt;/li&gt;
&lt;li&gt;mount {device id} {directory} =&amp;gt; mount a device to a directory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Eg: &lt;code&gt;mount /dev/sda4 /ex/mnt&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Flags:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;-a =&amp;gt; mounts all devices in /etc/fstab&lt;/li&gt;
&lt;li&gt;-l =&amp;gt; lists all mounted devices&lt;/li&gt;
&lt;li&gt;&lt;p&gt;-t =&amp;gt; type of filesystem (eg: ext4)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;umount {device id} =&amp;gt; unmounts a device&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  manual
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;man {command name} =&amp;gt; shows usage of a command&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;cover image by &lt;a href="https://unsplash.com/photos/Tjbk79TARiE?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditShareLink"&gt;Sai Kiran Anagani&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>linux</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
