Wednesday, March 24, 2021

How to find the file size of a remote HTTP object?


There are times when you need to know the file size of an HTTP object without actually downloading the file. This little trick comes in very handy when web servers respond with the Content-Length of an object in the HEAD request itself.


Using curl command:


Example:


curl -sI https://releases.ubuntu.com/20.04.2.0/ubuntu-20.04.2.0-desktop-amd64.iso | grep Content-Length


Content-Length: 2877227008


Let’s try and make this human-friendly:


curl -sI https://releases.ubuntu.com/20.04.2.0/ubuntu-20.04.2.0-desktop-amd64.iso | grep Content-Length | sed 's/[^0-9]//g' | numfmt --to=si

2.9G



Where The Numfmt command will convert the numbers to/from human-readable format.


Wednesday, December 30, 2020

how to convert from one date format to another format in linux ?

 how to convert from one date format to another format in linux ?

echo 04-11-2021 | { IFS=- read d m y && echo "$y$m$d"; }

20211104


echo 04-11-2021 | { IFS=- read d m y && echo "$y/$m/$d"; }

2021/11/04


 echo 04-11-2021 | { IFS=- read d m y && echo "$d$m$y"; }

04112021


 echo 04-11-2021 | awk -F- '{print $3$2$1}'

20211104


echo 04-11-2021 |gawk -F, '{split($1, a, "-"); print a[3] a[2] a[1]}' 

20211104


echo 04/11/2021 | { IFS=/ read d m y && echo "$y$m$d"; }

20211104


date -d "20211104" "+%Y-%m-%d"

2021-11-04


date -d 04/11/2021 +%F

2021-04-11


 %F   full date; same as %Y-%m-%d


Monday, December 28, 2020

how to add user using shell script?

 #!/bin/bash -x 


if [ $(id -u) -eq 0 ]; then

read -p "Enter username : " username

read -s -p "Enter password : " password

egrep "^$username" /etc/passwd >/dev/null

if [ $? -eq 0 ]; then

echo "$username exists!"

exit 1

else

if [ -f /usr/bin/mkpasswd ]; then


pass=$(mkpasswd -m sha-512 $password)

#mkpasswd will create encrypted password.

useradd -m -p "$pass" "$username"

[ $? -eq 0 ] && echo "User has been added to system!" && chage -d0 $username  || echo "Failed to add a user!"

else

echo "install package apt-get install whois"

       fi

fi

else

echo "Only root may add a user to the system."

exit 2

fi

Monday, November 23, 2020

how to check If the file Is Empty or not using shell script?

 how to check If the file Is Empty or not using shell script?


-s FILE

              FILE exists and has a size greater than zero


Example 1:


touch /$HOME/f1


echo "data" >/$HOME/f2

ls -l /$HOME/f{1,2}

[ -s /$HOME/f1 ] 

echo $?


Sample outputs:


1



The non-zero output indicates that the file is empty.


[ -s /$HOME/f2 ]

echo $?

Sample outputs:


0


$? is the return code of the last executed command.


Example 2:


$ ls

$ touch file_1

$ dd if=/dev/zero of=$HOME/file_2 bs=1 count=100

100+0 records in

100+0 records out

$ ls -l file_1 file_2 file_3

ls: file_3: No such file or directory

-rw-rw-r--  1 foo bar   0 Nov 20 20:28 file_1

-rw-rw-r--  1 foo bar 100 Nov 20 20:28 file_2

$ [[ -s file_1 ]]

$ echo $?

1

$ [[ -s file_2 ]]

$ echo $?

0

$ [[ -s file_3 ]]

$ echo $?

1


Thursday, October 1, 2020

Input/Output Error How to Reboot or shutdown the Linux server?

 Input/Output Error : Bad Blocks - How to Reboot or shutdown the Linux server?  


Input/output error while running the command mostly due to it could be bad blocks on the disk. 

In this situation, first suggestion would be to check /var/log/messages for any disk-related alerts (might see some sense key alerts).


tail -n 100 /var/log/messages or tail -n 100 /var/log/syslog

tail -f /var/log/messages


#du

bash: /usr/bin/du: Input/output error




Now if try to reboot,it can also give the same output. You can try to init 6.



# reboot or init 6

bash: /sbin/reboot: Input/output error


# shutdown -r now

bash: /sbin/shutdown: Input/output error



If the above reboot commands don’t work try either forced reboot or shutdown


echo "number" >/proc/sys/kernel/sysrq


The number may be written here either as decimal or as hexadecimal with the 0x prefix. CONFIG_MAGIC_SYSRQ_DEFAULT_ENABLE must always be written in hexadecimal.


Note that the value of /proc/sys/kernel/sysrq influences only the invocation via a keyboard. Invocation of any operation via /proc/sysrq-trigger is always allowed (by a user with admin privileges).



0 - disable sysrq completely


1 - enable all functions of sysrq


Where  b Will immediately reboot the system without syncing or unmounting your disks.

              o Will shut your system off



Forced Reboot


echo 1 > /proc/sys/kernel/sysrq

echo b > /proc/sysrq-trigger


Forced Shutdown

echo 1 > /proc/sys/kernel/sysrq

echo o > /proc/sysrq-trigger