Thursday, 30 April 2020

Install SQLite3 from Source on Linux

Install SQLite3 from Source

$ wget https://www.sqlite.org/src/tarball/sqlite.tar.gz

$ tar xzf sqlite.tar.gz     #  Unpack the source tree into "sqlite"
$ mkdir bld                   #  Build will occur in a sibling directory
$ cd bld                        #  Change to the build directory
$ ../sqlite/configure     #  Run the configure script
$ make                         #  Run the makefile.
$ make sqlite3.c           #  Build the "amalgamation" source file
$ make test                  #  Run some tests (requires Tcl)

$ whereis sqlite3
sqlite3: /usr/bin/sqlite3 /usr/include/sqlite3.h /usr/share/man/man1/sqlite3.1.gz

$ sqlite3 --version
3.7.17 2013-05-20 00:56:22 118a3b35693b134d56ebd780123b7fd6f1497668

$ sqlite3
   SQLite version 3.7.17 2013-05-20 00:56:22
   Enter ".help" for instructions
   Enter SQL statements terminated with a ";"
   sqlite> .help
   [...]
   sqlite> .show
   [...]
   sqlite> .database
   [...] 
   sqlite> .table
   [...]
   sqlite> .quit

Ref :- sqlite.org sqlite.org/cli.html

Monday, 27 April 2020

Install Python 3.8 using tarball in Linux

Install Python 3.8.2

$ cat /etc/redhat-release
  CentOS Linux release 7.7.1908 (Core)

$ python --version
  Python 2.7.5
$ which python
  /usr/bin/python

$ yum update
$ yum install yum-utils
$ yum-builddep python3

$ wget https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tgz
$ tar -xvzf Python-3.8.2.tgz
$ cd Python-3.8.2

$ ./configure --enable-optimizations
$ make
$ make test
$ make install

$ python3.8 --version
  Python 3.8.2
$ which python3.8
  /usr/local/bin/python3.8

Since the python2.7 is a default system wide python interpreter and need explicitly set to new version. To change python version only for a single user edit ~/.bashrc.

$ vi ~/.bashrc
  alias python='/usr/local/bin/python3.8'
$ cd /root
$ . .bashrc #apply changes

$ python
  Python 3.8.2 (default, Apr 27 2020, 08:41:59)
  [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
  Type "help", "copyright", "credits" or "license" for more information.
  >>>

Ref:- linuxconfig.org

Saturday, 11 April 2020

Debug initramfs images in Linux

Debug initramfs images

$ mkdir /tmp/testdir

$ ls /boot/
   config-3.10.0-957.el7.x86_64
   efi
   grub
   grub2
   initramfs-0-rescue-e45307abc30a407dbbd42a0632810083.img
   initramfs-3.10.0-957.el7.x86_64.img
   initramfs-3.10.0-957.el7.x86_64kdump.img
   symvers-3.10.0-957.el7.x86_64.gz
   System.map-3.10.0-957.el7.x86_64
   vmlinuz-0-rescue-e45307abc30a407dbbd42a0632810083
   vmlinuz-3.10.0-957.el7.x86_64

$ cp -r /boot/initramfs-3.10.0-957.el7.x86_64.img /tmp/testdir/i

$ ls /tmp/testdir/i
   i

$ cd /tmp/testdir/

$ file i
   i: ASCII cpio archive (SVR4 with no CRC)

$ cpio -i --no-absolute-filenames <i
   86917 blocks

$ ls /tmp/testdir/
   bin  etc init   lib64  root   sbinb          sys          tmp   var
   dev  i    lib    proc   run    shutdown   systoot    usr

$ ls -l init  
   lrwxrwxrwx. 1 root root 11 Apr   20 11:12 init -> usr/lib/systemd/systemd

Make a custom GRUB entry in Linux

How to make a custom GRUB entry

Copy menuentry for CentOS Linux 3.10.0-957.el7.x86_64 to 40_custom edit as,
$ vi /etc/grub.d/40_custom
#!/bin/sh
exec tail -n +3 $0
# This file provides an easy way to add custom menu entries.  Simply type the
# menu entries you want to add after this comment.  Be careful not to change
# the 'exec tail' line above.
menuentry 'Custom Linux Boot Entry' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-957.el7.x86_64-advanced-4f940ed7-acd6-4631-bf35-ee2f43b49c9f' {
        load_video
        set gfxpayload=keep
        insmod gzio
        insmod part_msdos
        insmod xfs
        set root='hd0,msdos1'
        if [ x$feature_platform_search_hint = xy ]; then
          search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1'  5971f3cb-eb26-4c88-af0a-b6c96e12965c
        else
          search --no-floppy --fs-uuid --set=root 5971f3cb-eb26-4c88-af0a-b6c96e12965c
        fi
        linux16 /vmlinuz-3.10.0-957.el7.x86_64 root=/dev/mapper/centos-root ro crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet initcall_debug
        initrd16 /initramfs-3.10.0-957.el7.x86_64.img
}

                                                OR

menuentry 'Custom Linux Boot' {
        linux16 /vmlinuz-3.10.0-957.el7.x86_64 root=/dev/mapper/centos-root ro rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet initcall_debug
        initrd16 /initramfs-3.10.0-957.el7.x86_64.img
}

$ grub2-mkconfig -o /etc/grub2.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-957.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-957.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-e45307abc30a407dbbd42a0632810083
Found initrd image: /boot/initramfs-0-rescue-e45307abc30a407dbbd42a0632810083.img
done

$ dmesg | grep initcall #check the current kernel have any init call
If no output then reboot and check.

$ dmesg | grep initcall | head
[    0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.10.0-957.el7.x86_64 root=/dev/mapper/centos-root ro crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet initcall_debug
[    0.000000] Kernel command line: BOOT_IMAGE=/vmlinuz-3.10.0-957.el7.x86_64 root=/dev/mapper/centos-root ro crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet initcall_debug
[    0.131139] initcall init_hw_perf_events+0x0/0x5f1 returned 0 after 0 usecs
[    0.131167] initcall set_real_mode_permissions+0x0/0x102 returned 0 after 0 usecs
[    0.131173] initcall trace_init_flags_sys_exit+0x0/0xf returned 0 after 0 usecs
[    0.131178] initcall trace_init_flags_sys_enter+0x0/0xf returned 0 after 0 usecs
[    0.131184] initcall register_trigger_all_cpu_backtrace+0x0/0x16 returned 0 after 0 usecs
[    0.131190] initcall kvm_spinlock_init_jump+0x0/0x28 returned 0 after 0 usecs
[    0.131197] initcall early_efi_map_fb+0x0/0x32 returned 0 after 0 usecs
[    0.131228] initcall spawn_ksoftirqd+0x0/0x26 returned 0 after 0 usecs

How to Interrupt GRUB and Override init process in Linux

Interrupt GRUB and Override init

This can be achieved by making temporary changes to a kernel menu entry
requirements: cent OS 7 installed in a virtual machine.

Booting with rdinit=/bin/sh will start with a shell in the initramfs.
while booting with init=/bin/bash will complete the initramfs and then start with a shell on the disk.

Scenario 1:  Change kernel parameters only during a single boot process. reboot vm on the GRUB 2 boot screen press 'e' key for edit regular kernel entry
at the end of last to previous line, add rdinit=/bin/sh



Press Ctrl+x, it will boot as shell prompt with logged in.
The init is restarted process as /bin/sh.
There will be a limited file system loaded in RAM











Scenario 2: reboot vm, on the GRUB 2 boot screen press 'e' key for edit regular kernel entry. At the end of last to previous line, add init=/bin/bash.



Press Ctrl+x to boot, It will boot as shell prompt with logged in.
The init is restarted process as /bin/bash, with no ordinary service like GUI or init processes.



Thursday, 9 April 2020

Bash script to monitor cpu usage in Linux

Script to monitor cpu usage 

$ vi cpu_usage.sh

#!/bin/bash

CPU_USAGE=$(top -b -n2 -p 1 | fgrep "Cpu(s)" | tail -l | awk -F'id,' -v prefix="$prefix" '{ split($1, vs, ","); v=vs[length(vs)]; sub("%", "", v); printf "%s%.1f%%\n", prefix, 100 -v}' )

DATE=$(date "+%d %B %Y %H:%M")
CPU_USAGE="$DATE CPU: $CPU_USAGE"
echo $CPU_USAGE >> /var/log/scripts/cpu_usage.log

$ ./cpu_usage.sh

$ cat /var/log/scripts/cpu_usage.log
09 April 2020 08:36 CPU: 3.2% 0.2%
09 April 2020 15:00 CPU: 9.7% 0.0%
09 April 2020 15:34 CPU: 0.0% 0.2%

Bash script to guess a random one-digit number

Script to guess a random one-digit number

$ vi ch4_solution.sh game

#!/bin/bash
rand=$RANDOM
secret=${rand:0:1}

function game {
        read -p "Guess a random one-digit number! " guess
        while [[ $guess != $secret ]]; do
                read -p "Nope, try again! " guess
        done
        echo "Good job, $secret is it! You're great at guessing!"
}

function generate {
        echo "A random number is: $rand"
        echo -e "Hint: type \033[1m$0 game\033[0m for a fun diversion!"
}

if [[ $1 =~ game|Game|GAME ]]; then
        game
else
        generate
fi

$ ./ch4_solution.sh game
Guess a random one-digit number! 1
Nope, try again! 4
Nope, try again! 5
Good job, 5 is it! You're great at guessing!

Tuesday, 7 April 2020

Sample bash script to generate system report

Generate system report

vim /usr/local/src/test_my_scripts/system_report.sh
#!/bin/bash
# System Report
greentext="\033[32m"
bold="\033[1m"
normal="\033[0m"
freespace=$(df -h / | grep -E "\/$" | awk '{print $4}')
logdate=$(date +"%Y%m%d")
path=/var/log/script
logfile="$path$logdate"_report.log

echo -e $bold"Quick system report for "$greentext"$HOSTNAME"$normal
printf "\tSystem type:\t%s\n" $MACHTYPE
printf "\tBash Version:\t%s\n" $BASH_VERSION
printf "\tFree Space:\t%s\n" $freespace
printf "\tFiles in dir:\t%s\n" $(ls | wc -l)
printf "\tGenerated on:\t%s\n" $(date +"%m/%d/%y")
echo -e $greentext"A summary of this info has been saved to $logfile"$normal

cat <<- EOF > $logfile
        This report was generated using Bash script.
EOF

printf "\tGenerated on:\t%s\n" $(date +"%m/%d/%y") >> $logfile
printf "\tFree Space:\t%s\n" $freespace >> $logfile
printf "\tFiles in dir:\t%s\n" $(ls | wc -l) >> $logfile

sh /usr/local/src/test_my_scripts/system_report.sh
Quick system report for localhost.localdomain
        System type:    x86_64-redhat-linux-gnu
        Bash Version:   4.2.46(2)-release
        Free Space:     15G
        Files in dir:   6
        Generated on:   04/07/20
A summary of this info has been saved to /var/log/script20200407_report.log

cat /var/log/script20200407_report.log
This report was generated using Bash script.
        Generated on:   04/07/20
        Free Space:     15G
        Files in dir:   6

Monday, 6 April 2020

40 Examples of grep command in Linux

Searching and mining text inside a file with grep

man grep
grep "string" demo_file      #string search
grep "string" demo_file test_file.txt
grep "string" *.*

grep “^hello” file #match all lines start with ‘hello’
grep “done$” file #match all lines end with 'done'
grep “[^aeiou]” file #Match all lines not contain a vowel
grep -n "string" demo_file      # show line numbers with match

egrep is the same as grep -E
fgrep is the same as grep -F
rgrep is the same as grep -r
egrep IP /etc/hosts
egrep 'IP1|IP2' /etc/hosts

seq 10 | grep 5 -A 3        #print 5-8
seq 10 | grep 5 -B 3        #print 2-5
seq 10 | grep 5 -C 3        #print 2-8
echo -e "a\nb\nc\na\nb\nc" | grep a -A 1

grep word filename --color=auto #add color
grep --color "string" demo_file
grep --color -n "string" demo_file
yum search php | grep gd
grep -E "[a-z]+" demo_file #extented
grep "[a-z]+" demo_file

grep -v "string" demo_file     #inverted result
grep -c "string" demo_file #count the occurrence
echo -e "1 2 3 4\nhellow\n5 6" | grep -c "[0-9]"
grep -r  --exclude-dir=log "string" *
grep -c "[0-9]" demo_file

grep -o "[0-9]" demo_file | wc -l     #count no of times
echo -e "1 2 3 4\nhellow\n5 6" | grep -o "[0-9]" | wc -l
cat filename | grep -b -o "word"
grep -l "string" sample.txt sample1.txt     #with-match
grep -L "string" sample.txt sample1.txt     #without-match

grep "string" . -R -n       #recursive search
grep -i "string" demo_file #case insensitive
grep -i 'Model' /proc/cpuinfo
grep -e "count" -e "image" -o rename.sh     #multiple patterns
grep "main()" . -r --include *.{c,cpp}
grep "main()" . -r --exclude "README"
grep "string" file* -lZ | xargs -0 rm -rf

grep -o "is.*line" demo_file      #matched string
grep -v -E '^\#|^$' demo_file #skips line beginning "#" or empty
grep -v -f file1 file2 > file.out    #diff of 2nd with 1st
find . -type f -exec grep -il 'string' {} \; #find in directory tree

grep authentication failure /var/log/secure-20200329
export GREP_OPTIONS='--color=auto' #to enable all the time
grep -i break-in autth.log | awk {'print $12'}
ping -c 1 linuxmissive.com | grep 'bytes from'
ping -c 1 linuxmissive.com | grep 'bytes from' | cut -d = -f 4

# find all files named "*.java,v" containing both 'prevayl' and 'jtable'
grep -li "jtable" $(find . -name "*.java,v" -exec grep -li "prevayl" {} \;)
egrep 'sting1|sting2|sting3|sting4' file.txt # all lines matching multiple patterns
locate -i calendar | grep Users | egrep -vi 'twiki|gif|shtml|drupal-7|java|PNG'
   
ps auxwww | grep httpd               # all processes containing 'httpd'
ps auxwww | grep -i java             # all processes containing 'java', ignoring case
ls -al | grep '^d'                        # list all dirs in the current dir
#printing lines before and after of matching
grep --context=6 "string" demo_file      #6 lines before and after

Ref:- thegeekstuff.com

Saturday, 4 April 2020

Install MySQL on CentOS 7

Install and Configure MySQL

Install MySQL

wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
md5sum mysql57-community-release-el7-9.noarch.rpm
rpm -ivh mysql57-community-release-el7-9.noarch.rpm

Start MySQL

systemctl start mysqld
systemctl status mysqld #Active: active (running)
grep 'temporary password' /var/log/mysqld.log #A temporary password is generated

Configure MySQL

mysql_secure_installation #set a new password
mysqladmin -u root -p version

Packages for MySQL:- dev.mysql.com

Ref:- digitalocean.com

Friday, 3 April 2020

Bash script to List all the machines alive on a network

Ping all the machines alive

vi ping.sh
#!/bin/bash
#ping ping.sh

for ip in 192.168.0.{1..255} ;
do
 (
 ping $ip -c 2 &> /dev/null;

 if [ $? -eq 0 ];
 then
  echo $ip is alive
 fi
 )&
done
wait

./ping.sh
192.168.0.1 is alive
192.168.0.3 is alive
192.168.0.16 is alive
192.168.0.32 is alive
192.168.0.201 is alive


Linux script to monitor top ten CPU consuming process

CPU monitoring bash script for an hour

vi cpu_usage.sh
#!/bin/bash
#calculate cpu usage by processes for 1 hour

SECS=3600
UNIT_TIME=60

STEPS=$(( $SECS / $UNIT_TIME ))

echo Watching CPU usage... ;

for ((i=0;i<STEPS;i++))
do
 ps -eocomm,pcpu | tail -n +2 >> /tmp/cpu_usage.$$
 sleep $UNIT_TIME
done

echo
echo CPU eaters :

cat /tmp/cpu_usage.$$ | \
awk '
{ process[$1]+=$2; }
END{
 for(i in process)
 { printf("%-20s%d\n",i, process[i]); }
 }' | sort -nrk 2 | head

rm -rf /tmp/cpu_usage.$$

./cpu_usage.sh
Watching CPU usage...

CPU eaters :
xfs-reclaim/sda      0
xfs-reclaim/dm-     0
xfs_mru_cache       0
xfs-log/sda1           0
xfs-log/dm-0          0
xfs-eofblocks/s       0
xfs-eofblocks/d      0
xfs-data/sda1         0
xfs-data/dm-0        0
xfs-conv/sda1         0

Monday, 30 March 2020

Bash script to remove duplicate files

Identify the duplicate files and remove.

vi remove_duplicate.sh
#!/bin/bash
ls -ls --time-style=long-iso | awk 'BEGIN {
 getline; getline;
 name1=$9; size=$5
}
{
 name2=$9;
 if (size==$5)
 {
  "md5sum "name1 | getline; csum1=$1;
  "md5sum "name2 | getline; csum2=$1;
  if ( csum1==csum2 )
        {
         print name1; print name2;
        }
 };

 size=$5; name1=name2;
}' | sort -u > duplicate_files

cat duplicate_files | xargs -I {} md5sum {} | sort | uniq -w 32 | awk '{ print "^"$2"$" }' | sort -u > duplicate_sample

if [ -s duplicate_sample ]
 then
  echo Removing...
  comm duplicate_files duplicate_sample -2 -3 | tee /dev/stderr | xargs rm -rf
  echo Removed duplicate files successfully.
 else
 echo No duplicate files found.
fi

Friday, 27 March 2020

20 Examples of Linux xargs Command


 xargs Command usage

man xargs
xargs --version

ls -l /usr/local/src/ | xargs                #single line output
cat example.txt | xargs -n 7
xargs -a example.txt
echo 'one two three' | xargs mkdir #creating three dirs
echo 'one two three' | xargs -p touch #creating three files

printf "one  three  two" | xargs -i touch {}.txt #add extention
echo "Text1XText2XText3XText4" | xargs -d X -n 2
printf %s\\n {1..30} | xargs -n 5 -P 8                     #executing commands in parallel
cat args.txt | xargs -n 2 '/usr/local/src/cecho.sh'

cat args.txt | xargs -I {} ./cecho.sh -p {} -l
echo "file1 file2 file3" | xargs -t -I % sh -c '{ touch %; ls -l %; }' #multiple commands
find ./*.txt -print0 | xargs -0 -n 1 -P 3 bzip2    #bzip2 processes in parallel
ls "b.txt" | xargs -n 1 sed -i "s/color/colour/g" #replace all occurrences

cut -d: -f1 < /etc/passwd | sort | xargs #compact list of all Linux users
ls *digital* | xargs wc                             #number of lines/words/characters in each file
echo dir1/ dir2/ dir3/ | xargs -n 1 cp -v dir/test.txt #copy a file to multiple dirs at once
find . -type f -name “*.java” | xargs tar cvf myfile.tar
find / -type f -name "*.sh" -print0 | xargs -0 wc -l #cout no of lines

find . -type f -name "*.txt" -print0 | xargs -0 rm -f                 #remove files
find . -type f -name "b.txt" -print0 | xargs -0 -p -n 1  rm -rf   #prompt before execution
find /tmp -mtime +7 | xargs rm

find . -type f -not -name '*.txt' -print0 | xargs -0 -I {} rm -v {} #remove except extension
find . -type d -name "abb" -print0 | xargs -0 rm -v -rf "{}"        #remove directory
echo 'one two three' | xargs -t rm -rf                               #remove three files/dirs

Ref:- linuxtechi.com shapeshed.com tecmint.com


Thursday, 26 March 2020

60 Examples of Linux find command

Using find command in Linux

Find becomes extremely useful when combined with other commands,

man find
info find
find . -type d -print  #list directories
find . -type f -print  #regular files
find . -type l -print  #symbolic files

find . -name demo.sh
find . -iname demo.sh #case-insensitive search
find /home -iname demo.sh      #both capital and small letters
find . -exec ls -ld {} \;         #List out the found files
find . \( -name "*.mp4" -o -name "*.txt" \) -print
find /home/test -path "*/public/*" -print
find . -iregex ".*\(\.py\|\.sh\)$"
find . -type f -not -name "*.sh" #invert match
find . ! -name "*.txt" -print

find /tmp -type f -empty         #find Empty files
find /tmp -type d -empty find   #Empty directories
find /tmp -type f -name ".*"  #find hidden files
find ./ -type f -name "ab.txt" -exec grep 'This' {} \; #print lines which have ‘This’
find ./test -name 'abc*' ! -name '*.php' #combine multiple search criteria
find -name '*.php' -o -name '*.txt'
find ./test ./dir2 -type f -name "abc*"       #search multiple directories
find . -maxdepth 1 -name "f*" -print
find . -mindepth 2 -name "f*" -print

find . -type f -atime -7 -print    #accessed in last 7 days
find . -type f -atime 7 -print     #accessed in last 7th day
find . -type f -atime +7 -print    #accessed older that 7 days
find / -mtime +7 –mtime -10     #modified 7 days back and less than 10 days
find / -cmin -60  #files which are changed in last 1 hour
find / -mmin -60 #the files which are modified in last 1 hour
find / -amin -60 #files which are accessed in last 1 hour
find . -type f -amin +7 -print                  #accessed in last 7 mins
find . -type f -newer cal_seq.sh -print    #newer than a file

find . -type f -size +2k                      #file size
find / -size +50M -size -100M             #greater than 50MB and less than 100MB
find . -type f -exec ls -s {} \; | sort -n -r | head -5  #find largest
find . -type f -exec ls -s {} \; | sort -n | head -5     #smallest file
find . -type f -name "a.txt" -exec cp {} test/ \;     #find and move
find / -type f -size +100M -exec rm -f {} \;               #find all 100MB files and delete
find / -type f -name *.mp3 -size +10M -exec rm {} \;#find all .mp3 & delete more than 10MB
find / -name name.txt -exec rm -i {} \; #delete a file with confirmation
find . -type f -name *.swp -delete                 #delete

find / -perm 1551 #sticky bit files
find / -perm /u=s #SUID
find / -perm /g=s #SGID
find / -perm /u=r #read only
find / -perm /a=x # executable

find /etc -maxdepth 1 -perm /u=r
find . -type f -perm 644 -print    #permission
find / -type f ! -perm 777 #with out permission
find . -type f -name "*.php" ! -perm 644 -print #don't have permission levels
find / -type f -perm 0777 -print -exec chmod 644 {} \;  #change permission
find . -type f -user root -print #find based on user
find /data -owner smith #find all files owned by smith
find /home -group developer #find based on group
find . -type f -user root -exec chown test {} \; #find and change ownership

find ./ -type f -exec sed -i 's/find/replace/g' {} \; #find and replace
find / -name \*.txt -exec sed -i "s/one/five/g" {} \;
find . -type f -name "*.c" -exec cat {} \;>all_c_files.txt #all .c to .txt
find . -type f -mtime +10 -name "*.txt" -exec cp {} OLD \; #cp 10 days old OLD dir
find . -type f -name "*.txt" -exec printf "Text file: %s\n" {} \;  #list .txt

find / -type f -name "*.sh" -print0 | xargs -0 wc -l           #cout no of lines
find . -type f -name "*.txt" -print0 | xargs -0 rm -f          # remove files
find . -type f -name “*.java” | xargs tar cvf myfile.tar    #find and tar files
find [paths] [expression] [actions] 2>/dev/null          #finding error messages
find ~/documents -type f -name '*.txt' \ -exec grep -s DOGS {} \; -print   #grep

#remove duplicate files
find . ! -empty -type f -exec md5sum {} + | sort | uniq -w32 -dD
find -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | xargs -I{} -n1 find -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --all-repeated=separate

Monday, 23 March 2020

tput in Linux

tput usage

$ man tput
$ info tput

$ tput longname #full name of the current terminal type
xterm terminal emulator (X Window System)

$ tput -T screen longname
VT 100/ANSI X3.64 virtual terminal

Foreground & background color commands

tput setab [1-7] # Set the background color using ANSI escape
tput setaf [1-7] # Set the foreground color using ANSI escape

Colors are as follows:

No   Color        #define                R G B

 0    black       COLOR_BLACK        0,0,0
 1    red           COLOR_RED          1,0,0
 2    green       COLOR_GREEN       0,1,0
 3    yellow      COLOR_YELLOW     1,1,0
 4    blue         COLOR_BLUE          0,0,1
 5    magenta   COLOR_MAGENTA   1,0,1
 6    cyan        COLOR_CYAN           0,1,1
 7    white       COLOR_WHITE         1,1,1

















Text mode commands

tput bold    # Select bold mode
tput dim     # Select dim (half-bright) mode
tput smul    # Enable underline mode
tput rmul    # Disable underline mode
tput rev     # Turn on reverse video mode
tput smso    # Enter standout (bold) mode
tput rmso    # Exit standout mode

Cursor movement commands

tput cup Y X # Move cursor to screen postion X,Y (top left is 0,0)
tput cuf N   # Move N characters forward (right)
tput cub N   # Move N characters back (left)
tput cuu N   # Move N lines up
tput ll      # Move to last line, first column (if no cup)
tput sc      # Save the cursor position
tput rc      # Restore the cursor position
tput lines   # Output the number of lines of the terminal
tput cols    # Output the number of columns of the terminal

Clear and insert commands

tput ech N   # Erase N characters
tput clear   # Clear screen and move the cursor to 0,0
tput el 1    # Clear to beginning of line
tput el      # Clear to end of line
tput ed      # Clear to end of screen
tput ich N   # Insert N characters (moves rest of line forward!)
tput il N    # Insert N lines

Reset and play bell commands

tput sgr0    # Reset text format to the terminal's default
tput bel     # Play a bell

Error Messages

tput displays various error messages if problems occur. In addition, it exits with one of the following status values:

0 Normal status; the given capability is present.
1 The given Boolean or string capability is not present.
2 Usage error; tput was given invalid arguments.
3 The terminal type given is unknown, or the termcap database can not be read.
4 The given capability is unknown.


Ref:- stackoverflow.com gnu.org linuxcommand.org

Linux date command usage

Date command usages in Linux

The date command displays or sets the system date and time. It is most commonly used to print the date and time in different formats and calculate future and past dates.

$ man date
$ info date

$ date
Mon Mar 23 08:08:21 EDT 2020

$ date +"Year: %Y, Month: %m, Day: %d"
Year: 2020, Month: 03, Day: 23

$ date "+DATE: %D%nTIME: %T"
DATE: 03/23/20
TIME: 08:08:41

$ date -d "2010-02-07 12:10:53" #date string option
Sun Feb  7 12:10:53 EST 2010

$ date -d '16 Dec 1974' +'%A, %d %B %Y'
Monday, 16 December 1974

$ date -d "last week"             #Display past date
Mon Mar 16 08:10:54 EDT 2020

$ date -d 'TZ="Australia/Sydney" 06:30 next Monday'
Sun Mar 29 15:30:00 EDT 2020

$ TZ=GMT date
Mon Mar 23 12:15:31 GMT 2020

$ TZ='Australia/Melbourne' date #current time of some other location
Mon Mar 23 23:11:29 AEDT 2020 (ls /usr/share/zoneinfo)

$ date -u               #Display universal time
Mon Mar 23 16:57:45 UTC 2020

$ date --iso-8601=seconds #ISO 8601 format
2020-03-23T13:06:30-0400

$ date --rfc-3339=seconds #RFC 3339 format
2020-03-23 13:08:54-04:00

$ date +%s                #Epoch converter (seconds since 00:00:00, Jan 1, 1970)
1584965527

$ date -d @1234567890         #Convert epoch to a date
Fri Feb 13 18:31:30 EST 2009

$ date -d "2020-01-01" +"%s" #calculate the seconds from epoch to provided date/time
1577854800

$ date_now=$(date "+%F-%H-%M-%S") #use with shell scripts
$ echo $date_now
2020-03-23-08-13-50

$ date -r /etc/hosts                 #Last Modification Time of a File
Wed Jan 23 17:30:26 EST 2019

$ date --set="20200801 12:30" #Set the System Time and Date

$ date +"Week number: %V Year: %y"
Week number: 13 Year: 20

$ touch ~/Desktop/`date +%F`.txt #create a file with the date and time

$ tar cfz /backup-`date +%F`.tar.gz /home/ #create archives with the date and time

$ mysqldump  db_name > db_name-$(date +%Y%m%d).sql #mysqldump file with date


Sunday, 22 March 2020

Color code in Linux shell

Color modes, colors for bash prompt, tput and ANSI color 

Setting colors in terminal session is only temporary and relatively unconditional.
Modern systems usually default to at least xterm-256color.

$ echo $TERM
xterm (old)

Check current settings

$ echo $LS_COLORS
rs=0:di=01;34:ln=01;36:mh=00:pi=40;
33:so=01;35:do=01;35:bd=40;33;01:cd=40;
[...]

$ dircolors --print-database
[...].jpg 01;35
.jpeg 01;35
.gif 01;35[...]

$ dircolors --bourne-shell
LS_COLORS='rs=0:di=01;34:ln=01;36:mh=00:pi=40;
33:so=01;35:do=01;35:bd=40;33;01:cd=40;
[...]

The Color modes:

1. Color-mode

It modifies the style of color NOT text. For example make the color bright or darker.

0; reset
1; lighter than normal
2; darker than normal
This mode is not supported widely. It is fully support on Gnome-Terminal.

2. Text-mode

This mode is for modifying the style of text NOT color.

3; italic
4; underline
5; blinking (slow)
6; blinking (fast)
7; reverse
8; hide
9; cross-out
and are almost supported.
For example KDE-Konsole supports 5; but Gnome-Terminal does not and Gnome supports 8; but KDE does not.

3. Foreground mode
This mode is for colorizing the foreground.

4. Background mode
This mode is for colorizing the background.

Colors to current BASH prompt (PS1)

To add colors to the shell prompt use the following export command syntax:
'\e[x;ym $PS1 \e[m'
Where,

\e[ : Start color scheme.
x;y : Color pair to use (x;y)
$PS1 : Your shell prompt variable.
\e[m : Stop color scheme.

Examples using export and tput,
$ export PS1="\e[0;31m[\u@\h \W]\$ \e[m "
$ export PS1="\[$(tput setaf 2)\]\u@\h:\w $ \[$(tput sgr0)\]"

tput

Foreground & background colour commands
tput setab [1-7] # Set the background colour using ANSI escape
tput setaf [1-7] # Set the foreground colour using ANSI escape

Colours are as follows:

No  Colour      #define              R G B

0    black       COLOR_BLACK       0,0,0
1    red           COLOR_RED          1,0,0
2    green       COLOR_GREEN       0,1,0
3    yellow      COLOR_YELLOW    1,1,0
4    blue          COLOR_BLUE        0,0,1
5    magenta   COLOR_MAGENTA  1,0,1
6    cyan         COLOR_CYAN        0,1,1
7    white        COLOR_WHITE      1,1,1

Text mode commands
tput bold    # Select bold mode
tput dim     # Select dim (half-bright) mode
tput smul    # Enable underline mode
tput rmul    # Disable underline mode
tput rev     # Turn on reverse video mode
tput smso    # Enter standout (bold) mode
tput rmso    # Exit standout mode

echo "$(tput setaf 1)Red text $(tput setab 7)and white background$(tput sgr 0)"

Color summary


ANSI Color variables












Code  Effect

  0       reset
  1       lighter than normal
  2       darker than normal
  3       italic
  4       underline
  5       blinking (slow)
  6       blinking (fast)
  7       reverse
  8       hide
  9       cross-out


































# Reset
Color_Off='\033[0m'       # Text Reset

# Regular Colors
Black='\033[0;30m'        # Black
Red='\033[0;31m'          # Red
Green='\033[0;32m'        # Green
Yellow='\033[0;33m'       # Yellow
Blue='\033[0;34m'         # Blue
Purple='\033[0;35m'       # Purple
Cyan='\033[0;36m'         # Cyan
White='\033[0;37m'        # White

# Bold
BBlack='\033[1;30m'       # Black
BRed='\033[1;31m'         # Red
BGreen='\033[1;32m'       # Green
BYellow='\033[1;33m'      # Yellow
BBlue='\033[1;34m'        # Blue
BPurple='\033[1;35m'      # Purple
BCyan='\033[1;36m'        # Cyan
BWhite='\033[1;37m'       # White

# Underline
UBlack='\033[4;30m'       # Black
URed='\033[4;31m'         # Red
UGreen='\033[4;32m'       # Green
UYellow='\033[4;33m'      # Yellow
UBlue='\033[4;34m'        # Blue
UPurple='\033[4;35m'      # Purple
UCyan='\033[4;36m'        # Cyan
UWhite='\033[4;37m'       # White

# Background
On_Black='\033[40m'       # Black
On_Red='\033[41m'         # Red
On_Green='\033[42m'       # Green
On_Yellow='\033[43m'      # Yellow
On_Blue='\033[44m'        # Blue
On_Purple='\033[45m'      # Purple
On_Cyan='\033[46m'        # Cyan
On_White='\033[47m'       # White

# High Intensity
IBlack='\033[0;90m'       # Black
IRed='\033[0;91m'         # Red
IGreen='\033[0;92m'       # Green
IYellow='\033[0;93m'      # Yellow
IBlue='\033[0;94m'        # Blue
IPurple='\033[0;95m'      # Purple
ICyan='\033[0;96m'        # Cyan
IWhite='\033[0;97m'       # White

# Bold High Intensity
BIBlack='\033[1;90m'      # Black
BIRed='\033[1;91m'        # Red
BIGreen='\033[1;92m'      # Green
BIYellow='\033[1;93m'     # Yellow
BIBlue='\033[1;94m'       # Blue
BIPurple='\033[1;95m'     # Purple
BICyan='\033[1;96m'       # Cyan
BIWhite='\033[1;97m'      # White

# High Intensity backgrounds
On_IBlack='\033[0;100m'   # Black
On_IRed='\033[0;101m'     # Red
On_IGreen='\033[0;102m'   # Green
On_IYellow='\033[0;103m'  # Yellow
On_IBlue='\033[0;104m'    # Blue
On_IPurple='\033[0;105m'  # Purple
On_ICyan='\033[0;106m'    # Cyan
On_IWhite='\033[0;107m'   # White


Monday, 16 March 2020

Digital clock using watch, echo, banner and tput

Digital Clock

watch

#!/bin/bash
watch -tn 1 date +%T

echo

#!/bin/bash
clear
while [ 1 ] ; do echo -e "$(date +%T)" ; sleep 1; done

banner

#!/bin/bash
clear
while [ 1 ] ; do banner "$(date +%r)" ; sleep 1; clear ; done

while

#!/bin/bash
clear
while :
do
 echo -e "\033[92m $(date '+%r')"
 sleep 1
 clear
done

tput

#!/bin/bash
while sleep 1;do tput sc;tput cup $(($(tput lines)-1)) 1;printf `date +%r`;tput rc;done

#!/bin/bash
clear
while :
do
 ti= date '+%r'
 echo -e -n "\033[7s"
 tput cup 0 69
 echo -n $ti
 echo -e -n "\033[8u"
 sleep 1
 clear
done

Ref:- Google

Friday, 2 August 2019

Reset Kali Linux Root Password in Windows Subsystem for Linux


WSL manages the default login user from DefaultUID registry.

To edit this registry value, open Registry Editor or regedit.exe from start menu
Go to this registry path,
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss

Double click on the DefaultUID value and change it to ZERO. Zero is for root user and 1000 (Decimal) or 3e8 (in hexadecimal) for normal users.

Open wsl.exe in command prompt. The prompt changes from $ to # (means root user). Run passwd command in Kali, change root password as usual.


Now go back to previous registry key, change Zero to previous value (or 3e8 in hex).

Ref:- superuser.com