【问题标题】:Traverse subdirectores and symlink certain files (but not all)遍历子目录并符号链接某些文件(但不是全部)
【发布时间】:2014-01-16 23:48:04
【问题描述】:

我有以下从 github 提取的目录结构;

dotfiles/bash
     .bashrc
     .bash_profile
     .some_other
     env

dotfiles/tmux/
     .tmux.conf
dotfiles/???/
     .whatever

bash/在我的 bashcode(如下)中,我想(在我的脚本 symlink.sh 中)遍历子文件夹并将某些(不是全部)点文件符号链接到我的 ~home 目录。我怎样才能动态地做到这一点?由于我不知道有多少子文件夹,以及我不想符号链接的子文件夹中的哪些文件,因此必须动态完成。

下面是我的代码

!/bin/bash
############################
# .make.sh
# This script creates symlinks from the home directory to any desired dotfiles in ~/dotfiles
############################
set -x
trap read debug
########## Variables


dir=~/dotfiles                   # dotfiles directory
olddir=~/dotfiles_old             # old dotfiles backup directory
files=".bashrc .bash_profile env .tmux.conf"    # list of files/folders to symlink in homedir

##########

# create dotfiles_old in homedir
echo "Creating $olddir for backup of any existing dotfiles in ~"
mkdir -p $olddir
echo "...done"

# change to the dotfiles directory
echo "Changing to the $dir directory"
cd $dir
echo "...done"
paus -p ;clear
# move any existing dotfiles in homedir to dotfiles_old directory, then create symlinks 
for file in $dir/*/*; do
  echo $dir;ls -a
        if [[ -f $dir/${files##*/} && ! -L $dir/${files##*/} ]]; then
                $olddir=$(mktemp -d olddotfiles.XXXXXX)
                #mv "~/${files##*/}" "$backupdir"
                ls -a
        ln -s "$files" ~/${files##*/}
      fi
done

【问题讨论】:

  • 你看过find这个命令了吗?
  • 你说“因为我不知道有多少子文件夹,以及这些子文件夹中有哪些文件我不想符号链接”...如果 不连自己想要什么都不知道,别人怎么可能帮到你?
  • 我的意思是我不知道会有多少子文件夹,可能是 2 或者可能更多。取决于我如何指定它们。

标签: bash directory dotfiles symlink-traversal


【解决方案1】:

只需使用 find 命令 - 更改:

for file in $dir/*/*; do

到这里:

for file in $(find $dir); do

【讨论】:

    猜你喜欢
    • 2015-05-18
    • 2019-05-07
    • 1970-01-01
    • 2016-02-27
    • 2020-05-19
    • 1970-01-01
    • 1970-01-01
    • 2012-11-24
    • 2015-07-31
    相关资源
    最近更新 更多