You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何仅下载GitHub指定目录且不包含其子目录?

仅下载GitHub单个目录(不含子目录)的方法

以下是两种实用的解决方法,无需依赖第三方在线工具:

方法一:GitHub API + 命令行工具(curl/wget + jq)

直接通过GitHub的内容API筛选出当前目录的文件并批量下载:

  1. 将目标目录的tree链接转换为API格式:比如原链接https://github.com/danielmiessler/SecLists/tree/master/Passwords,对应API地址为https://api.github.com/repos/danielmiessler/SecLists/contents/Passwords
  2. 执行以下命令批量下载目录内的所有文件(需提前安装curl、wget和jq):
    curl -s "https://api.github.com/repos/danielmiessler/SecLists/contents/Passwords" | jq -r '.[] | select(.type != "dir") | .download_url' | xargs wget
    
    这条命令会先获取目录的元数据,筛选出非目录类型的文件,提取它们的直接下载链接,最后用wget批量下载。

方法二:Git稀疏检出(Sparse Checkout)

通过Git的稀疏检出功能,只拉取目标目录的直接文件,排除子目录:

  1. 初始化本地仓库并关联远程:
    git init seclists-passwords
    cd seclists-passwords
    git remote add origin https://github.com/danielmiessler/SecLists.git
    
  2. 启用稀疏检出并配置规则:
    git config core.sparseCheckout true
    # 只包含Passwords目录下的直接文件
    echo "Passwords/*" >> .git/info/sparse-checkout
    # 排除Passwords目录下的所有子目录
    echo "!Passwords/**/*/" >> .git/info/sparse-checkout
    
  3. 拉取对应分支的内容:
    git pull origin master
    
    完成后本地seclists-passwords/Passwords目录下只会有原目录的直接文件,不会包含子目录内容。

目前主流的在线GitHub目录下载工具大多默认递归下载,上述命令行方法能更精准地满足仅下载当前目录的需求。

内容的提问来源于stack exchange,提问作者allexj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 14:12:06