Linux常用命令

 

前言

linux 移动文件夹的前500个文件 到新的文件夹 原始文件夹为 /home/wq/tem/bilibili/video_info 新文件夹需要创建,为/home/wq/tem/bilibili/500

# 创建目标文件夹并移动文件
mkdir -p /home/wq/tem/bilibili/500 && \
ls -p /home/wq/tem/bilibili/video_info | grep -v / | head -500 | xargs -I {} mv "/home/wq/tem/bilibili/video_info/{}" "/home/wq/tem/bilibili/500/"

查看端口占用或根据名字查找运行的程序

sudo lsof -i :3001

ps -u liuhu | grep node
ps -u liuhu | grep scrapy

sudo pkill -9 -u liuhu -f "python"
sudo pkill -9 -u liuhu -f "node"


查找占用8000端口的进程

lsof -i :8000

直接杀死占用8000端口的进程

kill -9 $(lsof -t -i :8000)

后台运行程序

nohup doccano webserver --port 9000 > /home/liuhu/doccano/log_video_topics.txt 2>&1 &
nohup doccano task > /home/liuhu/doccano/log_video_topics_task.txt 2>&1 &

nohup python 6_5_spider_video_and_audio.py > /home/liuhu/projects/bili_spider/log.txt 2>&1 &

硬盘挂载

sudo mount /dev/sdb1 /mnt/sdb1

linux 复制大批量文件

rsync -avh --progress /mnt/sdb1/liuhu/bilibili/profile_spidered_temp/start_0end_310000/ /mnt/sdb1/liuhu/bilibili/bili_depression_profile_face/


start_200000end_1500000
start_500000end_1000000
start_1000000end_3000000
start_1000000end_4000000
start_1001000end_2000000

linux 移动大量文件

rsync -avh --progress --remove-source-files /mnt/sdb1/liuhu/bilibili/profile_spidered_temp/start_0end_600000/ /mnt/sdb1/liuhu/bilibili/bili_depression_profile_face/
mv /mnt/sdb1/liuhu/bilibili/profile_spidered_temp/start_0end_500000/* /mnt/sdb1/liuhu/bilibili/bili_depression_profile_face/
# 最快:仅修改元数据,不实际复制数据。

#要求:源和目标必须在同一文件系统(如都是 /mnt/sdb1)。


windows 传输文件到linux

# 传递文件夹所有文件
scp -r D:\temp\bili_depression_profile_face\* username@IP:/mnt/sdb1/liuhu/bilibili/bili_depression_profile_face/



D:\mnt\sdb1\liuhu\bilibili\profile_spidered_temp\start_500000end_1000001\



linux 传入文件到windows:

scp -r username@ip:/mnt/sdb1/liuhu/bilibili_all_disease/bvids_comment/tem/ F:/bilibili/video_info/

对于大批量文件,scp可能会不稳定,不支持断开续传,

所以得用rynsc ,需要安装wsl 参考这个

使用 rsync 实现 Windows 到 Linux 的高效文件同步:从入门到精通 — geek-blogs.com

rsync [选项] [Windows源路径] [Linux用户名]@[Linux IP地址]:[Linux目标路径]

通过 WSL 安装 rsync(推荐)

WSL 是 Windows 10/11 内置的 Linux 子系统,性能优于 Cygwin,且原生支持 Linux 命令。

  1. 启用 WSL:

    • 以管理员身份打开 PowerShell,运行:

      wsl --install
      
    • 重启电脑后,系统会自动安装 Ubuntu(默认发行版)。若需其他发行版(如 Debian),可通过 Microsoft Store 搜索安装。

  2. 启动 WSL:在开始菜单搜索 “Ubuntu” 或直接在 PowerShell 中输入 wsl

  3. 初始化 WSL:首次启动需设置用户名和密码(非 root,后续可通过 sudo 获取权限)。

  4. 安装 rsync:在 WSL 终端中运行:

    sudo apt update && sudo apt install -y rsync openssh-client
    
  5. 验证安装:rsync --version,输出类似 rsync version 3.2.3 protocol version 31 即成功。

注意:WSL 中访问 Windows 文件路径需使用 /mnt/[盘符]/,例如 Windows 的 C:\Users\用户名\Documents 在 WSL 中为 /mnt/c/Users/用户名/Documents

如果本地windows是外接的硬盘,需要进行挂载

# 1. 创建挂载点
sudo mkdir -p /mnt/f

# 2. 挂载 F 盘
sudo mount -t drvfs F: /mnt/f

# 3. 验证挂载
ls -la /mnt/f
df -h | grep /mnt/f

安装pytorch

1. 首先添加清华镜像源(如果尚未添加)

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --set show_channel_urls yes

2. 创建并激活一个新的 conda 环境(可选但推荐)

conda create -n pytorch23 python=3.10
conda activate pytorch23

3. 安装指定版本的 PyTorch 和相关包

conda install pytorch==2.3.0 torchvision==0.18.0 torchaudio==2.3.0 pytorch-cuda=11.8 -c pytorch -c nvidia
pip install torch==2.3.0+cu118 torchvision==0.18.0+cu118 torchaudio==2.3.0 --index-url https://pypi.tuna.tsinghua.edu.cn/simple

4. 验证安装

import torch
print(torch.__version__)  # 应该输出 2.3.0
print(torch.cuda.is_available())  # 应该返回 True

临时指定清华镜像

! pip install -U sentence-transformers -i https://pypi.tuna.tsinghua.edu.cn/simple
! pip install opencc -i https://pypi.tuna.tsinghua.edu.cn/simple