prompts

bash prompts with change-mode and cursorshape
1.8.1 -- bash prompts with change-mode and cursorshape

settings

[!NOTE|label:inputrc]

# https://marslo.github.io/ibook/screenshot/colors/ansi/color-formatting.png
fgc=$(RGBcolor 5 6 8)                             # 240;  fgc=$(RGBcolor 5 6 5)  # 237
COL_SD_GREEN='\[\033[32;2m\]'                     # COL_SD_GREEN='\[\033[2;32m\]'
COL_IF_SL_RED='\[\033[0\;31m\]'
COL_DEFAULT="\[\033[38;5;${fgc}m\]"               # "\[$(tput setaf ${fgc})\]"; $"\e[38;5;${fgc}m";
COL_IF_DEFAULT="\[\033[38\;5\;${fgc}m\]"          # COL_IF_DEFAULT="\[\033[1\;38\;5\;${fgc}m\]"  | COL_IF_DEFAULT="${COL_IF_SD_GREEN}"
COL_RESET='\[\033[1m\]'                           # COL_RESET="\[$(tput sgr0)\]" | COL_RESET='\[\033[1m\]'
COL_NONE='\[\033[0m\]'                            # COL_NONE='\[\033[38;5;3m\]'

PS1="\\n${COL_RESET}${COL_DEFAULT}╭╶ (\\u@\\h${COL_RESET} "
PS1+="${COL_SL_RED}\\w${COL_RESET}${COL_DEFAULT}) "
PS1+="\$(__git_ps1 \"- (${COL_SD_GREEN}%s${COL_NONE}${COL_DEFAULT}) \")${COL_RESET}"
PS1+="${COL_DEFAULT}\`if [ \$? = 0 ]; then echo ${COL_IF_DEFAULT}\; else echo ${COL_IF_SL_RED}\; fi\`${COL_RESET}"
PS1+="\\n${COL_DEFAULT} \$ ${COL_RESET}"
PS1+="${COL_NONE}"
PS2="${COL_DEFAULT}  -> ${COL_RESET}${COL_NONE}"
PS4=' ${BASH_SOURCE}:$FUNCNAME:$LINENO: '
for wsl
PS1="\\n${COL_RESET}${COL_DEFAULT}╭╶ (\\u@\\h${COL_RESET} "
PS1+="${COL_SL_RED}\\w${COL_RESET}${COL_DEFAULT}) "
PS1+="\`__git_ps1 '- (%s) '\`"
PS1+="${COL_RESET}${COL_DEFAULT}->${COL_RESET}"
PS1+="\\n${COL_DEFAULT}╰╶ ${COL_RESET}"
PS1+="\`if [ \$? = 0 ]; then echo ${COL_IF_DEFAULT}\\$; else echo ${COL_IF_SL_RED}\\$; fi\` ${COL_RESET}"
PS1+="${COL_NONE}"
# PS2="${COL_DEFAULT} |-> ${COL_RESET}${COL_NONE}"
PS2="${COL_DEFAULT}  -> ${COL_RESET}${COL_NONE}"
PS4=' ${BASH_SOURCE}:$FUNCNAME:$LINENO: '

export PS1 PS2 PS4
colors
UMARK='\[\033(0\]l\[\033(B\]'
DMARK='\[\033(0\]m\[\033(B\]'
LMARK='\[\033(0\]q\[\033(B\]'

COL_SL_GREEN='\[\033[32;0m\]'
COL_SD_GREEN='\[\033[32;2m\]'                     # COL_SD_GREEN='\[\033[2;32m\]'
COL_SD_YELLOW='\[\033[2;33m\]'
COL_SL_YELLOW='\[\033[0;33m\]'
COL_B_YELLOW='\[\033[2;33m\]'
COL_IF_SD_YELLOW='\[\033[2\;33m\]'

COL_D_BLACK='\[\033[30;1m\]'
COL_L_BLACK='\[\033[30;0m\]'
COL_SD_BLACK='\[\033[1;30m\]'
COL_SL_BLACK='\[\033[0;30m\]'
COL_SD_RED='\[\033[1;31m\]'
COL_SL_RED='\[\033[0;31m\]'
COL_SD_BLUE='\[\033[1;34m\]'
COL_SL_BLUE='\[\033[0;34m\]'
COL_L_RED='\[\033[31;0m\]'
COL_D_RED='\[\033[31;1m\]'

COL_IF_D_BLACK='\[\033[30\;1m\]'
COL_IF_L_BLACK='\[\033[30\;0m\]'
COL_IF_SD_BLACK='\[\033[1\;30m\]'
COL_IF_SL_BLACK='\[\033[0\;30m\]'
COL_IF_SL_YELLOW='\[\033[0\;33m\]'
COL_IF_D_RED='\[\033[31\;1m\]'
COL_IF_SD_RED='\[\033[1\;31m\]'
COL_IF_SL_RED='\[\033[0\;31m\]'
COL_IF_SL_GREEN='\[\033[0\;32m\]'
COL_IF_SD_GREEN='\[\033[2\;32m\]'
COL_IF_SL_BLUE='\[\033[0\;34m\]'
COL_IF_SD_BLUE='\[\033[1\;34m\]'
deprecated
if [ -z "$DISPLAY" ]; then
  export PS1="\n${COL_D_BLACK}┌─ (\u@\h ${COL_RESET} ${COL_D_RED}\w${COL_RESET}${COL_D_BLACK}) ->${COL_RESET}\n${COL_D_BLACK}└─ ${COL_RESET}\`if [ \$? = 0 ]; then echo ${COL_SD_BLACK}\\$ ${COL_RESET}; else echo ${COL_SD_RED}\\$ ${COL_RESET}; fi\`${COL_NONE}"
  export PS2="${COL_D_BLACK} -> ${COL_RESET}${COL_NONE}"
else
  export PS1="\n${COL_D_BLACK}${UMARK}${LMARK} (\u@\h ${COL_RESET} ${COL_D_RED}\w${COL_RESET}${COL_D_BLACK}) ->${COL_RESET}\n${COL_D_BLACK}${DMARK}${LMARK} ${COL_RESET}\`if [ \$? = 0 ]; then echo ${COL_SD_BLACK}\\$ ${COL_RESET}; else echo ${COL_SD_RED}\\$ ${COL_RESET}; fi\`${COL_NONE}"
  export PS2="${COL_D_BLACK} ->${LMARK} ${COL_RESET}${COL_NONE}"
fi

# for mac os -> Solarized Dark
export PS1="\n${COL_DEFAULT}┌─ (\u@\h${COL_RESET} ${COL_SD_RED}\w${COL_RESET}${COL_DEFAULT}) ->${COL_RESET}\n${COL_DEFAULT}└─ ${COL_RESET}\`if [ \$? = 0 ]; then echo ${COL_IF_DEFAULT}\\$ ${COL_RESET}; else echo ${COL_IF_SL_RED}\\$ ${COL_RESET}; fi\`${COL_NONE}"

colors

  • Bash tips: Colors and formatting (ANSI/VT100 Control sequences)

    # https://misc.flogisoft.com/bash/tip_colors_and_formatting
    #  ----+---+---+----+---+----       --------+--------     ----+------+------
    # | ⌌・| ⠋ | ⌜ | ╭╶ | ┌ | ┌─ |    | 0\;xxm | Normal |    | \e | \033 | \x1B |
    # | ⌎・| ⠦ | ⌞ | ╰╶ | └ | └─ |    | 1\;xxm | Bright |     ----+------+------
    #  ----+---+---+----+---+----      --------+--------
    #
    #  ---+--------------++---------------------++----------------------+    +-----------------
    # | # |  color name  ||      foregrand      ||      background      |    |   attribute     |
    #  ---+--------------++---------------------++----------------------+    +---+-------------
    # | 9 | Default      ||      \e[0;39m       ||       e[0;49m        |    | 0 | Normal      |
    # | 0 | Black        || \e[0;30m |          || \e[0;40m |           |    | 1 | Bold        |
    # | 1 | Red          || \e[0;31m |          || \e[0;41m |           |    | 2 | Faint       |
    # | 2 | Green        || \e[0;32m |          || \e[0;42m |           |    | 3 | Italic      |
    # | 3 | Yellow       || \e[0;33m |          || \e[0;43m |           |    | 4 | Underline   |
    # | 4 | Blue         || \e[0;34m |          || \e[0;44m |           |    | 5 | Slow Blink  |
    # | 5 | Purple       || \e[0;35m |          || \e[0;45m |           |    | 6 | Rapid Blink |
    # | 6 | Cyan         || \e[0;36m |          || \e[0;46m |           |    | 7 | reverse     |
    # | 7 | Light Gray   || \e[0;37m |          || \e[0;47m |           |     ---+-------------
    # | 0 | Dark Gray    || \e[1;30m | \e[0;90m || \e[1;40m | \e[0;100m |     ---------------------
    # | 1 | Light Red    || \e[1;31m | \e[0;91m || \e[1;41m | \e[0;101m |    |       others        |
    # | 2 | Light Green  || \e[1;32m | \e[0;92m || \e[1;42m | \e[0;102m |     -------------+-------
    # | 3 | Light Yellow || \e[1;33m | \e[0;93m || \e[1;43m | \e[0;103m |    | color none  | \e[0m |
    # | 4 | Light Blue   || \e[1;34m | \e[0;94m || \e[1;44m | \e[0;104m |    | color reset | \e[1m |
    # | 5 | Light Purple || \e[1;35m | \e[0;95m || \e[1;45m | \e[0;105m |     -------------+-------
    # | 6 | Light Cyan   || \e[1;36m | \e[0;96m || \e[1;46m | \e[0;106m |     ----------------+---------------------------------
    # | 7 | White        ||      \e[0;97m       ||      \e[0;107m       |    | \[\e[xx;0m]\]  | system color                    |
    #  ---+--------------++---------------------++----------------------     | \[\e[xx\;0m]\] | system color in ``              |
    # |   |              ||  \e[38;5;{0..255}m# ||  \e[48;5;{0..255}m#  |    | \[\e[0;xxm]\]  | profile color (solarized)       |
    #  ---+--------------++---------------------++----------------------     | \[\e[0\;xxm]\] | profile color (solarized) in `` |
    #                                                                         ----------------+---------------------------------
    #  ------------+-------------------------------------------------------   ----------------+-------------------------------
    # |                   256 colors standard format                       | |                   comments                     |
    #  ------------+-------------------------------------------------------   ----------------+-------------------------------
    # | attributes | 0 1 2 3 4 5 6 7                                       | | COL_NONE       | no color                      |
    # | foregrand  | {0..255}                                              | | COL_RESET      | reset color                   |
    # | background | {30..37} {90..97} 39                                  | | COL_DEFAULT    | default color                 |
    #  ------------+-------------------------------------------------------  | COL_IF_DEFAULT | default color in if-statement |
    # | usage : \e[${attributes};{background};5;${foreground}m <str> \e[0m |  ----------------+-------------------------------
    #  ------------+-------------------------------------------------------
    #
    # Solarized color table from http://ethanschoonover.com/solarized.
    #
    # | SOLARIZED | HEX     | ANSI      | TERMCOL   | cmd.exe     | PowerShell  | ColorTable | DWORD    |
    # |-----------|---------|-----------|-----------|-------------|-------------|------------|----------|
    # | base03    | #002b36 | ESC[0;30m | brblack   | Black       | Black       | 00         | 00362b00 |
    # | base02    | #073642 | ESC[1;30m | black     | Gray        | DarkGray    | 08         | 00423607 |
    # | base01    | #586e75 | ESC[0;32m | brgreen   | Green       | DarkGreen   | 02         | 00756e58 |
    # | base00    | #657b83 | ESC[0;33m | bryellow  | Yellow      | DarkYellow  | 06         | 00837b65 |
    # | base0     | #839496 | ESC[0;34m | brblue    | Blue        | DarkBlue    | 01         | 00969483 |
    # | base1     | #93a1a1 | ESC[0;36m | brcyan    | Aqua        | DarkCyan    | 03         | 00a1a193 |
    # | base2     | #eee8d5 | ESC[0;37m | white     | White       | Gray        | 07         | 00d5e8ee |
    # | base3     | #fdf6e3 | ESC[1;37m | brwhite   | BrightWhite | White       | 15         | 00e3f6fd |
    # | yellow    | #b58900 | ESC[1;33m | yellow    | LightYellow | Yellow      | 14         | 000089b5 |
    # | orange    | #cb4b16 | ESC[0;31m | brred     | Red         | DarkRed     | 04         | 00164bcb |
    # | red       | #dc322f | ESC[1;31m | red       | LightRed    | Red         | 12         | 002f32dc |
    # | magenta   | #d33682 | ESC[1;35m | magenta   | LightPurple | Magenta     | 13         | 008236d3 |
    # | violet    | #6c71c4 | ESC[0;35m | brmagenta | Purple      | DarkMagenta | 05         | 00c4716c |
    # | blue      | #268bd2 | ESC[1;34m | blue      | LightBlue   | Blue        | 09         | 00d28b26 |
    # | cyan      | #2aa198 | ESC[1;36m | cyan      | LightAqua   | Cyan        | 11         | 0098a12a |
    # | green     | #859900 | ESC[1;32m | green     | LightGreen  | Green       | 10         | 00009985 |
    
    PS1="\[$(tput setaf 0) \]my prompt\[$(tput sgr0)\]> "
    PS1="\[$(tput setaf 1) \]my prompt\[$(tput sgr0)\]> "
    PS1="\[$(tput setaf 2) \]my prompt\[$(tput sgr0)\]> "
    PS1="\[$(tput setaf 3) \]my prompt\[$(tput sgr0)\]> "
    PS1="\[$(tput setaf 4) \]my prompt\[$(tput sgr0)\]> "
    PS1="\[$(tput setaf 5) \]my prompt\[$(tput sgr0)\]> "
    PS1="\[$(tput setaf 6) \]my prompt\[$(tput sgr0)\]> "
    PS1="\[$(tput setaf 7) \]my prompt\[$(tput sgr0)\]> "
    PS1="\[$(tput setaf 8) \]my prompt\[$(tput sgr0)\]> "
    PS1="\[$(tput setaf 9) \]my prompt\[$(tput sgr0)\]> "
    PS1="\[$(tput setaf 10)\]my prompt\[$(tput sgr0)\]> "
    PS1="\[$(tput setaf 11)\]my prompt\[$(tput sgr0)\]> "
    PS1="\[$(tput setaf 12)\]my prompt\[$(tput sgr0)\]> "
    PS1="\[$(tput setaf 13)\]my prompt\[$(tput sgr0)\]> "
    PS1="\[$(tput setaf 14)\]my prompt\[$(tput sgr0)\]> "
    PS1="\[$(tput setaf 15)\]my prompt\[$(tput sgr0)\]> "
    PS1="\[$(tput setaf 16)\]my prompt\[$(tput sgr0)\]> "
    PS1="\[$(tput setaf 17)\]my prompt\[$(tput sgr0)\]> "
    PS1="\[$(tput setaf 18)\]my prompt\[$(tput sgr0)\]> "
    
    bash ps1
    1.8.2 -- bash ps1
    • or ps1 with conditions

      $ DEFAULT="\[$(tput setaf 3)\]"         # or '\[\033[1;38;5;3m\]'     or '\[\e[1;33m\]'
      $ ifDEFAULT='\[\e[1\;33m\]'             # or '\[\033[1\;38\;5\;3m\]'
      $ ifRED='\[\e[1\;31m\]'                 # or '\[\033[1\;38\;5\;1m\]'
      $ PS1="${DEFAULT}my prompt${RESET} \$( if [ \$? != 0 ]; then echo -e ${ifRED}\\$; else echo -e ${ifDEFAULT}\\$; fi) ${RESET}"
      
      bash ps1 in conditional
      1.8.3 -- bash ps1 in conditional

functions

function showKeyMap() { RET=$?; bind -v | awk '/keymap/ {print $NF}'; return "${RET}"; }
function rightPrompt() {
  if [ "$(bind -v | awk '/keymap/ {print $NF}')" == 'emacs' ]; then
    kmap=else
    kmap=⚙︎
  fi
  printf "%*s" $COLUMNS "${kmap}"
}

PS1+="- [\$(showKeyMap)] "
PS1+="- [\$(tput sc; rightPrompt; tput rc)]"
  • right prompt

    rightprompt() { printf "%*s" $COLUMNS "right prompt"; }
    PS1='\[$(tput sc; rightprompt; tput rc)\]left prompt > '
    
    bash ps1 right-prompt
    1.8.4 -- bash ps1 right-prompt

character

metacharacter

Character Where Meaning
<RETURN> csh, sh Execute command
# csh, sh, ASCII files Start a comment
<SPACE> csh, sh Argument separator
` csh, sh Command substitution
" csh, sh Weak Quotes
' csh, sh Strong Quotes
\ csh, sh Single Character Quote
variable sh, csh Variable
variable csh, sh Same as variable
\ csh, sh Pipe character
^ sh Pipe Character
& csh, sh Run program in background
? csh, sh Match one character
* csh, sh Match any number of characters
; csh, sh Command separator
;; sh End of Case statement
~ csh Home Directory
~user csh User's Home Directory
! csh History of Commands
- Programs Start of optional argument
$# csh, sh Number of arguments to script
$* csh, sh Arguments to script
$@ sh Original arguments to script
$- sh Flags passed to shell
$? sh Status of previous command
$$ sh Process identification number
$! sh PID of last background job
&& sh Short-circuit AND
|| sh Short-circuit OR
. csh, sh Typ. filename extension
. sh Source a file and execute as command
: sh Nothing command
: sh Separates Values in environment variables
: csh Variable modifier
Character Where Meaning
[ ] csh, sh Match range of characters
[ ] sh Test
%job csh Identifies job Number
(cmd;cmd) csh. sh Runs cmd;cmd as a sub-shell
{ } csh In-line expansions
{cmd;cmd } sh Like (cmd;cmd ) without a subshell
>ofile csh, sh Standard output
>>ofile csh, sh Append to standard output
<ifile csh, sh Standard Input
<<word csh, sh Read until word, substitute variables
<<\word csh, sh Read until word, no substitution
<<-word sh Read until word, ignoring TABS
>>!file csh Append to file, ignore error if not there
>!file csh Output to new file, ignore error if not there
>&file csh Send standard & error output to file
<&digit sh Switch Standard Input to file
<&- sh Close Standard Input
>&digit sh Switch Standard Output to file
>&- sh Close Standard Output
digit1<&digit2 sh Connect digit2 to digit1
digit<&- sh Close file digit
digit2>&digit1 sh Connect digit2 to digit1
digit>&- sh Close file digit

process substitution

[!TIP] Process substitution is a form of redirection where the input or output of a process (some sequence of commands) appear as a temporary file. reference:

  • chapter 23. process substitution Command list enclosed within parentheses
    >(command_list)
    <(command_list)
    
    Process substitution uses /dev/fd/<n> files to send the results of the process(es) within parentheses to another process. [1]
$ while read branch; do
    git fetch --all --force;
  done < <(git rev-parse --abbrev-ref HEAD)
$ echo >(true)
/dev/fd/63
$ echo <(true)
/dev/fd/63

$ echo >(true) <(true)
/dev/fd/63 /dev/fd/62

$ echo <(date)
/dev/fd/63
$ cat <(date)
Tue Dec 15 22:32:08 CST 2020

named pipe similar

$ wc <(cat /usr/share/dict/words)
 235886  235886 2493109 /dev/fd/63
$ cat /usr/share/dict/words | wc
 235886  235886 249310

$ wc <(grep script /usr/share/dict/words)
    176     176    5414 /dev/fd/63
$ grep script /usr/share/dict/words | wc
    176     176    5414

# https://superuser.com/a/1059790/112396
$ cat file | while read line; do ((count++)); done
$ while read line; do ((count++)); done < <(cat file)

get diff in two folders

$ diff <(ls ibook) <(ls mbook)
1c1,2
< book.json
---
> _book
> book.json
3a5
> node_modules

$ diff <(sleep 4; date) <(sleep 5; date)
1c1
< Tue Dec 15 22:48:31 CST 2020
---
> Tue Dec 15 22:48:32 CST 2020

example: run script without download

$ bash < <(wget -qO - https://raw.githubusercontent.com/ubports/unity8-desktop-install-tools/master/install.sh)
  • via curl

    $ python < <(curl -s https://raw.githubusercontent.com/giampaolo/psutil/master/scripts/meminfo.py)
    
    • or
      $ curl -so - https://raw.githubusercontent.com/giampaolo/psutil/master/scripts/meminfo.py | python
      
  • via wget

    $ python < <(wget -O - -q -t 1 https://raw.githubusercontent.com/giampaolo/psutil/master/scripts/meminfo.py)
    
    • or
      $ wget -qO - https://raw.githubusercontent.com/giampaolo/psutil/master/scripts/meminfo.py | python
      
  • or

    $ python < <(curl -s https://raw.githubusercontent.com/giampaolo/psutil/master/scripts/meminfo.py)
    MEMORY
    ------
    Total      :   16.0G
    Available  :    5.8G
    Percent    :    63.8
    Used       :    8.8G
    Free       :  204.5M
    Active     :    5.4G
    Inactive   :    5.6G
    Wired      :    3.4G
    
    SWAP
    ----
    Total      :    1.0G
    Used       :  269.2M
    Free       :  754.8M
    Percent    :    26.3
    Sin        :   38.3G
    Sout       :   63.9M
    
  • or

    $ python < <(curl -s https://raw.githubusercontent.com/giampaolo/psutil/master/scripts/disk_usage.py)
    Device               Total     Used     Free  Use %      Type  Mount
    /dev/disk1s5        233.5G    10.6G    70.6G    13%      apfs  /
    /dev/disk1s1        233.5G   149.6G    70.6G    67%      apfs  /System/Volumes/Data
    /dev/disk1s4        233.5G     2.0G    70.6G     2%      apfs  /private/var/vm
    /Library/Input Methods/SogouInput.app   233.5G   149.1G    71.1G    67%    nullfs  /private/var/folders/s3/mg_f3cv54nn7y758j_t46zt40000gn/T/AppTranslocation/E29031DE-FE63-4ABC-BA3D-E99C743E57D2
    

example: merge lines of file

inspired by here and here

$ cat a
t1
t2
t3
t4
$ cat b
11
22
33
44

$ paste <(cat a) <(cat b)
t1  11
t2  22
t3  33
t4  44
  • additional usage

    $ cat a | paste -d'\t' - - - -
    a b c d
    e f
    

    or

    $ cat a | paste -d, - -
    a,b
    c,d
    e,f
    
  • /dev/fd/63 is not a regular file

    $ more <( ls -l )
    /dev/fd/63 is not a regular file (use -f to see it)
    
    $ more -f <( ls -l )
    total 12
    -rw-r--r--  1 marslo staff 3457 Nov  2 15:53 README.md
    -rw-r--r--  1 marslo staff 4314 Nov  2 15:53 SUMMARY.md
    drwxr-xr-x  6 marslo staff  192 Oct 12 22:10 artifactory
    drwxr-xr-x  8 marslo staff  256 Sep 29 17:40 cheatsheet
    drwxr-xr-x 12 marslo staff  384 Oct 15 15:27 devops
    drwxr-xr-x  7 marslo staff  224 Sep 28 22:17 jenkins
    drwxr-xr-x 11 marslo staff  352 Sep 28 22:17 kubernetes
    drwxr-xr-x  8 marslo staff  256 Oct  9 19:15 linux
    drwxr-xr-x  8 marslo staff  256 Nov  2 15:51 osx
    drwxr-xr-x  6 marslo staff  192 Sep 28 22:17 programming
    drwxr-xr-x 45 marslo staff 1440 Nov  2 13:56 screenshot
    drwxr-xr-x 11 marslo staff  352 Oct 14 21:22 tools
    

nstalling tools via running random scripts from unknown sites

$ ( wget -O - pi.dk/3 || lynx -source pi.dk/3 || curl pi.dk/3/ || \
    fetch -o - http://pi.dk/3 ) > install.sh

strace

[!NOTE|label:reference]

basic commands

du

  • top biggest directories under [path]
    $ du -a [path] | sort -n -r | head -n 5
    
  • display the largest files according to human-readable format
    $ du -hs * | sort -rh | head -5
    
  • display the largest folders/files including the sub-directories
    $ du -Sh | sort -rh | head -5
    
  • biggest file sizes

    $ find -type f -exec du -Sh {} + | sort -rh | head -n 5
    
    # or
    $ find [PATH] -type f -printf "%s %p\n" | sort -rn | head -n 5
    

sort

sort result via human-readable format

$ sudo du -ahx --max-depth=1 <path> | sort -k1 -rh
  • or

    $ du -sk * | sort -g | awk '{
    
        numBytes = $1 * 1024;
        numUnits = split("B K M G T P", unit);
        num = numBytes;
        iUnit = 0;
    
        while(num >= 1024 && iUnit + 1 < numUnits) {
            num = num / 1024;
            iUnit++;
        }
    
        $1 = sprintf( ((num == 0) ? "%6d%s " : "%6.1f%s "), num, unit[iUnit + 1]);
        print $0;
    
    }'
    
    # or in one-line
    $ du -sk * | sort -g | awk '{ numBytes = $1 * 1024; numUnits = split("B K M G T P", unit); num = numBytes; iUnit = 0; while(num >= 1024 && iUnit + 1 < numUnits) { num = num / 1024; iUnit++; } $1 = sprintf( ((num == 0) ? "%6d%s " : "%6.1f%s "), num, unit[iUnit + 1]); print $0; }'
    
  • or

    #! /usr/bin/env bash
    ducks () {
        du -cks -x | sort -n | while read size fname; do
            for unit in k M G T P E Z Y; do
                if [ $size -lt 1024 ]; then
                    echo -e "${size}${unit}\t${fname}"
                    break
                fi
                size=$((size/1024))
            done
        done
    }
    ducks > .ducks && tail .ducks
    
  • or

    $ du -k ./* |
         sort -nr |
         awk '{ split("KB,MB,GB",size,","); }
              { x = 1;while ($1 >= 1024) {$1 = $1 / 1024;x = x + 1} $1 = sprintf("%-4.2f%s", $1, size[x]); print $0; }'
    

others

you have new mail

  • remove

    $ mail
    ? delete *
    No applicable messages.
    ? q
    
    • or
      $ mail -N
      ? d *
      ? quit
      

crontab

[!NOTE|label:references:]

  • format

    *    *    *    *    *   /home/user/bin/somecommand.sh
    |    |    |    |    |            |
    |    |    |    |    |    Command or Script to execute
    |    |    |    |    |
    |    |    |    | Day of week(0-6 | Sun-Sat)
    |    |    |    |
    |    |    |  Month(1-12)
    |    |    |
    |    |  Day of Month(1-31)
    |    |
    |   Hour(0-23)
    |
    Min(0-59)
    
  • tips

    • every odd hours
      0 1-23/2 * * *
      
    • every even hours
      0 */2 * * *
      
  • sample

    • delete .DS_

      15 */2 * * 1-5 /usr/local/bin/fd --type f --hidden --follow --unrestricted --color=never --exclude .Trash --glob '*\.DS_*' $HOME | xargs -r rm
      # or
      30 */2 * * 1-5 /usr/local/bin/fd -Iu --glob '*\.DS_*' $HOME | xargs -r rm
      # or
      30 */2 * * 1-5 /usr/local/bin/rg --hidden --smart-case --color=never --files "$HOME" -g '*\.DS_*' | xargs -r rm
      
    • flush disk cache

      0 * * * * /usr/sbin/purge
      
    • flush DNS

      0 1-23/6 * * * /usr/bin/killall -HUP mDNSResponder
      
  • localtion

    • macos

      $ sudo ls -Altrh /usr/lib/cron/tabs/<USERNAME>
      
    • freebsd/openbsd/netbsd

      $ sudo ls -Altrh /var/cron/tabs/<USERNAME>
      
    • centos/rhel/fedora/scientific/rocky/alma linux

      $ sudo ls -Altrh /var/spool/cron/<USERNAME>
      
    • debian/ubuntu/mint linux

      $ sudo ls -Altrh /var/spool/cron/crontabs/<USERNAME>
      
    • p-ux unix

      $ sudo ls -Altrh /var/spool/cron/crontabs/<USERNAME>
      
    • ibm aix unix

      $ sudo ls -Altrh /var/spool/cron/<USERNAME>
      

tricky

unicode

references:

Your description is vague. Either your terminal does not support Unicode, or you're editing a file that's in latin1 (= iso-8859-1). On a bash or zsh shell, with LC_CTYPE=en_US.UTF-8, run echo $'\xc3a9'. If you see é, your terminal supports UTF-8. If you see é, your terminal is showing latin1.

$ echo $'\xc3a9'
a9
$ echo $'\xe2\x98\xa0'
☠
$ echo $'(U+2620) \U02620' | xxd
00000000: 2855 2b32 3632 3029 20e2 98a0 0a         (U+2620) ....

# test
for (( i=0x2500; i<0x2600; i++ )); do
    UnicodePointToUtf8 $i
    [ "$(( i+1 & 0x1f ))" != 0 ] || echo ""
done
─━│┃┄┅┆┇┈┉┊┋┌┍┎┏┐┑┒┓└┕┖┗┘┙┚┛├┝┞┟
┠┡┢┣┤┥┦┧┨┩┪┫┬┭┮┯┰┱┲┳┴┵┶┷┸┹┺┻┼┽┾┿
╀╁╂╃╄╅╆╇╈╉╊╋╌╍╎╏═║╒╓╔╕╖╗╘╙╚╛╜╝╞╟
╠╡╢╣╤╥╦╧╨╩╪╫╬╭╮╯╰╱╲╳╴╵╶╷╸╹╺╻╼╽╾╿
▀▁▂▃▄▅▆▇█▉▊▋▌▍▎▏▐░▒▓▔▕▖▗▘▙▚▛▜▝▞▟
■□▢▣▤▥▦▧▨▩▪▫▬▭▮▯▰▱▲△▴▵▶▷▸▹►▻▼▽▾▿
◀◁◂◃◄◅◆◇◈◉◊○◌◍◎●◐◑◒◓◔◕◖◗◘◙◚◛◜◝◞◟
◠◡◢◣◤◥◦◧◨◩◪◫◬◭◮◯◰◱◲◳◴◵◶◷◸◹◺◻◼◽◾◿

## test harness
for (( i=0x2500; i<0x2600; i++ )); do
    unichr $i
done
─━│┃┄┅┆┇┈┉┊┋┌┍┎┏┐┑┒┓└┕┖┗┘┙┚┛├┝┞┟┠┡┢┣┤┥┦┧┨┩┪┫┬┭┮┯┰┱┲┳┴┵┶┷┸┹┺┻┼┽┾┿╀╁╂╃╄╅╆╇╈╉╊╋╌╍╎╏═║╒╓╔╕╖╗╘╙╚╛╜╝╞╟╠╡╢╣╤╥╦╧╨╩╪╫╬╭╮╯╰╱╲╳╴╵╶╷╸╹╺╻╼╽╾╿▀▁▂▃▄▅▆▇█▉▊▋▌▍▎▏▐░▒▓▔▕▖▗▘▙▚▛▜▝▞▟■□▢▣▤▥▦▧▨▩▪▫▬▭▮▯▰▱▲△▴▵▶▷▸▹►▻▼▽▾▿◀◁◂◃◄◅◆◇◈◉◊○◌◍◎●◐◑◒◓◔◕◖◗◘◙◚◛◜◝◞◟◠◡◢◣◤◥◦◧◨◩◪◫◬◭◮◯◰◱◲◳◴◵◶◷◸◹◺◻◼◽◾◿

# https://stackoverflow.com/a/55639328/2940319
$ echo -e "\U1F304"
�
$ echo -e "�" | hexdump -C
00000000  f0 9f 8c 84 0a                                    |.....|
00000005
  • unicode to utf8

    UnicodePointToUtf8()
    {
        local x="$1"               # ok if '0x2620'
        x=${x/\\u/0x}              # '\u2620' -> '0x2620'
        x=${x/U+/0x}; x=${x/u+/0x} # 'U-2620' -> '0x2620'
        x=$((x)) # from hex to decimal
        local y=$x n=0
        [ $x -ge 0 ] || return 1
        while [ $y -gt 0 ]; do y=$((y>>1)); n=$((n+1)); done
        if [ $n -le 7 ]; then       # 7
            y=$x
        elif [ $n -le 11 ]; then    # 5+6
            y=" $(( ((x>> 6)&0x1F)+0xC0 )) \
                $(( (x&0x3F)+0x80 ))"
        elif [ $n -le 16 ]; then    # 4+6+6
            y=" $(( ((x>>12)&0x0F)+0xE0 )) \
                $(( ((x>> 6)&0x3F)+0x80 )) \
                $(( (x&0x3F)+0x80 ))"
        else                        # 3+6+6+6
            y=" $(( ((x>>18)&0x07)+0xF0 )) \
                $(( ((x>>12)&0x3F)+0x80 )) \
                $(( ((x>> 6)&0x3F)+0x80 )) \
                $(( (x&0x3F)+0x80 ))"
        fi
        printf -v y '\\x%x' $y
        echo -n -e $y
    }
    
  • unichr

    fast_chr() {
        local __octal
        local __char
        printf -v __octal '%03o' $1
        printf -v __char \\$__octal
        REPLY=$__char
    }
    
    function unichr {
        local c=$1  # Ordinal of char
        local l=0   # Byte ctr
        local o=63  # Ceiling
        local p=128 # Accum. bits
        local s=''  # Output string
    
        (( c < 0x80 )) && { fast_chr "$c"; echo -n "$REPLY"; return; }
    
        while (( c > o )); do
            fast_chr $(( t = 0x80 | c & 0x3f ))
            s="$REPLY$s"
            (( c >>= 6, l++, p += o+1, o>>=1 ))
        done
    
        fast_chr $(( t = p | c ))
        echo -n "$REPLY$s"
    }
    

useful functions

  • universal extract
    function extract () {
      case $1 in
        *.tar.bz2) tar xjf "$1" ;;
        *.tar.gz) tar xzf "$1" ;;
        *.tar.xz) tar Jxvf "$1" ;;
        *.tar.Z) tar xzf "$1" ;;
        *.tar) tar xf "$1" ;;
        *.taz) tar xzf "$1" ;;
        *.tb2) tar xjf "$1" ;;
        *.tbz) tar xjf "$1" ;;
        *.tbz2) tar xjf "$1" ;;
        *.tgz) tar xzf "$1" ;;
        *.txz) tar Jxvf "$1" ;;
        *.zip) unzip "$1" ;;
        *.gz) gunzip "$1" ;;
        *) echo "'$1' cannot be extracted" ;;
      esac
    }
    

    search manual page

  • whatis -r <string>
  • apropos -r <string>
Copyright © marslo 2020-2023 all right reserved,powered by GitbookLast Modified: 2024-03-12 15:01:28

results matching ""

    No results matching ""