elma.dev - Can ELMA 10月02日
Stow工具管理dotfiles
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

Stow是一个用于管理dotfiles的工具,它通过创建符号链接来组织和维护用户的配置文件。本文介绍了如何使用Stow来管理SSH相关的配置文件,包括安装Stow、创建dotfiles目录结构、备份和替换现有的dotfiles,以及在不同计算机间迁移配置的简单方法。Stow使得版本控制和同步dotfiles变得更加便捷,特别适合GNU/Linux用户。

📁 Stow是一个通过创建符号链接来管理和组织dotfiles的工具,使得配置文件版本控制和同步更加便捷。

🔧 安装Stow非常简单,在Arch系统上使用`pacman -S stow`,在Ubuntu系统上使用`apt-get install stow`。

📂 使用Stow管理dotfiles时,首先需要创建一个包含子目录的dotfiles结构,例如将SSH配置文件放在`dotfiles/ssh/.ssh`目录下。

🔗 Stow会自动在目标目录(通常是家目录)创建符号链接,指向dotfiles目录中的实际文件,同时保留原始的目录结构。

🔄 当需要在另一台计算机上迁移dotfiles时,只需将dotfiles目录复制到新计算机,并使用`stow `命令重新创建链接即可。

Dotfiles are plain-text configuration files with a . at the beginning of their filename which makes them hidden. These files may be configuring your git tool, or functions you use in the shell, or something else.

Stow and dotfiles

If you're on a GNU/Linux distribution or similar, you already have many dotfiles. For example, check the ~/.bashrc file, or ~/.gitconfig if any. As you make your own changes to these files, you will want to be able to keep and manage these changes. The Stow tool helps me a lot in this regard and I can manage my dotfiles with a version control system such as git.

How to install Stow

Arch:

pacman -S stow

Ubuntu:

apt-get install stow

How to use Stow

It's simple. Let's go through an example. We want to manage our SSH related configuration files. We can find these files in the ~/.ssh folder.

First, create a folder named dotfiles, and another folder named ssh in it. Now copy your ~/.ssh folder with its content to this new ssh folder, excluding your private files you don't want to expose in your VCS. You'll have a folder hierarchy like this:

dotfiles└── ssh    └── .ssh        └── config

Now our ssh module can be managed with Stow. Move this dotfiles folder to where you want to keep all your dotfiles. Remember that if you want to use Stow without specifying a target directory, you need to keep the dotfiles directory in your home directory. I keep it as a hidden folder in my home directory for easy use: ~/.dotfiles. After taking a backup of your .ssh folder, delete it for testing. Run the following command in the ~/dotfiles directory:

$ stow ssh# or if your `dotfiles` folder is not in your home directory:$ stow -t ~/ ssh

With this command, Stow creates symbolic links in your home directory, preserving the folder structure. What just happened is that you now have a ~/.ssh folder again, but this time with symlinks to the ~/dotfiles directory. So the actual files are in ~/dotfiles:

$ ls -la ~/.sshdrwx------  2 user user 4096 Nov 12 16:50 .drwx------ 44 user user 4096 Nov 12 16:50 ..lrwxrwxrwx  1 user user   28 Nov 12  2021 config -> ../dotfiles/ssh/.ssh/config

When you switch to a new computer, all you have to do is to transfer your dotfiles folder to this computer and stow the modules you want with the stow command.

Whatever you put inside the folders (modules) you created in the dotfiles folder, the Stow tool will create symbolic links to them in the parent directory by default, preserving the folder structure. That's all. You can change the default target directory with the -t DIR, --target=DIR option.

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

Stow dotfiles 配置管理 GNU/Linux 符号链接
相关文章