<?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: Linux_guy</title>
    <description>The latest articles on DEV Community by Linux_guy (@nerdflash28).</description>
    <link>https://dev.to/nerdflash28</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%2F1409085%2F61466560-f372-4576-ad24-ed670aa68ff0.png</url>
      <title>DEV Community: Linux_guy</title>
      <link>https://dev.to/nerdflash28</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nerdflash28"/>
    <language>en</language>
    <item>
      <title>Terraform with AWS</title>
      <dc:creator>Linux_guy</dc:creator>
      <pubDate>Tue, 30 Sep 2025 15:58:11 +0000</pubDate>
      <link>https://dev.to/nerdflash28/terraform-with-aws-38o3</link>
      <guid>https://dev.to/nerdflash28/terraform-with-aws-38o3</guid>
      <description>&lt;p&gt;&lt;code&gt;Maine Goal&lt;/code&gt; : Primary goal of this post is to explain core concept of terraform&lt;br&gt;
&lt;code&gt;Terraform&lt;/code&gt; : Infrastructure as Code Tool&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Services&lt;/code&gt; : Basic AWS Services to demonstrate terraform concept&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Virtual Machine ( EC2 )&lt;/li&gt;
&lt;li&gt;Firewall ( Security Groups )&lt;/li&gt;
&lt;li&gt;AWS Users ( IAM Users )&lt;/li&gt;
&lt;li&gt;IP Address ( Elastic IP )&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Basics of Firewall
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Ports
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;ports acts as a endpoint of communication to identify a given application or process on an linux operating system&lt;/li&gt;
&lt;li&gt;to know what ports are opened on a system we use &lt;code&gt;netstat -ntlp&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Firewall
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;it's network security system that monitors and controls incoming and outgoing network traffic based on predetermined security rules&lt;/li&gt;
&lt;li&gt;for AWS we call it as security group&lt;/li&gt;
&lt;li&gt;Create Security group using terraform
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight terraform"&gt;&lt;code&gt;&lt;span class="k"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_security_group"&lt;/span&gt; &lt;span class="s2"&gt;"allow_tls"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"allow_tls"&lt;/span&gt;
  &lt;span class="nx"&gt;description&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Managed from Terraform | Allow TLS inbout traffic and all outbound traffic"&lt;/span&gt;
  &lt;span class="nx"&gt;vpc_id&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_vpc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;main&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;

  &lt;span class="nx"&gt;tags&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"allow_tls"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Block of code to add inboud or outboud rules to terraform
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight terraform"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Ingress : inbound rules&lt;/span&gt;
&lt;span class="c1"&gt;// Egress : Outbound rules&lt;/span&gt;
&lt;span class="k"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_vpc_security_group_ingress_rule"&lt;/span&gt; &lt;span class="s2"&gt;"allow_tls_ipv4"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;security_group_id&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_security_group&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;allow_tls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;
  &lt;span class="nx"&gt;cidr_ipv4&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_vpc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;main&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cidr_block&lt;/span&gt;
  &lt;span class="nx"&gt;from_port&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;
  &lt;span class="nx"&gt;ip_protocol&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"tcp"&lt;/span&gt;
  &lt;span class="nx"&gt;to_port&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_vpc_security_group_egress_rule"&lt;/span&gt; &lt;span class="s2"&gt;"allow_all_traffic_ipv4"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;security_group_id&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_security_group&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;allow_tls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;
  &lt;span class="nx"&gt;cidr_ipv4&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"0.0.0.0/0"&lt;/span&gt;
  &lt;span class="nx"&gt;ip_protocol&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"-1"&lt;/span&gt; &lt;span class="c1"&gt;# semantically equivalent to all ports&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>aws</category>
      <category>terraform</category>
      <category>cicd</category>
      <category>infrastructureascode</category>
    </item>
    <item>
      <title>SSH Tutorial</title>
      <dc:creator>Linux_guy</dc:creator>
      <pubDate>Wed, 16 Jul 2025 02:34:56 +0000</pubDate>
      <link>https://dev.to/nerdflash28/ssh-tutorial-1mgc</link>
      <guid>https://dev.to/nerdflash28/ssh-tutorial-1mgc</guid>
      <description>&lt;h2&gt;
  
  
  Create ssh keys
&lt;/h2&gt;

&lt;p&gt;to create new ssh key we use this command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ssh-keygen &lt;span class="nt"&gt;-t&lt;/span&gt; rsa &lt;span class="nt"&gt;-b&lt;/span&gt; 4096 &lt;span class="nt"&gt;-C&lt;/span&gt; &lt;span class="s2"&gt;"comment"&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; &lt;span class="s2"&gt;"key-name"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;after running this command two files will be generated. copy the contents of file with .pub extension and paste it on to remote hosts &lt;code&gt;.ssh/authorized_keys&lt;/code&gt; file&lt;/p&gt;

</description>
      <category>ssh</category>
      <category>webdev</category>
      <category>devops</category>
      <category>linux</category>
    </item>
    <item>
      <title>Installing Locate Package</title>
      <dc:creator>Linux_guy</dc:creator>
      <pubDate>Tue, 04 Mar 2025 15:13:27 +0000</pubDate>
      <link>https://dev.to/nerdflash28/installing-locate-package-2gl3</link>
      <guid>https://dev.to/nerdflash28/installing-locate-package-2gl3</guid>
      <description>&lt;h2&gt;
  
  
  To install locate command follow the steps
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Install mlocate package
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;yum &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-y&lt;/span&gt; mlocate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Run updatedb commands to build database of current files
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;updatedb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>bash</category>
      <category>devops</category>
      <category>developer</category>
      <category>development</category>
    </item>
    <item>
      <title>Docker Basics</title>
      <dc:creator>Linux_guy</dc:creator>
      <pubDate>Thu, 09 Jan 2025 11:43:49 +0000</pubDate>
      <link>https://dev.to/nerdflash28/docker-basics-37fe</link>
      <guid>https://dev.to/nerdflash28/docker-basics-37fe</guid>
      <description>&lt;h3&gt;
  
  
  Table of Contents
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Docker basics&lt;/li&gt;
&lt;li&gt;Docker working with containers&lt;/li&gt;
&lt;li&gt;Docker images&lt;/li&gt;
&lt;li&gt;Docker Volumes&lt;/li&gt;
&lt;li&gt;Docker Networking&lt;/li&gt;
&lt;li&gt;Docker Compose&lt;/li&gt;
&lt;li&gt;Docker Debug &amp;amp; Monitoring&lt;/li&gt;
&lt;li&gt;Clean Up&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;a id="basics"&gt;Docker Basics&lt;/a&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;To know currently Running docker version
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;To get detailed information about docker installation
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker info
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;to get help with docker use
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker &lt;span class="nb"&gt;help&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;a id="containers"&gt;Working with Containers
&lt;/a&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;to run a docker container
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# docker run [options] image-name [command] [args]&lt;/span&gt;
&lt;span class="c"&gt;# ex.&lt;/span&gt;
docker run nginx
&lt;span class="c"&gt;# this will run container in detached mode&lt;/span&gt;
docker run &lt;span class="nt"&gt;-d&lt;/span&gt; nginx 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;to list running containers
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker ps 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;to list all the containers running or stopped
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker ps &lt;span class="nt"&gt;-a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;to stop a running stop
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# docker stop [container_id/name]&lt;/span&gt;
docker stop nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;to start a stopped container
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# docker start [container_id/name]&lt;/span&gt;
docker start nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;to restart a container
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# docker restart [container_id/name]&lt;/span&gt;
docker restart nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;to remove a stopped container
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# docker rm [container_id/name]&lt;/span&gt;
docker &lt;span class="nb"&gt;rm &lt;/span&gt;nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;to check logs of a container
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# docker logs [container_id/name]&lt;/span&gt;
docker logs nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;to execute a command in a running container
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="nt"&gt;-it&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;container_id] &amp;lt;&lt;span class="nb"&gt;command&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;to get detailed information about a container
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker inspect &lt;span class="o"&gt;[&lt;/span&gt;container_id]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;to get real-time usage stats of a running container
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker stats
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;a id="images"&gt;Docker images
&lt;/a&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;to list all the docker images
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker images
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;to download an image from docker hub
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# docker pull [image_name]&lt;/span&gt;
docker pull nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;to push a docker image to a registry
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker push &lt;span class="o"&gt;[&lt;/span&gt;image_name]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;to export docker image to a tar file we use
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker save &lt;span class="nt"&gt;-o&lt;/span&gt; &amp;lt;file&amp;gt;.tar &lt;span class="o"&gt;[&lt;/span&gt;image_name]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;to import docker image from tar file
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker load &lt;span class="nt"&gt;-i&lt;/span&gt; &amp;lt;file&amp;gt;.tar 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;to build an image from a docker file
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# docker build -t [IMAGE_NAME:TAG]&lt;/span&gt;
docker build &lt;span class="nt"&gt;-t&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;image_name]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;to remove an docker image
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# docker rmi [IMAGE_NAME:TAG&lt;/span&gt;
docker rmi nignx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;a id="volume"&gt;Docker volume
&lt;/a&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;to create a docker volumes
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# docker volume create [VOLUME_NAME]&lt;/span&gt;
docker volume create &lt;span class="o"&gt;[&lt;/span&gt;VOLUME_NAME]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;to list all the volumes
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker volume &lt;span class="nb"&gt;ls&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;to remove a volume
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker volume &lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;VOLUME_NAME]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;to mount a volume to a container
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;-v&lt;/span&gt; &amp;lt;volume&amp;gt;:/path/in/container &lt;span class="o"&gt;[&lt;/span&gt;image_name]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;a id="networking"&gt; Docker networking
&lt;/a&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;to list all the docker networks
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker network &lt;span class="nb"&gt;ls&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;to create docker network
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker network create &lt;span class="o"&gt;[&lt;/span&gt;NETWORK_NAME]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;to connect a container to a specific docker network
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker network connect &lt;span class="o"&gt;[&lt;/span&gt;NETWORK_NAME]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;view details about specific docker network
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker network inspect &lt;span class="o"&gt;[&lt;/span&gt;NETWORK_NAME]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;a id="compose"&gt;Docker Compose
&lt;/a&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;start all the service defined in a yaml file
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose up
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;to get logs
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose logs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;build images defined in a docker-compose.yml file
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;stop and remove all servies defined in the file
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose down
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;list all services managed by compose
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose ps
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;a id="debug"&gt;Debugging and monitoring
&lt;/a&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;run a command inside a running container
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="nt"&gt;-it&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;container_id/name]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;View detailed information about a container or image
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker inspect &lt;span class="o"&gt;[&lt;/span&gt;container_id/name]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;monitor resource usage of container
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker stats
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;a id="clean"&gt;Clean up
&lt;/a&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;remove unused data(stopped container, images, networks)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker system prune &lt;span class="nt"&gt;-a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;remove unwanted containers
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker container prune
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;remove unused images
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker image prune &lt;span class="nt"&gt;-a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;remove unused volume
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker volume prune
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  END
&lt;/h2&gt;

</description>
      <category>docker</category>
      <category>container</category>
      <category>linux</category>
      <category>beginners</category>
    </item>
    <item>
      <title>DSA Using C++</title>
      <dc:creator>Linux_guy</dc:creator>
      <pubDate>Sat, 21 Dec 2024 20:19:50 +0000</pubDate>
      <link>https://dev.to/nerdflash28/dsa-using-c-1edf</link>
      <guid>https://dev.to/nerdflash28/dsa-using-c-1edf</guid>
      <description>&lt;h2&gt;
  
  
  Starting with Pointers
&lt;/h2&gt;




&lt;h3&gt;
  
  
  Code without Pointers
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;# include &amp;lt;iostream&amp;gt;
&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// defining number 1&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// defining number 2&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// redefining num1&lt;/span&gt;
    &lt;span class="n"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;22&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// print out the num1 and num2&lt;/span&gt;
    &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"num1 = "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"num2 = "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;num2&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Result : value will be different in both num1 and num2&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Code with Pointers
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;# include &amp;lt;iostream&amp;gt;
&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// defining num1 using pointer&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// defining num2 using pointer&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;num2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// print out the num1 and num2&lt;/span&gt;
    &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"num1 = "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"num2 = "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;num2&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Result : value will be same in both num1 and num2&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>cpp</category>
      <category>dsa</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Python : OS Module Introduction</title>
      <dc:creator>Linux_guy</dc:creator>
      <pubDate>Tue, 15 Oct 2024 23:54:13 +0000</pubDate>
      <link>https://dev.to/nerdflash28/python-os-module-introduction-1op2</link>
      <guid>https://dev.to/nerdflash28/python-os-module-introduction-1op2</guid>
      <description>&lt;h1&gt;
  
  
  OS module
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;in order to import OS module we use
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;To print all the available options in a method/function
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;dir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;to print Current working directory we use
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getcwd&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;to print directory location we use
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;/home/user/&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;# printing path before changing directory
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getcwd&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

&lt;span class="c1"&gt;# function used for changing directory
&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;chdir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# printing path after changing directory
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getcwd&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

&lt;span class="c1"&gt;# to list directories
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listdir&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;To make a single directory without any intermediate directories
&amp;lt;!-- make directory --&amp;gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;

&lt;span class="c1"&gt;# this wont create intermediate directories
&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;makedir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;single_dir&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listdir&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;To make a multiple directories with intermediate directories
&amp;lt;!-- make chain of directories --&amp;gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;

&lt;span class="c1"&gt;# this will create intermediate directories
&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;makedirs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;parent_dir/child_dir&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listdir&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;To remove a single directory 
&amp;lt;!-- remove Single Directory --&amp;gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;

&lt;span class="c1"&gt;# this wont remove intermediate directories
&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rmdir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;path&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listdir&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;To remove a multiple directory 
&amp;lt;!-- remove Multiple directory --&amp;gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;

&lt;span class="c1"&gt;# this will remove intermediate directories
&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removedirs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;path1/path2&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listdir&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;To rename a directory 
&amp;lt;!-- Renaming a Directory --&amp;gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;

&lt;span class="c1"&gt;# this will remove intermediate directories
&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rename&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;old-name&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;new-name&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listdir&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;To print information about OS, we use os.stat() function
&amp;lt;!-- Print details about directory --&amp;gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;

&lt;span class="c1"&gt;# this will remove intermediate directories
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;file-name&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="c1"&gt;# Example : to print when file was created 
&lt;/span&gt;&lt;span class="n"&gt;file_created&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;file-name&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;st_mtime&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromtimestamp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file_created&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;To list information about directories &amp;amp; Subdirectories
&amp;lt;!-- Navigating Directories and Subdirectories --&amp;gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;

&lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;chdir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;path&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Example : to print all the files under that above path 
&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;dirpath&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;filename&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;walk&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Current Path:&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;dirpath&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Directories:&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;dirname&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;filename:&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;To print Environment variables 
&amp;lt;!-- printing Environment Variables --&amp;gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;

&lt;span class="c1"&gt;# to print Environment variable home
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;HOME&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;To interact with path we use os.path module&lt;/p&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Examples of path module&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt; 

&lt;span class="c1"&gt;# to check if given path exists or not 
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;/home/user1/text.txt&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="c1"&gt;# to check if given path is a directory or file
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isdir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;/home/user2/demo&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isfilek&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;/home/user2/demo&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="c1"&gt;# to split filname name from extenstion we use
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splitext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;/home/demo1/book.txt&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="c1"&gt;# to print basename of any file we use
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;basename&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;/home/demo1/book.txt&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="c1"&gt;# to print the directory name we use
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dirname&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;/home/demo1/book.txt&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="c1"&gt;# to print both, dirname + basename we use
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;/home/demo1/book.txt&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="c1"&gt;# to join paths we use
&lt;/span&gt;&lt;span class="n"&gt;file_path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;HOME&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;test.txt&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file_path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>python</category>
      <category>automation</category>
      <category>linux</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Bash Script using Python</title>
      <dc:creator>Linux_guy</dc:creator>
      <pubDate>Sat, 28 Sep 2024 14:30:00 +0000</pubDate>
      <link>https://dev.to/nerdflash28/bash-script-using-python-n3j</link>
      <guid>https://dev.to/nerdflash28/bash-script-using-python-n3j</guid>
      <description>&lt;p&gt;have you ever felt stuck while writing bash script, which becomes complex over time and you find no other way to make it easier?&lt;/p&gt;

&lt;p&gt;in this blog, I'll try to demonstrate, how I've integrated the Python module with a bash script to make it easier.&lt;/p&gt;

&lt;h2&gt;
  
  
  What do we need? 📝
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Basic Python understanding&lt;/li&gt;
&lt;li&gt;Bash scripting&lt;/li&gt;
&lt;li&gt;free time to play with it&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  How to run a simple bash command using python 🎯
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#!/bin/python3
&lt;/span&gt;
&lt;span class="c1"&gt;# import subprocess modules
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;subprocess&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;sp&lt;/span&gt;

&lt;span class="c1"&gt;# run subprocess 
&lt;/span&gt;&lt;span class="n"&gt;sp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ls&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;shell&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmoweia5noxi871q1c14f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmoweia5noxi871q1c14f.png" alt="Image description" width="800" height="158"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  passing argument as a list 🔐
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;For security reasons we don't enable shell=True, instead we pass command arguments via a list in the process
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#!/bin/python3
&lt;/span&gt;
&lt;span class="c1"&gt;# import subprocess modules
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;subprocess&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;sp&lt;/span&gt;

&lt;span class="c1"&gt;# run subprocess
&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;sp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ls&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;-la&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="n"&gt;capture_output&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# text=True: this option converts the output into a string instead of a binary
# capture_output=True: this option directs output to be stored in a variable instead of directing it to the console
&lt;/span&gt;
&lt;span class="c1"&gt;# printing the output
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# more explanation:
# p1.stdout: this prints the standard output of the process
# p1.stderr: this will print the standard error of the process
# p1.returncode: this will print the return code for a process
# zero: if the command ran successfully
# non-zero: if command failed in execution
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frn3me28nkvcptxj2ih2o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frn3me28nkvcptxj2ih2o.png" alt="Image description" width="800" height="118"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Redirecting output to a file ➡️📁
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;We can also store the output of a command to file
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#!/bin/python3
&lt;/span&gt;
&lt;span class="c1"&gt;# import subprocess modules
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;subprocess&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;sp&lt;/span&gt;

&lt;span class="c1"&gt;# run subprocess
&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;sp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ls&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;-la&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="n"&gt;capture_output&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# using file handling we can output content of the process p2 to a file
&lt;/span&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;date.txt&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;w&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;sp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;date&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foj1xt737cemh6uv94tdj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foj1xt737cemh6uv94tdj.png" alt="Image description" width="800" height="208"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  What if!! we get an error!! 😲
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Usually python doesn't give an error by default if a command results in an error&lt;/li&gt;
&lt;li&gt;to fix this we use the check argument in the subprocess command
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#!/bin/python3
&lt;/span&gt;
&lt;span class="c1"&gt;# import subprocess modules
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;subprocess&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;sp&lt;/span&gt;

&lt;span class="c1"&gt;# run subprocess
&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;sp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ls&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;-la&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;-e&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="n"&gt;capture_output&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;check&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# printing the output
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stderr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feudrx9gdz60bne2xcjpl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feudrx9gdz60bne2xcjpl.png" alt="Image description" width="800" height="92"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  What if!! 🤔 we want to discard the error
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;to discard the error in bash we redirect it to the &amp;gt; /dev/null&lt;/li&gt;
&lt;li&gt;to make the same happen using Python we use
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#!/bin/python3
&lt;/span&gt;
&lt;span class="c1"&gt;# import subprocess modules
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;subprocess&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;sp&lt;/span&gt;

&lt;span class="c1"&gt;# run subprocess
&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;sp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ls&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;-la&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;-e&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="n"&gt;stderr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;sp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DEVNULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# printing the output
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stderr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frhugn77l166vrhn30utd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frhugn77l166vrhn30utd.png" alt="Image description" width="800" height="89"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Reading 📖 a file 📁
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;to read a file we use cat command
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#!/bin/python3
&lt;/span&gt;
&lt;span class="c1"&gt;# import subprocess modules
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;subprocess&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;sp&lt;/span&gt;

&lt;span class="c1"&gt;# run subprocess
&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;sp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;cat&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;sample-file.txt&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="n"&gt;capture_output&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# printing the output
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Output:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Felgpy6ns5gedeo1w2an2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Felgpy6ns5gedeo1w2an2.png" alt="Image description" width="800" height="112"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  grep the contents of the file
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;to grep the contents of the file we can use output of the previous process as input to the other process.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#!/bin/python3
&lt;/span&gt;
&lt;span class="c1"&gt;# import subprocess modules
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;subprocess&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;sp&lt;/span&gt;

&lt;span class="c1"&gt;# run subprocess to cat
&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;sp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;cat&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;sample-file.txt&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="n"&gt;capture_output&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# run subprocess to grep
&lt;/span&gt;&lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;sp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;grep&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;-n&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;text&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="n"&gt;capture_output&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# printing the output
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg6n4bkk98y33ryv6a86c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg6n4bkk98y33ryv6a86c.png" alt="Image description" width="800" height="81"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  ⚠️ Advanced ⚠️ !! content here!
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;whenever we run a command using subprocess.run it holds the console till the command execution is completed, this can be time-consuming in some cases.&lt;/li&gt;
&lt;li&gt;to fix this we use Popen() method from subprocess
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#!/bin/python3
&lt;/span&gt;
&lt;span class="c1"&gt;# import subprocess modules
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;subprocess&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;sp&lt;/span&gt;

&lt;span class="c1"&gt;# running a subprocess
&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;sp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Popen&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ping&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;-n&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;10&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;google.com&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="n"&gt;capture_output&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;#checking for the output
&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;poll&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;stderr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;communicate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# explanation
# In the above command, it will ping Google 10 times
# During that process p1.poll() command will be used to check out if the above command has finished running or not
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>python</category>
      <category>linux</category>
      <category>beginners</category>
      <category>automation</category>
    </item>
    <item>
      <title>Grid Engine Commands</title>
      <dc:creator>Linux_guy</dc:creator>
      <pubDate>Mon, 23 Sep 2024 16:27:17 +0000</pubDate>
      <link>https://dev.to/nerdflash28/grid-engine-commands-h9b</link>
      <guid>https://dev.to/nerdflash28/grid-engine-commands-h9b</guid>
      <description>&lt;h2&gt;
  
  
  QSTAT Command
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;To check the status of the jobs we use the qstat command
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;qstat

&lt;span class="c"&gt;# to get information about specific job&lt;/span&gt;
qstat &lt;span class="nt"&gt;-j&lt;/span&gt; &amp;lt;job-id&amp;gt;

&lt;span class="c"&gt;# to get the full information of the resources&lt;/span&gt;
qstat &lt;span class="nt"&gt;-f&lt;/span&gt; 

&lt;span class="c"&gt;# to get the full details of all the jobs running&lt;/span&gt;
qstat &lt;span class="nt"&gt;-F&lt;/span&gt;

&lt;span class="c"&gt;# To get the filter for a queue we use&lt;/span&gt;
qstat &lt;span class="nt"&gt;-q&lt;/span&gt; queue_list

&lt;span class="c"&gt;# to filter by queue state &lt;/span&gt;
qstat &lt;span class="nt"&gt;-qs&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;a|c|d|o|s|u|A|C|D|E|S&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;# to filter jobs by users&lt;/span&gt;
qstat &lt;span class="nt"&gt;-u&lt;/span&gt; &amp;lt;userid&amp;gt;

&lt;span class="c"&gt;# to show all user's job&lt;/span&gt;
qstat &lt;span class="nt"&gt;-u&lt;/span&gt; &lt;span class="se"&gt;\*&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  QSUB
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;To submit a batch job we use the qsub command
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Example 1: to submit a simple bash job&lt;/span&gt;
qsub &amp;lt;script_path&amp;gt;

&lt;span class="c"&gt;# Example 2: To submit an advanced batch job we use&lt;/span&gt;
qsub &lt;span class="nt"&gt;-cwd&lt;/span&gt; &lt;span class="nt"&gt;-S&lt;/span&gt; /bin/bash &lt;span class="nt"&gt;-i&lt;/span&gt; /data/example.in &lt;span class="nt"&gt;-o&lt;/span&gt; /results/example.out &lt;span class="nt"&gt;-j&lt;/span&gt; y example.sh arg1 arg2
&lt;span class="c"&gt;# Options explained:&lt;/span&gt;
&lt;span class="c"&gt;# -cwd: job will be executed from the same directory&lt;/span&gt;
&lt;span class="c"&gt;# -S: bash shell will be assigned to interpret the submitted job&lt;/span&gt;
&lt;span class="c"&gt;# -i: this file will be used as an input&lt;/span&gt;
&lt;span class="c"&gt;# -o: this file will be used to show output&lt;/span&gt;
&lt;span class="c"&gt;# -j y: this will merge the error into the same as the output file&lt;/span&gt;

&lt;span class="c"&gt;# Example 3: advanced script&lt;/span&gt;
qsub &lt;span class="nt"&gt;-N&lt;/span&gt; example3 &lt;span class="nt"&gt;-P&lt;/span&gt; project_test &lt;span class="nt"&gt;-p&lt;/span&gt; &lt;span class="nt"&gt;-28&lt;/span&gt; &lt;span class="nt"&gt;-l&lt;/span&gt; &lt;span class="nv"&gt;a&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;lx-amd64 example.sh
&lt;span class="c"&gt;# Options explained:&lt;/span&gt;
&lt;span class="c"&gt;# -N: this will be submitted by the given name, instead of the script name&lt;/span&gt;
&lt;span class="c"&gt;# -P: this job will be submitted to project_test&lt;/span&gt;
&lt;span class="c"&gt;# -p: this will assign lower priority to the given job than normal priority&lt;/span&gt;
&lt;span class="c"&gt;# -l: this will request for amd64 resource on the scheduler&lt;/span&gt;

&lt;span class="c"&gt;# Example 4: to submit a binary job&lt;/span&gt;
qsub &lt;span class="nt"&gt;-b&lt;/span&gt; y firefox

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Job Submission Script
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="c"&gt;#$ -N example3&lt;/span&gt;
&lt;span class="c"&gt;#$ -P "project-name"&lt;/span&gt;
&lt;span class="c"&gt;#$ -o test.out -e error.err&lt;/span&gt;
&lt;span class="c"&gt;#$ -l mem=6G&lt;/span&gt;
&lt;span class="c"&gt;#$ -l cpu=2&lt;/span&gt;

&lt;span class="c"&gt;# command to be executed&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"just a test"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  QDEL
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;qdel is used to delete a job in the queue
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Force delete a job in the queue&lt;/span&gt;
qdel &lt;span class="nt"&gt;-f&lt;/span&gt; &amp;lt;job-id&amp;gt;

&lt;span class="c"&gt;# to delete specific tasks of a job&lt;/span&gt;
qdel &amp;lt;job-id&amp;gt; &lt;span class="nt"&gt;-t&lt;/span&gt; &amp;lt;task-range&amp;gt;

&lt;span class="c"&gt;# to delete all the jobs from a specific user, only possible for managers&lt;/span&gt;
qdel &lt;span class="nt"&gt;-u&lt;/span&gt; &amp;lt;user-list&amp;gt; &lt;span class="s2"&gt;"*"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to check for the list of the nodes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;To check the list of the current nodes we use the qhost command
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;qhost

&lt;span class="c"&gt;# To check the jobs running under the specific hosts we use&lt;/span&gt;
qhost &lt;span class="nt"&gt;-q&lt;/span&gt; &lt;span class="nt"&gt;-j&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>operations</category>
      <category>gridengine</category>
      <category>altair</category>
      <category>hpc</category>
    </item>
    <item>
      <title>Working With Shell</title>
      <dc:creator>Linux_guy</dc:creator>
      <pubDate>Thu, 12 Sep 2024 18:12:55 +0000</pubDate>
      <link>https://dev.to/nerdflash28/linux-shell-4a17</link>
      <guid>https://dev.to/nerdflash28/linux-shell-4a17</guid>
      <description>&lt;h3&gt;
  
  
  What is a shell?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Linux shell is a program that allows text-based interaction between the user and the computer&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Types of shell in Linux
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Linux Commands
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Linux command consists of program name followed by the option and arguments.&lt;/li&gt;
&lt;li&gt;For ex.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cat&lt;/span&gt; &lt;span class="nt"&gt;-n&lt;/span&gt; myfile

&lt;span class="c"&gt;# program name: cat&lt;/span&gt;
&lt;span class="c"&gt;# option: -n&lt;/span&gt;
&lt;span class="c"&gt;# argument: myfile&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Home directory
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The home directory is a directory that stores user-specific data.&lt;/li&gt;
&lt;li&gt;this data can be of any type&lt;/li&gt;
&lt;li&gt;for ex. If there is a user named john, then john's data like images, documents, downloads, etc. data will reside in &lt;strong&gt;/home/john/&lt;/strong&gt; location &lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  How to know the Home directory
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$HOME&lt;/span&gt;
&lt;span class="c"&gt;# This command will read from the environment variable $HOME&lt;/span&gt;

&lt;span class="nb"&gt;echo&lt;/span&gt; ~
&lt;span class="c"&gt;# This command will expand the tilde symbol to the given user's home directory path&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqehbehmfbeiy691j2om2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqehbehmfbeiy691j2om2.png" alt="Image description" width="800" height="69"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Directory Management
&lt;/h3&gt;

&lt;h4&gt;
  
  
  How to know your current path
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;To know the current path, we use the command pwd, which stands for Present working directory
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;pwd&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  How to create &amp;amp; remove directories
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;mkdir is a command that is used to create a directory&lt;/li&gt;
&lt;li&gt;rmdir is a command that is used to delete an empty directory&lt;/li&gt;
&lt;li&gt;example
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# to create a directory&lt;/span&gt;
&lt;span class="nb"&gt;mkdir &lt;/span&gt;test_directory

&lt;span class="c"&gt;# to create multiple directories&lt;/span&gt;
&lt;span class="nb"&gt;mkdir &lt;/span&gt;test1 test2 test3

&lt;span class="c"&gt;# to delete a directory&lt;/span&gt;
&lt;span class="nb"&gt;rmdir &lt;/span&gt;test_directory

&lt;span class="c"&gt;# to remove multiple directories&lt;/span&gt;
&lt;span class="nb"&gt;rmdir &lt;/span&gt;test1 test2 test3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  How to list the content of a folder
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;to list the content of a directory we use the ls command
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# to list the content of a directory&lt;/span&gt;
&lt;span class="nb"&gt;ls&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  How to create a chain of directories
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;in order to perform this we use the "-p" parameter with mkdir command&lt;/li&gt;
&lt;li&gt;Example
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# We want to create 4 directories each inside of it's parent directory&lt;/span&gt;
&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; a/b/c/d
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  How to navigate through directories
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;To switch directories we use cd command
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Changing to the parent directory&lt;/span&gt;
&lt;span class="nb"&gt;cd&lt;/span&gt; ../

&lt;span class="c"&gt;# Changing to the home directory&lt;/span&gt;
&lt;span class="nb"&gt;cd&lt;/span&gt; /home/user1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  How to navigate folder directory in Linux
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;there are two types of path notation in Linux&lt;/li&gt;
&lt;li&gt;1. Relative Path&lt;/li&gt;
&lt;li&gt;2. Absolute Path&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Relative path
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;relative path is defined with respect to the current directory&lt;/li&gt;
&lt;li&gt;"." represents the current working folder&lt;/li&gt;
&lt;li&gt;".." represents the parent directory&lt;/li&gt;
&lt;li&gt;For example
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# to change to the parent directory for the current directory&lt;/span&gt;
&lt;span class="nb"&gt;cd&lt;/span&gt; ../

&lt;span class="c"&gt;# to run a file/script present in the current directory&lt;/span&gt;
bash ./example.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Absolute path
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;absolute paths are defined with respect to the root "/" location&lt;/li&gt;
&lt;li&gt;for example
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# navigating to the user1 document folder&lt;/span&gt;
&lt;span class="nb"&gt;cd&lt;/span&gt; /home/user1/documents
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  PUSHD &amp;amp; POPD
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;pushd and popd commands are used to manage the directory stack.&lt;/li&gt;
&lt;li&gt;Example
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# we created 3 directories&lt;/span&gt;
&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; a/b/c

&lt;span class="c"&gt;# now we navigate to c directory using pushd command, by pushing it's path to directory stack&lt;/span&gt;
&lt;span class="nb"&gt;pushd &lt;/span&gt;a/b/c

&lt;span class="c"&gt;# this command will take us to the given c directory and we can create a file here&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"hello world"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; test_file.txt

&lt;span class="c"&gt;# to navigate to the previous path we can use popd command&lt;/span&gt;
&lt;span class="c"&gt;# this will bring us to the parent directory&lt;/span&gt;
&lt;span class="nb"&gt;popd&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>bash</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>linux</category>
    </item>
    <item>
      <title>Terraform Beginners Blog</title>
      <dc:creator>Linux_guy</dc:creator>
      <pubDate>Mon, 02 Sep 2024 11:29:24 +0000</pubDate>
      <link>https://dev.to/nerdflash28/terraform-beginners-blog-1n87</link>
      <guid>https://dev.to/nerdflash28/terraform-beginners-blog-1n87</guid>
      <description>&lt;h2&gt;
  
  
  To install Terraform
&lt;/h2&gt;

&lt;p&gt;for more updated info refer to the official website link.&lt;br&gt;
&lt;a href="https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli" rel="noopener noreferrer"&gt;Documentation&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# download and install yum utils&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;yum &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-y&lt;/span&gt; yum-utils

&lt;span class="c"&gt;# add the respective repository&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;yum-config-manager &lt;span class="nt"&gt;--add-repo&lt;/span&gt; https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo

&lt;span class="c"&gt;# install the terraform&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;yum &lt;span class="nt"&gt;-y&lt;/span&gt; &lt;span class="nb"&gt;install &lt;/span&gt;terraform
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  How to create a resource in Terraform
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Resource blocks
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;resource Blocks are the basic Building blocks in HCL language&lt;/li&gt;
&lt;li&gt;to create a resource block we define, resource keyword followed by Resource Type and it's name&lt;/li&gt;
&lt;li&gt;example is given below&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Terraform Workflow
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;It consists of 4 major steps&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Config file&lt;/code&gt; : write the config file, which mentions what changes to be made in infrastructure&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;terraform init&lt;/code&gt; : run this command to initialize the modules and prepare it for execution.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;terraform plan&lt;/code&gt; : run this command to plan and review, about the changes that will be made in the infrastructure.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;terraform apply&lt;/code&gt; : run this command to apply the final changes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Example 1 : to create a local file in Terraform
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;to create a resource in HCL( Hashicorp Configuration Language ) we use the following code block.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;resource &lt;span class="s2"&gt;"local_file"&lt;/span&gt; &lt;span class="s2"&gt;"pet"&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    filename &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"/root/pets.txt"&lt;/span&gt;
    content &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"We love pets!"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;# description of the code block&lt;/span&gt;
&lt;span class="nt"&gt;--------------------------------&lt;/span&gt;
&lt;span class="c"&gt;# resource_argument : Description&lt;/span&gt;
&lt;span class="nt"&gt;---------------------------------&lt;/span&gt;
&lt;span class="c"&gt;# resource : block type&lt;/span&gt;
&lt;span class="c"&gt;# "local_file" : Resource type&lt;/span&gt;
&lt;span class="c"&gt;# "pet" : Resource Name&lt;/span&gt;
&lt;span class="c"&gt;# filename : file path where file has to be created&lt;/span&gt;
&lt;span class="c"&gt;# content : content which needs to be written in that file&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Example 2 : to create an EC2 instance on AWS cloud
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;to create ec2 instance using Terraform we can use below code.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;resource &lt;span class="s2"&gt;"aws_instance"&lt;/span&gt; &lt;span class="s2"&gt;"webserver"&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    ami &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"ami-id"&lt;/span&gt;
    instance_type &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"t2.micro"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;# description of the code block&lt;/span&gt;
&lt;span class="nt"&gt;--------------------------------&lt;/span&gt;
&lt;span class="c"&gt;# resource_argument : Description&lt;/span&gt;
&lt;span class="nt"&gt;---------------------------------&lt;/span&gt;
&lt;span class="c"&gt;# resource : block type&lt;/span&gt;
&lt;span class="c"&gt;# "aws_instance" : Resource type&lt;/span&gt;
&lt;span class="c"&gt;# "webserver" : Resource Name&lt;/span&gt;
&lt;span class="c"&gt;# ami : image id which will be used while creating the ec2 resource&lt;/span&gt;
&lt;span class="c"&gt;# instance_type : type of instance which needs to be provisioned&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Example 3 : to create S3 bucket on AWS cloud
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;to create s3 bucket using terraform on AWS Cloud we can use below code.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;resource &lt;span class="s2"&gt;"aws_s3_bucket"&lt;/span&gt; &lt;span class="s2"&gt;"data"&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    bucket &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"webserver-bucket-org-2207"&lt;/span&gt;
    acl &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"private"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;# description of the code block&lt;/span&gt;
&lt;span class="nt"&gt;--------------------------------&lt;/span&gt;
&lt;span class="c"&gt;# resource_argument : Description&lt;/span&gt;
&lt;span class="nt"&gt;---------------------------------&lt;/span&gt;
&lt;span class="c"&gt;# resource : block type&lt;/span&gt;
&lt;span class="c"&gt;# "aws_s3_bucket" : Resource type&lt;/span&gt;
&lt;span class="c"&gt;# "data" : Resource Name&lt;/span&gt;
&lt;span class="c"&gt;# bucket : bucket name which will be assigned after resource has been created&lt;/span&gt;
&lt;span class="c"&gt;# acl : type of access to private to given S3 bucket&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Provider Versioning
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;During Terraform init, if version arguemnt is not specified, the most recent provider will be downloaded during init process.&lt;/li&gt;
&lt;li&gt;For production use, we should constrain the acceptable version via configuration, to ensure that new version with breaking changes wont get automatically installed.&lt;/li&gt;
&lt;li&gt;to specify specific version we use &lt;code&gt;&amp;gt;=1.0&lt;/code&gt; | &lt;code&gt;&amp;lt;=1.0&lt;/code&gt; | &lt;code&gt;~&amp;gt;2.0&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;by mentioning version terraform creates a lock file. which keeps the version mentioned in lock file.&lt;/li&gt;
&lt;li&gt;to define version in code we use
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;terraform &lt;span class="o"&gt;{&lt;/span&gt;
  required_providers &lt;span class="o"&gt;{&lt;/span&gt;
    aws &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="nb"&gt;source&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"hashicorp/aws"&lt;/span&gt;
      version &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"~&amp;gt; 3.0"&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

provider &lt;span class="s2"&gt;"aws"&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  region &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"us-east-1"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;dependency lock file allows us to lock to a specific version of the provider&lt;/li&gt;
&lt;li&gt;If a particular provider already has a selection recorded in the lock file, Terraform will always re-select that version for installation, even if a newer version has become available&lt;/li&gt;
&lt;li&gt;we can override this behaviour by adding the &lt;code&gt;-upgrade&lt;/code&gt; option when we run terraform init.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Terraform Refresh
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;terraform refresh command will check the latest state of your infrastructure and update the state file accordingly.&lt;/li&gt;
&lt;li&gt;we don't run this commande explicity&lt;/li&gt;
&lt;li&gt;this will be triggered automatically when we run &lt;code&gt;terraform plan&lt;/code&gt; or &lt;code&gt;terraform apply&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;this command is deprecated in newer versions of terraform&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Authentication Configuration
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;from security point of view, we should not keep credentials in a terraform file.&lt;/li&gt;
&lt;li&gt;we want our code to run successfully without hardcoding the secrets in the provider block.&lt;/li&gt;
&lt;li&gt;how to add config files
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;provider &lt;span class="s2"&gt;"aws"&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  shared_config_files &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"/Users/tf_user/.aws/conf"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
  shared_credentials_files &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"/Users/tf_user/.aws/creds"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
  profile &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"customprofile"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

resource &lt;span class="s2"&gt;"aws_eip"&lt;/span&gt; &lt;span class="s2"&gt;"lb"&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  domain &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"vpc"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;if shared files config lines are not added to provider block, by default, terraform will locate files $HOME/.aws/config and $HOME/.aws/credentials on Linux and macOS.&lt;/li&gt;
&lt;li&gt;best way to store aws credentials is to use &lt;code&gt;aws cli&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>devops</category>
      <category>development</category>
      <category>terraform</category>
    </item>
    <item>
      <title>Ansible for Beginners</title>
      <dc:creator>Linux_guy</dc:creator>
      <pubDate>Sun, 25 Aug 2024 14:26:05 +0000</pubDate>
      <link>https://dev.to/nerdflash28/ansible-for-beginners-1ffj</link>
      <guid>https://dev.to/nerdflash28/ansible-for-beginners-1ffj</guid>
      <description>&lt;h2&gt;
  
  
  What is Ansible?
&lt;/h2&gt;

&lt;p&gt;Ansible is a automation used to carry our repetitive task.&lt;br&gt;
ex. of these can be following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;provisioning&lt;/li&gt;
&lt;li&gt;Configuration Management&lt;/li&gt;
&lt;li&gt;Continout Devlivery&lt;/li&gt;
&lt;li&gt;Application Deployment&lt;/li&gt;
&lt;li&gt;Security Compliance&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Features of Ansible
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Simple&lt;/li&gt;
&lt;li&gt;Powerful&lt;/li&gt;
&lt;li&gt;Agentless&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Ansible configuration File
&lt;/h2&gt;

&lt;p&gt;Default location on linux:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# configuration file default path on linux&lt;/span&gt;
/etc/ansible/ansible.cfg
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sections of Configuration file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;[&lt;/span&gt;defaults]
&lt;span class="c"&gt;# default location for inventory files&lt;/span&gt;
inventory   &lt;span class="o"&gt;=&lt;/span&gt; /etc/ansible/hosts
log_path    &lt;span class="o"&gt;=&lt;/span&gt; /var/log/ansible.log

library     &lt;span class="o"&gt;=&lt;/span&gt; /usr/share/my_modules/
roles_path  &lt;span class="o"&gt;=&lt;/span&gt; /etc/ansible/roles
action_plugins  &lt;span class="o"&gt;=&lt;/span&gt; /usr/share/ansible/plugins/action

gathering  &lt;span class="o"&gt;=&lt;/span&gt; /implicit

&lt;span class="c"&gt;# SSH timeout &lt;/span&gt;
&lt;span class="nb"&gt;timeout&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; 10
forks  &lt;span class="o"&gt;=&lt;/span&gt; 5

&lt;span class="o"&gt;[&lt;/span&gt;inventory]

&lt;span class="o"&gt;[&lt;/span&gt;privilege_escalation]

&lt;span class="o"&gt;[&lt;/span&gt;paramiko_connection]

&lt;span class="o"&gt;[&lt;/span&gt;colors]

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  How to override Default config file
&lt;/h3&gt;

&lt;p&gt;in some complex scenarios we need to override current default config file path for different hosts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# using Environment variable &lt;/span&gt;
&lt;span class="nv"&gt;$ANSIBLE_CONFIG&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/opt/ansible-web.cfg ansible-playbook playbook.yml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Priority to be used for configuration files
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;file path which is used in environment variables&lt;/li&gt;
&lt;li&gt;directory where ansible paths are being running from&lt;/li&gt;
&lt;li&gt;config file which are stored in users home directory&lt;/li&gt;
&lt;li&gt;default Config file stored at default location&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Scenario 2&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;If there is a case we have to store value value of specific ansible playbook&lt;/em&gt;&lt;br&gt;
To achieve this we can set the specific Config variables for the specific ansible playbook&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# using variable defination&lt;/span&gt;
&lt;span class="nv"&gt;ANSIBLE_GATHERING&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;explicit anisble-playbook playbook.yml

&lt;span class="c"&gt;# using export method&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;ANSIBLE_GATHERING&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;explicit
ansible-playbook playbook.yml

&lt;span class="c"&gt;# by defining the variable in ansible config file&lt;/span&gt;
&lt;span class="c"&gt;# path : /opt/web-playbooks/ansible.cfg&lt;/span&gt;
gathering  &lt;span class="o"&gt;=&lt;/span&gt; explicit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How to check all the configuration for the file
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# this command will list all the configuration&lt;/span&gt;
ansible-config list

&lt;span class="c"&gt;# shows the current config file&lt;/span&gt;
ansible-config view

&lt;span class="c"&gt;# shows the current settings&lt;/span&gt;
ansible-config dump
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to Write YAML
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;YAML Stands for &lt;em&gt;Yet Another Markup Language&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;it's a scripting language used to created automation&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Thing to learn about yaml
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;How to store information using Key value pair&lt;/li&gt;
&lt;li&gt;How to create Arrays/List &lt;/li&gt;
&lt;li&gt;How to create Dictionary/Map&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  How to create Key value pair
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;to create key value pair we use colon sign
ex.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;key : value
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  How to create Array/List
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;to create array we use hypen(-) in front of and element
ex.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Fruits:
- Orange
- Apple
- Banana
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Ansible Inventory
&lt;/h3&gt;

&lt;p&gt;Ansible can manage multiple hosts, this is done using existing agents on the systems.&lt;br&gt;
For linux -&amp;gt; SSH&lt;br&gt;
for windows hosts -&amp;gt; Powershell Remoting&lt;/p&gt;

&lt;p&gt;To store these Hosts information we use inventory file, if custom inventory file is not created then ansible uses default invetory file stored at&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;/etc/ansible/hosts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ansible supports 2 types of formats for inventory files:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;INI format&lt;/li&gt;
&lt;li&gt;YAML format&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's look at the sample inventory file in INI format&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# list of number of servers&lt;/span&gt;
serv1.company.com
serv2.company.com
serv3.company.com

&lt;span class="c"&gt;# we can also group server by a common name&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt;mail]
mail_serv1.company.com
mail_serv2.company.com
mail_serv3.company.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Inventory Parameters
&lt;/h3&gt;

&lt;p&gt;to connect to a specific host we use can define multiple parameters.&lt;br&gt;
like:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;host alias&lt;/li&gt;
&lt;li&gt;ansible connection type&lt;/li&gt;
&lt;li&gt;ansible connection port&lt;/li&gt;
&lt;li&gt;ansible user &lt;/li&gt;
&lt;li&gt;ansible ssh password&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;example of ansible inventory parameters&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;web &lt;span class="nv"&gt;ansible_host&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;serv1.company.com &lt;span class="nv"&gt;ansible_connection&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;ssh &lt;span class="nv"&gt;ansible_user&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;root &lt;span class="nv"&gt;ansible_ssh_pass&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;root
window_host &lt;span class="nv"&gt;ansible_host&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;serv2.company.com &lt;span class="nv"&gt;ansible_connection&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;winrm &lt;span class="nv"&gt;ansible_user&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;administrator &lt;span class="nv"&gt;ansible_pass&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;admin

&lt;span class="c"&gt;# to interact with local machine&lt;/span&gt;
localhost &lt;span class="nv"&gt;ansible_connections&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;localhost
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Ansible Variables
&lt;/h3&gt;

&lt;p&gt;variable are used to store information in ansible.to use variables in Ansible we use jinja 2 templating.&lt;br&gt;
in this example all the codes and variables are defined in the same file.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;-
   name: Add DNS server to resolve.conf
   hosts: localhost
    vars:
       dns_server: 10.1.250.10
   tasks:
      - lineinfile:
          path: /etc/resolve.conf
          line: &lt;span class="s1"&gt;'nameserver {{ dns_server }}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to organise code in a better way we can define variables in separate file.&lt;br&gt;
ex. variables file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;http_port: 8081
snmp_port: 161-162
inter_ip_range: 192.0.2.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;example playbook file to use these variables&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;-
    name: Set firewall configuration 
    hosts: web
    tasks: 
    - firewalld:
         service: https
         permanent: &lt;span class="nb"&gt;true
         &lt;/span&gt;state: enabled
    - firewalld:
         port: &lt;span class="s1"&gt;'{{ http_port }}'&lt;/span&gt;/tcp
         permanent: &lt;span class="nb"&gt;true
         &lt;/span&gt;state: disabled
    - firewalld:
         port: &lt;span class="s1"&gt;'{{ snmp_port }}'&lt;/span&gt;/udp
         permanent: &lt;span class="nb"&gt;true
         &lt;/span&gt;state: disabled
    - firewalld:
         &lt;span class="nb"&gt;source&lt;/span&gt;: &lt;span class="s1"&gt;'{{ inter_ip_range }}'&lt;/span&gt;/24
         Zone: internal
         state: enabled
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Variable Types
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Number variables: which holds numeric values&lt;/li&gt;
&lt;li&gt;Boolean variables: which holds boolean values&lt;/li&gt;
&lt;li&gt;List variables: which holds list of items&lt;/li&gt;
&lt;li&gt;Dictionary variables&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Variable Precedence
&lt;/h4&gt;

&lt;p&gt;when defining variables, some methods of defining variables takes higher precedence then others.&lt;br&gt;
these precedence are defined from higher to lower given below:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Extra vars&lt;/li&gt;
&lt;li&gt;play vars&lt;/li&gt;
&lt;li&gt;Host vars&lt;/li&gt;
&lt;li&gt;Group vars&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;
  
  
  Explanantion:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;first ansible checks for the variables defined for a group, if there is a specific value defined for a specific hosts, then that value will 
overwrite the Groups Variables.&lt;/li&gt;
&lt;li&gt;similarly variables defined at playbook level with overwrite the variables defined at host level.&lt;/li&gt;
&lt;li&gt;at last variables defined with extra vars keywords will overwrite the playbook variables and have highest precedence.&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;
  
  
  Registering variables
&lt;/h4&gt;

&lt;p&gt;in some scenarios we want to pass some variables to other commands, this can be done by using registering the variables and storing them.&lt;br&gt;
ex. for the same&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;- shell: &lt;span class="nb"&gt;cat&lt;/span&gt; /etc/hosts
  register: results
- debug: 
    var: results
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Output of the variable depends on the type of module which has been used&lt;/li&gt;
&lt;li&gt;another way to view output of the debug module is to use -v parameter while running the playbook
ex.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ansible-playbook &lt;span class="nt"&gt;-i&lt;/span&gt; inventory playbook.yml &lt;span class="nt"&gt;-v&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Variables Scope
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;scope defines the accessiblity and visiblity of a variable to elements in the code.&lt;/li&gt;
&lt;li&gt;scope depends on the position how and where it has been defined in the given code&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;we'll talk about various types of scopes&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Host scope
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;host scope is available in the play which is running for the given host&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  2. Play scope
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;play scope is available in while the current play is running &lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  3. Global variables scope
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;scope of this variable is visible to all as it passed while running the playbook by the parameter knows as extra-vars
ex.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ansible-playbook playbook.yml &lt;span class="nt"&gt;--extra-vars&lt;/span&gt; &lt;span class="s2"&gt;"ntp_server=10.1.1.1"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Magic Variables
&lt;/h3&gt;

&lt;p&gt;magic variables are used to access the information of the other hosts.&lt;br&gt;
list of mostly used magic variables:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;hostvars : this return the parameters associated with given host&lt;/li&gt;
&lt;li&gt;groups: this return list of hosts which comes under the specified group&lt;/li&gt;
&lt;li&gt;group_names: this return all group names which are associate with specific host&lt;/li&gt;
&lt;li&gt;inventory_hostname: this gives out the name configured in the inventory file for the given host&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Ansible Playbooks
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;ansible playbooks are used to define what actions need to be performed&lt;/li&gt;
&lt;li&gt;playbooks are YAML scripts which defines the instructions which are needed to be performed&lt;/li&gt;
&lt;li&gt;tasks are the actions to be performed on the host 
ex. 1.1 execute a command  
   1.2 Run a script  
   1.3 Install a package 
   1.4 Shutdown/Restart a server  &lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>devops</category>
      <category>automation</category>
      <category>ansible</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>GIT for Beginners</title>
      <dc:creator>Linux_guy</dc:creator>
      <pubDate>Wed, 24 Apr 2024 04:30:17 +0000</pubDate>
      <link>https://dev.to/nerdflash28/git-for-beginners-2hb9</link>
      <guid>https://dev.to/nerdflash28/git-for-beginners-2hb9</guid>
      <description>&lt;p&gt;Hello Readers! welcome to this Git series of tutorials.&lt;/p&gt;

&lt;p&gt;I'll divide this series into different topics, so please hang in there.&lt;/p&gt;

&lt;p&gt;For the starting I'll start with basics!  &lt;/p&gt;




&lt;h2&gt;
  
  
  What is a GIT
&lt;/h2&gt;

&lt;p&gt;Git is a version Control Software which is used to maintain change history of a codebase.  &lt;/p&gt;




&lt;h2&gt;
  
  
  What is a GitHub
&lt;/h2&gt;

&lt;p&gt;GitHub is a online website which is used to store all these git commit histories.  &lt;/p&gt;




&lt;h2&gt;
  
  
  Install Git
&lt;/h2&gt;

&lt;p&gt;To install git on linux host we can use these following commands.&lt;br&gt;
Note: Please refer to the official website if case of any errors!!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# for debian Destros&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt update
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;git-all
&lt;span class="c"&gt;# for RHEL Destros&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;yum update
&lt;span class="nb"&gt;sudo &lt;/span&gt;yum &lt;span class="nb"&gt;install &lt;/span&gt;git-all
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Confirm successful installation of the Git?
&lt;/h2&gt;

&lt;p&gt;to ensure git is available on the local machine, we can check it's version by using the command!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  How to initialize a Git repository on the local machine!!?
&lt;/h2&gt;

&lt;p&gt;to instalize a git repository on the local machine.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Step 1 : navigate to the given directory&lt;/li&gt;
&lt;li&gt;Step 2 : initialize the git repository
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Gitignore
&lt;/h2&gt;

&lt;p&gt;sometimes there are certain files which we dont want to upload on remote repository. to make this happen, we need to add the name of that file into .gitignore file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &amp;lt;filename&amp;gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; .gitignore
git add .gitignore
git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"Git ignore file added"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Manage Git branches
&lt;/h1&gt;

&lt;h3&gt;
  
  
  How to create a new branch?
&lt;/h3&gt;

&lt;p&gt;branches contains sequence of commits in git.&lt;br&gt;
we can create or delete as much as branch as we required.&lt;/p&gt;

&lt;p&gt;To create new branch&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# this command will create a new branch with given branch name&lt;/span&gt;
git branch &amp;lt;branch-name&amp;gt;
&lt;span class="c"&gt;# this command will create a new branch and switch to that branch&lt;/span&gt;
git checkout &lt;span class="nt"&gt;-b&lt;/span&gt; &amp;lt;branch-name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Rename a branch
&lt;/h3&gt;

&lt;p&gt;to rename a branch in git, use the given command!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# first switch to the main branch&lt;/span&gt;
git checkout &amp;lt;branch-name&amp;gt;

&lt;span class="c"&gt;# second use the following command to change the branch name&lt;/span&gt;
git branch &lt;span class="nt"&gt;-m&lt;/span&gt; &amp;lt;old-name&amp;gt; &amp;lt;new-name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Delete a branch
&lt;/h3&gt;

&lt;p&gt;to delete a branch first we need to switch to other branch reason being we can't delete currently active branch&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# change to other branch for example master.&lt;/span&gt;
git checkout master
&lt;span class="c"&gt;# then run the delete branch command&lt;/span&gt;
git branch &lt;span class="nt"&gt;-d&lt;/span&gt; &amp;lt;branch-name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Watch the commit history
&lt;/h3&gt;

&lt;p&gt;to watch the commit history we have git log command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Add Email and name
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;to add email and username we use the following commands
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# to add username we use the command&lt;/span&gt;
git config &lt;span class="nt"&gt;--global&lt;/span&gt; user.name &lt;span class="s2"&gt;"Full Name"&lt;/span&gt;
&lt;span class="c"&gt;# to add email we use the command&lt;/span&gt;
git config &lt;span class="nt"&gt;--global&lt;/span&gt; user.email &lt;span class="s2"&gt;"Email"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Store git credentials in linux
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;to add git credentials we use credential.helper utility&lt;/li&gt;
&lt;li&gt;Step 1:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git config &lt;span class="nt"&gt;--global&lt;/span&gt; credential.helper store
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Step 2:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git add &lt;span class="nb"&gt;.&lt;/span&gt;
git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"message"&lt;/span&gt;
git push origin master
&lt;span class="c"&gt;# enter username &amp;amp; password&lt;/span&gt;
username : github_username
password : Personal-Access-Token
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;to get personal tokken from github

&lt;ul&gt;
&lt;li&gt;go to user settings&lt;/li&gt;
&lt;li&gt;click on developer settings&lt;/li&gt;
&lt;li&gt;click on generate classic token&lt;/li&gt;
&lt;li&gt;select all the repo options and copy token in git CLI &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h3&gt;
  
  
  Setup SSH keys
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;navigate to ssh directory in home

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;cd ~/.ssh/&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Create ssh keys

&lt;ul&gt;
&lt;li&gt;For Legacy Systems:&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ssh-keygen -o -t rsa -C "Email@address"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;For modern systems which support ED25519 algorithm:&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ssh-keygen -t ed25519 -C "your_email@example.com"&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;add ssh key to ssh-agent

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;eval "$(ssh-agent -s)"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ssh-add ~/.ssh/&amp;lt;path&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;copy ssh-key.pub file to github to access private repos&lt;/li&gt;

&lt;li&gt;check ssh connection with git using this command

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;ssh -T git@github.com&lt;/code&gt; &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

</description>
      <category>github</category>
      <category>git</category>
      <category>devops</category>
      <category>development</category>
    </item>
  </channel>
</rss>
