The os module in Python

The os module in Python provides a portable way to interact with the operating system, including Linux. While it doesn’t cover every aspect of Linux system administration, it offers functionalities for basic operations like file and directory manipulation, process management, and environment variables. Below are some of the key functions and classes in the os module:

  1. File and Directory Operations:
    • os.getcwd(): Get the current working directory.
    • os.chdir(path): Change the current working directory to the specified path.
    • os.listdir(path='.'): Return a list of the entries in the directory given by path.
    • os.mkdir(path): Create a directory named path.
    • os.makedirs(path): Recursive directory creation function.
    • os.remove(path): Remove (delete) the file path.
    • os.rmdir(path): Remove (delete) the directory path.
  2. Process Management:
    • os.system(command): Execute the command in a subshell.
    • os.spawn*(): Functions for spawning a new process.
    • os.fork(): Fork a child process.
    • os.kill(pid, sig): Send a signal to the process pid.
  3. Environment Variables:
    • os.environ: Dictionary containing the environment variables.
    • os.getenv(var, default=None): Get an environment variable, optionally returning a default value if the variable is not set.
  4. Miscellaneous:
    • os.path: Submodule for common pathname manipulations.
    • os.name: String representing the current operating system.
    • os.utime(path, times=None): Set the access and modified times of the file specified by path.
  5. Permissions:
    • os.chmod(path, mode): Change the mode (permissions) of path to the numeric mode.
    • os.access(path, mode): Check if a user has access to a file.

Remember, the os module provides basic functionalities. For more advanced operations, you might need to use other modules like subprocess, shutil, or os.path. Additionally, for system administration tasks on Linux, modules like subprocess, sys, shutil, socket, multiprocessing, and os.path are often used in conjunction with os.

Find and replace text within a file on Linux

To find and replace text within a file on Linux, you can use various command-line tools such as sed, awk, or even grep combined with sed. Here’s how you can do it using sed, which is one of the most commonly used tools for this purpose:

sed -i 's/old_text/new_text/g' filename

Explanation:

  • -i: This option edits files in place. If you want to create a backup of the original file, you can use -i.bak instead, which will create a backup with the extension .bak.
  • 's/old_text/new_text/g': This is the substitution command in sed. It tells sed to substitute old_text with new_text globally (i.e., all occurrences).
  • filename: Replace this with the name of the file you want to modify.

For example, if you want to replace all occurrences of “hello” with “world” in a file named example.txt, you would use:

sed -i 's/hello/world/g' example.txt

Make sure to use caution when using the -i option, as it directly modifies the file. Always double-check the results to ensure they are as expected. If you want to preview the changes before actually modifying the file, you can omit the -i option and redirect the output to a different file:

sed 's/old_text/new_text/g' filename > new_filename

This command will perform the replacement and save the modified content to a new file called new_filename, leaving the original file unchanged.

UNIX-Linux: How to identify what type of Unix or Linux System we are using

To identify the type of Unix or Linux system we are using run:

$ uname -a

Linux Example:

[root@redhat9-01 ~]# uname -a
Linux redhat9-01 5.14.0-284.30.1.el9_2.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Aug 25 09:13:12 EDT 2023 x86_64 x86_64 x86_64 GNU/Linux
[root@redhat9-01 ~]#

AIX Example:

# uname -a

AIX crnimx01 1 7 000482ADD600

#

Linux Foundation Collaboration Summit 2014

Long time since I wanted to start this new blog, in the last five years I have experienced a few professional transitions, changing employer, from a Software Engineering role to System Administrator role, from developing and/or testing software for “Legacy” operating system and proprietary software to infrastructure Services Delivery using large scale UNIX and Linux customer’s environments. In the past trying to imagine what challenges Systems Administrators have to develop systems management software to actually know it from the first hand. Now in the last year got a new job role working on process, procedures and tools improvements and knowledge management activities for UNIX and Linux Infrastructure Delivery.

Some of my stronger skills are on operating systems running on Power Systems (AIX and IBM i). Since 2006 until 2013 I have participated as speaker and attendee on Power Systems technical conferences sharing experiences using “i” and AIX operating systems with Power Systems Community.

However we can see how business have been migrating workloads from “Legacy” operating systems to Linux successfully. For that reason one objectives for this year was to attend to a Linux community conference to get deeper skills on Linux. Late February I got an invitation from Linux Foundation staff to attend to Collaboration Summit 2014 that was an exclusive invitation-only event in Napa, CA, so I decided to take the opportunity to attend to tree day summit and also attend to an one day KVM workshop.

Knowing that open source linux virtualization technologies are getting mature, I decided to take KVM workshop and some of the Open Virtualization Sessions on the summit.

Kernel-based Virtual Machine (KVM) is an open source hypervisor that provides enterprise-class performance, scalability and security to run Windows and Linux workloads.

To get started with KVM I used my work business class laptop, to avoid changing the internal encrypted Windows disk setup, I used an external USB 3 portable disk to perform a CentOS 6.5 Linux Install, selecting “Virtualization Tools” and “Virtualization Platform” group installs. Other popular distribution will also include these virtualization tools like Redhat, Fedora or Ubuntu.

For detailed instructions you can consult Kernel Based Virtual Machine project site.

Performance was very good during the workshop, running the main Linux CentOS image with KVM hypervisor installed, a small custom linux distribution provided by the Instructor as a guest operating system and from my side I installed a Desktop Debian 7.4 as guest operating system to put more stress workload on the setup. That was just one day testing, but I am happy with the initial results. In the future I will be doing more prove of concepts installs to push KVM technology to the limit and continue learning about it.

During the Linux Collaboration Summit Sessions – Open Virtualization track, the message I got from speakers and other attendees was that KVM is ready for business to virtualize Linux workloads in the datacenter.

Some Open Virtualization technologies with more active development that are using KVM foundation includes:

In addition to this, other virtualization technologies also mentioned on the Summit are:

That give us a lot of open source virtualization technologies to review, use and learn in the future.

In addition to the Open Virtualization staff I can mention that first day keynotes provided a very good overview about Linux development trends and good understanding on how open source community collaborate.

Others interesting sessions I was able to attend where about how to go from an Idea to a Corporate Sponsored Open Source Project and Open Source Compliance, between others.

In summary Linux Collaboration Summit was a great way to understand how Linux Kernel and Open Source community collaborate and I look forward to collaborate with them in the future.