Posts

Showing posts with the label Bash script

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!

=======…

PowerShell move matching files in a folder

Requires PS 3.0
This is a two lines code snippet to move matching files in a source folder to another folder.
#==============================

$SourceFolder = "C:\File_monitor_records\"
# Find all files matching *.csv in the folder specified  # Find csv files with 2016 in its filename Get-ChildItem -Path $SourceFolder -Filter *2016*.csv | move-item  -destination C:\File_monitor_archives\2016\

#==============================
-Filter *2016*.csv –adjust or create your own pattern, use regex if necessary
Test the pattern before moving the files to make sure correct files are being moved. Check and double check, to avoid wasting time and effort.
This will just like matching files.

Get-ChildItem -Path $SourceFolder -Filter *2016*.csv


Move files and change file name:
http://quickbytesstuff.blogspot.sg/2015/06/powershell-move-files-and-change-file.html

================================ Free Android Apps:
Click on links below to find out more:
Linux Android App cheat sheet: https://play.google.com/s…

VB.Net remove line feed or new line

In VB.net or any programming language, if the program requires getting data from external programs, results or data from third party software that needs to be processed  may or may not have expected desired output.

For example, getting a string from a third party software. String is quite straight forward and it's quite basic.

But the issue arises,  if the desired output requires a specific string or text and other text should be discarded or deleted. Regex or any other methods might help on getting the specific text or data.

One basic issue, if the string needs to be formatted in one line but a line feed or a new line keeps breaking the line.

The first instinct that I think of, is to use "Trim" function but it doesn't solve the issue. Trim, Ltrim or Rtrim just delete spaces but it doesn't delete line feed.

In VB.Net a line feed can easily be removed by this code snippet below:

Dim strNoLineFeed As String = String_with_Line_Feed.Replace(vbCr, "").Replac…

Linux bash script copy folders and files from list

In Linux special characters such as "#", "-" and other special characters cannot be access directly either by manual copy or via script. For example this path below it has the sharp "#" key:
/usr/dfiles/$line/Tasks/#msgs/
cp "/usr/dfiles/$line/Tasks/#msgs/*.eml" "/mnt/NAS/Tasks1/line/"
Even though the path is enclosed by quotation marks, Linux system will show "No such file or directory".

Since the system is unable to find the "#msgs" directory.

As a work around in this issue is to append "--" double dash before the cp command and everything will work fine.

So, this command below will work.

cp  -- "/usr/dfiles/$line/Tasks/#msgs/*.eml" "/mnt/NAS/Tasks1/line/"
Copying one folder is quite practical to do it by hand rather by script.
But if you are copying hundreds or thousands of folders, doing it manually is quite painful.
To copy hundreds or thousands of folders to another folder the pra…

Hide files in Linux

Linux protect file from deletion

To protect file from deletion in Linux system, the chattr command is able to set the attributes that protects the file.

chattr +a my_protected_file.txt

chattr +a  == means that the file can be appended and the file can't be deleted as well.

To set the file to immutable, "+i"  attribute can be used.

chattr +i the_protected_file.txt

Immutable file is protected from deletion and the original contents of the file is also preserved because no changes can be made.

This command below:

ls xx*.txt >> the_protected_file.txt

The command above will show "permission denied" if the file is immutable.

If the file is set with +a, then the above command will append the output of "ls" to the file.

To unset or removed the attribute use the minus sign, "-a", "-i".

For example, chattr -a the_protected_file.txt or  chattr -i the_protected_file.txt

To know more about chattr type the command below:

man chattr
chattr --help
info chattr

This wiki link shows …

Linux search string in text files

Search a string or a pattern in text files without opening the file.

Grep is a handy tool to find or search a string in text files.

Grep is an available tool in Linux and Unix OSes.

So if you have some data or information stored in text files and forget where the file is located.

As long as you know the keyword or a string to search for, then grep and find command will be your utmost friend.

"Grep" and "find" are tools to make life easier to get the information you want but how to use it?

Command below will search recursively in the patch specified for all the text files and display the file where the match is found, output will also include the path and the filename.

find /home/00_Notes -name '*.txt' -print0 | xargs -0r grep -H 'vanity baseline'



The xargs -0r,  is zero r.

The above command will search recursively in all folders and subfolders for text files which contains the string "vanity baseline".

If a match is found an output will be…

PowerShell list processes by username

List all processes by username in PowerShell, requires that PowerShell to be run in elevated mode.




List all running processes for all users:

   Get-Process -IncludeUserName  | ft -AutoSize

     ft - Autosize --will not truncate the output



List all running processes and filter by UserName:

Get-Process -IncludeUserName  | Where UserName -EQ -EQ "This_User_Name"  | ft -AutoSize

Above script will filter the output to the specified username.


Get running word or winword process for the specified user and kill or stop the process.

Get-Process winword -IncludeUserName  | Where UserName -EQ "This_User_Name"  | Stop-Process -Force

-Force parameter will suppress the prompt whether to kill or stop the process, use sparingly in production environment

Requires PS 4.0 version to run above commands.

Cheers!! till next time.
================================ Free Android Apps:
Click on links below to find out more:
Linux Android App cheat sheet: https://play.google.com/store/apps/details?…

Searching files in Linux

Search the whole directory:

    find / -name "php.ini" -print

    find / -name "owncloud.conf" -print

Using whereis command:

  whereis php.ini

From man whereis (command):
NAME
       whereis  -  locate the binary, source, and manual page files for a com-
       mand


Using locate command:

locate php.ini

From man locate (command):
NAME
       locate - find files by name


To find out more, type at terminal:

    info locate
    info whereis

    locate --help
    whereis --help


Cheers..till next time.

================================ Free Android Apps:
Click on links below to find out more:
Linux Android App cheat sheet: https://play.google.com/store/apps/details?id=com.LinuxMobileKit
Multiplication Table for early learners https://play.google.com/store/apps/details?id=com.TableMultiplication

https://play.google.com/store/apps/details?id=soulrefresh.beautiful.prayer
Catholic Rosary Guide  for Android:
https://play.google.com/store/apps/details?id=com.myrosaryapp
http://quickbytesstuff.blogspot…

Linux Awk insert text at specific line number

One liner command for Awk to insert a new string or text at a specific line number on a file.

awk 'NR==3{print "new line text at row 3"}1' original_file.txt > modified_file.txt


'NR==3 --insert string or data at line number 3
"}1' -- number 1 (can be any number) means to append the new text or string
> modified_file.txt -- redirect the output to a new file

To find out more, type this at the terminal.

                info awk
                info print
                man awk
                man print

Or you can redirect the output of info and man to a file for offline viewing.


Check out links below for other examples of awk command.

http://quickbytesstuff.blogspot.sg/2016/02/linux-awk-simple-example.html

http://quickbytesstuff.blogspot.sg/2016/05/basic-awk-usage-tutorial.html

Centos Samba failed to add user