Skip to content

商务合作:vTikTok


公众号:



用户

使用shell写一段脚本,这个脚本是用来给某个目录下的文章类目增加index.md索引的,比如note目录下有历史篇,认知篇,工作篇等文件夹(每个文件夹下有相关的xx.md文章),需要给每个note下所有的篇章下都增加一个index.md文件,这个文件的格式如:

markdown
1. [如何学习](如何学习.md)
2. [克服内心的恐惧](克服内心的恐惧.md)
1. [如何学习](如何学习.md)
2. [克服内心的恐惧](克服内心的恐惧.md)

请写出这样的shell脚本。

助手

你可以使用以下的bash脚本:

bash
#!/bin/bash

# 待查找的目录
basedir="/path/to/note"

# 遍历该目录下的所有子目录
for dir in $(find ${basedir} -type d)
do
    # 切换到当前子目录
    cd ${dir}
    
    # 创建或清空index.md文件
    > index.md
    
    # 遍历当前子目录下的所有.md文件,并添加到index.md文件中
    i=0
    for file in $(find . -name "*.md")
    do
        ((i++))
        # 去掉文件名的前面两个字符,即'./'
        file=${file:2}
        
        # 除非是index.md,其他的.md文件才添加到index.md文件中
        if [ "${file}" != "index.md" ]; then
            echo "${i}. [${file%.*}](${file})" >> index.md
        fi
    done
done
#!/bin/bash

# 待查找的目录
basedir="/path/to/note"

# 遍历该目录下的所有子目录
for dir in $(find ${basedir} -type d)
do
    # 切换到当前子目录
    cd ${dir}
    
    # 创建或清空index.md文件
    > index.md
    
    # 遍历当前子目录下的所有.md文件,并添加到index.md文件中
    i=0
    for file in $(find . -name "*.md")
    do
        ((i++))
        # 去掉文件名的前面两个字符,即'./'
        file=${file:2}
        
        # 除非是index.md,其他的.md文件才添加到index.md文件中
        if [ "${file}" != "index.md" ]; then
            echo "${i}. [${file%.*}](${file})" >> index.md
        fi
    done
done

以上脚本详情:

  1. 我们首先定义了一个作为搜索起点的基础路径。
  2. 然后通过find命令,我们迭代处理目录下的所有子目录。
  3. 接下来我们使用>操作符创建或清空index.md文件。
  4. 用另一个find命令迭代当前子目录中的所有.md文件,并添加到index.md中。
  5. 通过${file:2}去掉了文件名的前两个字符(即 './')。
  6. 最后,使用${file%.*}除去扩展名.md,将处理后的文件名和对应链接写入index.md中。

注意:将上面的脚本保存为.sh文件(比如create_index.sh),然后通过bash create_index.sh运行。 记住要替换变量basedir为实际的路径。

用户

给上面的脚本做个小优化,就是在每个index.md的开头增加一个固定的文本内容:

shell
---
layout: doc
aside: true
---
---
layout: doc
aside: true
---

助手

好的,这个很容易办到。在创建index.md文件时,我们只需要预先写入想要的内容。以下是更新的脚本:

bash
#!/bin/bash

# 待查找的目录
basedir="/path/to/note"

# 遍历该目录下的所有子目录
for dir in $(find ${basedir} -type d)
do
    # 准备固定的头部内容
    header='---
layout: doc
aside: true
---'
    
    # 切换到当前子目录
    cd ${dir}
    
    # 创建或清空index.md文件,并写入固定的头部内容
    echo "$header" > index.md
    
    # 遍历当前子目录下的所有.md文件,并添加到index.md文件中
    i=0
    for file in $(find . -name "*.md")
    do
        ((i++))
        # 去掉文件名的前面两个字符,即'./'
        file=${file:2}
        
        # 除非是index.md,其他的.md文件才添加到index.md文件中
        if [ "${file}" != "index.md" ]; then
            echo "${i}. [${file%.*}](${file})" >> index.md
        fi
    done
done
#!/bin/bash

# 待查找的目录
basedir="/path/to/note"

# 遍历该目录下的所有子目录
for dir in $(find ${basedir} -type d)
do
    # 准备固定的头部内容
    header='---
layout: doc
aside: true
---'
    
    # 切换到当前子目录
    cd ${dir}
    
    # 创建或清空index.md文件,并写入固定的头部内容
    echo "$header" > index.md
    
    # 遍历当前子目录下的所有.md文件,并添加到index.md文件中
    i=0
    for file in $(find . -name "*.md")
    do
        ((i++))
        # 去掉文件名的前面两个字符,即'./'
        file=${file:2}
        
        # 除非是index.md,其他的.md文件才添加到index.md文件中
        if [ "${file}" != "index.md" ]; then
            echo "${i}. [${file%.*}](${file})" >> index.md
        fi
    done
done

这次在创建index.md的时候,我们加入了一个变量header,该变量包含了你希望在文件开头添加的字符。这样首次创建index.md时就会输入这些字符。后面加入的链接,依旧会附加在这些字符后面。

用户

递归的时候,需要忽略images文件夹,请再次针对上述代码进行优化。

助手

你可以通过增加-prune选项来实现这种需求,这个选项能够让find命令忽略特定的文件夹。这是更新后的脚本:

bash
#!/bin/bash

# 待查找的目录
basedir="/path/to/note"

# 遍历该目录下的所有子目录(忽略'images'文件夹)
for dir in $(find ${basedir} -type d -not -path '*/images/*')
do
    # 准备固定的头部内容
    header='---
layout: doc
aside: true
---'
    
    # 切换到当前子目录
    cd ${dir}
    
    # 创建或清空index.md文件,并写入固定的头部内容
    echo "$header" > index.md
    
    # 遍历当前子目录下的所有.md文件(忽略'images'文件夹),并添加到index.md文件中
    i=0
    for file in $(find . -name "*.md" -not -path '*/images/*')
    do
        ((i++))
        # 去掉文件名的前面两个字符,即'./'
        file=${file:2}
        
        # 除非是index.md,其他的.md文件才添加到index.md文件中
        if [ "${file}" != "index.md" ]; then
            echo "${i}. [${file%.*}](${file})" >> index.md
        fi
    done
done
#!/bin/bash

# 待查找的目录
basedir="/path/to/note"

# 遍历该目录下的所有子目录(忽略'images'文件夹)
for dir in $(find ${basedir} -type d -not -path '*/images/*')
do
    # 准备固定的头部内容
    header='---
layout: doc
aside: true
---'
    
    # 切换到当前子目录
    cd ${dir}
    
    # 创建或清空index.md文件,并写入固定的头部内容
    echo "$header" > index.md
    
    # 遍历当前子目录下的所有.md文件(忽略'images'文件夹),并添加到index.md文件中
    i=0
    for file in $(find . -name "*.md" -not -path '*/images/*')
    do
        ((i++))
        # 去掉文件名的前面两个字符,即'./'
        file=${file:2}
        
        # 除非是index.md,其他的.md文件才添加到index.md文件中
        if [ "${file}" != "index.md" ]; then
            echo "${i}. [${file%.*}](${file})" >> index.md
        fi
    done
done

在这个版本中,find命令的-not -path '*/images/*'选项表示排除images文件夹。为了保持一致,我们在两个find命令中都增加了这个选项,一个用于迭代子目录,一个用于迭代.md文件。

最后更新时间: