Skip to main content

Posts

Showing posts with the label Bash script

Use tar command to backup in Linux

tar or tape archive is a basic tool that every Linux user should know.

Its syntax is quite different from the traditional copy or move command.

Since tar works from a 'current working directory' perspective.

Traditional syntax for copy or move is source then the destination.

In tar, the syntax is the filename or the path where the backup will be save and the source or the folder to be backed up or archived.

Typing: man tar (at the terminal shows the following)

tar -cf archive.tar foo bar
# Create archive.tar from files foo and bar.

Basically, it's like the destination where the file will be saved and the source or the files that will be archived.

Here's a simple bash script that will append the time and date the tar file was executed to the filename of the tar file.

If the bash script is automated via cron, the filename of the tar file will include the date and time the cron executes the script.


#script begins

#!/bin/bash
NOW=$(date +%Y%m%d%H%M)
echo $NOW
tar -Pczvf   /mnt/o…

PowerShell get folder capacity

Get the folder capacity using PowerShell via the old-school method.
Old-school since we will utilize the native “dir” command, just like the good old days of DOS.
PowerShell dir command output is not the same with the native windows system32 dir command line just like in DOS system.
The output will be in bytes since it is from the command line but of course, if you are command line junkie it will be easy to read even if the capacity is written in bytes.
First, fire up notepad write the dir command plus the full folder path of the specified folder in which you like to monitor or want to know the capacity.
 dir “c:\users\dmusic\music folder 001”
The path is enclosed in quotes since the folder has spaces and save the notepad as a batch file with “.bat” extension.
Open PowerShell command line window or PowerShell ISE and type the following command:
#change the path to where the batch file was saved
$folder_base_cap = d:\read_folder_cap.bat 
$ folder_base_cap | Select-String "bytes&qu…

PowerShell disable USB ports

Disabling USB ports using PowerShell by modifying registry values.
How to user PowerShell to modify an existing value in Windows Registry?
PowerShell code below modifies the registry value and disable USB ports in Windows.
The code must be run in an elevated mode to modify the registry.
Here's the code:
Modify the registry key value using PowerShell:
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\USBSTOR\" -Name "start" -Value 4
To enable the USB ports, change the value to 3:
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\USBSTOR\" -Name "start" -Value 3
Disabling USB ports is quite annoying of course, but if security is a concern then I guess it is justifiable especially computers that can easily be accessed by unwanted guests or individuals.

It will also protect the network or computers from getting malware's or viruses that is transferred or copied via USB storage.



Cheers! Till next time.


==============…

Bash check if folder exists

Simple bash script for beginners trying to get their hands wet on bash scripting.
The code below will ask for input and the input should be a string or a folder name.
The script will just do a basic ls and if the folder name is found it will show “The folder is found” or else the next echo statement is displayed.
Instead of simply displaying an echo command a function or another script can be called to execute a process further.
#!/bin/bash # This script will ask for a string or folder name and check whether it is present or not
echo "Enter folder name, followed by [ENTER]:"
read folder_name
ls | grep ^$folder_name && echo "The folder is found" || echo "The folder cannot be found" #Script ends
It’s a one liner evaluation, some sort an if then else in other programming languages, the && statement is executed if the evaluation is true, and if the evaluation is false the || is executed.
The read folder_name line, is the statement that will hold the inp…

PowerShell validate list of email addresses

Validate list of email addresses in a text file and check which email is a valid email and which one has the incorrect format.
PowerShell code below can easily check or validate incorrect email, output will show true if valid email and false if the email is not valid.
To check list of valid emails PowerShell utilizes regex. This is useful to check typo errors when sending out mass email or a list of emails which has not yet been verified.

But of course, the code will not be able to check whether the emails are active or not.

#===============================
$reader = [System.IO.File]::OpenText("c:\all_emails.txt") #get-content can also be used
while($null -ne ($line = $reader.ReadLine())) {
    #$line     $regx="[a-z0-9!#\$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#\$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"
    [regex]::Match($line, $regx, "IgnoreCase ")  |  format-Table success, value -AutoSize }

#========…

How to escape in PowerShell

In bash script the backslash is use as escape sequence and even in Java programming.

But what's the equivalent of bash escape sequence in PowerShell?

Grave accent or back tick, which is found at the top left corner of the keybord below the "esc" key on some keyboard, This character "`" is the escape in PowerShell.

For example:

Write-host "This line is above `r`n and  `r`n this line is below"

Output is:
This line is above
 and
 this line is below

r is equal to carriage return character putting a backtick before r, `r tells PowerShell that r is not a character but a carriage return
n is a new line character putting a backtick before n, `n tells PowerShell that n is a new line character.

Another example:

$ sign in PowerShell is to indicate a variable

But what if you need to display a dollar sign as a character and not as a special character to declare a variable. Then we need to escape the dollar sign by prefixing a backtick before the character.

Exampl…

Linux basic device utility tools

Knowing the devices in your computer is quite important in order to check what devices are installed or running in the computer
Getting to use Linux or starting to use it may seem daunting for others.
But Linux is awesome aside that its free and open source, in order to get used to it then you have to use it.
How to check installed USB drives or external USB devices in Linux?
How to check what graphic chipset or video chipset your computer is using in Linux?
How to check the audio chipset in your Linux system?
All these questions will pop-up once you start using Linux.
Or how would to check whether the inserted USB thumbdrive or an external USB drive is detected or not.
Well, some Linux distro provides a GUI interface to do it.
But the very basic way to check is to open via the Terminal window.
Or basically the command line in Linux.
ls - or list directory contents is the most basic way to learn on command line
ls will list the files and directories
To issue the commands below, open …

PowerShell create active directory user

Create bulk users for Active Directory in PowerShell with the help of a text file or a CSV file can be done easily. But the code below is to create a single user account. The code below can easily be tweak by using for loop command and reading an external file where the data will be imported to PowerShell variables.
When I was creating this script, I encountered an error that says:
new-aduser attribute value was not in the acceptable range
Basically, the error is good and is useful as well. Since it already gives a hint that one of the value is not acceptable.
And on this case, it was the Country variable, I was writing the whole word “Philippines” when I changed it to PH and the error went away.
Here’s the code: #------Code Start--------  $Domain="@gui.local" $Firstname="Yorem" $Lastname="Goas" $Email="yorem.goas@gui.com" $Password="Wh@t!s1t0k" $Username=$Firstname +"."+ $Lastname $UPN=$Username+$Domain $DisplayName= "$Firstname…

PowerShell remove line feed

Line feed is useful in some ways like formatting lines so it will be easy to read if there’s a line in between the other line.
But there are also instances that line feed might be annoying or simply we need to get rid of it since it’s not desired on the output.
There instances that Out-string parameter in PowerShell will append a line feed on its output.
Or if you take the input or output from other sources but want to remove the line feed, PowerShell can remove the line feed without any hassle.
Code below is using PowerShell to remove line feed from an input, the input could either be a text file or a string pipe to another variable.
$string_with_line_feed -replace "`n|`r"
`n -  backtick with n refers to the line feed character `r – backtick with r refers to the carriage return
Or the code above can be written as follows:
$string_with_line_feed -replace "`n|"
So, basically line feed is replace with nothing. But for readability and to avoid confusion it’s better to …

Linux bash script get folder size

Having an error, no space left on the device?
But you don't know which folder is consuming the most space?
Bash script below will get all the folder size and display the size for each folder.
How to use the script below?
Open your favourite terminal editor, vi, vim or other editor.
At terminal, type: vi dirfoldersize.sh
Copy and paste the code below to the editor.
Save the file.
Then make the script executable, chmod +x dirfoldersize.sh
Run the script by typing, ./dirfoldersize.sh
If everything goes well, you will get the folder size for each folder on the directory where the script runs. See sample output below.
=============== #code begins start copy below
#!/bin/bash curdir=$(pwd) compath="$curdir/dirlist.txt" ls -l | grep ^d | awk '{print $9}'  > $compath

while IFS= read -r var do

  foldersize=$(du -sh $var |awk '{print $1}')
  echo "$curdir/$var folder size: $foldersize"
done < "$compath"
#code ends, end copying from above line ======…

Powershell move selected files

Powershell script below will sort files base on its last write time, it will select the files and move the selected files to another folder.

This script will remove the first 3 old files base on the lastwritetime property, quite useful to archive files or grouping large files into multiple folders.
#=====Code Snippet Start
$xfiles= Get-ChildItem d:\the_log_folder |  Sort-Object -Property @{Expression={$_.LaswriteTime}; Ascending = $true} | Select-Object -first 3 | select fullname | ft -HideTableHeaders
write $xfiles >  d:\move_files_listing.txt
ForEach ($movefiles in (Get-Content "d:\move_files_listing.txt"))
{     
  If ($movefiles[1].Length –gt 0) {
      move-item $movefiles d:\the_archive_folder
      write $movefiles
     }
}
#=====Code Snippet Ends

Code explanation:
--# Sort-Object -Property @{Expression={$_.LaswriteTime}; Ascending = $true # this will sort the files beginning from the file with the  last write time or basically the old files base on its write time.
--# …

List completed cron jobs in Centos

Listing cron jobs or log files within a specific time frame is quite hard especially if the log or logs are quite a big file.

But of course, doing the lazy way but a smarter way is always a good option.

Use SED or stream editor.

In Centos the log is in: /var/log/cron

/var/log - path for the file
 cron - is the file that keeps the record for cron jobs, there is no filename extension

To check the logs within the 24 hours time, sed can do it easily and quickly.

Here's one line, time saver command to check the cron log file:

sed -n '/Mar 10 00:00:01/ , /Mar 11 00:01:01/p' /var/log/cron

To redirect the output to a file:

sed -n '/Apr 10 00:00:01/ , /Apr 11 00:01:01/p' /var/log/cron > cron24_hours.record.log

You can replace /var/log/cron with any other files as long as it follows the time format of Month, Day of the month and the time in HR:MM:SS format.


Download the free Linux Android App cheat sheet, see link below. It's free. Enjoy.



Cheers..till next time!

=======…