目录
Conda环境配置
Linux终端操作
Screen操作
环境变量设置
Git相关操作
1. Conda环境配置
1.1 安装Miniconda
1 2 3 4 5 6 7 8 9 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 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 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 conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia 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 destinationcp -r sourcedir destdir mv source destinationrm filenamerm -f filename rm -r dirname rm -rf dirname cat filename less filename head filename tail filename tail -f logfile for file in AAA_*; do mv "$file " "${file#AAA_} " done 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 chmod +x filename chmod -R 755 dirname chown user:group filenamechown -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" find /path -type f find /path -type d find /path -size +100M find /path -mtime -7
2.4 文件压缩与解压
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 tar -cvf archive.tar files/ tar -xvf archive.tar tar -tvf archive.tar tar -czvf archive.tar.gz files/ tar -xzvf archive.tar.gz tar -cjvf archive.tar.bz2 files/ tar -xjvf archive.tar.bz2 zip -r archive.zip files/ unzip archive.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/ 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 -hdu -sh /pathdu -h --max-depth=1 /path top htop free -h nvidia-smi watch -n 1 nvidia-smi
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 sudo apt-get install screenscreen screen -S session_name screen -ls screen -r session_id screen -r session_name screen -d -r session_id exit
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 echo "startup_message off" >> ~/.screenrc echo "defscrollback 10000" >> ~/.screenrc screen -dmS train_model bash -c "cd /path/to/project && python train.py" 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=valueecho $VARIABLE_NAME env | grep VARIABLE_NAME
4.2 永久环境变量
编辑~/.bashrc
或~/.zshrc
(取决于你使用的shell):
添加以下内容:
1 2 3 4 5 6 export PATH=$PATH :/path/to/addexport VARIABLE_NAME=value
使修改生效:
4.3 设置PyTorch相关环境变量
1 2 3 4 5 6 7 8 9 10 export TORCH_HOME=$HOME /.cache/torchexport PYTORCH_PRETRAINED_BERT_CACHE=$HOME /.cache/torch/transformersexport PYTORCH_DISABLE_RASTERIZE=1export CUDA_VISIBLE_DEVICES=0,1
4.4 设置Hugging
Face相关环境变量
1 2 3 4 5 6 7 8 9 10 11 export HF_HOME=$HOME /.cache/huggingfaceexport TRANSFORMERS_CACHE=$HOME /.cache/huggingface/transformersexport DATASETS_CACHE=$HOME /.cache/huggingface/datasetsexport HF_DATASETS_CACHE=$HOME /.cache/huggingface/datasetsexport HF_ENDPOINT=https://hf-mirror.com
4.5 其他常用环境变量
1 2 3 4 5 6 7 8 9 10 11 export PYTHONPATH=$PYTHONPATH :/path/to/your/projectexport http_proxy=http://proxy.example.com:portexport https_proxy=http://proxy.example.com:portexport all_proxy=socks5://proxy.example.com:portunset 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 config --global url."https://" .insteadOf git:// git config --list
5.2 Git代理设置
1 2 3 4 5 6 7 8 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
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 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 sudo apt-get install git-lfsgit lfs install git lfs track "*.weights" git lfs track "models/*" git add .gitattributes git commit -m "Track large files with Git LFS" 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 git remote set-url origin https://github.com.cnpmjs.org/username/repo.git
5.9 Git Hooks
Git钩子可以在特定Git事件发生时执行自定义脚本:
1 2 3 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