DAILY DOCDAILY DOC
Rust
Node
Notes
Ubuntu
Leetcode
  • it-tools
  • excalidraw
  • linux-command
Rust
Node
Notes
Ubuntu
Leetcode
  • it-tools
  • excalidraw
  • linux-command
  • linux
  • bash alias
  • chmod
  • linux useful command
  • date
  • extract translation from git diff
  • fail2ban
  • globbing
  • localhost
  • mail
  • memo 内存测试
  • nohup(no hang up)
  • setup env
  • ssh

    • ssh 教程
    • github clone
    • ssh.localhost.run 端口转发
    • ssh 安全
  • systemd service
  • 分析ubuntu系统登录日志文件
  • vpn

    • vpn 教程
    • Algo
    • clashX
    • firezone
    • lantern
    • pac 代理配置
    • Setup vpn
    • shadowsocks
    • VPN
    • VPN 速度优化
    • wireguard cron
    • 修改wireguard端口
  • webhook

linux useful command

rm -rf

rm -rf ./  # remove folder
rm -rf ./* # remove file in folder
rm -rf ./*.log # remove file in folder ends with .log

反向删除

grep log 过滤包含 log 的文件

grep -v 'log' 过滤不包含 log的文件

rm `ls | grep -v "log"` 

查看磁盘空间

# 查看当前文件夹 -h human readable 
du -sh ./ 
du -sh ./ --max-depth=0
# 查看系统
df -h

批量重命名文件后缀

#!/usr/bin/env bash
# Rename all *.js to *.ts
for f in ./**/*.js; do
  mv -- "$f" "${f%.js}.ts"
done

提取指定字段,替换制定字段value

ListenPort=3004
# ==================================
PreviousListenPort=$(grep '^ListenPort' /etc/xx/x.conf | cut -d " " -f 3)
# Replace the ListenPort value in /etc/x/x.conf
sed -i "s/ListenPort.*/ListenPort = $ListenPort/g" /etc/xx/x.conf
# Verify the change
grep "ListenPort" /etc/xx/x.conf

删除文件中 “---”包含的部分

details
#!/bin/bash

# 遍历当前目录中 .md 结尾的文件
for file in *.md; do
  # 判断文件是否存在且有读写权限
  if [[ -f "$file" ]] && [[ -r "$file" ]] && [[ -w "$file" ]]; then
    # 判断文件头部是否包含 "---"
    if grep -q "^---$" "$file" && grep -qx "\-\-\-" <(head -n 2 "$file"); then
      echo $file

      if [[ $(uname) == "Darwin" ]]; then
        sed -i.bak '/^---$/,/^---$/d' $file
        # macOS 命令
      else
        # echo "Running on Linux"
        sed -i '/^---$/,/^---$/d' $file

        # Linux 命令
      fi

      # # 删除头部信息
      # awk '/^---$/ {p++} p<2' "$file" >"$file.tmp"
      # mv "$file.tmp" "$file"
    else
      # 文件没有头部信息,不执行任何操作
      continue
    fi
  fi
done

# 删除mac 备份文件
rm -rf ./*.bak

查找系统中的大文件

bash
# 查找超过 100M 的文件
find / -type f -size +100M 
# 查找日志存档
find / -type f -name "*.gz"

# 查找gz 日志,统计占用磁盘空间
find /var/log -name '*.gz' -exec du -ch {} + | grep total$

du ./ -h --max-depth=1 |sort -hr
tree -h -L 2

awk

bash
# transform seconds into hh:mm:ss
echo 60 | awk '{printf "%02d:%02d:%02d\n", $1/3600, ($1%3600)/60, $1%60}'
# 把字节转成 MB
echo 10240 | awk '{printf "%.2f MB\n", $1/1024/1024}' 

curl 并发请求调试

seq 21 | xargs -n1 -P10 -I{} curl -v http://example.com >> log.log

git delete tag

# 删除本地标签
git tag -l 'v2.2.*' | xargs -n 1 git tag -d

# 删除远程标签
git tag -l 'v2.2.*' | xargs -n 1 -I {} git push origin :refs/tags/{}
Last Updated:
Contributors: rosendo
Prev
chmod
Next
date