<?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: SHIVAJEE</title>
    <description>The latest articles on DEV Community by SHIVAJEE (@shivajee98).</description>
    <link>https://dev.to/shivajee98</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%2F1461002%2Fac10e3a6-3c68-4de1-804b-52c2ea8304ae.jpeg</url>
      <title>DEV Community: SHIVAJEE</title>
      <link>https://dev.to/shivajee98</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shivajee98"/>
    <language>en</language>
    <item>
      <title>Day 1 of Learning Scripting</title>
      <dc:creator>SHIVAJEE</dc:creator>
      <pubDate>Tue, 26 Nov 2024 19:26:14 +0000</pubDate>
      <link>https://dev.to/shivajee98/day-1-of-learning-scripting-14m5</link>
      <guid>https://dev.to/shivajee98/day-1-of-learning-scripting-14m5</guid>
      <description>&lt;p&gt;Hi, folks from today 26th Nov 2024, I have started learning scripting to become a Highly Skilled DevOps engineer.&lt;br&gt;
Today I learned the basic workflow of scripting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Basics of Scripting:-&lt;/strong&gt; Scripting is used to automate the repetitive or routine works which is engineers often face in DevOps culture.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Applications of Scripting:-&lt;/strong&gt; So, what are the applications of scripting?&lt;br&gt;
It is commonly used in &lt;strong&gt;File Management&lt;/strong&gt; such as renaming, organising, deleting, etc.&lt;br&gt;
Other Application involves system monitoring, automation such as fetching APIs, backups etc.&lt;br&gt;
&lt;strong&gt;3. Steps Involved to Automate a task:-&lt;/strong&gt; &lt;br&gt;
&lt;strong&gt;(i)&lt;/strong&gt; The first step is to identify the problem and break it down into smaller pieces. eg.&lt;br&gt;
"Renaming files" -&amp;gt; first list all the files -&amp;gt; Modify the names -&amp;gt; save with new names.&lt;br&gt;
&lt;strong&gt;(ii)&lt;/strong&gt; Choose a language, most commonly used in industries is Python or BASH, as they directly run without compilation.&lt;br&gt;
&lt;strong&gt;(iii)&lt;/strong&gt; Design the Workflow- eg. File renaming involves:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Identify the directory&lt;/li&gt;
&lt;li&gt;Loop through files.&lt;/li&gt;
&lt;li&gt;Rename files using a pattern.
&lt;strong&gt;(iv)&lt;/strong&gt; Error Handling- Always ensure to handle issues like:-&lt;/li&gt;
&lt;li&gt;Missing files.&lt;/li&gt;
&lt;li&gt;Permission Denied.&lt;/li&gt;
&lt;li&gt;Invalid input.
&lt;strong&gt;4. Core Concepts involved in Scripting:-&lt;/strong&gt; 
&lt;u&gt;&lt;strong&gt;Variables:&lt;/strong&gt;&lt;/u&gt; Store data for reusability. eg:- directory = "./files"
&lt;u&gt;&lt;strong&gt;Loops:&lt;/strong&gt;&lt;/u&gt; Automate repetitive tasks. eg:-
_for file in os.listdir(directory):
 print(file)
_
&lt;u&gt;&lt;strong&gt;Functions:&lt;/strong&gt;&lt;/u&gt; Habit to modularise your script and keep it reusable. eg:- 
_def rename_file(old_name, new_name):
 os.rename(old_name, new_name)
_
&lt;u&gt;&lt;strong&gt;File Handling:&lt;/strong&gt;&lt;/u&gt; Understanding of file handling is cruicial in Scripting. eg: 
_with open("abc.txt", "r") as f:
 data = f.read()
_
&lt;strong&gt;5. Common Tools and Libraries:-&lt;/strong&gt; 
&lt;u&gt;&lt;strong&gt;Python Libraries&lt;/strong&gt;&lt;/u&gt;
&lt;strong&gt;os:-&lt;/strong&gt; Interacts with the operating system.
&lt;strong&gt;shutil:-&lt;/strong&gt; Advanced file operations.
&lt;strong&gt;requests:-&lt;/strong&gt; Fetches data from API.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;Linux/BASH Commands&lt;/strong&gt;&lt;/u&gt;&lt;br&gt;
&lt;strong&gt;ls:-&lt;/strong&gt; List Directory.&lt;br&gt;
&lt;strong&gt;mv:-&lt;/strong&gt; Rename or move files.&lt;br&gt;
&lt;strong&gt;grep:-&lt;/strong&gt; Searches within file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;&lt;em&gt;Automate file renaming in Python:&lt;/em&gt;&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To automate file renaming using Python, you can use the os and the glob modules.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisite:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Python(3.x) should installed in the system.&lt;/li&gt;
&lt;li&gt;Familiarity with basic Python concepts.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Steps to Automate File Renaming:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Identify the Directory:&lt;/strong&gt; The first step is to specify the directory where your files are located. You need to be able to access and modify these files programmatically.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;List All Files:&lt;/strong&gt; Use Python’s os or glob module to list the files in the target directory. You can filter files based on extension if needed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Define the Renaming Pattern:&lt;/strong&gt; Based on your needs, you can define a renaming pattern. This could involve adding prefixes, suffixes, or even modifying filenames based on a regex pattern.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rename Files:&lt;/strong&gt; Using the os.rename() function, rename each file based on your defined pattern. Ensure that you handle potential errors (e.g., file already exists, permission issues, etc.).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example: Renaming Files Based on a Pattern&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Below is a simple Python script to rename files in a directory, appending a prefix and suffix:&lt;/p&gt;

&lt;p&gt;**_&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os

def rename_files_in_directory(directory, prefix, suffix, extension):
    # Change working directory to the target folder
    os.chdir(directory)

    # Get all files with the given extension
    for filename in os.listdir(directory):
        # Check if the file has the specified extension
        if filename.endswith(extension):
            # Define the new file name based on the prefix and suffix
            new_name = f"{prefix}_{filename[:-len(extension)]}_{suffix}{extension}"
            # Rename the file
            os.rename(filename, new_name)
            print(f"Renamed: {filename} -&amp;gt; {new_name}")
directory = r"C:\path\to\your\folder"  # Change this to your directory path
prefix = "new"  # Example: Adds "new" as prefix to filenames
suffix = "v1"  # Example: Adds "v1" as suffix to filenames
extension = ".txt"  # Modify this to match the file extension you're working with

rename_files_in_directory(directory, prefix, suffix, extension)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;`&lt;br&gt;
_**&lt;br&gt;
&lt;strong&gt;Explanation of the Script:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;os.chdir(directory):&lt;/strong&gt; Changes the working directory to the specified path so you can access the files.&lt;br&gt;
&lt;strong&gt;os.listdir(directory):&lt;/strong&gt; Lists all the files in the directory.&lt;br&gt;
&lt;strong&gt;filename.endswith(extension):&lt;/strong&gt; Filters files based on their extension (e.g., .txt).&lt;br&gt;
os.rename(filename, new_name): Renames the file with the new name.&lt;/p&gt;

&lt;p&gt;So, that's all for today, I will be posting regular updates about my Learning journey of Scripting.&lt;/p&gt;

&lt;p&gt;Make sure to follow.&lt;/p&gt;

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