<?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: yash sugandh</title>
    <description>The latest articles on DEV Community by yash sugandh (@yashsugandh).</description>
    <link>https://dev.to/yashsugandh</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%2F377281%2F1837bec8-09d1-41a0-babe-6165e22fd45a.jpeg</url>
      <title>DEV Community: yash sugandh</title>
      <link>https://dev.to/yashsugandh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yashsugandh"/>
    <language>en</language>
    <item>
      <title>Optional: Handling NullPointer Graciously</title>
      <dc:creator>yash sugandh</dc:creator>
      <pubDate>Tue, 21 Dec 2021 13:30:53 +0000</pubDate>
      <link>https://dev.to/yashsugandh/optional-handling-nullpointer-graciously-107j</link>
      <guid>https://dev.to/yashsugandh/optional-handling-nullpointer-graciously-107j</guid>
      <description>&lt;p&gt;We all know about a billion dollar mistake known as the &lt;strong&gt;NullPointer Reference&lt;/strong&gt;, we all have regular encounters with Null Pointer and sometimes even had to face the wrath of &lt;strong&gt;NullPointer Exception&lt;/strong&gt;. The topic we are going to look into today &lt;strong&gt;Optional&lt;/strong&gt; is going to provide us a way to handle this issue much more graciously. &lt;/p&gt;

&lt;h3&gt;
  
  
  What is Optional and why was it introduced?
&lt;/h3&gt;

&lt;p&gt;Optional is class introduced in Java 8 under &lt;code&gt;java.util&lt;/code&gt; package that allows us to avoid NullPointer Exception that we encounter a lot of times.&lt;/p&gt;

&lt;p&gt;Optional provides us a way to check and use whether a value is null or not .This way we can save ourself from the wrath of NullPointers and handle them in a way that is &lt;strong&gt;elegant, easy to use and maintain&lt;/strong&gt; i.e. without using a ton of if-else everywhere to check if an object is null or not.&lt;/p&gt;

&lt;h3&gt;
  
  
  Where can we find Optional being used?
&lt;/h3&gt;

&lt;p&gt;Now we all know that we should never return null from a method, right?&lt;/p&gt;

&lt;p&gt;If you are one of those people who return null from a method and you know who you are don't hide it, please refrain from doing so.&lt;/p&gt;

&lt;p&gt;Jokes aside, there are other scenarios where we are not sure if we will receive a value or not, such as when we use Streams &lt;code&gt;findfirst()&lt;/code&gt; method i.e. a method that returns the first value of a Stream if the Stream is non-empty so there is possibility a value may not be returned in case the Stream is empty hence an Optional is returned.&lt;/p&gt;

&lt;p&gt;Another example that we generally use is the &lt;code&gt;findById()&lt;/code&gt; method which is used to get the data from database on the bases of an &lt;strong&gt;id&lt;/strong&gt; so the method may not return a value in case Id is not found in database hence provides us with an optional.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do we create an Optional?
&lt;/h3&gt;

&lt;p&gt;There are three different ways of creating an Optional&lt;/p&gt;

&lt;p&gt;1.Creating an empty Optional&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Optional.empty()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The above method creates an empty Optional i.e. an optional with no value inside&lt;/p&gt;

&lt;p&gt;2.Creating an Optional some non null value &lt;br&gt;
&lt;code&gt;Optional.of(10)&lt;/code&gt; &lt;br&gt;
&lt;code&gt;Optional.of("abcd")&lt;/code&gt;&lt;br&gt;
and so on&lt;/p&gt;

&lt;p&gt;We can create Optional with different type of values integer, string or an object.&lt;/p&gt;

&lt;p&gt;For the cases where values can also be null we have the following&lt;/p&gt;

&lt;p&gt;3.Creating an Optional which can also contain null value&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Optioanl.ofNullable(10)&lt;/code&gt;&lt;br&gt;
&lt;code&gt;Optional.ofNullable(null)&lt;/code&gt;&lt;br&gt;
and so on.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Optional.ofNullable()&lt;/code&gt; can be used in the cases where there is a possibility of the value being a null reference. &lt;/p&gt;

&lt;p&gt;Now that we know how to create Optionals the next step is to check whether an Optional contains a value or not&lt;/p&gt;

&lt;p&gt;1.isPresent()&lt;/p&gt;

&lt;p&gt;In Java 8 the best way to check whether an Optional has a value or not is to use the method &lt;code&gt;isPresent()&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;This method structure is as follows&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   public boolean isPresent() {
        return value != null;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The method return us a boolean after checking if a value is present inside an Optional or not.&lt;/p&gt;

&lt;p&gt;It returns a true if a value is present otherwise it returns false.&lt;/p&gt;

&lt;p&gt;Let's look at example on how to use the method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   final Optional&amp;lt;Integer&amp;gt; optionalInteger = Optional.of(10);
   final Optional&amp;lt;Object&amp;gt; emptyOptional = Optional.empty();

   System.out.println(emptyOptional.isPresent());
   System.out.println(optionalInteger.isPresent());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output for &lt;strong&gt;emptyOptional&lt;/strong&gt; is be false and the &lt;strong&gt;optionalInteger&lt;/strong&gt; is true.&lt;/p&gt;

&lt;p&gt;2.isEmpty()&lt;/p&gt;

&lt;p&gt;In Java 11 another method was introduced for checking whether optional contains a value or not.&lt;/p&gt;

&lt;p&gt;This method structure is as follows&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    public boolean isEmpty() {
        return value == null;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If a value is not present it returns true, otherwise it returns false.&lt;/p&gt;

&lt;p&gt;Let's look at example on how to use the &lt;code&gt;isEmpty()&lt;/code&gt; method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; final Optional&amp;lt;Integer&amp;gt; optionalInteger = Optional.of(10);
 final Optional&amp;lt;Object&amp;gt; emptyOptional = Optional.empty();
 System.out.println(emptyOptional.isEmpty());
 System.out.println(optionalInteger.isEmpty());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output for &lt;strong&gt;emptyOptional&lt;/strong&gt; is be true and the &lt;strong&gt;optionalInteger&lt;/strong&gt; is false.&lt;/p&gt;

&lt;p&gt;From what we can see from examples both the &lt;code&gt;isPresent()&lt;/code&gt; and &lt;code&gt;isEmpty()&lt;/code&gt; work in kind of similar manner except that one checks whether the value is present in Optional and other checks for whether the Optional is empty or not.&lt;/p&gt;

&lt;p&gt;So based on the requirement we have we can use either or those.&lt;/p&gt;

&lt;p&gt;There are cases where we want to perform an operation if the value is present.&lt;/p&gt;

&lt;p&gt;In this scenario we can of course use the combination of &lt;code&gt;if(isPresent())&lt;/code&gt; but this will just lead us back to how we use to deal with null references before Optional using if-else statement which we don't want to do.&lt;/p&gt;

&lt;p&gt;So for use cases like this we have a method &lt;/p&gt;

&lt;p&gt;3.ifPresent()&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;ifPresent()&lt;/code&gt; method will only execute the operation if the Optional contains some non null value.&lt;/p&gt;

&lt;p&gt;This method structure is as follows&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    public void ifPresent(Consumer&amp;lt;? super T&amp;gt; action) {
        if (value != null) {
            action.accept(value);
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The method does not return anything and what it takes as a parameter is a &lt;code&gt;Consumer&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's take an example on how to use &lt;code&gt;ifPresent()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; final Optional&amp;lt;Integer&amp;gt; optionalInteger = Optional.of(10);

 optionalInteger.ifPresent(integer -&amp;gt; System.out.println(integer + 10));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above we can see that the output will only be printed if the value is present and in other cases no output will be printed&lt;/p&gt;

&lt;p&gt;What if we want to print something or execute a different function if Optional does not contain any value?&lt;/p&gt;

&lt;p&gt;4.ifPresentOrElse()&lt;/p&gt;

&lt;p&gt;Introduced in Java 9, the &lt;code&gt;ifPresentOrElse()&lt;/code&gt; allows us to provide an alternative in which even if the Optional is empty we can perform an action&lt;/p&gt;

&lt;p&gt;This method structure is as follows&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    public void ifPresentOrElse(Consumer&amp;lt;? super T&amp;gt; action, Runnable emptyAction) {
        if (value != null) {
            action.accept(value);
        } else {
            emptyAction.run();
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So if a value is present, it will perform the given action with the value, otherwise it will perform the given empty-based action.&lt;/p&gt;

&lt;p&gt;Let's take an example on how to use &lt;code&gt;ifPresentOrElse()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    final Optional&amp;lt;Object&amp;gt; emptyOptional = Optional.empty();

    emptyOptional.ifPresentOrElse(
        val -&amp;gt; System.out.println("The value present inside the optional is  " + val),
        () -&amp;gt; System.out.println("The optional is empty"));

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

&lt;/div&gt;



&lt;p&gt;In the above example we can see that the method will print the value if it is present else it will print "The optional is empty".&lt;/p&gt;

&lt;p&gt;Till now we have various methods that help us in checking whether the value is present or not bu what if we want to use the value.&lt;/p&gt;

&lt;p&gt;1.get()&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NEVER USE THIS METHOD&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;get()&lt;/code&gt; as the name suggest tries to get the value from the Optional and if the value is not present it will throw a &lt;strong&gt;NoSuchElementException&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Using this method after checking if the value is present or not is still considerable but using it without even checking defeats the whole purpose of using the Optional in the first place.&lt;/p&gt;

&lt;p&gt;The method structure is as follows&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; public T get() {
   if (value == null) {
        throw new NoSuchElementException("No value present");
      }
        return value;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Methods that can be used instead of &lt;code&gt;get()&lt;/code&gt; if you just want to access the value inside the Optional are&lt;/p&gt;

&lt;p&gt;2.orElse()&lt;/p&gt;

&lt;p&gt;One of the way is that we can provide a default value to be used in case no value is found inside the Optional.&lt;/p&gt;

&lt;p&gt;The method structure is as follows&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    public T orElse(T other) {
        return value != null ? value : other;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As the name suggests the method &lt;code&gt;orElse()&lt;/code&gt; will return the value if a value is present inside the Optional if no value is present we can provide a &lt;strong&gt;default value&lt;/strong&gt; i.e. "other" parameter to be returned.&lt;/p&gt;

&lt;p&gt;Let's look at an example on how to use &lt;code&gt;orElse()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; final Optional&amp;lt;Object&amp;gt; emptyOptional = Optional.empty();

 System.out.println(emptyOptional.orElse(10));
System.out.println(emptyOptional.orElse(generateRandomNumber()));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example the first output will display &lt;strong&gt;10&lt;/strong&gt; as the Optional we used is an &lt;strong&gt;emptyOptional&lt;/strong&gt; so the default value we passed in orElse() will be used and for the second print statement a default value will be provide from the method "generateRandomNumber()".&lt;/p&gt;

&lt;p&gt;Another way we can get value without worrying about "NoSuchElementException" is &lt;/p&gt;

&lt;p&gt;3.orElseGet()&lt;/p&gt;

&lt;p&gt;The method &lt;code&gt;orElseGet()&lt;/code&gt; is very similar to the method we saw above &lt;code&gt;orElse()&lt;/code&gt;. We can provide both a default value directly as well call a function to provide us with a value.&lt;/p&gt;

&lt;p&gt;The method structure is as follows&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    public T orElseGet(Supplier&amp;lt;? extends T&amp;gt; supplier) {
        return value != null ? value : supplier.get();
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see that the method &lt;code&gt;orElseGet()&lt;/code&gt; takes in a Supplier as an input which is used in cases where Optional does not contain any value.&lt;/p&gt;

&lt;p&gt;The difference between the structure of &lt;code&gt;orElse()&lt;/code&gt; and &lt;code&gt;orElseGet()&lt;/code&gt; is clear that orElse method accepts a generic parameter T where as orElseGet method accepts a supplier.&lt;/p&gt;

&lt;p&gt;Let's look at an example on how to use &lt;code&gt;orElseGet()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; final Optional&amp;lt;Object&amp;gt; emptyOptional = Optional.empty();

  System.out.println(emptyOptional.orElseGet(() -&amp;gt; 10));
  System.out.println(emptyOptional.orElseGet(() -&amp;gt; generateRandomNumber()));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example the first output will display &lt;strong&gt;10&lt;/strong&gt; as the Optional we used is an &lt;strong&gt;emptyOptional&lt;/strong&gt; so the default value we passed in orElseGet() will be used and for the second print statement a default value will be provide from the method "generateRandomNumber()".&lt;/p&gt;

&lt;h4&gt;
  
  
  The difference between &lt;code&gt;orElse()&lt;/code&gt; and &lt;code&gt;orElseGet()&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;We were able to see that on the surface both the methods look and works almost the same but structure wise &lt;code&gt;orElse&lt;/code&gt; uses the generic parameter where as the &lt;code&gt;orElseGet&lt;/code&gt; uses the Supplier.&lt;/p&gt;

&lt;p&gt;The major difference between the two method which impacts the performance is when we use &lt;code&gt;orElse&lt;/code&gt; even in the cases where the default value is not used it will be created and stored in memory but in case of &lt;code&gt;orElseGet&lt;/code&gt; it will only be created if and when needed.&lt;/p&gt;

&lt;p&gt;If the default value is a simple int it may not such a big difference but imagine the cases where a method is being called it will not only take a place in call stack but also has the ability to affect the performance.&lt;/p&gt;

&lt;p&gt;Another case that may be possible is when we do not want to provide a default value but we want to throw an error in case a value is not present in Optional.&lt;/p&gt;

&lt;p&gt;4.orElseThrow()&lt;/p&gt;

&lt;p&gt;There are 2 different variants of orElseThow&lt;/p&gt;

&lt;p&gt;1.Default variant&lt;/p&gt;

&lt;p&gt;Introduced in Java 10 the default variant of &lt;code&gt;orElseThrow&lt;/code&gt; works similar to how get() works i.e. if will check whether an element is present or not and if an element is not present it will throw and "NoSuchElementException".&lt;/p&gt;

&lt;p&gt;The structure is as follows&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  public T orElseThrow() {
     if (value == null) {
        throw new NoSuchElementException("No value present");
      }
     return value;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The difference between the&lt;code&gt;get()&lt;/code&gt; and &lt;code&gt;orElseThrow()&lt;/code&gt; method is that even though internally both will throw the same "NoSuchElementException" exception when a value is not present, the person going through the code will be able to understand that method &lt;code&gt;orElseThrow&lt;/code&gt; can throw an error by just looking at its name where as in the method &lt;code&gt;get&lt;/code&gt; it will be a side-effect.&lt;/p&gt;

&lt;p&gt;Let's look at an example on how to use &lt;code&gt;orElseThrow()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    final Optional&amp;lt;Object&amp;gt; emptyOptional = Optional.empty();

    emptyOptional.orElseThrow();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example we can see that an "NoSuchElementException" will be thrown.&lt;/p&gt;

&lt;p&gt;2.Parameterized variant&lt;/p&gt;

&lt;p&gt;Introduced in Java 8, in this variant we can define the error we want to throw if no value is present inside the Optional.&lt;/p&gt;

&lt;p&gt;The structure is as follows&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    public &amp;lt;X extends Throwable&amp;gt; T orElseThrow(Supplier&amp;lt;? extends X&amp;gt; exceptionSupplier) throws X {
        if (value != null) {
            return value;
        } else {
            throw exceptionSupplier.get();
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's look at an example on how to use &lt;code&gt;orElseThrow()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   final Optional&amp;lt;Object&amp;gt; emptyOptional = Optional.empty();

    emptyOptional.orElseThrow(IllegalStateException::new);
    emptyOptional.orElseThrow(() -&amp;gt; new IllegalStateException("Data Not Found"));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example we can see that an "IllegalStateException" will be thrown if value is not present in Optional.&lt;/p&gt;

&lt;p&gt;We can either create an exception with default constructor or we can also pass in a message.&lt;/p&gt;

&lt;p&gt;Now we have gone through &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How to create an Optional&lt;/li&gt;
&lt;li&gt;How to check if an Optional contains any value or not&lt;/li&gt;
&lt;li&gt;How to proceed if an Optional has a value &lt;/li&gt;
&lt;li&gt;How to provide a default value&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What if we want to transform a value inside an Optional&lt;/p&gt;

&lt;p&gt;For the purpose of transformation Optional provides us with various functions such as map, flatmap and filter.&lt;/p&gt;

&lt;h2&gt;
  
  
  When To Use an Optional
&lt;/h2&gt;

&lt;p&gt;1.Return Value&lt;br&gt;
Optional is suitable to be a return value where expected that a value may or may not be present.&lt;/p&gt;

&lt;p&gt;For example the &lt;code&gt;findById&lt;/code&gt; method or the &lt;code&gt;findFirst&lt;/code&gt; method. &lt;/p&gt;
&lt;h2&gt;
  
  
  When Not To Use an Optional
&lt;/h2&gt;

&lt;p&gt;1.As a parameter&lt;/p&gt;

&lt;p&gt;I have seen some cases where someone has used Optional as a parameter of a method&lt;/p&gt;

&lt;p&gt;For example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  public void function1(Optional&amp;lt;Integer&amp;gt; val){
    if (val.isPresent()){
      function2(val);
    }else{
      function3();
    }
  } 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above we can see that optional is being misused.&lt;/p&gt;

&lt;p&gt;Issues in the above example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Single Responsibility Principle is being violated&lt;/li&gt;
&lt;li&gt;The parent function should have handled the choice instead of the child function&lt;/li&gt;
&lt;li&gt;Why would you want to check if a parameter exists or not, instead why not create 2 separate functions for each use case &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Wrapping a collection such as ArrayList in Optional&lt;/p&gt;

&lt;p&gt;I have seen some cases where an ArrayList is being passed wrapped inside an Optional and later is being checked where it is empty or not&lt;/p&gt;

&lt;p&gt;I don't know why would someone do it, if you want to check a collection is empty or not than there are already methods created for it so why would you need to wrap a collection in Optional.&lt;/p&gt;

&lt;p&gt;Let me know if you have some more use cases of when and when not use an Optional.&lt;/p&gt;

&lt;p&gt;See you in the funny papers 🚀&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>computerscience</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Generics In Java</title>
      <dc:creator>yash sugandh</dc:creator>
      <pubDate>Wed, 21 Apr 2021 11:20:13 +0000</pubDate>
      <link>https://dev.to/yashsugandh/generics-in-java-3mb5</link>
      <guid>https://dev.to/yashsugandh/generics-in-java-3mb5</guid>
      <description>&lt;h2&gt;
  
  
  What is the meaning of Generics?
&lt;/h2&gt;

&lt;p&gt;One of the definitions from Java docs :&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Generics allow you to abstract over types. The most common examples are container types, such as those in the Collections hierarchy.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In English:&lt;/p&gt;

&lt;p&gt;Abstract over type means to group together similar and generalize them.&lt;/p&gt;

&lt;p&gt;For Example: what do we do if we need to save firstName, lastName and email of multiple people do we create n number of variables to store and retrieve them manually OR &lt;br&gt;
we create an Abstraction over it known as &lt;strong&gt;Class&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Great!, Now we know what abstract over type means, but I still don't get how is it beneficial to us???&lt;/p&gt;

&lt;p&gt;In short: Generics provides us with compile time type-checking and saves the need of manual type-casting.&lt;/p&gt;

&lt;p&gt;In depth: Let's find out&lt;/p&gt;

&lt;p&gt;Let's take an example of how Collection's used to work before introduction of generics.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;List numList = new ArrayList(); // 1
numList.add(new Integer(1)); // 2
Integer x = (Integer) numList.iterator().next(); // 3    
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Line 1 and Line 2 are simple, we created an ArrayList and added an Integer.&lt;/p&gt;

&lt;p&gt;On Line 3 we have something that looks painful and will probably make you a little irritated &lt;strong&gt;Manual Type Conversion&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Just imagine doing that every single time.&lt;/p&gt;

&lt;p&gt;In the above case even though the developer knows that the data is Integer at compile time we still need to manually convert it to Integer.&lt;/p&gt;

&lt;p&gt;This was the best case scenario where even though it looks bad it still works &lt;strong&gt;what if&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;List list = new ArrayList(); //1
list.add("abc"); //2
list.add(new Integer(5)); //3 

for(Object obj : list){
    Integer numbers=(Integer) obj; 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Line 1 we created an ArrayList&lt;br&gt;
Line 2 we add a String to list&lt;br&gt;
Line 3 we add an Integer to the list&lt;/p&gt;

&lt;p&gt;Wait what!!&lt;br&gt;
We first added a String and now an Integer to the same list&lt;/p&gt;

&lt;p&gt;Okay what's next&lt;br&gt;
we iterate over List and try to manually type cast it to Integer &lt;/p&gt;

&lt;p&gt;So, we added a String and an Integer, and now we are expecting both of them to be type-casted to an Integer &lt;/p&gt;

&lt;p&gt;You know what's coming&lt;/p&gt;
&lt;h3&gt;
  
  
  ClassCastException 🎆 💥
&lt;/h3&gt;

&lt;p&gt;We all know someone who is capable of doing this 😉&lt;/p&gt;

&lt;p&gt;What were the problems we found till now:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We need an abstraction over these so that we don't need to manually convert it every time&lt;/li&gt;
&lt;li&gt;We need a compile time check to save us from &lt;strong&gt;ClassCastException&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is what our savior Generics helps us with &lt;/p&gt;

&lt;p&gt;How? Let's find out&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;List&amp;lt;String&amp;gt; listOfString = new ArrayList&amp;lt;&amp;gt;(); //1
listOfString.add("Generics"); //2
listOfString.add("are"); //3
listOfString.add("Awesome"); //4

for(String str : listOfString){ //5
     //no type casting needed, avoids ClassCastException
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Most of the lines in the above code looks just as before except the manual typecast is gone 😌 .&lt;/p&gt;

&lt;p&gt;There is also something new on line 1&lt;br&gt;
&lt;code&gt;List&amp;lt;String&amp;gt; listOfString = new ArrayList&amp;lt;&amp;gt;();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;&amp;gt;&lt;/code&gt; is used in generics to help us define the type of data we are going to store in the list.&lt;/p&gt;

&lt;p&gt;That's it that all we need to do for adding a compile time type safety.&lt;/p&gt;

&lt;p&gt;What if someone tries to add an Integer to the above list?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2F2ODmFsk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2F2ODmFsk.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we try to add an Integer we get a compile time error.&lt;/p&gt;

&lt;p&gt;So, can we use Generics only with Collections?&lt;/p&gt;

&lt;p&gt;No, Collections is just one of the places where we can use Generics. Let's look at other places where we can use Generics.&lt;/p&gt;

&lt;p&gt;Let's start small &lt;/p&gt;
&lt;h2&gt;
  
  
  Generic Interface
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface Print&amp;lt;T&amp;gt;{
  void display(T input);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We can create generic interfaces which can be implemented by Classes with their respective data-type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class PrintInteger implements Print&amp;lt;Integer&amp;gt;{

  @Override
  public void display(Integer input) {
    System.out.println("Printing an int "+input);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have implemented Print interface and specified the data-type as Integer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Generics Class and Generic methods
&lt;/h2&gt;

&lt;p&gt;Let's take a situation where we want to store firstName, lastName and an id of an employee but the data-type of id can be an int or long or String.&lt;/p&gt;

&lt;p&gt;Code without Generics&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class EmployeeWithIntegerId {
private String firstName;
private String lastName;
private int id;
}

class EmployeeWithLongId {
private String firstName;
private String lastName;
private long id;
}

class EmployeeWithStringId {
private String firstName;
private String lastName;
private String id;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see that without using generics we have to create 3 different classes to cater the situation.&lt;/p&gt;

&lt;p&gt;Now, let's use generics to get us out of this situation&lt;/p&gt;

&lt;p&gt;Code with Generics&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Employee&amp;lt;T&amp;gt;{
  private String firstName;
  private String lastName;
  private T id;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;T&lt;/strong&gt; in the above code represents the data-type of our id which will be decided by us while creating the instance of the class.&lt;/p&gt;

&lt;p&gt;Is &lt;strong&gt;T&lt;/strong&gt; mandatory to use?&lt;/p&gt;

&lt;p&gt;No, &lt;strong&gt;T&lt;/strong&gt; is just the most commonly used one, but it is not mandatory. We can use any alphabet we like in place of &lt;strong&gt;T&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For people who are interested in the best practices in naming convention&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The most commonly used type parameter names are:

E - Element (used extensively by the Java Collections Framework)
K - Key
N - Number
T - Type
V - Value
S,U,V etc. - 2nd, 3rd, 4th types
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;courtesy of java docs&lt;/p&gt;

&lt;p&gt;Now, when we use generics we only needed to create a single class where the only thing that needs to be decided was the type of our variable id.&lt;/p&gt;

&lt;p&gt;Okay, but how will the getters, setters and constructor work ??&lt;/p&gt;

&lt;h3&gt;
  
  
  Constructor
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  public Employee(String firstName, String lastName, T id) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.id = id;
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The constructor will look the same as before. It is one of the best and easy to understand example of generic methods.&lt;/p&gt;

&lt;h3&gt;
  
  
  Getters and Setters
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  public T getId() {
    return id;
  }

  public void setId(T id) {
    this.id = id;
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Okay, so we have defined the class, so how can we use it??&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Employee&amp;lt;String&amp;gt; employee= new Employee&amp;lt;&amp;gt;("yash","sugandh","1234"); 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example we created an object of the class Employee and while creating it we used &lt;code&gt;&amp;lt;&amp;gt;&lt;/code&gt; operator to define the data-type of &lt;strong&gt;T&lt;/strong&gt; as String.&lt;/p&gt;

&lt;p&gt;There is a restriction on data-type that we cannot use primitive data-types, so we cannot use int we have to Integer, Long instead of long and so on.&lt;/p&gt;

&lt;p&gt;Now, we know about generics and how it helps us in Collections, Interface, Classes and methods.&lt;/p&gt;

&lt;p&gt;But, there are still few questions left&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What are wildcards in generics ?&lt;/li&gt;
&lt;li&gt;What is the meaning of bounded and unbounded wildcard ?&lt;/li&gt;
&lt;li&gt;How does Java ensure backward compatibility from code with generics to code without generics?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's cover all these topics in the next post.&lt;/p&gt;

&lt;p&gt;Please let me know if there are any questions in the comments below.&lt;/p&gt;

&lt;p&gt;See you in the funny papers 🚀 &lt;/p&gt;

</description>
      <category>java</category>
      <category>computerscience</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Package Management in Linux System</title>
      <dc:creator>yash sugandh</dc:creator>
      <pubDate>Mon, 07 Sep 2020 09:53:49 +0000</pubDate>
      <link>https://dev.to/yashsugandh/package-management-in-linux-system-1egp</link>
      <guid>https://dev.to/yashsugandh/package-management-in-linux-system-1egp</guid>
      <description>&lt;p&gt;In the last few posts, we explored &lt;a href="https://dev.to/yashsugandh/permissions-in-linux-system-gj"&gt; Permissions in Linux System &lt;/a&gt; and &lt;a href="https://dev.to/yashsugandh/changing-permissions-in-linux-system-4a2b"&gt;Changing Permissions in Linux System&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Today, let's take a step in a different direction and understand Package management in Linux System.&lt;/p&gt;

&lt;p&gt;Package management is a method of installing and maintaining(update, remove, manage dependency, etc) software in the system.&lt;/p&gt;

&lt;p&gt;Softwares are usually distributed in packages and stored in repositories.&lt;/p&gt;

&lt;p&gt;Earlier, one had to download and compile source code to install the software.&lt;/p&gt;

&lt;p&gt;Now, most of the modern Unix-like operating systems offer a centralized mechanism for finding and installing software.&lt;/p&gt;

&lt;p&gt;But before we jump into how the package management system works, let's look into what are the advantages of using it &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ease of installation and removal of software&lt;/li&gt;
&lt;li&gt;To keep track of updates and upgrades&lt;/li&gt;
&lt;li&gt;To provide consistent usage throughout the devices.&lt;/li&gt;
&lt;li&gt;To manage dependencies while installing a new software&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Okay, there are different Linux distributions. Do all of them use the same packaging management system?&lt;/p&gt;

&lt;p&gt;No, different distributions use different packaging management systems.&lt;/p&gt;

&lt;p&gt;So, a package intended for one distribution is generally not compatible with another distribution.&lt;/p&gt;

&lt;h2&gt;
  
  
  How a Package System Works
&lt;/h2&gt;

&lt;p&gt;Most package management systems are built around collections of package files.&lt;/p&gt;

&lt;p&gt;A package file is a collection of files that comprise the software package.&lt;/p&gt;

&lt;p&gt;A package may consist of numerous package files and pre- and post-installation scripts that perform configuration tasks before and after the package installation.&lt;/p&gt;

&lt;p&gt;Package management systems usually consist of two types of tools.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Low-level tools&lt;/strong&gt; which handle tasks such as installing and removing package files&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;High-level tools&lt;/strong&gt; that perform metadata searching and dependency resolution&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The functionality may be similar in all the distributions but their tools are different&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Distribution&lt;/th&gt;
&lt;th&gt;Format&lt;/th&gt;
&lt;th&gt;Low Level Tool&lt;/th&gt;
&lt;th&gt;High Level Tool&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Debian&lt;/td&gt;
&lt;td&gt;.deb&lt;/td&gt;
&lt;td&gt;dpkg&lt;/td&gt;
&lt;td&gt;apt,apt-cache,apt-get,aptitude&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ubuntu&lt;/td&gt;
&lt;td&gt;.deb&lt;/td&gt;
&lt;td&gt;dpkg&lt;/td&gt;
&lt;td&gt;apt,apt-cache,apt-get&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CentOS&lt;/td&gt;
&lt;td&gt;.rpm&lt;/td&gt;
&lt;td&gt;rpm&lt;/td&gt;
&lt;td&gt;yum&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fedora&lt;/td&gt;
&lt;td&gt;.rpm&lt;/td&gt;
&lt;td&gt;rpm&lt;/td&gt;
&lt;td&gt;dnf&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;dpkg&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;dpkg is a low-level tool to install, build, remove and manage Debian packages.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;apt&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;apt provides a high-level command-line interface for the package management system. &lt;/p&gt;

&lt;p&gt;It is intended as an end-user interface and enables some options better suited for interactive usage.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;aptitude&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;aptitude is a text-based interface package management system. &lt;/p&gt;

&lt;p&gt;It allows the user to view the list of packages and to perform package management tasks such as installing, upgrading, and removing packages.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;rpm&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;RPM is a package management system used to build, install, verify, update, and uninstall software.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;yum&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;yum is the high level tool for getting, installing, deleting, querying, and managing Red Hat Enterprise Linux RPM software packages.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;dnf&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;DNF or Dandified YUM is the next-generation version of the Yellowdog Updater, Modified. It is used for getting, installing, deleting, querying, and managing Red Hat Enterprise Linux RPM software packages.&lt;/p&gt;

&lt;p&gt;Let's have a look at how do we use these tools:&lt;/p&gt;

&lt;h3&gt;
  
  
  Debian and Ubuntu
&lt;/h3&gt;

&lt;h4&gt;
  
  
  dkg
&lt;/h4&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Task&lt;/th&gt;
&lt;th&gt;Command&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Install a .deb file&lt;/td&gt;
&lt;td&gt;dpkg -i package-file-name.deb&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Remove a .deb file&lt;/td&gt;
&lt;td&gt;dpkg -r package-file-name.deb&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;List installed Packages&lt;/td&gt;
&lt;td&gt;dpkg --list search-pattern&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Check if a package is installed&lt;/td&gt;
&lt;td&gt;dpkg -s package-name&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;location of installed file&lt;/td&gt;
&lt;td&gt;dpkg -L package-name&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h4&gt;
  
  
  apt
&lt;/h4&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Task&lt;/th&gt;
&lt;th&gt;Command&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Installs the package(s) with dependency&lt;/td&gt;
&lt;td&gt;apt-get install package-name(s)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Remove the package(s) but not dependency&lt;/td&gt;
&lt;td&gt;apt-get remove package-name(s)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Remove unused dependency&lt;/td&gt;
&lt;td&gt;apt-get autoremove&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Update Package List&lt;/td&gt;
&lt;td&gt;apt-get update&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;search Packages&lt;/td&gt;
&lt;td&gt;apt search search_string&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;get info about a package&lt;/td&gt;
&lt;td&gt;apt show package&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  CentOS and Fedora
&lt;/h3&gt;

&lt;h4&gt;
  
  
  rpm
&lt;/h4&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Task&lt;/th&gt;
&lt;th&gt;Command&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Install a .rpm file&lt;/td&gt;
&lt;td&gt;rpm -i package-file-name.rpm&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Remove a .npm file&lt;/td&gt;
&lt;td&gt;rpm --erase package-name(s)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Listing Installed Files&lt;/td&gt;
&lt;td&gt;rpm -qa&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Check if a given package is installed&lt;/td&gt;
&lt;td&gt;rpm --query package-name(s)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h4&gt;
  
  
  yum
&lt;/h4&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Task&lt;/th&gt;
&lt;th&gt;Command&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Installs the package(s) with dependency&lt;/td&gt;
&lt;td&gt;yum install package-name(s)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Remove the package(s) but not dependency&lt;/td&gt;
&lt;td&gt;yum erase package-name(s)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Update Package List&lt;/td&gt;
&lt;td&gt;yum update&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;search Packages&lt;/td&gt;
&lt;td&gt;yum search search-pattern&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;get info about a package&lt;/td&gt;
&lt;td&gt;yum info package-name(s)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h4&gt;
  
  
  dnf
&lt;/h4&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Task&lt;/th&gt;
&lt;th&gt;Command&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Installs the package(s) with dependency&lt;/td&gt;
&lt;td&gt;dnf install package-name(s)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Remove the package(s) but not dependency&lt;/td&gt;
&lt;td&gt;dnf remove package-name(s)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Update Package List&lt;/td&gt;
&lt;td&gt;dnf upgrade&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;search Packages&lt;/td&gt;
&lt;td&gt;dnf search search-pattern&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;get info about a package&lt;/td&gt;
&lt;td&gt;dnf info package-name(s)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;These are the most common functionalities that these Package Managers Provide.&lt;/p&gt;

&lt;p&gt;Hope this will help you better understand the Package management in Linux.&lt;/p&gt;

&lt;p&gt;See you in funny papers 🚀&lt;/p&gt;

</description>
      <category>ubuntu</category>
      <category>linux</category>
      <category>beginners</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Changing Permissions in Linux System</title>
      <dc:creator>yash sugandh</dc:creator>
      <pubDate>Sun, 30 Aug 2020 12:30:21 +0000</pubDate>
      <link>https://dev.to/yashsugandh/changing-permissions-in-linux-system-4a2b</link>
      <guid>https://dev.to/yashsugandh/changing-permissions-in-linux-system-4a2b</guid>
      <description>&lt;p&gt;In the last post, we explored &lt;a href="https://dev.to/yashsugandh/permissions-in-linux-system-gj"&gt;Permissions in Linux System&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;If you have not read the previous post I would strongly recommend you read the last post first.&lt;/p&gt;

&lt;p&gt;Now, let's move onto Changing Permissions in Linux System.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. &lt;code&gt;chmod&lt;/code&gt; Command&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;chmod&lt;/code&gt; stands for change file mode is used to change permissions of a file/directory.&lt;/p&gt;

&lt;p&gt;Can anyone change the permissions of a file/directory?&lt;/p&gt;

&lt;p&gt;No, only the owner or the root user(superuser) can change the file/directory permissions.&lt;/p&gt;

&lt;p&gt;There are two way's in which we can change the mode(Permissions)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Absolute mode&lt;/li&gt;
&lt;li&gt;Symbolic mode&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Absolute mode:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Absolute mode, we use the Octal Number to represents Permissions for Owner, Group, and Others.&lt;/p&gt;

&lt;p&gt;Okay, but what do you mean by Octal Number?&lt;/p&gt;

&lt;p&gt;Octal is a number system that is used to represent numbers on a computer.&lt;/p&gt;

&lt;p&gt;In Octal, counting is done with numbers from 0 to 7 &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Octal Number&lt;/th&gt;
&lt;th&gt;Type of Permission&lt;/th&gt;
&lt;th&gt;Symbol&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;No Permission&lt;/td&gt;
&lt;td&gt;---&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Execute Only&lt;/td&gt;
&lt;td&gt;--x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Write Only&lt;/td&gt;
&lt;td&gt;-w-&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Write and Execute&lt;/td&gt;
&lt;td&gt;-wx&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Read Only&lt;/td&gt;
&lt;td&gt;r--&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;Read and Execute&lt;/td&gt;
&lt;td&gt;r-x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;Read and Write&lt;/td&gt;
&lt;td&gt;rw-&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;Read, Write and Execute&lt;/td&gt;
&lt;td&gt;rwx&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Let's look at the syntax for &lt;code&gt;chmod&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fj6uwb9ri9s3rnxaln499.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fj6uwb9ri9s3rnxaln499.png" alt="chmod syntax"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To change the mode of a file/directory we just use &lt;code&gt;chmod&lt;/code&gt; along with one of the modes from the above table and files/directories.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fne5d52gm27woln24anph.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fne5d52gm27woln24anph.png" alt="chmod example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;ls -l hello.txt&lt;/code&gt; to check the current permissions for owner, group, and others and found that the owner had read and write (rw-) permission, group also had read and write (rw-) permission and other had read-only permission (r--)&lt;/p&gt;

&lt;p&gt;What if to make it more secure we wanted to change the permission for the group to read-only and keep the rest as is&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdehbhwm7u0w34sxzoon5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdehbhwm7u0w34sxzoon5.png" alt="chmod-update-permission"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;we used the command &lt;code&gt;chmod 644 hello.txt&lt;/code&gt; where&lt;/p&gt;

&lt;p&gt;&lt;code&gt;chmod&lt;/code&gt; represents the command change mode&lt;br&gt;
&lt;code&gt;644&lt;/code&gt; represents read-write, read-only and read-only permissions &lt;br&gt;
&lt;code&gt;hello.txt&lt;/code&gt; represents name of the file we want to change permissions for.&lt;/p&gt;

&lt;p&gt;Now that we know how to use &lt;code&gt;chmod&lt;/code&gt; command let's look at what are some most used modes&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Mode&lt;/th&gt;
&lt;th&gt;File Attributes&lt;/th&gt;
&lt;th&gt;Meaning of mode&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;777&lt;/td&gt;
&lt;td&gt;rwxrwxrwx&lt;/td&gt;
&lt;td&gt;No restrictions on permissions. Generally, not a good setting as anyone can change and execute your file&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;700&lt;/td&gt;
&lt;td&gt;rwx------&lt;/td&gt;
&lt;td&gt;The owner has full access. Nobody else has any rights. This setting is useful if we want to keep our files and directories private. Similarly we can use 600 for non-executable file&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;644&lt;/td&gt;
&lt;td&gt;rw-r--r--&lt;/td&gt;
&lt;td&gt;The owner may read and write a file, while all others may only read the file. This  setting is useful if the owner should be the only one to change the file&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;666&lt;/td&gt;
&lt;td&gt;rw-rw-rw-&lt;/td&gt;
&lt;td&gt;All users may read and write the file. This setting is useful if you have some common file&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Now, let's move on the 2nd way we can change mode &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Symbolic Notation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Symbolic notation is divided into three parts&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Who the change will affect&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Unlike Absolute mode in Symbolic notation, we can actually specify who should be affected by the change&lt;/p&gt;

&lt;p&gt;&lt;code&gt;u&lt;/code&gt; represents user(Owner)&lt;br&gt;
&lt;code&gt;g&lt;/code&gt; represents group&lt;br&gt;
&lt;code&gt;o&lt;/code&gt; represents others&lt;br&gt;
&lt;code&gt;a&lt;/code&gt; represents all(i.e. &lt;code&gt;u&lt;/code&gt;,&lt;code&gt;g&lt;/code&gt; and &lt;code&gt;o&lt;/code&gt;)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Which operation will be performed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;+&lt;/code&gt; represents an operation to add a permission&lt;br&gt;
&lt;code&gt;-&lt;/code&gt; represents an operation to remove a permission &lt;br&gt;
&lt;code&gt;=&lt;/code&gt;  represents an operation to set permission and replace the previous one&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What permission will be set&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;r&lt;/code&gt; represents read permission&lt;br&gt;
&lt;code&gt;w&lt;/code&gt; represents write permission&lt;br&gt;
&lt;code&gt;x&lt;/code&gt; represents execute permission&lt;/p&gt;

&lt;p&gt;Let's take an example of each operation&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add Permission&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7c2dstiezbnbp9ac7i0l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7c2dstiezbnbp9ac7i0l.png" alt="add-permission"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;chmod u+x hello.txt&lt;/code&gt; to add executable permission to the Owner where&lt;/p&gt;

&lt;p&gt;&lt;code&gt;chmod&lt;/code&gt; represents the command to change mode&lt;br&gt;
&lt;code&gt;u&lt;/code&gt; represents user(Owner)&lt;br&gt;
&lt;code&gt;+&lt;/code&gt; represents the addition of permission&lt;br&gt;
&lt;code&gt;x&lt;/code&gt; represents executable permission&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Remove Permission&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fiiz56g7pho4j0s4ai4e2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fiiz56g7pho4j0s4ai4e2.png" alt="remove permission"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;chmod o-r hello.txt&lt;/code&gt; to remove the read-only permission from Other where&lt;/p&gt;

&lt;p&gt;&lt;code&gt;chmod&lt;/code&gt; represents the command to change mode&lt;br&gt;
&lt;code&gt;o&lt;/code&gt; represents other&lt;br&gt;
&lt;code&gt;-&lt;/code&gt; represents the removal of permission&lt;br&gt;
&lt;code&gt;r&lt;/code&gt; represents read permission&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Assignment of Permission&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3m6b7yv8kmg9ejxt5hfd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3m6b7yv8kmg9ejxt5hfd.png" alt="Assignment Permission"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;chmod g=wr hello.txt&lt;/code&gt; to assign the read and write permission to group where&lt;/p&gt;

&lt;p&gt;&lt;code&gt;chmod&lt;/code&gt; represents the command to change mode&lt;br&gt;
&lt;code&gt;g&lt;/code&gt; represents group&lt;br&gt;
&lt;code&gt;=&lt;/code&gt; represents the assignment of permission&lt;br&gt;
&lt;code&gt;wr&lt;/code&gt; represents read and write permission&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; If we don't specify who will be affected, it is by default taken as &lt;strong&gt;all&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This was all about changing permissions of a file/directory.&lt;/p&gt;

&lt;p&gt;Now, let's look at how to change ownership and group of a file/directory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;chown&lt;/code&gt; Command&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;chown&lt;/code&gt; command is used to change the owner/group of a file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fn3rhdmpopo0s7uxpnmda.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fn3rhdmpopo0s7uxpnmda.png" alt="chown syntax"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's take an example&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fne5d52gm27woln24anph.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fne5d52gm27woln24anph.png" alt="chown example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the example, we can see that the owner and group of the file &lt;strong&gt;hello.txt&lt;/strong&gt; is &lt;strong&gt;"yash"&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's try and change the owner of the file to &lt;strong&gt;"root"&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Since we are changing the ownership of the file this operation will require the root access&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwn04vpsg0bstvgtuu8od.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwn04vpsg0bstvgtuu8od.png" alt="chown-owner"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we first tried changing the ownership without using root privileges and it gave us an error.&lt;/p&gt;

&lt;p&gt;Then we used the command &lt;code&gt;sudo chown root hello.txt&lt;/code&gt; command where&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo&lt;/code&gt; for executing as a superuser&lt;br&gt;
&lt;code&gt;chown&lt;/code&gt; represents the change ownership command &lt;br&gt;
&lt;code&gt;root&lt;/code&gt; represents the new owner&lt;br&gt;
&lt;code&gt;hello.txt&lt;/code&gt; represents the file to be affected&lt;/p&gt;

&lt;p&gt;Let's take another example where we change our owner back to "yash" but change our group to "root"&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffm7nd9hutr0jt2vyi728.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffm7nd9hutr0jt2vyi728.png" alt="chown-grp"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;sudo chown yash:root  hello.txt&lt;/code&gt; where&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo&lt;/code&gt; for executing as a superuser&lt;br&gt;
&lt;code&gt;chown&lt;/code&gt; represents the change ownership command &lt;br&gt;
&lt;code&gt;yash&lt;/code&gt; represents the new owner&lt;br&gt;
&lt;code&gt;root&lt;/code&gt; represents the new group&lt;br&gt;
&lt;code&gt;hello.txt&lt;/code&gt; represents the file to be affected&lt;/p&gt;

&lt;p&gt;What if we just wanted to change our group?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;chgrp&lt;/code&gt; Command&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To change the group using &lt;code&gt;chgrp&lt;/code&gt; command we just use &lt;code&gt;chgrp newGroupName fileName&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let's take an example, we recently changed the group of our file "hello.txt" from "yash" to "root".&lt;/p&gt;

&lt;p&gt;Now, let's change it  back&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flhvikh7d252epui1k6bw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flhvikh7d252epui1k6bw.png" alt="chgrp-example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;chgrp yash hello.txt&lt;/code&gt; where&lt;/p&gt;

&lt;p&gt;&lt;code&gt;chgrp&lt;/code&gt; represents the change group command &lt;br&gt;
&lt;code&gt;yash&lt;/code&gt; represents the new group&lt;br&gt;
&lt;code&gt;hello.txt&lt;/code&gt; represents the file to be affected&lt;/p&gt;

&lt;p&gt;So, this was all about Changing Permissions in Linux System. Hope you understood.&lt;/p&gt;

&lt;p&gt;Please let me know if there are any queries and suggestions.&lt;/p&gt;

&lt;p&gt;See you in the funny papers 🚀&lt;/p&gt;

</description>
      <category>linux</category>
      <category>ubuntu</category>
      <category>computerscience</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Permissions in Linux System</title>
      <dc:creator>yash sugandh</dc:creator>
      <pubDate>Sat, 22 Aug 2020 11:07:39 +0000</pubDate>
      <link>https://dev.to/yashsugandh/permissions-in-linux-system-gj</link>
      <guid>https://dev.to/yashsugandh/permissions-in-linux-system-gj</guid>
      <description>&lt;p&gt;In the last post, we explored &lt;a href="https://dev.to/yashsugandh/controlling-processes-in-linux-system-4974"&gt;Controlling Processes in Linux System&lt;/a&gt; where we explored how to control processes running in our System.&lt;/p&gt;

&lt;p&gt;Today we are going to explore one of the most important topics in the Linux System: &lt;strong&gt;Permissions&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;What are permissions and why should we know about them?&lt;/p&gt;

&lt;p&gt;Our Linux Systems are built on top of the Unix system, so they are not just multitasking but also a multi-user system.&lt;/p&gt;

&lt;p&gt;This is not something that has been recently introduced but has been deeply embedded in the Linux systems from the early days.&lt;/p&gt;

&lt;p&gt;What does a multi-user system mean?&lt;/p&gt;

&lt;p&gt;In a multi-user system, more than one person can be using the computer at the same time.&lt;/p&gt;

&lt;p&gt;For example, if you are connected to the internet, the remote users can access your system using &lt;strong&gt;Secure Shell(ssh)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Okay, this feature provide users with great power to login from anywhere.&lt;/p&gt;

&lt;p&gt;To quote a great man:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;With Great POWER comes Great Responsibility&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Sorry just could not miss the opportunity to say this 😁&lt;/p&gt;

&lt;p&gt;Okay let's get back to the topic since multiple users can log in into the same system along with a malicious user, how can you make sure that your files are secure?&lt;/p&gt;

&lt;p&gt;This is where Permissions comes into the picture.&lt;/p&gt;

&lt;p&gt;Permissions are divided into two major parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Ownership&lt;/li&gt;
&lt;li&gt;Access Rights&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;1. Ownership&lt;/strong&gt;&lt;br&gt;
Files and directories in the Linux system is assigned 3 types of owner&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Owner (the user who created the file/directory)&lt;/li&gt;
&lt;li&gt;Group (to which the owner belongs to)&lt;/li&gt;
&lt;li&gt;Others (all other users)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Access Rights&lt;/strong&gt;&lt;br&gt;
Files and directories in the Linux system have 3 access rights defined for all the 3 owners discussed above.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Read access denoted by &lt;strong&gt;r&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Write access denoted by &lt;strong&gt;w&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Execution access denoted by &lt;strong&gt;x&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Okay so we have just found the answer what are Permissions and why should we know about them.&lt;/p&gt;

&lt;p&gt;Now, the next question that comes into our mind is how can we check Permissions for a file or directory?&lt;/p&gt;

&lt;p&gt;There are two easy ways we can check for Permissions on a file/directory&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Using the UI&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's start with the human-readable way&lt;/p&gt;

&lt;p&gt;To check Permissions for a file/directory all we need to do is &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Right-click on a file&lt;/li&gt;
&lt;li&gt;Select properties from the menu&lt;/li&gt;
&lt;li&gt;There we have 4 tabs Basic, Permissions Open with and Image(Type of file)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4ir9hmdngcgy107t44or.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4ir9hmdngcgy107t44or.png" alt="properties-UI"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Select the Permissions tab&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After selecting Permissions we see the following data&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkxjgzpone8hbfi687ikr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkxjgzpone8hbfi687ikr.png" alt="Properties-Permissions"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above image, &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Owner has a value of &lt;strong&gt;me&lt;/strong&gt; since I was the one who created the file&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Access provided for &lt;strong&gt;Owner&lt;/strong&gt; is read and write&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Group has a value of &lt;strong&gt;yash&lt;/strong&gt; since that is my username&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Access for the group is also read and write&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Others have read-only access.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To change Permissions, the user can open the drop-down menu for each category and choose the desired permission.&lt;/p&gt;

&lt;p&gt;Aren't we missing something? Where is the execution access?&lt;/p&gt;

&lt;p&gt;If we look below the Access of others we can see that there is another field name Execute.&lt;/p&gt;

&lt;p&gt;Execute allows us to check whether this file is executable or not and since in the above image checkbox is not ticked we can say that the file is not executable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Using our Terminal&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To find Permissions for files all we need to do is use &lt;code&gt;ls&lt;/code&gt; command along with &lt;code&gt;-l&lt;/code&gt; option.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fuz8rabuyqh72qljsnort.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fuz8rabuyqh72qljsnort.png" alt="terminal-all-permissions"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;ls -l&lt;/code&gt; to list all the files and directories along with Permissions.&lt;/p&gt;

&lt;p&gt;But to understand how to read Permissions better let's take an example of a single file&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ff0q9hamkzr6ghl3ntt3b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ff0q9hamkzr6ghl3ntt3b.png" alt="file-permissions"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;ls -l I_O\ Redirection\ In\ Linux\ System.png&lt;/code&gt; to find the permissions for the respective files &lt;/p&gt;

&lt;p&gt;In the Output &lt;strong&gt;-rw-rw-r-- 1 yash yash   75880&lt;/strong&gt; the first 10 letters are file attributes&lt;/p&gt;

&lt;p&gt;The first letter describes the type of file, most common file types are &lt;/p&gt;

&lt;p&gt;&lt;code&gt;-&lt;/code&gt; specifies a regular file&lt;br&gt;
&lt;code&gt;d&lt;/code&gt; specifies a directory&lt;br&gt;
&lt;code&gt;l&lt;/code&gt; describes a symbolic link&lt;/p&gt;

&lt;p&gt;After the type of file, we have the rest of 9 characters to describe access permissions i.e. rwx( read, write and execute). &lt;/p&gt;

&lt;p&gt;In the above example, we have &lt;/p&gt;

&lt;p&gt;rw - Read and Write for Owner &lt;br&gt;
rw - Read and Write for Group&lt;br&gt;
r  - Read Only for all the Others&lt;/p&gt;

&lt;p&gt;Similarly, we can have many different combinations of Permissions. Let's look at a few&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;File Attributes&lt;/th&gt;
&lt;th&gt;The Meaning of Attributes&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;-rw-rw-r--&lt;/td&gt;
&lt;td&gt;A regular file that is readable and writable for Owner and Group but only readable for others&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;drw-r-r--&lt;/td&gt;
&lt;td&gt;A directory that is readable and writable only for Owner and for Group and Others it's only readable&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;-rwx------&lt;/td&gt;
&lt;td&gt;A regular file that is readable, writable, and executable by the Owner. No one else has any access.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;-rwxr-xr-x&lt;/td&gt;
&lt;td&gt;A regular file that is readable, writable, and executable by the Owner. The file can only be read and executed by everybody else.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;So, this is how we can read Permissions for a file/directory.&lt;/p&gt;

&lt;p&gt;This is it for this post. In the next post, we will explore how to add/edit these permissions from Terminal.&lt;/p&gt;

&lt;p&gt;See you in the funny papers 🚀&lt;/p&gt;

</description>
      <category>linux</category>
      <category>ubuntu</category>
      <category>computerscience</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Controlling Processes in Linux System</title>
      <dc:creator>yash sugandh</dc:creator>
      <pubDate>Sun, 16 Aug 2020 05:41:03 +0000</pubDate>
      <link>https://dev.to/yashsugandh/controlling-processes-in-linux-system-4974</link>
      <guid>https://dev.to/yashsugandh/controlling-processes-in-linux-system-4974</guid>
      <description>&lt;p&gt;In the last post, we explored &lt;a href="https://dev.to/yashsugandh/all-about-processes-and-how-can-we-view-them-1n2i"&gt;Processes&lt;/a&gt;. what are processes and how can we monitor them?&lt;/p&gt;

&lt;p&gt;In this post, we will explore how can we control these processes?&lt;/p&gt;

&lt;p&gt;We learned in the last post that there are two types of processes :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Foreground Processes&lt;br&gt;
The foreground processes are those which can be seen on UI and require some sort of user input.&lt;br&gt;
For example, a text editor.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Background Processes&lt;br&gt;
The background processes are those which do not require any form of input from the user and run automatically in the background.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's take an example, that we want to run a Spring Boot Gradle application using the command &lt;code&gt;gradle bootrun&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--G7X0-CRw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/q5lw76yhb2zzyr30le8g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--G7X0-CRw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/q5lw76yhb2zzyr30le8g.png" alt="spring-boot-run" width="880" height="538"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now the process has started but we cannot access the terminal.&lt;/p&gt;

&lt;p&gt;What if we want to move the process in the background?&lt;/p&gt;

&lt;p&gt;To move the process in the background all we have to do is press &lt;code&gt;Ctrl + Z&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iJ7-8QJd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vealrc67djprnx04jobb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iJ7-8QJd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vealrc67djprnx04jobb.png" alt="terminal-background-move" width="880" height="170"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we can see that the process is now moved to the background and it returns the process id (PID) along with status of suspended.&lt;/p&gt;

&lt;p&gt;But what is the meaning of &lt;strong&gt;suspended&lt;/strong&gt; here?&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;suspend&lt;/strong&gt; state specifies that our process is not terminated but is present in a suspended state.&lt;/p&gt;

&lt;p&gt;Okay, so when we use the &lt;code&gt;Ctrl + Z&lt;/code&gt; the process is temporarily stopped.&lt;/p&gt;

&lt;p&gt;What if we wanted to bring the process to the foreground again?&lt;/p&gt;

&lt;p&gt;To bring the process to &lt;strong&gt;foreground&lt;/strong&gt; we use the command &lt;code&gt;fg&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VqaEXIRC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9zobb0nmk36t35ra6ouz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VqaEXIRC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9zobb0nmk36t35ra6ouz.png" alt="foreground-process" width="880" height="308"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we moved our process to the foreground again using the command &lt;code&gt;fg&lt;/code&gt; and it returned us the status &lt;strong&gt;continued&lt;/strong&gt; with PID.&lt;/p&gt;

&lt;p&gt;Now, what if we want to terminate the process?&lt;/p&gt;

&lt;p&gt;To terminate a process we press &lt;code&gt;Ctrl + C&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NugtZjNA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ffydvcappq8pjt634lb2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NugtZjNA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ffydvcappq8pjt634lb2.png" alt="terminate-process" width="718" height="198"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we pressed &lt;code&gt;Ctrl + C&lt;/code&gt; to terminate the running process.&lt;/p&gt;

&lt;p&gt;Till now we have seen how to run a process in the foreground, move the process to background, and bring the process to the foreground again.&lt;/p&gt;

&lt;p&gt;What if we wanted to start the process in the background?&lt;/p&gt;

&lt;p&gt;To run a process in the background all we need to do is write &lt;code&gt;&amp;amp;&lt;/code&gt; at the end of our command&lt;/p&gt;

&lt;p&gt;Let's take an example that we want to run &lt;code&gt;gedit&lt;/code&gt; in the background&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--W1u-SbQy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/r11irt0rlsogc72szmnv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--W1u-SbQy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/r11irt0rlsogc72szmnv.png" alt="Alt Text" width="510" height="246"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;gedit &amp;amp;&lt;/code&gt; and ran the process in the background and we got a process id(PID) in return.&lt;/p&gt;

&lt;p&gt;So the process is running in the background but how can we confirm it?&lt;/p&gt;

&lt;p&gt;To check all the processes in our current terminal we use the command &lt;code&gt;jobs&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ycf9AORj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/p920s01l1ixqlf0uo3im.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ycf9AORj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/p920s01l1ixqlf0uo3im.png" alt="gedit-jobs" width="646" height="320"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we can see that a job is running in the background named &lt;code&gt;gedit&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, we have already seen how to bring a process to the foreground but what if there are multiple processes?&lt;/p&gt;

&lt;p&gt;To bring a specific process to the foreground we use the following syntax&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fg #{process_number}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's take an example and bring our &lt;code&gt;gedit&lt;/code&gt; job to the foreground&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BKDK_6he--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2w47bajwyau6pwlhfezd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BKDK_6he--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2w47bajwyau6pwlhfezd.png" alt="gedit-foreground" width="780" height="358"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;fg #1&lt;/code&gt; and brought the &lt;code&gt;gedit&lt;/code&gt; process to the foreground again.&lt;/p&gt;

&lt;p&gt;Now, let's have a look at how to control processes using signals&lt;/p&gt;

&lt;p&gt;We use the kill command to send signals to programs.&lt;/p&gt;

&lt;p&gt;There are many different signals we can send using the kill command and we can list these signals using the command &lt;code&gt;kill -l&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZAgM9GHx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rwvc9eyiwjs0pg7xrd6i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZAgM9GHx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rwvc9eyiwjs0pg7xrd6i.png" alt="kill-list" width="760" height="347"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let us explore a few of the most used signals&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Signal&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;SIGHUP&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Hangup detected on controlling terminal or death of controlling the process&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SIGINT&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Interrupt from keyboard It is the action performed when we press Ctrl + C&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SIGQUIT&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Quit from keyboard&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SIGKILL&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;Immidiately kill the process without any time for cleanup&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SIGTERM&lt;/td&gt;
&lt;td&gt;15&lt;/td&gt;
&lt;td&gt;It is used to Terminate a process. It is the default action with KILL command&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SIGCONT&lt;/td&gt;
&lt;td&gt;19,18,25&lt;/td&gt;
&lt;td&gt;Used to Continue the process if stopped&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SIGSTOP&lt;/td&gt;
&lt;td&gt;17,19,23&lt;/td&gt;
&lt;td&gt;Used to Stop a process&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.&lt;/p&gt;

&lt;p&gt;To kill any process using kill command we need the PID of the respective process.&lt;br&gt;
We can either use the name of the signal or the value of a signal along with PID to kill any process.&lt;/p&gt;

&lt;p&gt;Let's take an example, that we want to kill the screenshot application &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9R_E4Dc9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/aui1n3bf901el05aa85w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9R_E4Dc9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/aui1n3bf901el05aa85w.png" alt="kill-screenshot" width="880" height="205"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we first searched for the PID using the command we learned in the last post &lt;code&gt;ps -ef | grep screenshot&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;We used the &lt;code&gt;kill&lt;/code&gt; command along with signal value as well as with signal name to &lt;strong&gt;kill&lt;/strong&gt; the screenshot application.&lt;/p&gt;

&lt;p&gt;What if we wanted to kill the process with name and not PID?&lt;/p&gt;

&lt;p&gt;To kill a process with a name we use the &lt;code&gt;pkill {regular_expression}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let's take an example, we used the command &lt;code&gt;pkill fire&lt;/code&gt;. This command, when executed will kill every process starting with &lt;strong&gt;fire&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;What if we want to kill all the instances of a running application? Is there any way we can do it in 1 go?&lt;/p&gt;

&lt;p&gt;We can kill multiple instances of an application using the &lt;code&gt;killall&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;The main difference between &lt;code&gt;killall&lt;/code&gt; command and &lt;code&gt;pkill&lt;/code&gt; command is that we need to provide exact value to &lt;code&gt;killall&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZGdvjOd7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/lrjpyu62td3bm35dho0s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZGdvjOd7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/lrjpyu62td3bm35dho0s.png" alt="pkill-killall" width="727" height="272"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the commands &lt;code&gt;pkill&lt;/code&gt; and &lt;code&gt;killall&lt;/code&gt; to kill &lt;strong&gt;gedit&lt;/strong&gt;. While executing we observed that when we give &lt;code&gt;killall&lt;/code&gt; command an incomplete name the &lt;code&gt;killall&lt;/code&gt; was unable to find the processes.&lt;/p&gt;

&lt;p&gt;This was all about how we can control the processes.&lt;/p&gt;

&lt;p&gt;Please let me know if you have any suggestions or queries in the discussion below. See you in the funny papers.&lt;/p&gt;

</description>
      <category>ubuntu</category>
      <category>linux</category>
      <category>computerscience</category>
      <category>beginners</category>
    </item>
    <item>
      <title>All about processes and how can we view them </title>
      <dc:creator>yash sugandh</dc:creator>
      <pubDate>Sat, 08 Aug 2020 12:22:03 +0000</pubDate>
      <link>https://dev.to/yashsugandh/all-about-processes-and-how-can-we-view-them-1n2i</link>
      <guid>https://dev.to/yashsugandh/all-about-processes-and-how-can-we-view-them-1n2i</guid>
      <description>&lt;p&gt;Linux is a multitasking operating system, which means that it creates an illusion that multiple programs are running at the same time by rapidly switching from one program to another.&lt;/p&gt;

&lt;p&gt;The Linux kernel manages this through the use of processes.&lt;/p&gt;

&lt;p&gt;Each process has the illusion that it is the only process on the computer. The tasks share common processing resources (like CPU and memory).&lt;/p&gt;

&lt;p&gt;What exactly is a process?&lt;/p&gt;

&lt;p&gt;An instance of a program is called a Process. A process can be simply called as a program in execution.&lt;/p&gt;

&lt;p&gt;Every time we run a shell command, a program is run and a process is created for it.&lt;/p&gt;

&lt;p&gt;Each process in Linux is assigned an ID called process id (PID).&lt;/p&gt;

&lt;p&gt;There are two types of processes : &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Foreground Processes&lt;br&gt;
The foreground processes are those which can be seen on UI and require some sort of user input.&lt;br&gt;
For example, a text editor.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Background Processes&lt;br&gt;
The background processes are those which do not require any form of input from the user and run automatically in the background.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example, an antivirus.&lt;/p&gt;

&lt;p&gt;There are different state of a process:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Running - The state where a process is either in running or ready to run(waiting for CPU time).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Interruptible - A blocked state of a process that waits for an event or a signal from another process.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Uninterruptible - The process is forced to halt for certain conditions that a hardware status waits and a signal could not be handled.&lt;br&gt;
It is also known as a blocked state.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Stopped - Once the process is completed, this state occurs. This process can be restarted.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Zombie - In this state, the process will be terminated and the information will still be available in the process table. We get a Zombie process when a parent process dies before child.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The first process that starts when a Linux system boots up is the &lt;strong&gt;init process&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The kernel looks for &lt;strong&gt;init&lt;/strong&gt; in &lt;code&gt;/etc&lt;/code&gt;. If the kernel can't find &lt;strong&gt;init&lt;/strong&gt;, it tries to run &lt;code&gt;/bin/sh&lt;/code&gt;, and if that also fails, the startup of the system fails.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;PID&lt;/strong&gt; we find out about above is assigned to a process when it is created and since the init is the first process after the Linux system boots up the PID of init is &lt;strong&gt;1&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Till now we have seen what process is and how it works. Now let's look at how to view the processes that are running in our system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. &lt;code&gt;pidof&lt;/code&gt; command&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;pidof&lt;/code&gt; command is used to find the process id's of a running application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cItkrNRm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/dpgskj9w8gac8otmixgk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cItkrNRm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/dpgskj9w8gac8otmixgk.png" alt="pidof-syntax"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To get the PID of a process we just use &lt;code&gt;pidof&lt;/code&gt; along with the application name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4DnC4saw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/yh7kwil17nu2e9tzzc0p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4DnC4saw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/yh7kwil17nu2e9tzzc0p.png" alt="pidof-example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;pidof init&lt;/code&gt; which we know should return &lt;strong&gt;1&lt;/strong&gt; and it did.&lt;/p&gt;

&lt;p&gt;We also tried &lt;code&gt;pidof java&lt;/code&gt; which returned multiple processes running for java.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. &lt;code&gt;ps&lt;/code&gt; command&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;ps&lt;/code&gt; command returns the snapshot of the current processes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vLjFCWUw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/f56fixapephsdtqgc089.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vLjFCWUw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/f56fixapephsdtqgc089.png" alt="ps-basic"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, the &lt;code&gt;ps&lt;/code&gt; command by default shows us all the processes that are associated with the current terminal.&lt;/p&gt;

&lt;p&gt;TTY is short for “teletype,” and refers to the controlling terminal for the process. &lt;/p&gt;

&lt;p&gt;Unix is showing its age here. The TIME field is the amount of CPU time consumed by the process.&lt;/p&gt;

&lt;p&gt;To get the list of all the processes running we use the &lt;code&gt;ps&lt;/code&gt; command along with two options &lt;code&gt;e&lt;/code&gt; which specifies all processes and &lt;code&gt;f&lt;/code&gt; which specifies full description.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UYIl_ubn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7bsqrtklcgokecoflbt6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UYIl_ubn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7bsqrtklcgokecoflbt6.png" alt="ps-ef"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;ps -ef&lt;/code&gt; to get the details of all the processes running.&lt;/p&gt;

&lt;p&gt;What if we wanted to find a process id of a specific process?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;find the PID of firefox application&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--40gw6Kzt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9xzgpvoxn7744hqgrplk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--40gw6Kzt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9xzgpvoxn7744hqgrplk.png" alt="ps-ef grep"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;ps -ef | grep firefox&lt;/code&gt; to get processes running for firefox so that we can get the PID of firefox.&lt;/p&gt;

&lt;p&gt;But, what if I tell that there is a way through which we won't need to write such long command?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. &lt;code&gt;pgrep&lt;/code&gt; command&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;pgrep&lt;/code&gt; command is used to get the process id of an application.&lt;/p&gt;

&lt;p&gt;It is similar to the &lt;code&gt;pidof&lt;/code&gt; command is much more powerful as we do not need to provide the exact name of the application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dG9METLB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ohvt8b66inxtj2951edi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dG9METLB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ohvt8b66inxtj2951edi.png" alt="pgrep-basic"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we tried to find an application that has "idea" in its path.&lt;br&gt;
When we tried it with &lt;code&gt;pidof&lt;/code&gt; we got no response but when we tried the same with &lt;code&gt;pgrep&lt;/code&gt; we got the PID.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. &lt;code&gt;top&lt;/code&gt; command&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This utility tells the user about all the running processes on the Linux machine(It refreshes the data every 3 seconds by default).&lt;/p&gt;

&lt;p&gt;The name top comes from the fact that the top program is used to see the “top” processes on the system.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--p6n_73F6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/axvgtwmdrufw09446rg6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--p6n_73F6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/axvgtwmdrufw09446rg6.png" alt="top-command"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we can see the following&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Field&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;PID&lt;/td&gt;
&lt;td&gt;The process ID of each task&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;User&lt;/td&gt;
&lt;td&gt;The username of task owner&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PR&lt;/td&gt;
&lt;td&gt;Priority  Can be 20(highest) or -20(lowest)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;NI&lt;/td&gt;
&lt;td&gt;The nice value of a task&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;VIRT&lt;/td&gt;
&lt;td&gt;Virtual memory used (kb)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;RES&lt;/td&gt;
&lt;td&gt;Physical memory used (kb)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SHR&lt;/td&gt;
&lt;td&gt;Shared memory used (kb)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;%CPU&lt;/td&gt;
&lt;td&gt;% of CPU time&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;%MEM&lt;/td&gt;
&lt;td&gt;Physical memory used&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;TIME+&lt;/td&gt;
&lt;td&gt;Total CPU time&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Command&lt;/td&gt;
&lt;td&gt;Command Name&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;These were the tools we can use to view the processes. Please let me know if I missed something.&lt;/p&gt;

&lt;p&gt;In the next post, we will discuss various ways to control processes. See you in the funny papers.&lt;/p&gt;

</description>
      <category>ubuntu</category>
      <category>linux</category>
      <category>computerscience</category>
      <category>beginners</category>
    </item>
    <item>
      <title>History Command and How to Use It?</title>
      <dc:creator>yash sugandh</dc:creator>
      <pubDate>Sun, 02 Aug 2020 06:48:17 +0000</pubDate>
      <link>https://dev.to/yashsugandh/history-command-and-how-to-use-it-37k8</link>
      <guid>https://dev.to/yashsugandh/history-command-and-how-to-use-it-37k8</guid>
      <description>&lt;p&gt;In this series, we have looked at so many commands with their different syntaxes and options, and it's very very hard to remember each and every one of them.&lt;/p&gt;

&lt;p&gt;So, today we are going to have a look at a command which allows us to have a look at all the commands that we have used previously and use them again.&lt;/p&gt;

&lt;p&gt;By the title, you would have already guessed what the command is, right?&lt;/p&gt;

&lt;p&gt;It's &lt;code&gt;history&lt;/code&gt; command!!&lt;/p&gt;

&lt;p&gt;The first question we have is, what is the use of &lt;code&gt;history&lt;/code&gt; command?&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;history&lt;/code&gt; command is used to view all the previously executed commands. &lt;/p&gt;

&lt;p&gt;Why do we need history command?&lt;/p&gt;

&lt;p&gt;The main use of history is to find an already executed command and use it again.&lt;/p&gt;

&lt;p&gt;It can also be used for audit purposes or to find out what command was executed at a specific date and time.&lt;/p&gt;

&lt;p&gt;Now, without wasting any more time let's get started &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To get all the commands we have previously executed &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--E5JFSOta--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vtwb1695wamd0q64kd7s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--E5JFSOta--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vtwb1695wamd0q64kd7s.png" alt="history basic"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To use the &lt;code&gt;history&lt;/code&gt; command we simply type history and press ENTER&lt;/p&gt;

&lt;p&gt;As soon as we press ENTER we get the list of all the commands.&lt;/p&gt;

&lt;p&gt;By default, the bash used to store around &lt;strong&gt;500 commands&lt;/strong&gt; in the history but nowaday's the limit has gone up to &lt;strong&gt;1000 commands&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To make the scrolling of commands a little easier we can use the &lt;code&gt;history&lt;/code&gt; command along with the &lt;code&gt;less&lt;/code&gt; command i.e. &lt;code&gt;history | less&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But, what if we wanted to find only the last 10 commands&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;List the last 10 commands that we executed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2myxCGqf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/n8afjxdmsjiy98owdmct.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2myxCGqf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/n8afjxdmsjiy98owdmct.png" alt="history-last-10-updated"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;history&lt;/code&gt; along with the number of commands we wanted to have a look at.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The above syntax i.e. &lt;code&gt;history {number}&lt;/code&gt; will not work with &lt;strong&gt;zsh&lt;/strong&gt; because in &lt;strong&gt;zsh&lt;/strong&gt;, &lt;code&gt;history&lt;/code&gt; is an alias for &lt;code&gt;fc -l 1&lt;/code&gt;, so when you do &lt;code&gt;history 10&lt;/code&gt; it gets replaced by &lt;code&gt;fc -l 1 -10&lt;/code&gt; which just won't work, so instead use &lt;code&gt;fc&lt;/code&gt; directly: &lt;code&gt;fc -l -10&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, what if we want to list them along with the time&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jSB0wTHK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gcwtzkziv3rr9mje6j51.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jSB0wTHK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gcwtzkziv3rr9mje6j51.png" alt="history-with-time"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we first used the command&lt;br&gt;
&lt;code&gt;$ HISTTIMEFORMAT="%d/%m/%y %T "&lt;/code&gt; which is used to set the time format day/month/year timestamp&lt;br&gt;
After the format is set we used the command &lt;code&gt;history&lt;/code&gt; to get the desired results.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; For &lt;strong&gt;zsh&lt;/strong&gt; we just need to use &lt;code&gt;history -i&lt;/code&gt; to get the same results.&lt;/p&gt;

&lt;p&gt;What if we wanted to find the history of all the times we used a specific command?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Xfw_KSWa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jnyzklrddbjy2yp49fkv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Xfw_KSWa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jnyzklrddbjy2yp49fkv.png" alt="history-with-grep"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;history| grep man&lt;/code&gt; to get all the times we have used the &lt;code&gt;man&lt;/code&gt; command&lt;/p&gt;

&lt;p&gt;Till now we have seen different way's to get the commands.&lt;/p&gt;

&lt;p&gt;But what if we wanted to use these commands.&lt;/p&gt;

&lt;p&gt;For starters, what if after looking through history we found out the command we want to use has the x serial number. How will we use it?&lt;/p&gt;

&lt;p&gt;So let's see how we can use the commands from our history &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;use a command on the serial number &lt;strong&gt;1343&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RVXNP10E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8xwijsb36ytohkybpnhm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RVXNP10E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8xwijsb36ytohkybpnhm.png" alt="history-serial"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used &lt;code&gt;!1343&lt;/code&gt; to execute the command at the respective serial number.&lt;/p&gt;

&lt;p&gt;But this can land us into a lot of trouble if we typed the wrong serial number.&lt;/p&gt;

&lt;p&gt;Is there anything we can do to prevent this?&lt;/p&gt;

&lt;p&gt;Let's take an example to see how to prevent this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--f4WsxeHT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/nym99bj389jxrshy7507.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--f4WsxeHT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/nym99bj389jxrshy7507.png" alt="history-serial-check"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we first used the command &lt;code&gt;!1343:p&lt;/code&gt; which returns the command at the number and after verification, we can execute the command.&lt;/p&gt;

&lt;p&gt;There is another feature that history provides known as &lt;strong&gt;History Expansion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;History expansion is a way to &lt;strong&gt;recall&lt;/strong&gt; and &lt;strong&gt;edit commands&lt;/strong&gt; in the history list. The way to recall commands is by the use of &lt;strong&gt;event designators&lt;/strong&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Command&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;!!&lt;/td&gt;
&lt;td&gt;It refers to the last command&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;!-n&lt;/td&gt;
&lt;td&gt;It will execute the current number -n command&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;!string&lt;/td&gt;
&lt;td&gt;It will execute the command referring to the most recent command starting with input string&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;!?string&lt;/td&gt;
&lt;td&gt;It will execute the most recent command that ends with the input string&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;^string1^string2&lt;/td&gt;
&lt;td&gt;Repeat the last command, replacing  string1 with string2&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;We can also use &lt;code&gt;!!&lt;/code&gt; to execute the last command but we would rarely use it since we can easily use the &lt;code&gt;up&lt;/code&gt; key instead.&lt;/p&gt;

&lt;p&gt;Let's take a few example's to understand better&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;find the last &lt;code&gt;cat&lt;/code&gt; command that was executed and run it&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--haOgC3qC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/e34nz7v3zgbwp52lb0os.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--haOgC3qC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/e34nz7v3zgbwp52lb0os.png" alt="history-cat"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;find the last command performed on "Hello.txt" and perform it again&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FiJg1EG7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/c6udyjnkzpdif7gnqyci.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FiJg1EG7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/c6udyjnkzpdif7gnqyci.png" alt="history-hello"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Similarly, we can use other history expansions as well.&lt;/p&gt;

&lt;p&gt;So, this was all about the history keyword and how we can use it.&lt;/p&gt;

&lt;p&gt;Hope you understood it and please let me know if there are questions and suggestions down below.&lt;/p&gt;

&lt;p&gt;See you in the sunny papers :)&lt;/p&gt;

</description>
      <category>ubuntu</category>
      <category>linux</category>
      <category>beginners</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Keyboard Shortcuts For Linux Terminal </title>
      <dc:creator>yash sugandh</dc:creator>
      <pubDate>Sat, 25 Jul 2020 11:55:32 +0000</pubDate>
      <link>https://dev.to/yashsugandh/keyboard-shortcuts-for-linux-terminal-19ml</link>
      <guid>https://dev.to/yashsugandh/keyboard-shortcuts-for-linux-terminal-19ml</guid>
      <description>&lt;p&gt;In the last post, we looked at the use of an Alternative of &lt;code&gt;find&lt;/code&gt; command &lt;a href="https://dev.to/yashsugandh/fd-best-alternative-to-find-command-pko"&gt;fd command&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Today, we are going to have a look at the keyboard shortcuts for Linux Terminal that will make our life easier.&lt;/p&gt;

&lt;p&gt;Let's have a look at some basic and most useful commands&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Keyboard Shortcut&lt;/th&gt;
&lt;th&gt;Use of the keyboard shortcut&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Ctrl-Alt-t&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Open&lt;/strong&gt; a new &lt;strong&gt;Terminal&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ctrl-d&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Close&lt;/strong&gt; a terminal&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ctrl-c&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Cancels&lt;/strong&gt; the currently &lt;strong&gt;running&lt;/strong&gt; command.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ctrl-l&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Clears&lt;/strong&gt; the &lt;strong&gt;screen&lt;/strong&gt; just like clear command&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In the above command, the one that I use the most is the &lt;code&gt;Ctrl-l&lt;/code&gt; since it allows me to clear the screen without the need of typing the &lt;code&gt;clear&lt;/code&gt; command again and again.&lt;/p&gt;

&lt;p&gt;Now, Let's have a look at some advanced commands to help our movement in terminal&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Keyboard Shortcut&lt;/th&gt;
&lt;th&gt;Use of the keyboard shortcut&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Ctrl-a&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Move&lt;/strong&gt; the cursor to the &lt;strong&gt;beginning&lt;/strong&gt; of the &lt;strong&gt;line&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ctrl-e&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Move&lt;/strong&gt; the cursor to the &lt;strong&gt;end&lt;/strong&gt; of the &lt;strong&gt;line&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ctrl-f&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Move&lt;/strong&gt; the cursor &lt;strong&gt;forward&lt;/strong&gt; by &lt;strong&gt;one character&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ctrl-b&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Move&lt;/strong&gt; the cursor &lt;strong&gt;backward&lt;/strong&gt; by &lt;strong&gt;one character&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Alt-f&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Move&lt;/strong&gt; the cursor &lt;strong&gt;forward&lt;/strong&gt; by &lt;strong&gt;one word&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Alt-b&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Move&lt;/strong&gt; the cursor &lt;strong&gt;backward&lt;/strong&gt; by &lt;strong&gt;one word&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;We can use &lt;code&gt;home&lt;/code&gt; and &lt;code&gt;end&lt;/code&gt; key to move at the beginning and end of the line as well.&lt;/p&gt;

&lt;p&gt;The ability to move at the beginning or end of a word using &lt;code&gt;Alt-f  Alt-b&lt;/code&gt; is what I find most useful since we don't have to move back character by character.&lt;/p&gt;

&lt;p&gt;Now let's have a look at some shortcuts that can help us in modifying text&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Keyboard Shortcut&lt;/th&gt;
&lt;th&gt;Use of the keyboard shortcut&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Ctrl-d&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Delete&lt;/strong&gt; the &lt;strong&gt;character&lt;/strong&gt; at the &lt;strong&gt;cursor location&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ctrl-t&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Exchange&lt;/strong&gt; the &lt;strong&gt;character&lt;/strong&gt; at the &lt;strong&gt;current&lt;/strong&gt;  location with the character at &lt;strong&gt;preceding it&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ctrl-u&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Erases&lt;/strong&gt; the &lt;strong&gt;complete line&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Alt-t&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Exchange&lt;/strong&gt; the &lt;strong&gt;word&lt;/strong&gt; at the &lt;strong&gt;current&lt;/strong&gt; location with the word at &lt;strong&gt;preceding it&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Alt-l&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Move&lt;/strong&gt; the cursor &lt;strong&gt;backward&lt;/strong&gt; by &lt;strong&gt;one character&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Alt-u&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Convert&lt;/strong&gt; the &lt;strong&gt;characters&lt;/strong&gt; of a word to &lt;strong&gt;lowercase&lt;/strong&gt; from the &lt;strong&gt;current cursor&lt;/strong&gt; location&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Alt-b&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Convert&lt;/strong&gt; the &lt;strong&gt;characters&lt;/strong&gt; of a word to &lt;strong&gt;uppercase&lt;/strong&gt; from the &lt;strong&gt;current cursor&lt;/strong&gt; location&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;Alt-t&lt;/code&gt; is something that you don't think you need until you start using it.&lt;/p&gt;

&lt;p&gt;So this is all for today. Let me know what's your most used and if I missed something just let me know in the discussion below.&lt;/p&gt;

&lt;p&gt;You can also join me on my journey of Linux-for-beginners series&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/yashsugandh" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F377281%2F1837bec8-09d1-41a0-babe-6165e22fd45a.jpeg" alt="yashsugandh"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/yashsugandh/piping-in-linux-system-4456" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Piping in Linux System&lt;/h2&gt;
      &lt;h3&gt;yash sugandh ・ Jun 27 '20&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#linux&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#ubuntu&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#computerscience&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


</description>
      <category>beginners</category>
      <category>ubuntu</category>
      <category>linux</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Filter Commands in Linux System</title>
      <dc:creator>yash sugandh</dc:creator>
      <pubDate>Sun, 19 Jul 2020 05:50:25 +0000</pubDate>
      <link>https://dev.to/yashsugandh/filter-commands-in-linux-system-1f6i</link>
      <guid>https://dev.to/yashsugandh/filter-commands-in-linux-system-1f6i</guid>
      <description>&lt;p&gt;In the last post, we looked at &lt;strong&gt;viewing and sorting of files&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;There are many times that we want to look at only a specific part of the file and even more times when just want to analyze the file content.&lt;/p&gt;

&lt;p&gt;In this post, we are going to have a look at basic commands to filter the contents of a file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. &lt;code&gt;grep&lt;/code&gt; command&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;grep&lt;/code&gt; command is used to search text or search the given file for lines containing a match to the given strings or words.&lt;/p&gt;

&lt;p&gt;The syntax for &lt;code&gt;grep&lt;/code&gt; command&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CQ81Jhb8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4wwhwf3old55thikrf6f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CQ81Jhb8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4wwhwf3old55thikrf6f.png" alt="grep syntax"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To filter a file using &lt;code&gt;grep&lt;/code&gt; we use &lt;code&gt;grep&lt;/code&gt; along with the options like "case", patterns we want it to match and the source files.&lt;/p&gt;

&lt;p&gt;Let's take an example&lt;/p&gt;

&lt;p&gt;We have the following log file&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NZMo0dLu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/obm13asr3hnbrupm5xy7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NZMo0dLu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/obm13asr3hnbrupm5xy7.png" alt="log-img"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;filter out which user is mentioned in the logs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7R3HyWZ_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/yz8cdjivf52hslal81w0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7R3HyWZ_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/yz8cdjivf52hslal81w0.png" alt="grep-user"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;cat logs.txt | grep user@&lt;/code&gt; where&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cat&lt;/code&gt; represents &lt;code&gt;cat&lt;/code&gt; command to &lt;strong&gt;read the file content&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;logs.txt&lt;/code&gt; represents the &lt;strong&gt;file to read content from&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;grep&lt;/code&gt; represents the &lt;code&gt;grep&lt;/code&gt; command&lt;br&gt;
&lt;code&gt;User@&lt;/code&gt; represents the text we used to &lt;strong&gt;filter&lt;/strong&gt; the logs.txt file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The &lt;code&gt;grep&lt;/code&gt; command by default is case-sensitive&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;get the same output as above using &lt;code&gt;User@&lt;/code&gt; as the pattern&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HNDzA6bB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4cxwge14j9o9ce2fi844.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HNDzA6bB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4cxwge14j9o9ce2fi844.png" alt="grep-i"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we first used the command to check whether we can directly use the &lt;code&gt;User@&lt;/code&gt; but it did not work since&lt;code&gt;grep&lt;/code&gt; is case-sensitive so to fix this we used the command&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cat logs.txt | grep -i User@&lt;/code&gt; where&lt;br&gt;
&lt;code&gt;cat&lt;/code&gt; represents &lt;code&gt;cat&lt;/code&gt; command to &lt;strong&gt;read the file content&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;logs.txt&lt;/code&gt; represents the &lt;strong&gt;file to read content from&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;grep&lt;/code&gt; represents the &lt;code&gt;grep&lt;/code&gt; command&lt;br&gt;
&lt;code&gt;-i&lt;/code&gt; represents the option to &lt;strong&gt;filter case-insensitive&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;user@&lt;/code&gt; represents the &lt;strong&gt;text&lt;/strong&gt; we used to &lt;strong&gt;filter&lt;/strong&gt; the logs.txt file.&lt;/p&gt;

&lt;p&gt;We can also use the &lt;code&gt;grep&lt;/code&gt; command to get the count of a specific word&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;find number of occurrences of the word "exceed"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jMLq9LIR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8j9by9mcdkp5zhstj2ft.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jMLq9LIR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8j9by9mcdkp5zhstj2ft.png" alt="grep-count"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;cat logs.txt | grep -ic exceed&lt;/code&gt; where&lt;br&gt;
&lt;code&gt;cat&lt;/code&gt; represents &lt;code&gt;cat&lt;/code&gt; command to &lt;strong&gt;read the file content&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;logs.txt&lt;/code&gt; represents the file to &lt;strong&gt;read content from&lt;br&gt;
&lt;code&gt;grep&lt;/code&gt; represents the &lt;code&gt;grep&lt;/code&gt; command&lt;br&gt;
&lt;code&gt;-ic&lt;/code&gt; represents the option to count the number of **occurrences&lt;/strong&gt; of the word &lt;strong&gt;irrespective&lt;/strong&gt; of the &lt;strong&gt;case&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;exceed&lt;/code&gt; represents the &lt;strong&gt;text&lt;/strong&gt; we used to &lt;strong&gt;filter&lt;/strong&gt; the logs.txt file.&lt;/p&gt;

&lt;p&gt;What if we need to revert the above case?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;find the count of words that do not match the word "exceed"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MZE2Xozl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/p4n8acn1viy7f19exh50.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MZE2Xozl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/p4n8acn1viy7f19exh50.png" alt="grep-invert"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;cat logs.txt | grep -cv exceed&lt;/code&gt; where&lt;br&gt;
&lt;code&gt;cat&lt;/code&gt; represents &lt;code&gt;cat&lt;/code&gt; command to &lt;strong&gt;read the file content&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;logs.txt&lt;/code&gt; represents the file to &lt;strong&gt;read content from&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;grep&lt;/code&gt; represents the &lt;code&gt;grep&lt;/code&gt; command&lt;br&gt;
&lt;code&gt;-cv&lt;/code&gt; represents the option to &lt;strong&gt;count&lt;/strong&gt; the number of &lt;strong&gt;occurrences&lt;/strong&gt; of the words &lt;strong&gt;not matching&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;exceed&lt;/code&gt; represents the &lt;strong&gt;text&lt;/strong&gt; we used to &lt;strong&gt;filter&lt;/strong&gt; the logs.txt file.&lt;/p&gt;

&lt;p&gt;Let's move on to the next command&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.&lt;code&gt;wc&lt;/code&gt; command&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;wc&lt;/code&gt; stands for &lt;strong&gt;word count&lt;/strong&gt; is used for printing the number of lines, words, and bytes contained in files&lt;/p&gt;

&lt;p&gt;The syntax for &lt;code&gt;wc&lt;/code&gt; command &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8NVLB1jL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/iybeukc5rgbbz4s2xmf1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8NVLB1jL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/iybeukc5rgbbz4s2xmf1.png" alt="wc-syntax"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To use the &lt;code&gt;wc&lt;/code&gt; command we simply use &lt;code&gt;wc&lt;/code&gt; along with options and files&lt;/p&gt;

&lt;p&gt;Let's take an example&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VhiEYX65--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/6owlsktv1taijpruxoki.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VhiEYX65--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/6owlsktv1taijpruxoki.png" alt="wc-basic"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;wc logs.txt&lt;/code&gt; to get the number of lines, words, and bytes.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;get only the number of lines in a file&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IRA1gPgU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wnleouv14b0d0dyni1gt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IRA1gPgU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wnleouv14b0d0dyni1gt.png" alt="wc-no-of-lines"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the option &lt;code&gt;-l&lt;/code&gt; and created a command &lt;code&gt;wc -l logs.txt&lt;/code&gt; to get the number of lines.&lt;/p&gt;

&lt;p&gt;Similarly, we can use &lt;code&gt;-w&lt;/code&gt; for words, &lt;code&gt;-c&lt;/code&gt; for bytes and &lt;code&gt;-m&lt;/code&gt; for characters&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.&lt;code&gt;uniq&lt;/code&gt; command&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;uniq&lt;/code&gt; command is used to report or omit repeated lines.&lt;/p&gt;

&lt;p&gt;By default, &lt;code&gt;uniq&lt;/code&gt; prints its input lines, except that it filters all but the first of adjacent repeated lines so that no output lines are repeated.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;uniq&lt;/code&gt; is mostly used with &lt;code&gt;sort&lt;/code&gt; command. the &lt;code&gt;sort&lt;/code&gt; command provides with the sorted input and &lt;code&gt;uniq&lt;/code&gt; command removes the repeated lines.&lt;/p&gt;

&lt;p&gt;Let's take an example of the following file&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--H8Qx6rNS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/p95thb45mke161v2fh9b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--H8Qx6rNS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/p95thb45mke161v2fh9b.png" alt="random-file"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;use unique to filter file contents&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--r6Qp14Lr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/socvjxpurf0y3yw02ybz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--r6Qp14Lr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/socvjxpurf0y3yw02ybz.png" alt="uniq-count"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we first used the command &lt;code&gt;sort random-words.txt | we&lt;/code&gt; to find the current count and then to get the unique values only we used &lt;code&gt;sort random-words.txt | uniq | wc&lt;/code&gt; where&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sort&lt;/code&gt; represents &lt;strong&gt;&lt;code&gt;sort&lt;/code&gt;&lt;/strong&gt; command&lt;br&gt;
&lt;code&gt;random-words.txt&lt;/code&gt; represents the &lt;strong&gt;input file&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;uniq&lt;/code&gt; represents &lt;strong&gt;&lt;code&gt;uniq&lt;/code&gt;&lt;/strong&gt; command&lt;br&gt;
&lt;code&gt;wc&lt;/code&gt; represents &lt;strong&gt;wordcount&lt;/strong&gt; command&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4.&lt;code&gt;tr&lt;/code&gt; command&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;tr&lt;/code&gt; command is used to translate, squeeze, and/or delete characters.&lt;/p&gt;

&lt;p&gt;The syntax for &lt;code&gt;tr&lt;/code&gt; command&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--H282r2Tt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gvt3cpdx9kppz8wngt95.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--H282r2Tt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gvt3cpdx9kppz8wngt95.png" alt="tr-syntax"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By default, the &lt;code&gt;tr&lt;/code&gt; command will &lt;strong&gt;replace&lt;/strong&gt; each character in &lt;strong&gt;SET1&lt;/strong&gt; with each character in the &lt;strong&gt;same position&lt;/strong&gt; in &lt;strong&gt;SET2&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's take examples for better understanding&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;convert lowercase to uppercase&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2rF8Hznt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ec5osz00q6qois1dncwq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2rF8Hznt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ec5osz00q6qois1dncwq.png" alt="tr-lower-upper"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;tr "[:lower:]" "[:upper:]"&lt;/code&gt; where&lt;br&gt;
&lt;code&gt;tr&lt;/code&gt; represents &lt;code&gt;tr&lt;/code&gt; command&lt;br&gt;
&lt;code&gt;[:lower:]&lt;/code&gt; represents &lt;strong&gt;SET1&lt;/strong&gt; all the &lt;strong&gt;lower-case&lt;/strong&gt; letters[abcd...]&lt;br&gt;
&lt;code&gt;[:upper:]&lt;/code&gt; represents &lt;strong&gt;SET2&lt;/strong&gt; all the &lt;strong&gt;upper-case&lt;/strong&gt; letters[ABCD...]&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;delete a specified character&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JXDtvKrT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gz4g4c7g6i7x51xmrcsb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JXDtvKrT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gz4g4c7g6i7x51xmrcsb.png" alt="tr-delete"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;cat hello.txt | tr -d 'o'&lt;/code&gt; where&lt;br&gt;
&lt;code&gt;cat&lt;/code&gt; represents the &lt;strong&gt;concatenation&lt;/strong&gt; command&lt;br&gt;
&lt;code&gt;hello.txt&lt;/code&gt; represents the &lt;strong&gt;input file&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;tr&lt;/code&gt; represents &lt;code&gt;tr&lt;/code&gt; command&lt;br&gt;
&lt;code&gt;-d&lt;/code&gt; represents the &lt;strong&gt;delete option&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;o&lt;/code&gt; represents &lt;strong&gt;character&lt;/strong&gt; to be &lt;strong&gt;deleted&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;join all the lines into a single line&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--l7wDgb8w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ietj0shz17a1ygx8bhx8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--l7wDgb8w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ietj0shz17a1ygx8bhx8.png" alt="tr-single-line"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;tr -s '\n' ' ' &amp;lt; logs.txt&lt;/code&gt; where&lt;br&gt;
&lt;code&gt;tr&lt;/code&gt; represents &lt;code&gt;tr&lt;/code&gt; command&lt;br&gt;
&lt;code&gt;-s&lt;/code&gt; represents &lt;strong&gt;squeeze repeats&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;\n&lt;/code&gt; represents a &lt;strong&gt;new line&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;' '&lt;/code&gt; represents &lt;strong&gt;single space&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;logs.txt&lt;/code&gt; represents the &lt;strong&gt;input file&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5.&lt;code&gt;head&lt;/code&gt; and &lt;code&gt;tail&lt;/code&gt; command&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the recent past, we already had a look at this command. Let's take a recap.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;head&lt;/code&gt; command is used to print the lines from the start of the file whereas the &lt;code&gt;tail&lt;/code&gt; command is used to print the files from the bottom of the file.&lt;/p&gt;

&lt;p&gt;Let's take an example for better understanding&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;print only the first two line from a file&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Xbh64QYP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7ux4q9vgcbb07r3qceit.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Xbh64QYP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7ux4q9vgcbb07r3qceit.png" alt="head-example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;head -n 2 groceryFinal.txt&lt;/code&gt; where&lt;/p&gt;

&lt;p&gt;&lt;code&gt;head&lt;/code&gt; represents the &lt;strong&gt;head&lt;/strong&gt; command&lt;br&gt;
&lt;code&gt;-n 2&lt;/code&gt; represents the &lt;strong&gt;number of lines&lt;/strong&gt; option&lt;br&gt;
&lt;code&gt;groceryFinal.txt&lt;/code&gt; represents the &lt;strong&gt;file we want to read&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;print only the last two lines from a file&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2cXClvSG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uhd0lyhd5r6az5egnx75.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2cXClvSG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uhd0lyhd5r6az5egnx75.png" alt="tail-example-updated"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;tail&lt;/code&gt; represents the &lt;strong&gt;tail&lt;/strong&gt; command&lt;br&gt;
&lt;code&gt;-n 2&lt;/code&gt; represents the &lt;strong&gt;number of lines&lt;/strong&gt; option&lt;br&gt;
&lt;code&gt;groceryFinal.txt&lt;/code&gt; represents the &lt;strong&gt;file we want to read&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So &lt;code&gt;head&lt;/code&gt; and the &lt;code&gt;tail&lt;/code&gt; command is basically used to view the snippet of file content.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; There are two commands &lt;code&gt;sed&lt;/code&gt; and &lt;code&gt;awk&lt;/code&gt; that I have purposefully left out to cover them later in deep.&lt;/p&gt;

&lt;p&gt;So these were the commands to filter file contents. If I missed any please let me know in the discussions below. &lt;/p&gt;

&lt;p&gt;Also, let me know if you have any questions down below. See you in the funny papers.&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>ubuntu</category>
      <category>linux</category>
      <category>beginners</category>
    </item>
    <item>
      <title>fd - Best alternative to Find Command</title>
      <dc:creator>yash sugandh</dc:creator>
      <pubDate>Sat, 11 Jul 2020 14:26:35 +0000</pubDate>
      <link>https://dev.to/yashsugandh/fd-best-alternative-to-find-command-pko</link>
      <guid>https://dev.to/yashsugandh/fd-best-alternative-to-find-command-pko</guid>
      <description>&lt;p&gt;Last few posts in &lt;a href="https://dev.to/yashsugandh/commands-for-viewing-and-sorting-files-452j"&gt;Linux For Beginners&lt;/a&gt; has been quite hectic. So I thought why not learn something fun and useful today the &lt;code&gt;fd&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;I know what you are thinking that learning a command is not a very nice way to have fun but if you are like me then learning something new that easy to use but packs quite a punch will definitely bring a smile to your face.&lt;/p&gt;

&lt;p&gt;So let's not waste any time and go-ahead to our today's topic &lt;code&gt;fd&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;fd&lt;/code&gt; command is a &lt;strong&gt;simple, fast, and user-friendly&lt;/strong&gt; alternative to find.&lt;/p&gt;

&lt;p&gt;So why use this over &lt;code&gt;find&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;We use &lt;code&gt;fd&lt;/code&gt; over find because &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It's &lt;strong&gt;easy&lt;/strong&gt; to use&lt;/li&gt;
&lt;li&gt;Does not look in &lt;strong&gt;hidden files&lt;/strong&gt; and directories by default.&lt;/li&gt;
&lt;li&gt;It's &lt;strong&gt;faster&lt;/strong&gt; than the find command&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Parallel&lt;/strong&gt; command execution&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ignores&lt;/strong&gt; patterns from your &lt;strong&gt;.gitignore&lt;/strong&gt;, by default.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I can keep going but let's look at them in detail later :)&lt;/p&gt;

&lt;p&gt;So, can we replace &lt;code&gt;find&lt;/code&gt; with &lt;code&gt;fd&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;Well not exactly, &lt;code&gt;fd&lt;/code&gt; covers almost &lt;strong&gt;80%&lt;/strong&gt; of &lt;code&gt;find&lt;/code&gt; utilities. So think of &lt;code&gt;fd&lt;/code&gt; as an extension to the &lt;code&gt;find&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;Let's start with how to &lt;strong&gt;install&lt;/strong&gt; the &lt;code&gt;fd&lt;/code&gt; command&lt;/p&gt;

&lt;p&gt;To install &lt;code&gt;fd&lt;/code&gt; on Debian we use&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt install fd-find
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For some other Distros&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Fedora:&lt;br&gt;
&lt;br&gt;
&lt;code&gt;dnf install fd-find&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Alpine:&lt;br&gt;
&lt;br&gt;
&lt;code&gt;apk add fd&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Arch Linux:&lt;br&gt;
&lt;br&gt;
&lt;code&gt;pacman -S fd&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Mac OS:&lt;br&gt;
&lt;br&gt;
&lt;code&gt;brew install fd&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If I missed your Distro look into &lt;a href="https://github.com/sharkdp/fd"&gt;fd command docs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In some distros like &lt;strong&gt;Debian&lt;/strong&gt; there is already a package installed named &lt;code&gt;fd&lt;/code&gt; so we install &lt;code&gt;fdfind&lt;/code&gt;. To use &lt;code&gt;fd&lt;/code&gt; we can just set&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;alias fd=fdfind&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 in our shells initialization file. &lt;/p&gt;

&lt;p&gt;Now that we have installed &lt;code&gt;fd&lt;/code&gt; let's start using it&lt;/p&gt;

&lt;p&gt;The syntax of &lt;code&gt;fd&lt;/code&gt; command&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UXdw3sUW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/5unvzgx9vnvvtj2i2t4z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UXdw3sUW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/5unvzgx9vnvvtj2i2t4z.png" alt="fd-syntax" width="376" height="210"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To find something we just use &lt;code&gt;fd&lt;/code&gt; along with PATTERN(filename, textfiles, images)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kmkRHBQm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/itltx9utz4p7cg628tul.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kmkRHBQm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/itltx9utz4p7cg628tul.png" alt="find file-name" width="628" height="284"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;fd hello-world&lt;/code&gt; which returned us the path to the files &lt;strong&gt;named hello-world&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's take some more examples for better understanding&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In the examples mentioned below we are only seeing a portion of the actual output since the complete output is huge.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;find all the JSON files&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--v1WepSP_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gs0mcjfu56wjs48k7rnr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--v1WepSP_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gs0mcjfu56wjs48k7rnr.png" alt="fd-with-json-extension" width="578" height="432"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;fd -e json&lt;/code&gt; where&lt;br&gt;
&lt;code&gt;fd&lt;/code&gt; represents the &lt;code&gt;fd&lt;/code&gt; command&lt;br&gt;
&lt;code&gt;-e&lt;/code&gt; represents the options to &lt;strong&gt;filter using the extension&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;json&lt;/code&gt; represents the &lt;strong&gt;filter&lt;/strong&gt; type &lt;strong&gt;JSON files&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now what if we wanted all the JSON files but exclude some unwanted files &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eXENkmJ7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ywa0sl7a7m29q0ovx62k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eXENkmJ7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ywa0sl7a7m29q0ovx62k.png" alt="fd-idea-included" width="880" height="492"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;exclude idea JSON files&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sqlw3FXZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/f4beu9rgh6uavjzlmt5v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sqlw3FXZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/f4beu9rgh6uavjzlmt5v.png" alt="fd-json-excluded" width="712" height="320"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;fd -e json -E idea*&lt;/code&gt; where&lt;br&gt;
&lt;code&gt;fd&lt;/code&gt; represents the &lt;code&gt;fd&lt;/code&gt; command&lt;br&gt;
&lt;code&gt;-e&lt;/code&gt; represents the options to &lt;strong&gt;filter using the extension&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;json&lt;/code&gt; represents the &lt;strong&gt;filter&lt;/strong&gt; type &lt;strong&gt;JSON files&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;-E&lt;/code&gt; represents the options to &lt;strong&gt;exclude files&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;idea*&lt;/code&gt; represents the files to be excluded &lt;strong&gt;idea&lt;/strong&gt;*&lt;/p&gt;

&lt;p&gt;So that was awesome filtering out some unwanted files from search results&lt;/p&gt;

&lt;p&gt;Till now we have been searching through all our directories but what if we wanted to search files only in a specific directory&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;find all .txt files in the Documents directory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_Za35BEr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uvl2hqyk6rhgur75flmf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_Za35BEr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uvl2hqyk6rhgur75flmf.png" alt="fd-documents" width="646" height="284"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;fd .txt Documents&lt;/code&gt; where&lt;br&gt;
&lt;code&gt;fd&lt;/code&gt; represents the &lt;code&gt;fd&lt;/code&gt; command&lt;br&gt;
&lt;code&gt;.txt&lt;/code&gt; represents the &lt;strong&gt;filter&lt;/strong&gt; type &lt;strong&gt;text files&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;Documents&lt;/code&gt; represents the &lt;strong&gt;directories&lt;/strong&gt; we want to find the file in&lt;/p&gt;

&lt;p&gt;Another functionality that we need would be to filter the files on the basis of type&lt;/p&gt;

&lt;p&gt;-filter on the basis of type directory&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BxnzgV9R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2z0glmncnz8kurzvi2q5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BxnzgV9R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2z0glmncnz8kurzvi2q5.png" alt="fd-type" width="188" height="291"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;fd -td&lt;/code&gt; where&lt;br&gt;
&lt;code&gt;fd&lt;/code&gt; represents the &lt;code&gt;fd&lt;/code&gt; command&lt;br&gt;
&lt;code&gt;-td&lt;/code&gt; represents the option to filter the results on the basis of type which in &lt;code&gt;d&lt;/code&gt; directory in this case&lt;/p&gt;

&lt;p&gt;We can filter on the basis of other types as well&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vnyVUr5U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u3gi4vsxom6vyhmhr38c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vnyVUr5U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u3gi4vsxom6vyhmhr38c.png" alt="fd-type" width="744" height="458"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What about the hidden files and directories aren`t they ignored by default then how do we search for those?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;search .hidden.txt file&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vMTLq53V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/if5anig9yj5vd9y4q2lj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vMTLq53V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/if5anig9yj5vd9y4q2lj.png" alt="fd hidden" width="510" height="246"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;fd -H .hidden&lt;/code&gt; where&lt;br&gt;
&lt;code&gt;fd&lt;/code&gt; represents the &lt;code&gt;fd&lt;/code&gt; command&lt;br&gt;
&lt;code&gt;-H&lt;/code&gt; represents the option to search through hidden files and directories.&lt;br&gt;
&lt;code&gt;.hidden.txt&lt;/code&gt; represents the file to be searched for&lt;/p&gt;

&lt;p&gt;That's it for today let me know if you enjoyed it as much as I did.&lt;/p&gt;

&lt;p&gt;Please let me know in the discussion below if you have any questions and you can suggest me in the discussion if you have used some utility that you want others to know and learn about.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>ubuntu</category>
      <category>linux</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Commands for Viewing and Sorting Files</title>
      <dc:creator>yash sugandh</dc:creator>
      <pubDate>Mon, 06 Jul 2020 07:57:40 +0000</pubDate>
      <link>https://dev.to/yashsugandh/commands-for-viewing-and-sorting-files-452j</link>
      <guid>https://dev.to/yashsugandh/commands-for-viewing-and-sorting-files-452j</guid>
      <description>&lt;p&gt;In the last few posts, we looked at I/O Redirection and Piping in Linux System.&lt;/p&gt;

&lt;p&gt;Today we are going to look at commands that help us in viewing and sorting files.&lt;/p&gt;

&lt;p&gt;Let's start from the command that we have used before multiple times - &lt;code&gt;cat&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;cat&lt;/code&gt; command is used to concat multiple files together or just read the files.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;code&gt;cat&lt;/code&gt; command&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The syntax for cat command&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yJH22C5l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wtwdkbdr9o4t64enh4ff.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yJH22C5l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wtwdkbdr9o4t64enh4ff.png" alt="cat-syntax"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's take an example where we have two grocery lists grocery1.txt and grocery2.txt&lt;/p&gt;

&lt;p&gt;First, let's view the file content using cat&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bMKhJYXH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1qbuqbz0h3eyknjjhmjo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bMKhJYXH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1qbuqbz0h3eyknjjhmjo.png" alt="grocery-cat"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Similarly, we can view the 2nd file&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ejlP-b0l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gq3tyuijyzyc3goxpvhu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ejlP-b0l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gq3tyuijyzyc3goxpvhu.png" alt="grocery-2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above examples, we used the command &lt;code&gt;cat &amp;lt;filename&amp;gt;&lt;/code&gt; to view the file contents.&lt;/p&gt;

&lt;p&gt;What if we wanted to concat these 2 files?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;concat above files&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KW0FqnBP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9ava5iyz13l28cpgk8rv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KW0FqnBP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9ava5iyz13l28cpgk8rv.png" alt="cat-concat"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;cat grocery1.txt grocery2.txt &amp;gt; groceryFinal.txt&lt;/code&gt; where&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cat&lt;/code&gt; represents the concat command&lt;br&gt;
&lt;code&gt;grocery1.txt grocery2.txt&lt;/code&gt; represents &lt;strong&gt;files to concat&lt;/strong&gt;.&lt;br&gt;
&lt;code&gt;&amp;gt;&lt;/code&gt; represents redirection of &lt;strong&gt;Output Stream&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;groceryFinal.txt&lt;/code&gt; represents the &lt;strong&gt;output file&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Then to confirm that all the content is present we used &lt;code&gt;cat groceryFinal.txt&lt;/code&gt; to view the content of the output file.&lt;/p&gt;

&lt;p&gt;But the above output is a little hard to read, what if we wanted to view the output file but numbered?&lt;/p&gt;

&lt;p&gt;Let's check it out:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;View the output file numbered&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--a4XQ1gvX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gg1ual1de2sg2ut8h3gg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--a4XQ1gvX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gg1ual1de2sg2ut8h3gg.png" alt="grocery-final"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;cat -n groceryFinal.txt&lt;/code&gt; where&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cat&lt;/code&gt; represents the &lt;strong&gt;concat&lt;/strong&gt; command&lt;br&gt;
&lt;code&gt;-n&lt;/code&gt; represents the option to &lt;strong&gt;number all output line&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;groceryFinal.txt&lt;/code&gt; represents the &lt;strong&gt;file&lt;/strong&gt; to view the content of&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. &lt;code&gt;tac&lt;/code&gt; command&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;tac&lt;/code&gt; command which is just the reverse of cat command starts reading from bottom.&lt;/p&gt;

&lt;p&gt;It does not change the content it just reads from the bottom.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fI8HBKZ1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/qrlv706gteh9fyzca6ww.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fI8HBKZ1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/qrlv706gteh9fyzca6ww.png" alt="tac-syntax"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To use &lt;code&gt;tac&lt;/code&gt; command we simply use &lt;code&gt;tac&lt;/code&gt; along with the "filename"&lt;/p&gt;

&lt;p&gt;Let's just use the previously concatenated file as input for the &lt;code&gt;tac&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fvQyHvW5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3395tuog2hp4mm90ga62.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fvQyHvW5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3395tuog2hp4mm90ga62.png" alt="tac-command-output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;tac groceryFinal.txt&lt;/code&gt; which printed the content of the file from bottom up.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. &lt;code&gt;rev&lt;/code&gt; command&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;rev&lt;/code&gt; command is used to reverse the content of the file.&lt;/p&gt;

&lt;p&gt;It reads the file from the top but the content will be reversed.&lt;/p&gt;

&lt;p&gt;The syntax for &lt;code&gt;rev&lt;/code&gt; command&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tDnIA-uB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/crt05jpubes1zlhz5jub.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tDnIA-uB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/crt05jpubes1zlhz5jub.png" alt="rev-syntax"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To use &lt;code&gt;rev&lt;/code&gt; command we simply use &lt;code&gt;rev&lt;/code&gt; along with the "filename"&lt;/p&gt;

&lt;p&gt;Let's again use our &lt;strong&gt;groceryFinal.txt&lt;/strong&gt; as an input&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YKNg1NP3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fojkxtgfct2ucnqvhiqh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YKNg1NP3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fojkxtgfct2ucnqvhiqh.png" alt="rev-command"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;rev groceryFinal.txt&lt;/code&gt; which printed the content of the file reversed.&lt;/p&gt;

&lt;p&gt;I can't seem to find the practical usage of &lt;code&gt;rev&lt;/code&gt; command other than irritating someone by sending them the file after applying &lt;code&gt;rev&lt;/code&gt; command :)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. less command&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;less&lt;/code&gt; command provides us a way to view text files in our terminal itself.&lt;/p&gt;

&lt;p&gt;The syntax for opening a file using &lt;code&gt;less&lt;/code&gt; command&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3b-M5b8U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/22pi8d6tifritm0p2rlq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3b-M5b8U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/22pi8d6tifritm0p2rlq.png" alt="less-syntax"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To open a file using &lt;code&gt;less&lt;/code&gt; we only need to call &lt;code&gt;less&lt;/code&gt; command along with the filename.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--S5vF5e4J--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/hbquo8yy0ihb5vpdsfq9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--S5vF5e4J--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/hbquo8yy0ihb5vpdsfq9.png" alt="less commands"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;less&lt;/strong&gt; provides us tons of functionality such as search a character, scroll up/down, etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Head and Tail&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;head&lt;/code&gt; command is used to print the lines from the start of the file whereas the &lt;code&gt;tail&lt;/code&gt; command is used to print the files from the bottom of the file.&lt;/p&gt;

&lt;p&gt;Let's take an example for better understanding&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;print only the first two line from a file&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Xbh64QYP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7ux4q9vgcbb07r3qceit.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Xbh64QYP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7ux4q9vgcbb07r3qceit.png" alt="head-example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;head -n 2 groceryFinal.txt&lt;/code&gt; where&lt;/p&gt;

&lt;p&gt;&lt;code&gt;head&lt;/code&gt; represents the &lt;strong&gt;head&lt;/strong&gt; command&lt;br&gt;
&lt;code&gt;-n 2&lt;/code&gt; represents the &lt;strong&gt;number of lines&lt;/strong&gt; option&lt;br&gt;
&lt;code&gt;groceryFinal.txt&lt;/code&gt; represents the &lt;strong&gt;file we want to read&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;print only the last two lines from a file&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2cXClvSG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uhd0lyhd5r6az5egnx75.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2cXClvSG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uhd0lyhd5r6az5egnx75.png" alt="tail-example-updated"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;tail&lt;/code&gt; represents the &lt;strong&gt;tail&lt;/strong&gt; command&lt;br&gt;
&lt;code&gt;-n 2&lt;/code&gt; represents the &lt;strong&gt;number of lines&lt;/strong&gt; option&lt;br&gt;
&lt;code&gt;groceryFinal.txt&lt;/code&gt; represents the &lt;strong&gt;file we want to read&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So &lt;code&gt;head&lt;/code&gt; and the &lt;code&gt;tail&lt;/code&gt; command is basically used to view the snippet of file content.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. &lt;code&gt;sort&lt;/code&gt; command&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;sort&lt;/code&gt; command is used to sort lines of text files.&lt;/p&gt;

&lt;p&gt;The syntax for &lt;code&gt;sort&lt;/code&gt; command&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AoF7Fdv5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bmrdvlt2ilgiatk4egsk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AoF7Fdv5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bmrdvlt2ilgiatk4egsk.png" alt="sort-syntax"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To use &lt;code&gt;sort&lt;/code&gt; command we simply use &lt;code&gt;sort&lt;/code&gt; command along with &lt;code&gt;options&lt;/code&gt; and &lt;code&gt;filename&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4v0-UYDy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/dj3j13184rsbpw2soihe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4v0-UYDy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/dj3j13184rsbpw2soihe.png" alt="sort-basic"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we use the command &lt;code&gt;sort groceryFinal.txt&lt;/code&gt; which sorts the list in ascending order.&lt;/p&gt;

&lt;p&gt;What if we want to &lt;code&gt;sort&lt;/code&gt; in &lt;strong&gt;descending order&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UJ2nc5TE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/y6pmvbunbw06hm8ce417.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UJ2nc5TE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/y6pmvbunbw06hm8ce417.png" alt="sort-reversed"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;sort -r groceryFinal.txt&lt;/code&gt; where&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sort&lt;/code&gt; represents &lt;strong&gt;sort&lt;/strong&gt; command&lt;br&gt;
&lt;code&gt;-r&lt;/code&gt; represents the &lt;strong&gt;reverse&lt;/strong&gt; option&lt;br&gt;
&lt;code&gt;groceryFinal.txt&lt;/code&gt; represents &lt;strong&gt;the input file&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Can you guess what else can we use to get the same kind of output?&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sort groceryFinal.txt | tac&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you guessed the &lt;code&gt;tac&lt;/code&gt; command you were right. We can use the &lt;code&gt;tac&lt;/code&gt; command along with &lt;code&gt;sort&lt;/code&gt; command convert the &lt;strong&gt;ascending&lt;/strong&gt; sorted output to &lt;strong&gt;descending&lt;/strong&gt; output.&lt;/p&gt;

&lt;p&gt;But what about numbers?&lt;/p&gt;

&lt;p&gt;Let's see what happens when we use the &lt;code&gt;sort&lt;/code&gt; command with numbers&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iafn6t-9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/68si8lhbbizim0r81fis.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iafn6t-9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/68si8lhbbizim0r81fis.png" alt="sort-example-numbers"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Wait, that seems wrong but why?&lt;/p&gt;

&lt;p&gt;So the &lt;code&gt;sort&lt;/code&gt; command by default compares digit by digit.&lt;/p&gt;

&lt;p&gt;So what can we do for sorting numbers?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;sort file with respect to numbers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AH9DFC6X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/btgvw8c2dzspo6vc67de.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AH9DFC6X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/btgvw8c2dzspo6vc67de.png" alt="sort-number-example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;sort -n numbers.txt&lt;/code&gt; where&lt;br&gt;
&lt;code&gt;sort&lt;/code&gt; represents &lt;strong&gt;sort&lt;/strong&gt; command&lt;br&gt;
&lt;code&gt;-n&lt;/code&gt; represents &lt;strong&gt;numeric-sort&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;numbers.txt&lt;/code&gt; represents &lt;strong&gt;file-to-be-sorted&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;sort and only return distinct values&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oDoVZeeG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/qcswxs6378ecvh2vfmfl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oDoVZeeG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/qcswxs6378ecvh2vfmfl.png" alt="sort-unique"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;sort -un numbers.txt&lt;/code&gt; where&lt;br&gt;
&lt;code&gt;sort&lt;/code&gt; represents &lt;strong&gt;sort&lt;/strong&gt; command&lt;br&gt;
&lt;code&gt;-un&lt;/code&gt; represents &lt;strong&gt;unique numeric-sort&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;numbers.txt&lt;/code&gt; represents &lt;strong&gt;file-to-be-sorted&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;What if we have data with multiple columns and we want to sort data on the basis of a specific column?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;sort data based on column 2&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AvQSHjfE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uoye03i1urrrb1ly0upe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AvQSHjfE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uoye03i1urrrb1ly0upe.png" alt="sort-cplumn"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;sort -k 2n employees.txt&lt;/code&gt; where&lt;/p&gt;

&lt;p&gt;In the above example, we used the command &lt;code&gt;sort -un numbers.txt&lt;/code&gt; where&lt;br&gt;
&lt;code&gt;sort&lt;/code&gt; represents &lt;strong&gt;sort&lt;/strong&gt; command&lt;br&gt;
&lt;code&gt;-k 2n&lt;/code&gt; represents &lt;strong&gt;key i.e. column 2 numeric-sort&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;employees.txt&lt;/code&gt; represents &lt;strong&gt;file-to-be-sorted&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This was all about Viewing and Sorting Files in the Linux System. &lt;/p&gt;

&lt;p&gt;Please, let me know if there are any questions and suggestions on what you want us to explore next.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>linux</category>
      <category>ubuntu</category>
      <category>computerscience</category>
    </item>
  </channel>
</rss>
