在Linux中,有多种方法可以提取文件目录树。以下是几种常用的方法:
1. 使用 tree 命令(推荐)
# 安装 tree(如果尚未安装)
sudo apt install tree # Ubuntu/Debian
sudo yum install tree # CentOS/RHEL
# 基本用法
tree
# 显示所有文件(包括隐藏文件)
tree -a
# 只显示目录
tree -d
# 显示完整路径
tree -f
# 限制显示深度
tree -L 2
# 输出到文件
tree > directory_tree.txt
# 结合使用示例
tree -a -L 3 -f > project_structure.txt
2. 使用 find 命令
# 基本目录树
find . -type d | sort
# 显示文件和目录,带有缩进
find . -type d -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
# 只显示特定深度的内容
find . -maxdepth 2 -type d
# 排除某些目录
find . -type d -not -path "./.git*"
3. 使用 ls 命令递归显示
# 递归列出所有文件和目录
ls -R
# 以树状格式显示(需要安装)
ls --tree
# 结合 find 和 ls
find . -type d -exec ls -d {} \;
4. 使用 pstree 风格的脚本
# 创建一个简单的目录树显示脚本
#!/bin/bash
function tree() {
find "${1:-.}" -type d | sed -e "s/[^-][^\/]*\// │/g" -e "s/│\([^ ]\)/│── \1/"
}
5. 使用 du 命令
# 显示目录大小和结构
du -h --max-depth=2
# 图形化显示
du -h | sort -h
实用示例
生成项目目录结构文档:
tree -I 'node_modules|.git|__pycache__' -a --dirsfirst > PROJECT_STRUCTURE.md
创建带有文件大小的目录树:
tree -h -D
生成JSON格式的目录树:
tree -J > structure.json
各方法比较
tree: 功能最强大,输出美观,支持多种格式find: 灵活性高,可以精确控制过滤条件ls -R: 系统自带,简单快速- 自定义脚本: 可根据特定需求定制
对于日常使用,推荐使用 tree 命令,因为它专门为此目的设计,输出格式清晰易读,且支持多种输出格式(文本、HTML、JSON、XML等)。
获取指定目录下的文件目录树非常简单。以下是各种方法的详细说明:
1. 使用 tree 命令指定目录
# 基本用法
tree /path/to/directory
# 示例
tree /home/user/projects
tree /var/www/html
tree .
# 显示所有文件(包括隐藏文件)
tree -a /path/to/directory
# 只显示目录
tree -d /path/to/directory
# 限制显示深度为3级
tree -L 3 /path/to/directory
# 输出到文件
tree /path/to/directory > output.txt
2. 使用 find 命令指定目录
# 基本用法
find /path/to/directory -type d
# 显示树状结构
find /path/to/directory -type d | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
# 查找特定类型的文件
find /path/to/directory -name "*.txt"
find /path/to/directory -type f
# 结合使用
find /home/user/projects -maxdepth 2 -type d
3. 使用 ls 命令指定目录
# 递归列出指定目录
ls -R /path/to/directory
# 详细信息
ls -laR /path/to/directory
# 树状显示(如果支持)
ls --tree /path/to/directory
4. 实际应用示例
查看当前用户home目录结构:
tree ~ -L 2
查看系统日志目录结构:
tree /var/log -L 2
查看项目目录,排除某些文件:
tree /path/to/project -I 'node_modules|.git|*.pyc'
生成带权限和时间的详细目录树:
tree -pugh /path/to/directory
5. 高级用法
生成HTML格式的目录树:
tree -H /path/to/directory -o output.html
生成JSON格式:
tree -J /path/to/directory > structure.json
生成XML格式:
tree -X /path/to/directory > structure.xml
自定义输出格式:
# 只显示目录名,不显示文件
tree -dfi /path/to/directory
# 显示文件大小(人类可读格式)
tree -s -h /path/to/directory
6. 实用脚本示例
#!/bin/bash
# 创建一个函数来生成目录树报告
generate_tree_report() {
local target_dir="$1"
local output_file="${2:-directory_report.txt}"
echo "目录树报告: $target_dir" > "$output_file"
echo "生成时间: $(date)" >> "$output_file"
echo "======================================" >> "$output_file"
tree -a -h -D "$target_dir" >> "$output_file"
echo "报告已生成: $output_file"
}
# 使用示例
generate_tree_report "/home/user/projects" "project_structure.txt"
7. 排除特定目录/文件
# 排除多个目录
tree -I 'node_modules|.git|__pycache__|*.log' /path/to/directory
# 只显示特定文件类型
find /path/to/directory -name "*.py" -o -name "*.js" | tree --fromfile
常用组合命令
# 快速查看目录结构(3级深度)
tree -L 3 /path/to/your/directory
# 生成详细的目录文档
tree -a -h -D -I '.git|node_modules' /path/to/project > project_structure.txt
# 实时查看目录变化(需要安装watch)
watch tree -L 2 /path/to/directory
使用这些命令,你可以轻松获取任何指定目录的文件目录树结构!
你就会得到下面这样的一个目录结构数
/www/wwwroot/QQ.COM/public/plugins/addon/idcsmart_ticket
├── auth_clientarea.php
├── auth.php
├── config
│ ├── config.php
│ └── email_template
│ ├── admin_reply_ticket.html
│ ├── client_close_ticket.html
│ ├── client_create_ticket.html
│ └── client_reply_ticket.html
├── config.php
├── controller
│ ├── clientarea
│ │ └── TicketController.php
│ ├── TicketController.php
│ ├── TicketDeliveryController.php
│ ├── TicketNotesController.php
│ ├── TicketPrereplyController.php
│ ├── TicketStatusController.php
│ └── TicketTypeController.php
├── IdcsmartTicket.php
├── lang
│ ├── en-us.php
│ ├── zh-cn.php
│ └── zh-hk.php
├── logic
│ └── IdcsmartTicketLogic.php
├── model
│ ├── IdcsmartTicketDeliveryModel.php
│ ├── IdcsmartTicketForwardModel.php
│ ├── IdcsmartTicketHostLinkModel.php
│ ├── IdcsmartTicketModel.php
│ ├── IdcsmartTicketNotesModel.php
│ ├── IdcsmartTicketPrereplyModel.php
│ ├── IdcsmartTicketReplyModel.php
│ ├── IdcsmartTicketStatusModel.php
│ ├── IdcsmartTicketTypeAdminLinkModel.php
│ ├── IdcsmartTicketTypeModel.php
│ └── IdcsmartTicketUpstreamModel.php
├── README.md
├── route.php
├── sidebar_clientarea.php
├── sidebar.php
├── template
│ ├── admin
│ │ ├── api
│ │ │ ├── client.js
│ │ │ ├── common.js
│ │ │ ├── order.js
│ │ │ ├── refund.js
│ │ │ └── template.js
│ │ ├── bak
│ │ │ ├── ticket_internal_detail.php
│ │ │ └── ticket_internal.php
│ │ ├── client_ticket.html
│ │ ├── components
│ │ │ └── opinionButton.js
│ │ ├── css
│ │ │ ├── addTicket.css
│ │ │ ├── addTicket.less
│ │ │ ├── common
│ │ │ │ └── viewer.min.css
│ │ │ ├── opinionButton.css
│ │ │ ├── opinionButton.less
│ │ │ ├── order_copy.css
│ │ │ ├── order_copy.less
│ │ │ ├── order.css
│ │ │ ├── order.less
│ │ │ ├── refund.css
│ │ │ ├── ticket_setting.css
│ │ │ └── ticket_setting.less
│ │ ├── img
│ │ │ ├── admin.png
│ │ │ └── client.png
│ │ ├── index.html
│ ├── js
│ │ │ ├── client_ticket.js
│ │ │ ├── common
│ │ │ │ ├── axios.min.js
│ │ │ │ ├── lang.js
│ │ │ │ ├── layout.js
│ │ │ │ ├── moment.min.js
│ │ │ │ ├── tdesign.min.js
│ │ │ │ ├── viewer.min.js
│ │ │ │ └── vue.js
│ │ │ ├── index.js
│ │ │ ├── lang.js
│ │ │ ├── ticket_add.js
│ │ │ ├── ticket_detail.js
│ │ │ ├── ticket_setting.js
│ │ │ └── xss.js
│ │ ├── lang
│ │ │ ├── en-us.js
│ │ │ ├── zh-cn.js
│ │ │ └── zh-hk.js
│ │ ├── media
│ │ │ └── tip.wav
│ │ ├── ticket_add.html
│ │ ├── ticket_detail.html
│ │ └── ticket_setting.html
│ └── clientarea
│ ├── mobile
│ │ └── mfm201
│ │ ├── api
│ │ │ └── ticket.js
│ │ ├── css
│ │ │ ├── ticket.css
│ │ │ ├── ticketDetails.css
│ │ │ ├── ticketDetails.less
│ │ │ └── ticket.less
│ │ ├── img
│ │ │ └── img
│ │ │ ├── finance
│ │ │ │ └── back.png
│ │ │ └── referral
│ │ │ ├── back.png
│ │ │ ├── top1.png
│ │ │ ├── top2.png
│ │ │ ├── top3.png
│ │ │ └── top4.png
│ │ ├── js
│ │ │ ├── ticketDetails.js
│ │ │ └── ticket.js
│ │ ├── lang
│ │ │ └── index.js
│ │ ├── ticketDetails.html
│ │ └── ticket.html
│ └── pc
│ └── default
│ ├── addTicket.html
│ ├── api
│ │ └── ticket.js
│ ├── css
│ │ ├── addTicket.css
│ │ ├── addTicket.less
│ │ ├── ticket.css
│ │ ├── ticketDetails.css
│ │ ├── ticketDetails.less
│ │ └── ticket.less
├── img
│ │ ├── common
│ │ │ ├── bell.png
│ │ │ ├── cart.png
│ │ │ ├── CN.png
│ │ │ ├── exit.png
│ │ │ ├── maintain.png
│ │ │ ├── menu10.png
│ │ │ ├── menu1.png
│ │ │ ├── menu2.png
│ │ │ ├── menu3.png
│ │ │ ├── menu4.png
│ │ │ ├── menu5.png
│ │ │ ├── menu6.png
│ │ │ ├── menu7.png
│ │ │ ├── menu8.png
│ │ │ ├── menu9.png
│ │ │ ├── menu.png
│ │ │ ├── search.png
│ │ │ ├── toTop.png
│ │ │ └── US.png
│ │ ├── finance
│ │ │ └── back.png
│ │ └── ticket
│ │ ├── admin.png
│ │ ├── client.png
│ │ ├── top-bg-1.png
│ │ ├── top-bg-2.png
│ │ ├── top-bg-3.png
│ │ └── top-bg-4.png
│ ├── js
│ │ ├── addTicket.js
│ │ ├── ticketDetails.js
│ │ ├── ticket.js
│ │ ├── tinymce
│ │ │ ├── icons
│ │ │ │ └── default
│ │ │ │ └── icons.min.js
│ │ │ ├── jquery.tinymce.min.js
│ │ │ ├── langs
│ │ │ │ ├── README.md
│ │ │ │ └── zh_CN.js
│ │ │ ├── license.txt
│ │ │ ├── plugins
│ │ │ │ ├── advlist
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── anchor
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── autolink
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── autoresize
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── autosave
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── bbcode
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── charmap
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── code
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── codesample
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── colorpicker
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── contextmenu
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── directionality
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── emoticons
│ │ │ │ │ ├── js
│ │ │ │ │ │ ├── emojiimages.js
│ │ │ │ │ │ ├── emojiimages.min.js
│ │ │ │ │ │ ├── emojis.js
│ │ │ │ │ │ └── emojis.min.js
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── fullpage
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── fullscreen
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── help
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── hr
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── image
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── imagetools
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── importcss
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── insertdatetime
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── legacyoutput
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── link
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── lists
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── media
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── nonbreaking
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── noneditable
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── pagebreak
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── paste
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── preview
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── print
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── quickbars
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── save
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── searchreplace
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── spellchecker
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── tabfocus
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── table
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── template
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── textcolor
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── textpattern
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── toc
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── visualblocks
│ │ │ │ │ └── plugin.min.js
│ │ │ │ ├── visualchars
│ │ │ │ │ └── plugin.min.js
│ │ │ │ └── wordcount
│ │ │ │ └── plugin.min.js
│ │ │ ├── skins
│ │ │ │ ├── content
│ │ │ │ │ ├── dark
│ │ │ │ │ │ └── content.min.css
│ │ │ │ │ ├── default
│ │ │ │ │ │ └── content.min.css
│ │ │ │ │ ├── document
│ │ │ │ │ │ └── content.min.css
│ │ │ │ │ └── writer
│ │ │ │ │ └── content.min.css
│ │ │ │ └── ui
│ │ │ │ ├── oxide
│ │ │ │ │ ├── content.inline.min.css
│ │ │ │ │ ├── content.min.css
│ │ │ │ │ ├── content.mobile.min.css
│ │ │ │ │ ├── fonts
│ │ │ │ │ │ └── tinymce-mobile.woff
│ │ │ │ │ ├── skin.min.css
│ │ │ │ │ ├── skin.mobile.min.css
│ │ │ │ │ └── skin.shadowdom.min.css
│ │ │ │ └── oxide-dark
│ │ │ │ ├── content.inline.min.css
│ │ │ │ ├── content.min.css
│ │ │ │ ├── content.mobile.min.css
│ │ │ │ ├── fonts
│ │ │ │ │ └── tinymce-mobile.woff
│ │ │ │ ├── skin.min.css
│ │ │ │ ├── skin.mobile.min.css
│ │ │ │ └── skin.shadowdom.min.css
│ │ │ ├── themes
│ │ │ │ ├── mobile
│ │ │ │ │ └── theme.min.js
│ │ │ │ └── silver
│ │ │ │ └── theme.min.js
│ │ │ ├── tinymce.d.ts
│ │ │ └── tinymce.min.js
│ │ └── xss.js
│ ├── lang
│ │ └── index.js
│ ├── ticketDetails.html
│ └── ticket.html
├── upgrade.php
├── upload
└── validate
├── TicketDeliveryValidate.php
├── TicketStatusValidate.php
├── TicketTypeValidate.php
└── TicketValidate.php
您想要将文件夹下所有文件的内容都读取出来,保存到同一个txt文件中,并在每个文件内容前标明文件路径。以下是几种实现方法:
1. 使用Shell脚本
#!/bin/bash
DIR_PATH="/www/wwwroot/MOMO.COM/public/plugins/addon/idcsmart_ticket"
OUTPUT_FILE="~/all_files_content.txt"
{
echo "文件内容汇总报告"
echo "生成时间: $(date)"
echo "========================================"
echo ""
# 遍历所有文件
find "$DIR_PATH" -type f | while read file; do
echo "================================================================================"
echo "文件路径: $file"
echo "文件大小: $(du -h "$file" | cut -f1)"
echo "修改时间: $(stat -c %y "$file" 2>/dev/null || stat -f %Sm "$file")"
echo "================================================================================"
echo ""
# 尝试读取文件内容
if file "$file" | grep -q "text"; then
# 文本文件,直接显示内容
cat "$file"
else
# 二进制文件,显示文件类型
file "$file"
echo "[此为二进制文件,无法显示具体内容]"
fi
echo ""
echo ""
done
} > "$OUTPUT_FILE"
echo "所有文件内容已导出到: $OUTPUT_FILE"
2. 简化版本
#!/bin/bash
DIR_PATH="/www/wwwroot/MOMO.COM/public/plugins/addon/idcsmart_ticket"
OUTPUT_FILE="~/files_content_simple.txt"
find "$DIR_PATH" -type f | while read file; do
echo "=== 文件路径: $file ===" >> "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"
# 只读取文本文件
if file "$file" | grep -q "text"; then
cat "$file" >> "$OUTPUT_FILE" 2>/dev/null
else
echo "[二进制文件: $(file -b "$file")]" >> "$OUTPUT_FILE"
fi
echo -e "\n\n" >> "$OUTPUT_FILE"
done
echo "导出完成: $OUTPUT_FILE"
3. 只导出特定类型文件的内容
#!/bin/bash
DIR_PATH="/www/wwwroot/MOMO.COM/public/plugins/addon/idcsmart_ticket"
OUTPUT_FILE="~/source_code_content.txt"
{
echo "源代码文件内容汇总"
echo "生成时间: $(date)"
echo ""
# 只处理源代码文件
find "$DIR_PATH" -type f \( -name "*.php" -o -name "*.html" -o -name "*.js" -o -name "*.css" -o -name "*.txt" -o -name "*.md" \) | while read file; do
echo "################################################################################"
echo "# 文件: $file"
echo "################################################################################"
echo ""
cat "$file"
echo -e "\n\n\n"
done
} > "$OUTPUT_FILE"
echo "源代码文件内容已导出到: $OUTPUT_FILE"
4. 带行号的版本
#!/bin/bash
DIR_PATH="/www/wwwroot/MOMO.COM/public/plugins/addon/idcsmart_ticket"
OUTPUT_FILE="~/files_content_with_lines.txt"
find "$DIR_PATH" -type f | while read file; do
echo "========================================" >> "$OUTPUT_FILE"
echo "文件: $file" >> "$OUTPUT_FILE"
echo "========================================" >> "$OUTPUT_FILE"
if file "$file" | grep -q "text"; then
# 带行号显示内容
nl -ba "$file" >> "$OUTPUT_FILE" 2>/dev/null
else
echo "[二进制文件]" >> "$OUTPUT_FILE"
fi
echo -e "\n" >> "$OUTPUT_FILE"
done
echo "带行号的文件内容已导出到: $OUTPUT_FILE"
5. 一键执行命令
# 最简单的一行命令
find /www/wwwroot/MOMO.COM/public/plugins/addon/idcsmart_ticket -type f -exec sh -c 'echo "=== {} ==="; cat "{}" 2>/dev/null || echo "[无法读取]"' \; > ~/all_content.txt
6. 处理编码问题(中文支持)
#!/bin/bash
DIR_PATH="/www/wwwroot/MOMO.COM/public/plugins/addon/idcsmart_ticket"
OUTPUT_FILE="~/files_content_utf8.txt"
{
echo "文件内容汇总 (UTF-8编码)"
echo "生成时间: $(date)"
echo ""
find "$DIR_PATH" -type f | while read file; do
echo "文件路径: $file"
echo "----------------------------------------"
# 尝试用不同编码读取
if iconv -f UTF-8 -t UTF-8 "$file" >/dev/null 2>&1; then
cat "$file"
elif iconv -f GBK -t UTF-8 "$file" >/dev/null 2>&1; then
iconv -f GBK -t UTF-8 "$file"
else
echo "[无法正确读取文件内容,可能是二进制文件]"
file "$file"
fi
echo -e "\n\n"
done
} > "$OUTPUT_FILE"
echo "文件内容已导出到: $OUTPUT_FILE"
推荐使用
对于您的工单插件目录,建议使用第3种方法(只导出特定类型文件),因为:
- 避免二进制文件:不会包含图片、字体等无意义的二进制内容
- 文件大小可控:只导出源代码和配置文件
- 内容可读性强:都是文本文件,便于阅读分析
执行命令:
# 进入目录执行
cd /www/wwwroot/MOMO.COM/public/plugins/addon/idcsmart_ticket
./export_content.sh # 如果保存为脚本文件
或者直接复制第3种方法的代码保存为脚本执行。这样就能得到一个包含所有文件路径和内容的txt文件了!
~/all_files_content.txt 中的 ~ 代表的是当前用户的家目录。
具体路径解释:
~= 当前用户的家目录- 对于普通用户:
/home/用户名/ - 对于root用户:
/root/
实际路径示例:
# 如果当前用户是 www(举例)
~/all_files_content.txt = /home/www/all_files_content.txt
# 如果当前用户是 root
~/all_files_content.txt = /root/all_files_content.txt
# 如果当前用户是 ubuntu
~/all_files_content.txt = /home/ubuntu/all_files_content.txt
查看具体路径的方法:
# 方法1:查看当前用户家目录
echo ~
# 输出:/home/username
# 方法2:查看文件的真实路径
ls ~/all_files_content.txt
# 方法3:使用绝对路径查看
cat /home/你的用户名/all_files_content.txt
修改输出路径的方法:
如果您想将文件保存到其他位置,可以修改脚本中的路径:
# 保存到当前目录
OUTPUT_FILE="./all_files_content.txt"
# 保存到指定目录
OUTPUT_FILE="/tmp/all_files_content.txt"
# 保存到桌面(如果有桌面环境)
OUTPUT_FILE="~/Desktop/all_files_content.txt"
# 保存到web目录(便于下载查看)
OUTPUT_FILE="/www/wwwroot/MOMO.COM/all_files_content.txt"
推荐修改:
建议修改脚本,使用绝对路径:
#!/bin/bash
DIR_PATH="/www/wwwroot/MOMO.COM/public/plugins/addon/idcsmart_ticket"
OUTPUT_FILE="/www/wwwroot/MOMO.COM/all_files_content.txt" # 修改为web可访问路径
# 或者保存到tmp目录
# OUTPUT_FILE="/tmp/idcsmart_ticket_content.txt"
{
# 脚本内容...
} > "$OUTPUT_FILE"
echo "所有文件内容已导出到: $OUTPUT_FILE"
这样您就知道文件具体保存在哪里,并且可以通过web浏览器或其他方式方便地访问了。
















评论前必须登录!
注册