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
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐