使用 bash 自动化点文件

发布于 2024-10-21 01:55:23 字数 504 浏览 2 评论 0原文

我想创建自己的自动点文件文件夹。 (我将使用 git 对我的点文件使用版本控制,但这与问题无关)

我只想符号链接 ~/dotfiles< 中的所有文件和文件夹/code> 到我的主文件夹。由于我对 bash 一点都不擅长,所以我无法做到这一点。请帮我解决这个问题。

如果可能的话,我也希望获得以下功能。

  • 文件夹仅浅层链接
  • 我的文件可能位于 dotfiles 文件夹中,文件名中没有实际的点(例如 ~/dotfiles/vimrc 而不是 ~/dotfiles/.vimrc
  • 它应该能够忽略一些文件,比如我的 .git 文件,它们存储在同一文件夹中。

当然,如果您已经知道提供此服务的服务,那么这至少与提供一些 do-myself 命令一样好。请注意,我特别希望它是 bash 或最有可能存在于所有 UNIX 机器上的东西(所以我猜使用 g++ 的命令很好)。

I want to create my own automated dotfiles folder. (I'll be using git to use version control on my dotfiles, but that's irrelevant for the question)

What i simply want is to symbolically link all the files and folders in ~/dotfiles to my home folder. Being not good at all with bash I can't do this. Please help me with this.

I would also appreciate the following features if possible.

  • Folders are only shallowly linked
  • My files could be in the dotfiles folder without the actual dot in the file-name (like ~/dotfiles/vimrc rather than ~/dotfiles/.vimrc)
  • It should be able to ignore some files, like my .git file which are stored in the same folder

Of course if you already know a service providing this, that is at least as good as providing some do-myself commands. Note how I specifically want it to be bash or something that most likely exists on all unix machines (so i guess commands using g++ are fine).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

黄昏下泛黄的笔记 2024-10-28 01:55:23

尝试一下:

ln -s ~/dotfiles/* ~

不需要循环。当然,如果您需要递归,您可以使用find

编辑:

要隐藏目标文件:

for f in ~/dotfiles/*
do
    ln -s "$f" "$HOME/.${f##*/}"
done

Give this a try:

ln -s ~/dotfiles/* ~

There shouldn't be any need for a loop. Of course, you can use find if you need something recursive.

Edit:

To make the destination files hidden:

for f in ~/dotfiles/*
do
    ln -s "$f" "$HOME/.${f##*/}"
done
jJeQQOZ5 2024-10-28 01:55:23

我不确定我的问题是否正确,但如果您正在寻找 dir-content 的符号链接,请尝试

for f in `ls -1 .dotfiles`
do
   ln -s .dotfiles/$f ~/$f
done

也许这已经成功了

I am not sure if I'm getting the question right, but if you looking for symlinks of dir-content, try

for f in `ls -1 .dotfiles`
do
   ln -s .dotfiles/$f ~/$f
done

maybe that already does the trick

不交电费瞎发啥光 2024-10-28 01:55:23

为了管理点文件,我真的很喜欢 Zach Holman 的方法。我用我的点文件做了同样的事情,你可以在这里找到它:)

https://github.com/rhacker /点文件

For the sake of managing dotfiles, I really like Zach Holman's approach. I've made the same thing with my dotfiles which you can find it here :)

https://github.com/rhacker/dotFiles

城歌 2024-10-28 01:55:23

也许您正在寻找一个点文件管理器,我建议您检查DFM(点文件管理器)。 DFM 以非常干净的方式解决您遇到的问题:

Maybe you are looking for a dotfiles manager, I will recommend you to check DFM (dotfiles manager). DFM addresses the problem you have in a very clean way:

千笙结 2024-10-28 01:55:23

Atlassian 有一个关于 的教程使用 git 工作树而不是符号链接。该方法使用:

  1. 侧文件夹中的裸 git 存储库(例如 $HOME/.cfg$HOME/dotfiles),以及
  2. config bash 别名来执行管理配置文件的 git 命令。

例如,您可以运行 config status 来检查哪些文件已被修改,运行 config checkout 来获取存储库中的文件,并运行 config commit 来更新存储库。它只需要 gitbash

Atlassian has a tutorial on using a git work tree instead of symlinks. The approach uses:

  1. a bare git repository in a side folder (such as $HOME/.cfg or $HOME/dotfiles), and
  2. a config bash alias to execute git commands that manage the configuration files.

For instance, you can run config status to check which files have been modified, config checkout to get the files in the repository and config commit to update the repository. It requires only git and the bash.

手心的海 2024-10-28 01:55:23

我也在寻找某种方法以最少的步骤设置一台新机器,花了一些时间后我发现几乎所有开发人员都使用 git 来存储和共享这些文件和符号链接来同步它们。

嗯,符号链接可以工作,但它并不是将本地文件同步到 git 存储库的最佳方式。有一个更好的解决方案,由 Atlassian 的人员编写 - https://www.atlassian .com/git/tutorials/dotfiles

因此,git bare 存储库是将文件与远程副本同步的最佳且最优雅的方法,创建一个 bash 脚本来自动安装和设置。

管理点文件

管理这些点文件的技巧是创建一个裸 git 存储库。如果您是从头开始并且之前没有跟踪过点文件,请在 $HOME 目录中创建一个裸存储库。

git init --bare $HOME/.dotfiles

为了更容易使用,我们将其别名为 dotfiles,我们将使用它而不是常规 git 与我们的 dotfiles 存储库进行交互。

alias dotfiles="/usr/bin/git --git-dir=$HOME/.dotfiles --work-tree=$HOME"

现在我们可以使用 dotfiles 命令来跟踪我们的点文件,一些示例如下:

# to check the version history 
dotfiles log

# to check the status of the tracked and untracked files 
dotfiles status

# to add a file for tracking
dotfiles commit .vimrc -m ".vimrc added"

# push new files or changes to the github
dotfiles push origin main

点文件的用例和优点

这种管理和共享方法具有各种优点,下面列出了其中一些优点。

轻松设置

设置新机器可能是一项耗时的任务,但通过这种方法,我们可以在一分钟内使用我们的个性化配置。我们只需要克隆存储库并获取 .bashrc 或 .zshrc 文件。

git clone --bare https://github.com/<username>/dotfiles.git $HOME/.dotfiles && source ~/.zshrc

版本化点文件

最适合试验新配置并保留更改历史记录(基本上是使用 git 的所有优点)

# to check the version history 
dotfiles log

在多个设备上共享

使用分支共享多个设备的相同配置,只需进行最少的更改,为您的新机器创建一个分支,例如:-

# Create configurations specific to your aws machines
dotfiles checkout -b aws

点文件的配置文件

使用分支根据您的环境创建配置,创建分支并根据您的工作环境进行配置。

# Manage multiple profiles - check out to the work profile 
dotfiles checkout work

我还使用这种方式来同步和存储我的点文件,查看我的点文件存储库并且可以在 使用 Git 存储点文件,我在其中写了有关管理多个设备的文章。

I was also looking for some way to set up a new machine in a minimal number of steps, after spending some time I found that almost all the developers use git to store and share these files and symlinks to sync them.

Well, symlinks works, but it isn’t the best way to sync your local files to the git repository. There is a much better solution to this, written by people at Atlassian – https://www.atlassian.com/git/tutorials/dotfiles.

So, to git bare repository is the best and most elegant way to sync your files with your remote copy create a bash script to automate installation and set up.

Managing dotfiles

The trick to managing these dotfiles is by creating a bare git repository. If you are starting from scratch and have not tracked your dotfiles before, make a bare repository in the $HOME directory.

git init --bare $HOME/.dotfiles

Just to make it easier to use we’ll alias this to dotfiles which we will use instead of regular git to interact with our dotfiles repository.

alias dotfiles="/usr/bin/git --git-dir=$HOME/.dotfiles --work-tree=$HOME"

Now we are good to track our dotfiles using the dotfiles command, some of the examples are:

# to check the version history 
dotfiles log

# to check the status of the tracked and untracked files 
dotfiles status

# to add a file for tracking
dotfiles commit .vimrc -m ".vimrc added"

# push new files or changes to the github
dotfiles push origin main

Use Cases and Advantages of dotfiles

This method of managing and sharing has various advantages some of them are listed below.

Easy setup

Set up of a new machine can be a time consuming task, but with this method we can to use our personalized configs in under a minute. We just need to clone the repository and source the .bashrc or .zshrc file.

git clone --bare https://github.com/<username>/dotfiles.git $HOME/.dotfiles && source ~/.zshrc

Versioned dotfiles

Best for experimenting with new configurations and keep the change history (Basically all the pros of using git)

# to check the version history 
dotfiles log

Share on Multiple devices

Share the same configs of multiple devices with minimal changes using branch, create a branch for your new machine, example:-

# Create configurations specific to your aws machines
dotfiles checkout -b aws

Profiles for dotfiles

Create configs based on your environment using branch, create a branch and configure according to you work env.

# Manage multiple profiles - check out to the work profile 
dotfiles checkout work

I also use this way to sync and store my dotfiles, see my dotfiles repository and can read at Storing dotfiles with Git where I wrote about managing for multiple devices.

雾里花 2024-10-28 01:55:23

您还可以使用 dotfile_manager。它允许您为要创建的每个符号链接设置自定义路径。因此,通过这个,您可以轻松地忽略某些文件并将 . 添加为其他文件的前缀。

在您的情况下,您将设置配置文件 dotfile_manager.yaml 来包含

symlinks:
  - vimrc: ~/.vimrc

完全披露:我是这个包的作者并且每天都使用它。

You could also use dotfile_manager. It allows you to set up a custom path for every symlink you want to create. So with this, you can easily ignore some files and add the . as a prefix to other files.

In your case you would setup the config file dotfile_manager.yaml to contain

symlinks:
  - vimrc: ~/.vimrc

Full disclosure: I am the author of this package and use it every day.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文