• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives

convert

Bash Reformat Numbers

February 27, 2022

How to reformat numbers in Bash.

$ n=1023555
$ m=$(numfmt --to=si n)
$ echo $m
1.1M

$ n=1023555 $ m=$(numfmt --to=si n) $ echo $m 1.1M

Other options

--to=none     # no scaling. Output: 1023555
--to=si       # International System of Units (SI) standard. Output 1.1M
--to=iec      # International Electrotechnical Commission (IEC) standard. Output: 1.1Mi 
--to=iec-i    # International Electrotechnical Commission (IEC) standard. Output: 1.1M
--to=auto     # ‘auto’ can only be used with --from.

--to=none # no scaling. Output: 1023555 --to=si # International System of Units (SI) standard. Output 1.1M --to=iec # International Electrotechnical Commission (IEC) standard. Output: 1.1Mi --to=iec-i # International Electrotechnical Commission (IEC) standard. Output: 1.1M --to=auto # ‘auto’ can only be used with --from.

fmtnum

Filed Under: Linux Tagged With: bash, convert, fmtnum, format, numbers

AWS display Lightsail snapshots

December 1, 2021

How to display Lightsail snapshots.

#!/bin/bash
displaysnapshots () {
  echo '--------------------------------------------------------'
  aws lightsail get-instance-snapshots --query 'instanceSnapshots[].[name]' --output text | sort
  echo '--------------------------------------------------------'
}
displaysnapshots

#!/bin/bash displaysnapshots () { echo '--------------------------------------------------------' aws lightsail get-instance-snapshots --query 'instanceSnapshots[].[name]' --output text | sort echo '--------------------------------------------------------' } displaysnapshots

Result.

daily_ulyme_1637812801
daily_ulyme_1637899201
daily_ulyme_1637985601
daily_ulyme_1638072001

daily_ulyme_1637812801 daily_ulyme_1637899201 daily_ulyme_1637985601 daily_ulyme_1638072001

Adding timestamp to the output.

  aws lightsail get-instance-snapshots --query 'instanceSnapshots[].[name,createdAt]' --output text | sort

aws lightsail get-instance-snapshots --query 'instanceSnapshots[].[name,createdAt]' --output text | sort

Result.

daily_ulyme_1637812801	1637812806.853
daily_ulyme_1637899201	1637899208.285
daily_ulyme_1637985601	1637985606.184
daily_ulyme_1638072001	1638072006.579

daily_ulyme_1637812801 1637812806.853 daily_ulyme_1637899201 1637899208.285 daily_ulyme_1637985601 1637985606.184 daily_ulyme_1638072001 1638072006.579

Convert Unix timestamp to iso8601. Edit your ~/.aws/config file. Add this line.

cli_timestamp_format = iso8601

cli_timestamp_format = iso8601

Result.

daily_ulyme_1637812801	2021-11-25T04:00:06.853000+00:00
daily_ulyme_1637899201	2021-11-26T04:00:08.285000+00:00
daily_ulyme_1637985601	2021-11-27T04:00:06.184000+00:00
daily_ulyme_1638072001	2021-11-28T04:00:06.579000+00:00

daily_ulyme_1637812801 2021-11-25T04:00:06.853000+00:00 daily_ulyme_1637899201 2021-11-26T04:00:08.285000+00:00 daily_ulyme_1637985601 2021-11-27T04:00:06.184000+00:00 daily_ulyme_1638072001 2021-11-28T04:00:06.579000+00:00

Filed Under: Cloud Tagged With: aws, convert, lightsail, list, snapshots, timestamp, unix timestamp

Convert PPK to PEM

September 2, 2021

Convert PPK to PEM format.

  1. Open Puttygen.
  2. Click Load in the Actions section.
  3. Select the PPK file you wish to convert.
  4. Go to the Conversions menu and select Export OpenSSH key.
  5. Click Yes to convert key without a password.
  6. Name your private key and save it with a .pem extension.
  7. Click Save.

Your pem file should begin with “—–BEGIN RSA PRIVATE KEY—–” and ends with “—–END RSA PRIVATE KEY—–.”

You can also use puttygen in Linux. Install it first.

$ sudo apt-get install putty-tools

$ sudo apt-get install putty-tools

Convert your key.

$ puttygen yourkey.ppk -O private-openssh -o yourkey.pem

$ puttygen yourkey.ppk -O private-openssh -o yourkey.pem

SSH to your server.

$ chmod 400 yourkey.pem
$ ssh -i yourkey.pem ec2-user@server-ip

$ chmod 400 yourkey.pem $ ssh -i yourkey.pem ec2-user@server-ip

Filed Under: Windows Tagged With: convert, pem, ppk, puttygen

Dos2unix

September 18, 2020

In Windows a new line, is a CR (carriage return) followed by LF (line feed). In Linux, it’s just LF. If you bring over a text file from Windows to Linux, sometimes you’ll end up with a file with a bunch of ^M at the end of each line, which will breaks a lot of things. To fix, just run dos2unix.

dos2unix file.txt

dos2unix file.txt

Filed Under: Linux Tagged With: carriage return, convert, cr, dos2unix, lf, line feed

FFMPEG Convert MP3 to OGG

May 31, 2020

How to convert MP3 to OGG audio format.

ffmpeg -i in.mp3 -ar 48000 -vn -c:a libvorbis out.ogg

ffmpeg -i in.mp3 -ar 48000 -vn -c:a libvorbis out.ogg

If you have sample rate errors, use -vn.

Filed Under: Linux Tagged With: convert, ffmpeg, mp3, ogg, rate, sample

PFX to PEM

April 22, 2020

Here’s how to convert SSL certificate from PFX to PEM format.

#!/bin/bash
 
echo "This script converts SSL certificates from PFX to PEM."
read -p 'Enter PFX Certificate Name  : ' cert_pfx
read -p 'Enter the Import Passphrase : ' import_passphrase
 
openssl pkcs12 -in $cert_pfx -nocerts -out cert-key.pem -passin pass:$import_passphrase -passout pass:$import_passphrase
openssl pkcs12 -in $cert_pfx -clcerts -nokeys -out cert-body.pem -passin pass:$import_passphrase -passout pass:$import_passphrase
openssl pkcs12 -in $cert_pfx -nodes -nokeys -out cert-chain.pem -passin pass:$import_passphrase -passout pass:$import_passphrase
sleep 3
openssl rsa -in key.pem -out cert-private.key -passin pass:$import_passphrase -passout pass:$import_passphrase

#!/bin/bash echo "This script converts SSL certificates from PFX to PEM." read -p 'Enter PFX Certificate Name : ' cert_pfx read -p 'Enter the Import Passphrase : ' import_passphrase openssl pkcs12 -in $cert_pfx -nocerts -out cert-key.pem -passin pass:$import_passphrase -passout pass:$import_passphrase openssl pkcs12 -in $cert_pfx -clcerts -nokeys -out cert-body.pem -passin pass:$import_passphrase -passout pass:$import_passphrase openssl pkcs12 -in $cert_pfx -nodes -nokeys -out cert-chain.pem -passin pass:$import_passphrase -passout pass:$import_passphrase sleep 3 openssl rsa -in key.pem -out cert-private.key -passin pass:$import_passphrase -passout pass:$import_passphrase

This was covered in an earlier post, but this script prompts you for the passphrase.

Here’s the expected output.

  • cert-key.pem
  • cert-body.pem
  • cert-chain.pem
  • cert-private.key

Filed Under: Linux Tagged With: conversion, convert, openssl, pem, pfx

Bash Convert Bytes

January 26, 2020

Here’s a function to convert bytes into kilo, mega, giga, tera up to zeta bytes.

function convert_bytes {
    bytes=$credits
    b=${bytes:-0}; d=''; s=0; S=(Bytes {K,M,G,T,E,P,Y,Z}B)
    while ((b > 1024)); do
        d="$(printf ".%02d" $((b % 1000 * 100 / 1000)))"
        b=$((b / 1000))
        let s++
    done
    echo "$b$d ${S[$s]}"
}
credits=2308974418330
echo $credits 
convert_bytes $credits

function convert_bytes { bytes=$credits b=${bytes:-0}; d=''; s=0; S=(Bytes {K,M,G,T,E,P,Y,Z}B) while ((b > 1024)); do d="$(printf ".%02d" $((b % 1000 * 100 / 1000)))" b=$((b / 1000)) let s++ done echo "$b$d ${S[$s]}" } credits=2308974418330 echo $credits convert_bytes $credits

Result is:

2308974418330
2.30 TB

2308974418330 2.30 TB

Filed Under: Linux Tagged With: bash, bytes, conversion, convert, function

FFMPEG Convert TS to MP4

December 4, 2019

If you have video files that are formatted in MPEG-2, video files with a .m2ts extension, you can convert them to MP4 using ffmpeg.

ffmpeg -i input.ts -c:v libx264 -c:a aac output.mp4

ffmpeg -i input.ts -c:v libx264 -c:a aac output.mp4

The video is encoded using open format H.264, while audio is encoded using AAC.

Filed Under: Linux, Misc Tagged With: .m2ts, convert, ffmpeg, files, mp4, video

  • Go to page 1
  • Go to page 2
  • Go to Next Page »
  • Home
  • About
  • Archives

Copyright © 2023