目录

  1. Conda环境配置
  2. Linux终端操作
  3. Screen操作
  4. 环境变量设置
  5. Git相关操作

1. Conda环境配置

1.1 安装Miniconda

1
2
3
4
5
6
7
8
9
# 下载Miniconda安装脚本
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh

# 赋予执行权限
chmod +x Miniconda3-latest-Linux-x86_64.sh

# 运行安装脚本
./Miniconda3-latest-Linux-x86_64.sh

安装过程中按提示操作,建议接受默认安装位置并选择初始化conda。

1.2 配置Conda镜像源

创建或修改~/.condarc文件:

1
2
nano ~/.condarc

添加以下内容(使用清华镜像源):

1
2
3
4
5
6
7
8
9
10
11
channels:
- defaults
show_channel_urls: true
default_channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

1.3 创建与管理环境

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 创建新环境
conda create -n iqa-env python=3.8

# 激活环境
conda activate myenv

# 安装包
conda install numpy scipy pandas

# 列出已安装的包
conda list

# 安装pip包
pip install transformers

# 克隆环境
conda create -n newenv --clone myenv

# 导出环境
conda env export > environment.yml

# 从配置文件创建环境
conda env create -f environment.yml

# 删除环境
conda remove -n myenv --all

# 退出当前环境
conda deactivate

1.4 PyTorch安装(使用国内镜像)

1
2
3
4
5
6
# 安装支持CUDA的PyTorch
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia

# 或者使用pip安装(建议使用清华镜像)
pip install torch torchvision torchaudio -i https://pypi.tuna.tsinghua.edu.cn/simple

2. Linux终端操作

2.1 基本文件操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 列出文件和目录
ls
ls -l # 详细信息
ls -a # 显示隐藏文件
ls -lh # 以人类可读格式显示文件大小

# 创建目录
mkdir dirname
mkdir -p path/to/nested/dir # 创建嵌套目录

# 复制文件或目录
cp source destination
cp -r sourcedir destdir # 递归复制目录及其内容

# 移动或重命名文件/目录
mv source destination

# 删除文件
rm filename
rm -f filename # 强制删除,不提示

# 删除目录
rm -r dirname # 递归删除
rm -rf dirname # 强制递归删除(谨慎使用)

# 查看文件内容
cat filename # 显示全部内容
less filename # 分页显示
head filename # 显示前10行
tail filename # 显示后10行
tail -f logfile # 实时查看日志文件更新

for file in AAA_*; do
mv "$file" "${file#AAA_}"
done #去除所有以AAA开头的文件

find . -maxdepth 1 -type d -name "checkpoint-*" | while read dir; do
num=$(echo "$dir" | sed 's/.*checkpoint-//')
if [[ $num =~ ^[0-9]+$ ]] && [ $num -lt 47000 ]; then
rm -rf "$dir"
echo "删除了: $dir"
fi
done

2.2 文件权限管理

1
2
3
4
5
6
7
8
9
# 修改文件权限
chmod 755 filename # rwxr-xr-x
chmod +x filename # 添加执行权限
chmod -R 755 dirname # 递归修改目录权限

# 修改文件所有者
chown user:group filename
chown -R user:group dirname # 递归修改目录所有权

2.3 文件查找

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 按名称查找
find /path -name "filename"
find . -name "*.py" # 查找当前目录下所有Python文件

# 按类型查找
find /path -type f # 只查找文件
find /path -type d # 只查找目录

# 按大小查找
find /path -size +100M # 查找大于100MB的文件

# 按修改时间查找
find /path -mtime -7 # 查找7天内修改的文件

2.4 文件压缩与解压

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# tar格式
tar -cvf archive.tar files/ # 创建tar归档
tar -xvf archive.tar # 解压tar归档
tar -tvf archive.tar # 查看内容不解压

# tar.gz格式
tar -czvf archive.tar.gz files/ # 创建tar.gz压缩包
tar -xzvf archive.tar.gz # 解压tar.gz

# tar.bz2格式
tar -cjvf archive.tar.bz2 files/ # 创建tar.bz2压缩包
tar -xjvf archive.tar.bz2 # 解压tar.bz2

# zip格式
zip -r archive.zip files/ # 创建zip压缩包
unzip archive.zip # 解压zip
unzip -l archive.zip # 查看内容不解压

2.5 远程文件传输(SCP)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 从本地复制到远程
scp localfile user@remote_host:/remote/path/

# 从远程复制到本地
scp user@remote_host:/remote/path/file localpath/

# 复制目录(加-r参数)
scp -r localdir user@remote_host:/remote/path/
scp -r user@remote_host:/remote/path/dir localpath/

# 指定端口
scp -P 2222 localfile user@remote_host:/remote/path/

# 通过跳板机传输
scp -o "ProxyJump user1@jumphost" localfile user2@destination:/path/

2.6 磁盘与系统监控

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 查看磁盘使用情况
df -h

# 查看目录大小
du -sh /path
du -h --max-depth=1 /path # 只显示直接子目录大小

# 查看系统资源
top # 实时系统状态
htop # 更友好的top替代品
free -h # 内存使用情况
nvidia-smi # GPU状态和使用情况
watch -n 1 nvidia-smi # 每秒更新一次GPU状态

3. Screen操作

Screen是一个终端多路复用器,允许你在一个终端会话中使用多个窗口,即使断开连接,程序也能继续运行。

3.1 基本使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 安装screen(如果尚未安装)
sudo apt-get install screen

# 创建新的screen会话
screen

# 创建命名会话
screen -S session_name

# 分离当前会话(不终止会话中运行的程序)
# 使用快捷键:Ctrl+a 然后按 d

# 列出所有会话
screen -ls

# 重新连接到会话
screen -r session_id
screen -r session_name # 如果之前命名了会话

# 如果会话显示为Attached
screen -d -r session_id # 先分离再连接

# 终止当前会话
exit # 或者 Ctrl+d

3.2 Screen快捷键

所有Screen快捷键都以Ctrl+a为前缀,然后按下对应的功能键:

  • Ctrl+a c - 创建新窗口
  • Ctrl+a n - 切换到下一个窗口
  • Ctrl+a p - 切换到上一个窗口
  • Ctrl+a " - 显示所有窗口列表
  • Ctrl+a A - 重命名当前窗口
  • Ctrl+a d - 分离当前会话
  • Ctrl+a k - 杀死当前窗口
  • Ctrl+a S - 水平分割当前窗口
  • Ctrl+a | - 垂直分割当前窗口
  • Ctrl+a Tab - 在分割窗口间切换
  • Ctrl+a ? - 显示帮助

3.3 技巧与最佳实践

1
2
3
4
5
6
7
8
9
10
# 配置文件(〜/.screenrc)示例
echo "startup_message off" >> ~/.screenrc # 禁用启动消息
echo "defscrollback 10000" >> ~/.screenrc # 增加历史记录

# 在后台启动程序
screen -dmS train_model bash -c "cd /path/to/project && python train.py"

# 记录screen输出到文件
screen -L -Logfile train_output.log -dmS train_model bash -c "python train.py"

4. 环境变量设置

4.1 临时环境变量

1
2
3
4
5
6
7
# 设置临时环境变量(当前会话有效)
export VARIABLE_NAME=value

# 查看环境变量
echo $VARIABLE_NAME
env | grep VARIABLE_NAME

4.2 永久环境变量

编辑~/.bashrc~/.zshrc(取决于你使用的shell):

1
2
nano ~/.bashrc

添加以下内容:

1
2
3
4
5
6
# 添加路径到PATH变量
export PATH=$PATH:/path/to/add

# 设置其他环境变量
export VARIABLE_NAME=value

使修改生效:

1
2
source ~/.bashrc

4.3 设置PyTorch相关环境变量

1
2
3
4
5
6
7
8
9
10
# 使用国内PyTorch镜像源
export TORCH_HOME=$HOME/.cache/torch
export PYTORCH_PRETRAINED_BERT_CACHE=$HOME/.cache/torch/transformers

# 禁用并行光栅化以减少内存使用(某些情况下有用)
export PYTORCH_DISABLE_RASTERIZE=1

# 设置CUDA可见设备
export CUDA_VISIBLE_DEVICES=0,1 # 只使用GPU 0和1

4.4 设置Hugging Face相关环境变量

1
2
3
4
5
6
7
8
9
10
11
# 设置Hugging Face缓存目录
export HF_HOME=$HOME/.cache/huggingface
export TRANSFORMERS_CACHE=$HOME/.cache/huggingface/transformers
export DATASETS_CACHE=$HOME/.cache/huggingface/datasets
export HF_DATASETS_CACHE=$HOME/.cache/huggingface/datasets

# 使用国内镜像下载模型
export HF_ENDPOINT=https://hf-mirror.com
# 或者:
# export HF_ENDPOINT=https://huggingface.tsinghua.edu.cn

4.5 其他常用环境变量

1
2
3
4
5
6
7
8
9
10
11
# 增加Python路径
export PYTHONPATH=$PYTHONPATH:/path/to/your/project

# 设置代理
export http_proxy=http://proxy.example.com:port
export https_proxy=http://proxy.example.com:port
export all_proxy=socks5://proxy.example.com:port

# 禁用代理
unset http_proxy https_proxy all_proxy

5. Git相关操作

5.1 Git配置

1
2
3
4
5
6
7
8
9
10
11
12
13
# 设置用户信息
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# 设置默认编辑器
git config --global core.editor vim

# 设置git使用https而非git协议
git config --global url."https://".insteadOf git://

# 查看所有配置
git config --list

5.2 Git代理设置

1
2
3
4
5
6
7
8
# 为Github设置代理
git config --global http.https://github.com.proxy http://proxy.example.com:port
git config --global https.https://github.com.proxy http://proxy.example.com:port

# 取消代理
git config --global --unset http.https://github.com.proxy
git config --global --unset https.https://github.com.proxy

5.3 基本Git操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 初始化仓库
git init

# 克隆仓库
git clone https://github.com/username/repo.git
git clone https://github.com/username/repo.git --depth 1 # 浅克隆(只获取最新版本)

# 添加文件到暂存区
git add filename
git add . # 添加所有修改

# 提交更改
git commit -m "Commit message"
git commit -a -m "Commit message" # 添加所有已修改文件并提交

# 查看状态
git status

# 查看差异
git diff
git diff --staged # 查看已暂存的差异

# 查看提交历史
git log
git log --oneline # 简洁模式
git log --graph # 图形化显示分支和合并历史

5.4 分支操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 查看分支
git branch

# 创建分支
git branch branch_name

# 切换分支
git checkout branch_name
# 或使用新语法
git switch branch_name

# 创建并切换到新分支
git checkout -b new_branch
# 或使用新语法
git switch -c new_branch

# 合并分支
git merge branch_name

# 删除分支
git branch -d branch_name # 安全删除
git branch -D branch_name # 强制删除

5.5 远程仓库操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 添加远程仓库
git remote add origin https://github.com/username/repo.git

# 查看远程仓库
git remote -v

# 从远程获取更新
git fetch origin

# 拉取更新并合并
git pull origin branch_name

# 推送到远程仓库
git push origin branch_name

# 设置默认上游分支
git push -u origin branch_name # 之后可直接使用git push

5.6 Git高级操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 暂存当前修改
git stash
git stash save "stash message"

# 查看暂存列表
git stash list

# 应用暂存
git stash apply # 应用最近的暂存但不删除
git stash pop # 应用最近的暂存并删除
git stash apply stash@{n} # 应用特定的暂存

# 丢弃暂存
git stash drop
git stash drop stash@{n}

# 清除所有暂存
git stash clear

# 重置修改
git reset HEAD filename # 取消暂存但保留工作区修改
git reset --hard HEAD # 重置暂存区和工作区到最新提交

# 回退到特定提交
git reset --hard commit_hash

# 生成补丁
git format-patch -1 HEAD # 最新提交的补丁
git format-patch -n HEAD # 最新n个提交的补丁

# 应用补丁
git apply patch_file
git am patch_file # 应用并创建提交

5.7 Git LFS (Large File Storage)

用于大文件存储,如模型权重文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 安装Git LFS
sudo apt-get install git-lfs
git lfs install

# 跟踪大文件
git lfs track "*.weights" # 跟踪所有.weights文件
git lfs track "models/*" # 跟踪models目录下所有文件

# 确保.gitattributes被提交
git add .gitattributes
git commit -m "Track large files with Git LFS"

# 常规操作同普通Git
git add large_file.weights
git commit -m "Add model weights"
git push origin main

5.8 配置国内Git镜像

针对GitHub访问慢的问题,可使用以下镜像:

1
2
3
4
5
6
7
8
# 使用镜像加速克隆
git clone https://github.com.cnpmjs.org/username/repo.git
# 或
git clone https://ghproxy.com/https://github.com/username/repo.git

# 修改已克隆仓库的远程URL
git remote set-url origin https://github.com.cnpmjs.org/username/repo.git

5.9 Git Hooks

Git钩子可以在特定Git事件发生时执行自定义脚本:

1
2
3
# 创建预提交钩子(在.git/hooks目录下)
nano .git/hooks/pre-commit

示例pre-commit钩子(检查Python语法):

1
2
3
4
5
6
7
8
9
10
#!/bin/bash
for file in $(git diff --cached --name-only | grep '\.py$')
do
python -m py_compile $file
if [ $? -ne 0 ]; then
echo "Syntax error in $file"
exit 1
fi
done

赋予执行权限:

1
2
chmod +x .git/hooks/pre-commit