Written with StackEdit.

Common

查询一个文件夹及其子文件夹下的所有文件中是否包含某个字符串

powershell
1
Get-ChildItem -Path "." -Recurse | Select-String -Pattern ""

查询当前目录中的文件总数

powershell
1
(Get-Childitem -Path . -File | Measure-Object).Count

端口查询 PID

powershell
1
netstat -ano | findstr "<port_ID>"

PID 查询进程

powershell
1
tasklist | findstr "<PID>"

开启 / 关闭 ipv6 临时地址 ( 需重启网卡 )[1]

powershell
1
netsh interface ipv6 set privacy state=[disable | enable]

打印 CPU 占用最高的 10 进程 ( ? )

powershell
1
Get-Process | Sort-Object -Property CPU -Descending | Select -First 10 | Format-Table -Property ID, Name, CPU

更改一个文件夹及其子文件夹下所有文件的最后修改时间

powershell
1
Get-ChildItem -Path . -Recurse | ForEach-Object { $_.LastWriteTime = Get-Date }

打印文件结构

powershell
1
Tree . /f

打印当前文件夹下所有文件名,筛选后缀并打印行号

powershell
1
2
# /n 用于打印行号
Get-ChildItem -File | Select-Object -ExpandProperty Name | findStr /n 'str'

压缩 .zip[2]

powershell
1
Compress-Archive -Path ./dir -DestinationPath ./dir.zip
  • 压不了需分卷的特大文件

列出当前目录下所有子目录的大小

powershell
1
2
3
4
5
6
7
Get-ChildItem -Directory | ForEach-Object {
$dirSize = Get-ChildItem $_.FullName -Recurse | Measure-Object -Property Length -Sum
[PSCustomObject]@{
Directory = $_.FullName
Size = $dirSize.Sum / 1MB # 将字节转换为 MB
}
}

删除文件夹。本来是用于删除需要管理员权限的某个目录,执行该命令后能删除一般文件和文件夹,但仍然无法完全删空 .git,这部分还是要手动删除。

powershell
1
Remove-Item -Path C:\Example -Recurse

文件互传。在 Windows 下使用 scp 需有 OpenSSH。WIn10-Arm64 WSL1 日常找不到命令,需在命令行执行。

bash
1
2
# 远程拉到本地
scp -r remote_user@remote_ip:file_source file_target
  • 参数
    • -P: 配置为 SSH 端口
  • e.g.
    • scp -r admin@192.168.1.3:D:\Docs C:\Users\user\Documents\Docs
      
      plaintext
      1
      2
      3
      4

      ## WSL

      列出所有内核

wsl --list --verbose
wsl -l -v

plaintext
1
2

关闭子系统

wsl --terminate Ubuntu-22.04

plaintext
1
2

关闭 WSL

wsl --shutdown

plaintext
1
2
3
4
5
6

## SSH

跳板连接
```bash
ssh uname@target_ip -p 22 -J uname@proxy_ip:22

Git

回滚当前分支下所有未暂存的修改

bash
1
git checkout .

Undo git add .

bash
1
git reset

对比 diff

bash
1
git diff origin/<branch> [file]

clone 指定分支

bash
1
git clone -b <branch> <repo>

获取指定 commit 的仓库

bash
1
2
# clone 当前仓库后
git checkout <commit_hash>

git 能够识别文件移动和重命名,但应该有一些条件。

bash
1
2
rename docs/articles/LeetCode/{Vol.1.md => Vol.01.md} (97%)
rename docs/journals/{ => 2024}/Apr.md (98%)

解决 .gitignore 无效。一般是添加路径之前,再该路径下已经有文件被跟踪,这样即使添加了 .gitignore 也不会移除跟踪。

bash
1
2
# 手动移除 git 跟踪
git rm --cached </gitignore/path>

git 配置系统代理 (以 Clash 为例)

bash
1
2
git config --global http.proxy 'http://127.0.0.1:7890'
git config --global https.proxy 'https://127.0.0.1:7890'

解决 git push

bash
1
2
# hosts 文件添加解析,可使用 DNS 查询网站确认
151.101.72.249 github.global.ssl.fastly.net

修改后其实还是较慢 (~200KB),但至少不会 hung unexpectedly 了。一说开 TUN 代理,尝试后亦无明显提速。

有时误推送大文件,或在 git 跟踪某些文件后才添加 .gitignore,希望重整仓库时,可使用 reset 强制删掉部分提交

bash
1
2
git reset --hard <commit>
git push --force origin <branch>

然后把之前移走的文件搬回来,重新 push 即可。

Pip

查看已安装的包版本

plaintext
1
2
3
pip list | findstr <package>
--- or
pip show <package>

Screen

开一个终端

bash
1
screen  # 我喜欢不带参数

Detach

bash
1
Ctrl + A D

查看

bash
1
screen -ls`

Attach

bash
1
screen -r <ID>

Kill

bash
1
screen -XS <ID> quit

GCC / G++

检查已安装的所有版本

bash
1
dpkg -l | grep g++

修改 GCC / G++ 版本,临时

bash
1
2
3
export CC=g++-12
export CXX=g++-12
# unset CC / CXX

修改 GCC / G++ 版本,标准

bash
1
2
3
4
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 100
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 100
sudo update-alternatives --config gcc
sudo update-alternatives --config g++

Linux

查询数据包每一跳路径

plaintext
1
tracert

安装 ifconfig 和 ping

plaintext
1
apt-get update && apt-get install net-tools iputils-ping

kernel 版本

plaintext
1
uname -a

release 版本

plaintext
1
lsb_release -a

端口查询 PID ( netstat 需要安装 net-tools )

bash
1
netstat -anp | grep "<port_ID>"

四大件

bash
1
apt install net-tools lsb-release iputils-ping  # cname 自带

控制台输出同时重定向到文件

bash
1
${CMD} 2>&1 | tee ${CMD}_output.log

查询用户

bash
1
whoami

find

  • 查询一个文件夹及其子文件夹下的所有文件名中是否包含某个字符串
    bash
    1
    find /path/to/directory -type f | grep "str"
  • 在项目目录下查找所有可执行文件
    bash
    1
    find . -type f -executable

grep

  • -I: 不查找二进制文件

apt

查看已安装包

bash
1
dpkg -l

从 deb pkg 安装

bash
1
dpkg -i <pkg>.deb

dpkg 查询软件包

bash
1
dpkg -s <pkg>.deb

dpkg 卸载软件包

bash
1
dpkg -r <pkg>

Query available versions

plaintext
1
policy

Identify if the packages are available

plaintext
1
search

Verify if the packages are installed

plaintext
1
list --installed <library>

查询安装指定版本的软件

卸载软件包 ( 保留依赖 )

bash
1
remove

权限

文件创建时权限

bash
1
umask

打印权限 ( e.g. 755 )

bash
1
stat -c "%a" /path

权限修改

bash
1
# chmod / setfacl

环境变量

  • $USER

psql

PostgreSQL 交互。

默认用户 postgres

plaintext
1
psql -U postgres [-d <db_name>]

列出所有数据库表

plaintext
1
\l

查看用户

sql
1
SELECT rolnames FROM pg_roles;

创建用户

sql
1
CREATE USER <username> WITH PASSWORD '<password>';

创建数据库

sql
1
CREATE DATABASE <database>;

退出

plaintext
1
\q

Numpy

布尔矩阵 ( 遵循布尔运算 )[3]

python
1
np.array(A, dtype=bool)

常用

  • 数乘与 np.dot
  • 矩阵比较: A == B(A == B).all()
  • EE: np.eye()
  • 最大值 / 最大值索引: np.max()np.argmax()
  • 欧式距离: np.linalg.norm()

  1. window关闭临时ipv6地址、开启临时ipv6地址-CSDN博客 ↩︎

  2. Compress-Archive (Microsoft.PowerShell.Archive) - PowerShell | Microsoft Learn ↩︎

  3. 2101. 引爆最多的炸弹 - 力扣(LeetCode) ↩︎