Linux

Architecture

   Application
Shell | Lib Function
   System Call
   Kernel

I/O

传统 I/O (缓存 I/O):硬盘 -> 内核缓冲区 -> 用户缓冲区 -> 内核 socket 缓冲区 -> 协议引擎

sendfile: 硬盘 -> 内核缓冲区 -> 内核 socket 缓冲区 -> 协议引擎

sendfile(DMA 收集拷贝):硬盘 -> 内核缓冲区 -> 协议引擎

零拷贝技术 - sendfile

#include <sys/sendfile.h>
ssize_t snedfile(int out_fd, int in_fd, off_t* offset, size_t count);

sendfile 函数是在两个文件描述符中直接传递数据(完全在内核中操作),从而避免了用户缓冲区和内核缓冲区之间的数据拷贝,操作效率很高。

Process

Useful Command

Let non-root/normal user use port which less then 1024

setcap cap_net_bind_service=+eip /path/executable_program

Recover files on Linux

# backup sector
dd if=/path/filename of=/dev/vda1
# unmount
umount /dev/vda1
# force unmount
fuser -m -v -i -k /dev/vda1
# scan
extundelete --inode 2 /dev/vda1
# recover
extundelete /dev/vda1 --restore-all

Safe rm

#!/usr/bin/env bash
args=""

while getopts ':rRfiIdv' OPTION; do
    case "$OPTION" in
        r|R|\?)
            ;;

        *)
            args="$args$OPTION"
            ;;
    esac
done

shift "$(($OPTIND - 1))"

if [[ -n "$args" ]]; then
    mv "-$args" "$@" /opt/data/.trash
else
    mv "$@" /opt/data/.trash
fi

Read file

#!/usr/bin/env bash

# This is correct way to read file.
FILE="./tmp/use.py"
k=1

while read -r line; do
    echo "$line"
    ((k++))
done < "$FILE"

echo "Total number of lines in file: $k"

utils tools

Rust-alternatives [3/7]:

  • [ ] ls -> exa
  • [X] grep -> ripgrep
  • [X] find -> fd
  • [X] less -> bat
  • [ ] ps -> procs
  • [ ] (count code) -> tokei
  • [ ] du -> dust

Set timezone

timedatectl list-timezones | grep Shanghai
sudo timedatectl set-timezone Asia/Shanghai

git completely remove files

git filter-branch --force --index-filter 'git rm -r --cached --ignore-unmatch <files>' --prune-empty --tag-name-filter cat -- --all
git rm --cached -r .
git gc --prune

# repack
git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
git reflog expire --expire=now --all
git gc --prune=now

xargs

short parameter long parameter caption
-d   指定分隔符,默认是回车
-I / -i --replace 将传给 xargs 的参数,赋值给一个指定的值,默认为 {}
-0   指定 \0 为分隔符,可配合 find-print0 使用
湘ICP备19014083号-1