Skip to content

Instantly share code, notes, and snippets.

@aruku7230
aruku7230 / bash_retry.sh
Created January 12, 2023 06:37
Bash retry command
#! /usr/bin/env bash
# See https://stackoverflow.com/a/35977842/4910876
# Retries a command on failure.
# Sleep some seconds between each try, and increase the interval.
# Arguments:
# $1: the max number of attempts
# $2...: the command to run
@aruku7230
aruku7230 / common_used_commands.md
Last active January 30, 2023 11:40
Linux common used commands

compare two folder: diff -qr /path/to/folder1 /path/to/folder2

calculate TOTP: oathtool --totp -b -d 6 <secret-string>

copy folder with mode and timestamps preserverd (owership may be changed):

# folder1 will be copied to /path/to/folder2/folder1
#
# temporarily bypass an alias and use the non-aliased version of a command by prefixing it with \.
# If not prefix with backslash, cp may ask for confirmation before overwrite a file,
@aruku7230
aruku7230 / nfd2nfc
Last active November 27, 2022 10:23 — forked from hurlebouc/nfd2nfc
Command to convert *on Linux* unicode with NFD (Mac OSX) to unicode with NFC (Linux).
# Example: Convert `レポート.pdf` to `レポート.pdf`
# Dry run. Use --notest to finally rename the files.
convmv -f utf8 -t utf8 --nfc -r test
@aruku7230
aruku7230 / bash_errexit_option_pitfall.sh
Last active September 15, 2022 06:45
bash errexit option pitfall
#! /usr/bin/env bash
# The -e setting shall be ignored when executing any command of an AND-OR list other than the last.
# The following code is tested on GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)
set -e
false && true
# the following line will print
echo $? # 1
@aruku7230
aruku7230 / backup_rsync.sh
Created June 1, 2022 14:03
Backup data using rsync
#!/usr/bin/env bash
log_file=rsync.log
rm $log_file
dest=/dest/folder/
# Uhe trailing slash of source folder indicate the content to be copied.
# Without trailing slash, the folder itself will copied to destination folder.
rsync -av --delete --exclude=.DS_Store /source/folder/ $dest >> $log_file 2>&1
@aruku7230
aruku7230 / generate-csv-using-template.md
Last active May 2, 2022 09:12
Generate CSV using template

Problem

Given a CSV template file, generate a new CSV file based on the template rule.

Example 1:

  • Template CSV file:
    first_name,last_name,full_name
    Richard,Hendricks,{first_name} {last_name}
    

Bertram,Gilfoyle,{first_name} {last_name}

@aruku7230
aruku7230 / 24game_solver.md
Last active March 29, 2023 06:39
Algorithm problem 1: Calculate 24 given four numbers

Problem

Given four numbers n1, n2, n3, n4, return whether they can be calculated to be 24 by using only four basic arithmetic operations (addition, subtraction, multiplication, division). See also https://en.wikipedia.org/wiki/24_(puzzle).

Example 1:

  • Input: n1: 6, n2: 8, n3: 2, n4: 4
  • Output: true
  • Explanation: (6*2)+8+4 = 24
@aruku7230
aruku7230 / convert2utf8.py
Created March 14, 2021 04:01
Convert Shift-JIS encoded file to UTF-8
#!/usr/bin/env python
import sys
import tempfile
def convertToUtf8Encoded(sourceFileName, sourceEncoding = 'shift-jis'):
BLOCKSIZE = 1048576 # 1 MB. Unit is Byte.
with tempfile.TemporaryFile("w+t") as tempFile:
readSucceeded = False
@aruku7230
aruku7230 / javascript_number.js
Created July 7, 2018 12:40
javascript number manipulation
// reference: https://stackoverflow.com/a/16155417/4910876
// http://2ality.com/2012/02/js-integers.html
function IntegerToBin(dec){
let result = (dec >>> 0).toString(2);
while(result.length < 32) {
result = "0" + result;
}
return result;
}