Tuesday, June 4, 2019
Accessing Remote desktop using Python & performing some operations in Remote server
Monday, April 8, 2019
Python OS module in detail with examples & syntax
__author__ = "Narendra Boyina"
"""
The OS module allows us to interact with the underlying operating system in several different ways.
- Navigate the file system
- Get file information
- Look up and change the environment variables- Move files around
- Many moreTo begin, import the os module. This is a built in module,
no third party modules need to be installed.
"""
import os,time
from datetime import datetime
"""dir() will display all of the methods and attributes of os module"""
# print(dir(os))
"""To get current working directory"""
# path = os.getcwd() # @Answer /home/narendrab/Python/My Practice# print(path)
"""To Change directory, from one path to another path. this requires a path to change to
Syntax ==> os.chdir(path)"""
# path = "/home/narendrab/pytest"# os.chdir(path)
# print(os.getcwd()) # will display /home/narendrab/pytest
# To print list of directories & files names in provided path,
# Syntax ==> os.listdir(path) but by default path is in the current directory
# print(os.listdir("/home/narendrab/Python/"))
# To create a new directory. Syntax ==> mkdir("Directory/folder name")
# os.mkdir("Narendra")
# if you want to create multiple directories at once
# (directory inside directory inside directory ..ectc)
# Syntax ==> os.makedirs("directory_Name/Sub_directory_1/Sub_directory_2")
# os.makedirs("Narendra/Bhagyasree/Raj")
# Remove single directory/ multiple directories
# Syntax ==> os.rmdir(file)
# Dirs created ==>path changed ==>printed path after changed==> came back one stepDir
# path = "Narendra/Bhagyasree/Raj"
# os.makedirs(path)
# creation of directories/sub-dir
# print("Directories were created just check in PWD/(Pycharm tree structure)")
## os.chdir(path) # enter in to the directories created just now
# print(os.getcwd()) # will display PWD
## os.chdir("/home/narendrab/Python/My Practice/Narendra/Bhagyasree")
# come back without giving path
# os.rmdir("Raj") # Removes the last directory in the path
# print(os.getcwd())
# # Syntax ==> removedirs(file) # Removes intermediate directories if specified
# create a file with write mode
# fp=open("test.txt", "w+")
# fp.write("Hi,\n This file was created just for execution purpose")
# fp.close()
# # Rename a file or folder
# os.rename("test.txt","Narendra.txt") # This renames text.txt to Narendra.txt
## # Look at info about files# print(os.stat("Narendra.txt"))
# # Useful stat results: st_size (bytes), st_mtime (time stamp)
# print(os.stat("Narendra.txt").st_size) # display size of the file
# print(os.stat("Narendra.txt").st_mtime) # display the last modification time of the file (but can't understand by user)
# last_mod_time = os.stat("Narendra.txt").st_mtime
# print(datetime.fromtimestamp(last_mod_time)) # Now user can understand recently file modified time
# # To see entire directory tree and files within
# os.walk is a generator that yields a tuple of 3 values as it walks the directory tree
# path = os.getcwd()
# # for dirpath, dirnames,filenames in os.walk(path):
# print("current path : ",dirpath)
# print("Directories : ",dirnames)
# print("Files: ",filenames)
# print("***************************************")
# # # This is useful for locating a file that you can’t remember where it was
# If you had a web app, and you wanted to keep track of the file info within a certain directory structure,
# then you could to thru the os.walk method and go thru all files and folders and collect file information.
# # Access home directory location by grabbing home environment variable
# X = os.environ.get("HOME") # Returns a home directory path of the user
# print(X) # /home/narendrab
# # # # # To properly join two files together use os.path.join(). & will be inserted one slash
# # file_path = os.path.join(os.environ.get("HOME"), "test.txt")
# # # # the benefit of os.path.join, is it takes the guess work out of inserting a slash
# # print(file_path) # /home/narendrab/test.txte as a tuple
## os.path.exists("/tmp/test.txt")# # returns a boolean
## os.path.isdir("/tmp/test.txt")# # returns False
## os.path.isfile("/tmp/test.txt")# # returns True
## os.path.splitext("/tmp/test.txt")# # Splits file route of the path and the extension
# # returns ("/tmp/test", ".txt")# # This is alot easier than parsing out the extension.
Splitting off and taking the first value is much better.
# # Very useful for file manipulation
# os.path has other useful methods
## os.path.basename# # This will grab filename of any path we are working on
## os.path.dirname("/tmp/test.txt")# # returns the directory /tmp
## os.path.split("/tmp/test.txt")# # returns both the directory and the fil
"""We can find out, From the root path(provided by user)
what are the files (.txt/.py/.doc)available in each sub-folders. """
import os
# path = "/home/narendrab/Python"
# for root,subdir,files in os.walk(path):
# # we can find-out .py files available in each subdirectory from provided root directory
## for file in files:
# if file.endswith(".txt"):
# print("FileName ==> {}".format(os.path.join(root,file)))
## # same like above we can find-out .py files available in each subdirectory from provided root directory
## for file in files:
# if file.endswith(".py"):
# print("FileName --> %s" %(os.path.join(root,file)))
def get_files_based_(path, extension):
for root, subdir, files in os.walk(path):
for file in files:
if file.endswith(extension):
print("FileName %s" % (os.path.join(root, file)))
# get_files_based_(path, ".txt")
Request: If you find this information useful, please provide your valuable comments.
Linux basic commands and concepts
List of basic Linux commands
mkdir ==> The mkdir command will allow you to create directory.
Example: mkdir Narendra ==> This command will create a directory called "Narendra".
rmdir: The rmdir command will delete an empty directory.
- To navigate into the root directory, use cd / ↵
- To navigate to your home directory, use cd ↵ or cd ~ ↵
- To navigate up/back one directory level, use cd .. ↵
- To navigate to the previous directory (or before), use "cd - ↵
- To navigate through multiple levels of a directory at once, specify the full directory path that you want to go to. For example, use, "cd home/Narendra/test_scripts/" to go directly to the test_scripts subdirectory.
- As another example, "cd ~/Desktop" will move you to the Desktop subdirectory inside your home directory.
Files related Linux commands
touch file_name.txt ==> create a single file in pwd (present working directory)touch file_name1.txt file_name_2.txt ==> create multiple files in pwd
cat file_name1.txt ==> display the content of the file
find . -name "narendra.txt" ==> Find a file (narendra.txt /any extension-related files) in current and sub-directories
find /home -name *.jpg ==>
Look for all .jpg files (any extension-related files) in the /home and directories below it.
ls -l ==> Displays available files & folders available in pwd with permissions, sizes, date & time
diff ==> Compare the contents of two files line by line
Small clarification: pwd will display the file's path, but readlink -f file_name will display the path name and the file name at a time.
è What is chmod 777?
chmod 777 means granting all permissions to read, write, and execute any file/directory by any user of the system
General Linux commands
Upgrade RHEL OS
In Ubuntu OS --> sudo apt install openjdk-17-jdk
In RHEL OS --> sudo yum install java-17-openjdk-devel
or
In RHEL OS --> sudo rpm -ivh jdk-17_linux-aarch64_bin.rpm
If multiple package (java/python/) versions are available then set a specific version as the default version among multiple versions
Ex: 1
sudo update-alternatives --config java
Enter to keep the current selection[+], or type selection number: 1/2/3/4
Ex: 2
sudo update-alternatives --config python
Enter to keep the current selection[+], or type selection number: 1/2/3/4
ssh username@<IP address> press Enter, enter the password of the remote machine
ssh narendra@10.xx.xx.xx <Enter>
scp file_name.zip root@10.xx.41.xxx:/home/
How to copy a file within the same machine?
cp<space> file_name_to_be_copied<space>path_of _the_directory (where to copy)
How to copy a directory within the same machine?
cp<space> directory_name_to_be_copied<space> -r <space>path_of _the_directory (where to copy)
==============
Syntax: watch [OPTIONS] COMMAND
watch
the command will temporarily clear all of the terminal content and run the provided command at regular intervals. When used without any option watch
will run the specified command every two seconds.Example: 1
In Terminal==> watch date <Enter> by default, every 2 seconds date will update.
==> watch<space> -n <space>5 <space>date (for 5 seconds date will update)
==> watch <space>-n <space>5 <space>-t <space> date
Note: Ctrl+c to exit from it.
Example: 2
watch python log_parser.py
log_parser python will execute continuously.
Linux Keyboard Shortcutsctrl+alt+t ==> To open a terminal in Linux-related OS
ctrl+ Alt +t ==> To open a new terminal window
ctrl+ Shift +t ==> To open the terminal tab
ctrl + a ==> Move the cursor to the beginning of the line
Ctrl + e ==> Move the cursor to the end of the line
ctrl + c ==> To stop/come out / break the currently executing command (In the Terminal)
(if you have typed a long/short command in the terminal, but you don't want to run/ erase it, then you can use Ctrl+C in the Terminal)
ctrl +d ==> to close the current terminal (or) exit ==> logout of the current session.
ctrl +l ==> will clear the terminal screen (or) clear command will clear the terminal screen
ctrl +r ==> Recall the last command that matches the provided characters (handy command)
TAB ==> autofill typing (while typing directory names/file names /commands type starting 2 letters then type TAB )
!! ==> Repeat the last command
ctrl + k ==> cut part of the line after the cursor & add it to the clipboard
ctrl + u ==> cut part of the line before the cursor & add it to the clipboard
ctrl + w ==> cut one word before the cursor & add it to the clipboard
ctrl + y ==> paste from clipboard
ctrl + o ==> Run the previously recalled command (for some Linux flavours only)
ctrl+alt+t ==> To open a terminal in Linux-related OS
ctrl+ Alt +t ==> To open a new terminal window
ctrl+ Shift +t ==> To open the terminal tab
ctrl + a ==> Move the cursor to the beginning of the line
Ctrl + e ==> Move the cursor to the end of the line
ctrl + c ==> To stop/come out / break the currently executing command (In the Terminal)
(if you have typed a long/short command in the terminal, but you don't want to run/ erase it, then you can use Ctrl+C in the Terminal)
ctrl +d ==> to close the current terminal (or) exit ==> logout of the current session.
ctrl +l ==> will clear the terminal screen (or) clear command will clear the terminal screen
ctrl +r ==> Recall the last command that matches the provided characters (handy command)
TAB ==> autofill typing (while typing directory names/file names /commands type starting 2 letters then type TAB )
!! ==> Repeat the last command
ctrl + k ==> cut part of the line after the cursor & add it to the clipboard
ctrl + u ==> cut part of the line before the cursor & add it to the clipboard
ctrl + w ==> cut one word before the cursor & add it to the clipboard
ctrl + y ==> paste from clipboard
ctrl + o ==> Run the previously recalled command (for some Linux flavours only)
How to change hostname in Linux-based machines/servers
Screen tool
screen
tool is a terminal multiplexer. It allows you to create, manage, and maintain
multiple terminal sessions within a single window or SSH session.
commands to use the screen tool:
====================
python3 -V(capital V) ==> Displays python 3.X.X version installed on your machine.
What is a log file?
A log file is a computer-generated data file that contains information about usage patterns,
activities, and operations within an operating system, application, server or another device.
Log files show whether resources are performing properly and optimally.
What is the purpose of a log file?
Log files are used to record events or activities that occur within a computer system/ application/ program.
Log files serve as a detailed record of what has happened within a system,
which can be used for troubleshooting problems or investigating security incidents.
Example1: let’s assume server crash happened!
then "Log files are used to investigate what happened before server crashed"
Note: In Linux based OS, all log files will be saved at "/var/log/" path
Example2: After system turn ON, if you have faced any problem,
then we need to check in boot.log (available at "/var/log/" )