使用 bash 自动化点文件
我想创建自己的自动点文件文件夹。 (我将使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试一下:
不需要循环。当然,如果您需要递归,您可以使用
find
。编辑:
要隐藏目标文件:
Give this a try:
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:
我不确定我的问题是否正确,但如果您正在寻找 dir-content 的符号链接,请尝试
也许这已经成功了
I am not sure if I'm getting the question right, but if you looking for symlinks of dir-content, try
maybe that already does the trick
为了管理点文件,我真的很喜欢 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
也许您正在寻找一个点文件管理器,我建议您检查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:
Atlassian 有一个关于 的教程使用 git 工作树而不是符号链接。该方法使用:
$HOME/.cfg
或$HOME/dotfiles
),以及config
bash 别名来执行管理配置文件的 git 命令。例如,您可以运行
config status
来检查哪些文件已被修改,运行config checkout
来获取存储库中的文件,并运行config commit
来更新存储库。它只需要git
和bash
。Atlassian has a tutorial on using a git work tree instead of symlinks. The approach uses:
$HOME/.cfg
or$HOME/dotfiles
), andconfig
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 andconfig commit
to update the repository. It requires onlygit
and thebash
.我也在寻找某种方法以最少的步骤设置一台新机器,花了一些时间后我发现几乎所有开发人员都使用 git 来存储和共享这些文件和符号链接来同步它们。
嗯,符号链接可以工作,但它并不是将本地文件同步到 git 存储库的最佳方式。有一个更好的解决方案,由 Atlassian 的人员编写 - https://www.atlassian .com/git/tutorials/dotfiles。
因此,git
bare
存储库是将文件与远程副本同步的最佳且最优雅的方法,创建一个 bash 脚本来自动安装和设置。管理点文件
管理这些点文件的技巧是创建一个裸 git 存储库。如果您是从头开始并且之前没有跟踪过点文件,请在 $HOME 目录中创建一个裸存储库。
为了更容易使用,我们将其别名为 dotfiles,我们将使用它而不是常规 git 与我们的 dotfiles 存储库进行交互。
现在我们可以使用 dotfiles 命令来跟踪我们的点文件,一些示例如下:
点文件的用例和优点
这种管理和共享方法具有各种优点,下面列出了其中一些优点。
轻松设置
设置新机器可能是一项耗时的任务,但通过这种方法,我们可以在一分钟内使用我们的个性化配置。我们只需要克隆存储库并获取 .bashrc 或 .zshrc 文件。
版本化点文件
最适合试验新配置并保留更改历史记录(基本上是使用 git 的所有优点)
在多个设备上共享
使用分支共享多个设备的相同配置,只需进行最少的更改,为您的新机器创建一个分支,例如:-
点文件的配置文件
使用分支根据您的环境创建配置,创建分支并根据您的工作环境进行配置。
我还使用这种方式来同步和存储我的点文件,查看我的点文件存储库并且可以在 使用 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.
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.
Now we are good to track our dotfiles using the dotfiles command, some of the examples are:
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.
Versioned dotfiles
Best for experimenting with new configurations and keep the change history (Basically all the pros of using git)
Share on Multiple devices
Share the same configs of multiple devices with minimal changes using branch, create a branch for your new machine, example:-
Profiles for dotfiles
Create configs based on your environment using branch, create a branch and configure according to you work env.
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.
您还可以使用 dotfile_manager。它允许您为要创建的每个符号链接设置自定义路径。因此,通过这个,您可以轻松地忽略某些文件并将
.
添加为其他文件的前缀。在您的情况下,您将设置配置文件
dotfile_manager.yaml
来包含完全披露:我是这个包的作者并且每天都使用它。
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 containFull disclosure: I am the author of this package and use it every day.