linux将miniconda移去其它位置

本文讲述了如何在Linux系统中将miniconda文件夹从/data/itom/miniconda3迁移到/share/itom/miniconda3,涉及到.bashrc文件的修改、conda相关路径的调整以及处理文本和二进制文件的脚本编写。
部署运行你感兴趣的模型镜像

linux 机上将 miniconda 的文件夹从 /data/itom/miniconda3 移去 /share/itom/miniconda3:

mv /data/itom/miniconda3 /share/itom/

之后需要改一系列路径,参考 [1]。因为要改多个文件,遂写个脚本批量改。

~/.bashrc

~/.bashrc 中 conda 写入的初始化命令带路径,形如:

unset CONDA_AUTO_ACTIVATE_BASE
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/data/itom/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/data/itom/miniconda3/etc/profile.d/conda.sh" ]; then
        . "/data/itom/miniconda3/etc/profile.d/conda.sh"
    else
        export PATH="/data/itom/miniconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

路径前缀全改成新位置。

miniconda3/

miniconda 安装目录下(本例即新位置 /share/itom/miniconda3/)有几个子目录下的文件带路径:

miniconda3/
|- condabin/
|  `- conda
|- etc/profile.d/
|  |- conda.csh
|  `- conda.sh
|- bin/
|  |- 2to3-3.10          # 脚本
|  |- bunzip2            # 二进制文件
|  |- clear
|  |- ...
`- envs/<ENV_NAME>/bin/  # 各虚拟环境
   |- 2to3-3.10          # 脚本
   |- bunzip2            # 二进制文件
   |- clear
   |- ...

前两个路径下只有一两个文件,可以手动改;而 bin/ 下则有很多,且有些是脚本(文本)、有些是二进制文件;虚拟环境的 bin 目录 envs/<ENV_NAME>/bin/ 与 bin/ 一样,也有多个文件需要改。可以用 file 命令判断文件类型[2],如 file 2to3-3.10

2to3-3.10: Python script, ASCII text executable

file bunzip2

bunzip2: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, with debug_info, not stripped

只能改那些文本型的文件,脚本:

  • 类型中出现 ASCII text 就是文本。脚本第一行形如:#!/data/itom/miniconda3/bin/python
  • sed 替换,sed 中写正则时,路径中的 / 要转义,写成 \/
#!/bin/bash
# update-conda-path.sh

OLD_PREFIX=/data/itom/miniconda3  # 旧位置
NEW_PREFIX=/share/itom/miniconda3 # 新位置
P=$NEW_PREFIX/bin
# P=$NEW_PREFIX/envs/cu110_pt171/bin # 虚拟环境的 bin/

for f in `ls $P`; do
	type_info=`file -b $P/$f`
	if [[ $type_info == *"ASCII text"* ]]; then # 是文本
		first_row=`cat $P/$f | head -n 1`
		if [[ $first_row == *"$OLD_PREFIX"* ]]; then # 首行出现(旧)路经前缀
			# echo $first_row
			echo $P/$f
			sed -i "s/\/data\/itom\/miniconda3/\/share\/itom\/miniconda3/g" $P/$f # 注意转义:\/
		fi
	fi
done

clear

miniconda3/bin/ 下也有个 clear,且默认用的是它(因为 conda 将自己放在 PATH 最前),移动之后用之会报错:

terminals database is inaccessible

参考 [5],将 conda 的 clear 换个名,避免冲突:

cd /share/itom/miniconda3 # 新位置
mv bin/clear bin/clear.bak
# 虚拟环境
for e in `ls envs`; do
    mv envs/$e/bin/clear envs/$e/bin/clear.bak
done

References

  1. 解决在linux移动anaconda到其他文件夹,conda: command not found的问题
  2. file command in Linux with examples
  3. How can I detect if a file is binary (non-text) in Python?
  4. How to check if a string contains a substring in Bash
  5. clear command - terminals database is inaccessible

您可能感兴趣的与本文相关的镜像

Python3.9

Python3.9

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值