cheetsheets

Linux

cp -l jzpb jzpd # ln -s file symlink  # 创建指向文件 file 的软链接 symlink
cp -s jzpb jzpc # ln file hardlink  # 创建指向文件 file 的硬链接 hardlink
stat example.txt # 查看某个文件的inode信息. mtime 仅会在文件数据变化时更新,而 ctime 会在文件数据以及文件元信息(例如权限、所有权)变化时更新
ls -i # 看到文件名对应的inode号码
mkdir -p jzpdir0/jzpdir1/jzpdir2 # 如果中间目录不存在,则创建;如果要创建的目录已经存在,则不报错
find . -name '*report*' # 在当前目录搜索名为 ...report... 的文件
find / -size +1G # 全盘搜索大于 1G 的文件
find ~/ -name 'node_modules' -type d # 在用户目录搜索所有名为 node_modules 的文件夹
tar -c -f target.tar file1 file2 file3 # 将 file1、file2、file3 打包为 target.tar (tape archive 没有进行压缩的存档文件)
tar -x -f target.tar -C test/ # 将 target.tar 中的文件提取到 test 目录中
tar -cz -f target.tar.gz file1 file2 file3 # 将 file1、file2、file3 打包,并使用 gzip 算法压缩,得到压缩文件 target.tar.gz (-cz = -c -z) (经过 gzip 算法压缩(gz)的存档文件(tar))
tar -tv -f target.tar # 列出 target.tar 存档文件中的内容 (-v: 打印出文件的详细信息)
zip -r archive.zip path/file1 path/dir1  # (递归地)压缩文件和目录
unzip archive.zip -d path/ # 解压缩到指定目录



htop # htop 中自带向进程发送信号的功能。按下 K 键,在左侧提示栏中选择需要的信号,按下回车发送。同时可以使用空格对进程进行标记. type H for help.
ps aux # 显示所有进程
jobs # Display status of jobs in the current session
fg # Bring most recently suspended or running background job to foreground
bg # Resume the most recently suspended job and run it in the background
stty -a # Display all settings for the current terminal(rows, cols, ...)
stty rows 60 cols 150

./hello > out
./hello 2> err
./hello > out 2> err
./hello > outerr 2>&1 # 使用 2>&1 可以将 stderr 合并到 stdout。
echo "2 3" | ./plus # 左边的命令的 stdout 接到之后的命令的 stdin. 管道不会处理 stderr.
diff 2_3 2__3_ -w # 加参数 -w 可忽略所有空白字符, -b 可忽略空白字符的数量变化。
head -n 25 file  # 显示 file 前 25 行
head -c 20 file  # 显示 file 前 20 个字符
tail -f /var/log/syslog # 当文件末尾内容增长时,持续输出末尾增加的内容
wc hello.cpp # 输出文本的行数、单词数与字符(字节)数. e.g. 10  50 255 hello.cpp
grep 'hello' file  # 查找文件 file 中包含 hello 的行
ls | grep 'file'  # 查找当前目录下文件名包含 file 的文件
grep -i 'Systemd' file  # 查找文件 file 中包含 Systemd 的行(忽略大小写)
grep -R 'hello' .  # 递归查找当前目录下内容包含 hello 的文件
sed 's/hello/world/g' file  # 将文件 file 中的 hello 全局(global)替换为 world 后输出
sed -i 's/hello/world/g' file  # -i 参数会直接写入文件,操作前记得备份哦! -i -> -i.bak 可以让 sed 帮你备份到 file.bak
export JZPNUM=2; echo $JZPNUM # 按命令出现的先后,顺序执行
(cd /tmp; pwd;) # 使用 (命令1; 命令2; …),组命令会建立独立的 shell 子进程来执行组命令,这里的圆括号周围并不需要空格。
{ cd /tmp; pwd; } # 使用 { 命令1; 命令2; … },组命令在 shell 内执行,不会产生新的进程,注意花括号和命令之间的空格。
a=2 # export a=2
unset a
set -u # 如果变量未定义,就直接报错退出. 可以在脚本开头加上 set -u 来实现这一点
env | grep TERM # 环境变量
echo "$PWD"  # 双引号中仍然可以使用各种 shell 特殊符号
echo '$PWD'  # 但是单引号则不行
ls -lh $(which ls)
printf "Hello %s" "$name" # 和 echo 不同,printf 结尾默认不输出换行符
a=$((2-(4+1)))
expr length "abcdefghi"
test -s emptyfile # 文件存在且长度大于 0, 条件表达式写成 test 条件表达式,或 [ 条件表达式 ],注意表达式与方括号之间有空格。(true: 0)
read -r -p "Enter Number between (5 to 9) : " MAX_NO # In 'read', 反斜杠被认为是转义符号,结果被丢弃. 加上 -r 参数后,反斜杠完好无损
if ! [ "$MAX_NO" -ge 5 -a "$MAX_NO" -le 9 ] ; then   echo "I ask to enter number between 5 and 9, Okay";   exit 1; fi # 使用 exit 会直接结束退出当前脚本程序
bash -x jzp.sh # 提供跟踪执行信息,将执行的每一条命令和结果依次打印出来. Also can be used: 1. 脚本开头:#!/bin/bash -x。 2. 在脚本中用 set 命令调整(set -x 启用,set +x 禁用)。
for file in $(ls | grep .cpp); do        cp $file ${file}_test_for; done

gcc -o thread thread.c -lpthread  # 编译一个依赖于 pthread 线程库的应用
ldd ./thread
gcc -o hello-static hello.c -static  # 编译一个静态链接的应用, 静态链接则将依赖的库全部打包到程序文件中。
file thread # hello: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, ....

docker pull ubuntu:18.04 # 从 Docker 镜像仓库获取镜像
docker run -it --rm ubuntu:18.04 bash # 启动容器里面的 bash 并且进行交互式操作。 -it 交互式终端(-t 选项让Docker分配一个伪终端(pseudo-tty)并绑定到容器的标准输入上, -i 则让容器的标准输入保持打开),(在使用 -d 参数时,容器启动后会进入后台。) --rm 容器退出后将其删除
docker image ls # 列出已经下载下来的镜像
docker image prune # 删除虚悬镜像(旧的镜像上的这个名称则被取消,从而成为了 <none> 的镜像)
docker image ls -f label=com.example.version=0.1 # 按 label 来 filter
docker image rm 501 # 501:用 短 ID 来删除镜像。一般取前3个字符以上,只要足够区分于别的镜像就可以了。
docker image rm centos # 指定 <仓库名>:<标签>,来删除镜像。
docker image rm $(docker image ls -q -f before=mongo:3.2) # 删除所有在 mongo:3.2 之前的镜像。 -q:列出 ID
docker build -t nginx:v3 . # 指定上下文 ".",根据 "./Dockerfile" 构建一个名为 nginx tag 为 v3 的镜像(Dockerfile 里面 COPY 参考上下文 "."。docker build 命令得知这个路径后,会将路径下的所有内容打包,然后上传给 Docker 引擎,因此 COPY 这类指令中的源文件的路径都是相对路径)
docker container ls -a # 查看容器信息
docker container logs [container ID or NAMES] # 获取容器的输出信息
docker container stop ... # 终止一个运行中的容器
docker container start ... # 重新启动终止状态的容器。 restart 终止后重启
docker export 7691a814370e > ubuntu.tar # 导出容器快照到本地文件
cat ubuntu.tar | docker import - test/ubuntu:v1.0 # 使用 docker import 从容器快照文件中再导入为镜像
docker container prune # 清理掉所有处于终止状态的容器
docker container rm -f trusting_newton # 删除一个(可以是运行中的)容器,Docker 会发送 SIGKILL 信号给容器

分解长命令: 使用 \ 符号将一行长命令分解为多行书写,便于阅读。 e.g.

$ ./configure \
--sbin-path=/usr/local/nginx/nginx \
--conf-path=/usr/local/nginx/nginx.conf

Use VTune Profiler on the server

Initializing

source /opt/intel/oneapi/setvars.sh

Profile

vtune -collect performance-snapshot <command>
vtune -collect hotspots <command>
vtune -collect memory-access <command>
vtune -collect uarch-exploration <command>

example:

vtune -collect hotspots ./jzp

These will generate a directory like "r000hs", which includes performance data.

GUI

vtune-backend --web-port=<port number> --data-directory=<work_dir>

Caveat: don't let port number conflict with others.

example:

vtune-backend --web-port=8080 --data-directory="/home/jiazhaopeng/code"

Note: Don't forget to change ssh config file accordingly.

e.g.

Host desktop
    HostName 11.2.150.210
    User jiazhaopeng
    LocalForward 8080 127.0.0.1:8080

tips: https://lab.cs.tsinghua.edu.cn/hpc/doc/assignments/8.profiling_tools/#_4

posted @ 2025-02-07 08:45  JiaZP  阅读(22)  评论(0)    收藏  举报