2025-01-31
This commit is contained in:
parent
991fcdda6d
commit
83322ef967
8 changed files with 55 additions and 64 deletions
|
@ -1,4 +1,4 @@
|
||||||
© Головин Г.Г., 2021-2024
|
© Головин Г.Г., 2021-2025
|
||||||
|
|
||||||
Опубликовано под [Открытой лицензией 1.1](OPEN_LICENSE.txt)
|
Опубликовано под [Открытой лицензией 1.1](OPEN_LICENSE.txt)
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
© Golovin G.G., translation from Russian, 2021-2024
|
© Golovin G.G., translation from Russian, 2021-2025
|
||||||
|
|
||||||
Published under the [Open License 1.1](OPEN_LICENSE.txt)
|
Published under the [Open License 1.1](OPEN_LICENSE.txt)
|
||||||
|
|
||||||
|
|
13
WIKI.md
13
WIKI.md
|
@ -1,7 +1,5 @@
|
||||||
<hr>
|
|
||||||
|
|
||||||
<details open>
|
<details open>
|
||||||
<summary><h3>Оглавление</h3></summary>
|
<summary><b>Оглавление / Contents</b></summary>
|
||||||
|
|
||||||
<hr>
|
<hr>
|
||||||
|
|
||||||
|
@ -11,13 +9,6 @@
|
||||||
- [График функции в консоли](https://pomodoro5.mircloud.ru/ru/2023/02/05/function-graph-in-console.html) — 05.02.2023.
|
- [График функции в консоли](https://pomodoro5.mircloud.ru/ru/2023/02/05/function-graph-in-console.html) — 05.02.2023.
|
||||||
- [Рисуем простую капчу](https://pomodoro5.mircloud.ru/ru/2023/01/03/drawing-simple-captcha.html) — 03.01.2023.
|
- [Рисуем простую капчу](https://pomodoro5.mircloud.ru/ru/2023/01/03/drawing-simple-captcha.html) — 03.01.2023.
|
||||||
|
|
||||||
</details>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
|
|
||||||
<details open>
|
|
||||||
<summary><h3>Contents</h3></summary>
|
|
||||||
|
|
||||||
<hr>
|
<hr>
|
||||||
|
|
||||||
- [Directory tree with links](https://pomodoro5.mircloud.ru/en/2023/08/04/directory-tree.html) — 04.08.2023.
|
- [Directory tree with links](https://pomodoro5.mircloud.ru/en/2023/08/04/directory-tree.html) — 04.08.2023.
|
||||||
|
@ -27,5 +18,3 @@
|
||||||
- [Drawing simple captcha](https://pomodoro5.mircloud.ru/en/2023/01/04/drawing-simple-captcha.html) — 04.01.2023.
|
- [Drawing simple captcha](https://pomodoro5.mircloud.ru/en/2023/01/04/drawing-simple-captcha.html) — 04.01.2023.
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
<hr>
|
|
||||||
|
|
|
@ -7,9 +7,9 @@ url: "https://pomodoro5.mircloud.ru"
|
||||||
# подпапка этой сборки для относительных URL-ов
|
# подпапка этой сборки для относительных URL-ов
|
||||||
baseurl: "/color"
|
baseurl: "/color"
|
||||||
# ссылка в верхнем левом углу заглавных страниц
|
# ссылка в верхнем левом углу заглавных страниц
|
||||||
homepage_url: "https://gitea.com/pomodoro/5"
|
homepage_url: "https://codeberg.org/pomodoro/5"
|
||||||
# представление ссылки
|
# представление ссылки
|
||||||
homepage_name: "GITEA"
|
homepage_name: "CODEBERG"
|
||||||
# подпапка альтернативной сборки
|
# подпапка альтернативной сборки
|
||||||
older_tomato_baseurl: ""
|
older_tomato_baseurl: ""
|
||||||
# часовой пояс для формата даты ISO-8601
|
# часовой пояс для формата даты ISO-8601
|
||||||
|
|
|
@ -7,9 +7,9 @@ url: "https://pomodoro5.mircloud.ru"
|
||||||
# подпапка этой сборки для относительных URL-ов
|
# подпапка этой сборки для относительных URL-ов
|
||||||
baseurl: ""
|
baseurl: ""
|
||||||
# ссылка в верхнем левом углу заглавных страниц
|
# ссылка в верхнем левом углу заглавных страниц
|
||||||
homepage_url: "https://gitea.com/pomodoro/5"
|
homepage_url: "https://codeberg.org/pomodoro/5"
|
||||||
# представление ссылки
|
# представление ссылки
|
||||||
homepage_name: "GITEA"
|
homepage_name: "CODEBERG"
|
||||||
# подпапка альтернативной сборки
|
# подпапка альтернативной сборки
|
||||||
color_tomato_baseurl: "/color"
|
color_tomato_baseurl: "/color"
|
||||||
# часовой пояс для формата даты ISO-8601
|
# часовой пояс для формата даты ISO-8601
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
---
|
---
|
||||||
title: Directory tree with links
|
title: Directory tree with links
|
||||||
description: We write a Bash script for building a directory tree for a repository in the Markdown file. We use only Bash tools and basic Linux software without additional.
|
description: We write a Bash script for building a directory tree for a repository in the Markdown file. We use only Bash builtin tools and basic Linux software without...
|
||||||
sections: [Recursion,File processing,Web-navigation]
|
sections: [Recursion,File processing,Web-navigation]
|
||||||
tags: [linux,bash,markdown,html,structure,folders,files,references,sorting]
|
tags: [linux,bash,markdown,html,structure,folders,files,references,sorting]
|
||||||
canonical_url: /en/2023/08/04/directory-tree.html
|
canonical_url: /en/2023/08/04/directory-tree.html
|
||||||
|
@ -10,11 +10,11 @@ date: 2023.08.04
|
||||||
lang: en
|
lang: en
|
||||||
---
|
---
|
||||||
|
|
||||||
We write a Bash script for building a directory tree for a repository in
|
We write a Bash script for building a directory tree for a repository in the
|
||||||
the Markdown file. We use only Bash tools and basic Linux software — `ls`,
|
Markdown file. We use only Bash builtin tools and basic Linux software — `ls`,
|
||||||
`sed`, `tr` and `echo` — without additional programs. The obtained file
|
`sed`, `tr` and `printf` — without additional programs. The obtained file
|
||||||
[`DIRECTORY_TREE.md`]({{ site.homepage_url }} "{{ site.homepage_name }}")
|
[`DIRECTORY_TREE.md`]({{ site.homepage_url }} "{{ site.homepage_name }}")
|
||||||
will be used in the web interface to navigate through the objects of the repository.
|
is useful in the web-interface to navigate through the objects of the repository.
|
||||||
|
|
||||||
We create a recursive function and use it to bypass all files and directories of the repository,
|
We create a recursive function and use it to bypass all files and directories of the repository,
|
||||||
excluding the list from `.gitignore` — we build a directory structure in the form of a tree. We
|
excluding the list from `.gitignore` — we build a directory structure in the form of a tree. We
|
||||||
|
@ -24,31 +24,32 @@ concise Markdown file with links.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
# sorted list of files and directories
|
|
||||||
function list_directory_contents {
|
|
||||||
# first uppercase letters, then lowercase, first directories, then files
|
|
||||||
eval "LC_COLLATE=C ls -A --group-directories-first $exclusions $1"
|
|
||||||
}
|
|
||||||
# directory tree with links
|
# directory tree with links
|
||||||
function directory_tree {
|
function directory_tree {
|
||||||
# arguments
|
# arguments
|
||||||
local path="$1"
|
local path="$1"
|
||||||
local head="$2"
|
local head="$2"
|
||||||
local tail="$3"
|
local tail="$3"
|
||||||
# prefix for current element
|
# get directory contents
|
||||||
[ "one" == "$4" ] && printf '%s' "/" || printf '\n%s' "$head"
|
if [ -d "$path" ]; then
|
||||||
# current element of the tree
|
# first uppercase letters, then lowercase, first directories, then files
|
||||||
|
ls_sorted="LC_COLLATE=C ls -A --group-directories-first $exclusions $path"
|
||||||
|
# sorted array of files and directories
|
||||||
|
local list && readarray -t list < <(eval "$ls_sorted")
|
||||||
|
# length of array
|
||||||
|
local size=${#list[@]}
|
||||||
|
# skip empty directory
|
||||||
|
[ "$size" == 0 ] && return
|
||||||
|
fi
|
||||||
|
# prefix of current element, collapse singletons into one line
|
||||||
|
[ "$4" == "one" ] && printf '%s' "/" || printf '\n%s' "$head"
|
||||||
|
# current element of the tree — relative hyperlink
|
||||||
printf '%s' "<a href='${path#*/}'>${path##*/}</a>"
|
printf '%s' "<a href='${path#*/}'>${path##*/}</a>"
|
||||||
# recursive calls for subdirectories
|
# recursive calls for subdirectories
|
||||||
if [ -d "$path" ]; then
|
if [ -d "$path" ]; then
|
||||||
local list # array of files and directories
|
|
||||||
readarray -t list < <(list_directory_contents "$path")
|
|
||||||
local size=${#list[@]} # length of array
|
|
||||||
local i # counter
|
local i # counter
|
||||||
for ((i = 0; i < size; i++)); do
|
for ((i = 0; i < size; i++)); do
|
||||||
if [ -z "${list[$i]}" ]; then
|
if ((size == 1)); then
|
||||||
continue # skip empty directory
|
|
||||||
elif ((size == 1)); then
|
|
||||||
directory_tree "$path/${list[$i]}" "$tail" "$tail" "one"
|
directory_tree "$path/${list[$i]}" "$tail" "$tail" "one"
|
||||||
elif ((i < size - 1)); then
|
elif ((i < size - 1)); then
|
||||||
directory_tree "$path/${list[$i]}" "$tail├─ " "$tail│ "
|
directory_tree "$path/${list[$i]}" "$tail├─ " "$tail│ "
|
||||||
|
|
|
@ -12,10 +12,10 @@ lang: en
|
||||||
{%- assign articles = "" | split: "" %}
|
{%- assign articles = "" | split: "" %}
|
||||||
{%- assign articles = articles | push: "Directory tree with links" %}
|
{%- assign articles = articles | push: "Directory tree with links" %}
|
||||||
{%- capture article_brief %}
|
{%- capture article_brief %}
|
||||||
We write a Bash script for building a directory tree for a repository in the Markdown file.
|
We write a Bash script for building a directory tree for a repository in the Markdown file. We
|
||||||
We use only Bash tools and basic Linux software — `ls`, `sed`, `tr` and `echo` — without
|
use only Bash builtin tools and basic Linux software — `ls`, `sed`, `tr` and `printf` — without
|
||||||
additional programs. The obtained file `DIRECTORY_TREE.md` will be used in the web interface
|
additional programs. The obtained file `DIRECTORY_TREE.md` is useful in the web-interface to
|
||||||
to navigate through the objects of the repository.
|
navigate through the objects of the repository.
|
||||||
|
|
||||||
We create a recursive function and use it to bypass all files and directories of the repository,
|
We create a recursive function and use it to bypass all files and directories of the repository,
|
||||||
excluding the list from `.gitignore` — we build a directory structure in the form of a tree. We
|
excluding the list from `.gitignore` — we build a directory structure in the form of a tree. We
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
---
|
---
|
||||||
title: Дерево каталогов со ссылками
|
title: Дерево каталогов со ссылками
|
||||||
description: Напишем скрипт Bash для построения дерева каталогов для репозитория в файле Markdown. Будем использовать только средства Bash и базовое ПО Linux без...
|
description: Напишем скрипт Bash для построения дерева каталогов для репозитория в файле Markdown. Будем использовать только встроенные средства Bash и базовое ПО Linux...
|
||||||
sections: [Рекурсия,Обработка файлов,Вёб-навигация]
|
sections: [Рекурсия,Обработка файлов,Вёб-навигация]
|
||||||
tags: [linux,bash,markdown,html,структура,каталоги,файлы,ссылки,сортировка]
|
tags: [linux,bash,markdown,html,структура,каталоги,файлы,ссылки,сортировка]
|
||||||
canonical_url: /ru/2023/08/03/directory-tree.html
|
canonical_url: /ru/2023/08/03/directory-tree.html
|
||||||
|
@ -9,11 +9,11 @@ title_translated: Directory tree with links
|
||||||
date: 2023.08.03
|
date: 2023.08.03
|
||||||
---
|
---
|
||||||
|
|
||||||
Напишем скрипт Bash для построения дерева каталогов для репозитория в
|
Напишем скрипт Bash для построения дерева каталогов для репозитория в файле
|
||||||
файле Markdown. Будем использовать только средства Bash и базовое ПО Linux
|
Markdown. Будем использовать только встроенные средства Bash и базовое ПО Linux
|
||||||
— `ls`, `sed`, `tr` и `echo` — без дополнительных программ. Полученный файл
|
— `ls`, `sed`, `tr` и `printf` — без дополнительных программ. Полученный файл
|
||||||
[`DIRECTORY_TREE.md`]({{ site.homepage_url }} "{{ site.homepage_name }}")
|
[`DIRECTORY_TREE.md`]({{ site.homepage_url }} "{{ site.homepage_name }}")
|
||||||
будем использовать в вёб-интерфейсе для навигации по объектам репозитория.
|
пригодится в вёб-интерфейсе для навигации по объектам репозитория.
|
||||||
|
|
||||||
Создаём рекурсивную функцию и с её помощью обходим все файлы и каталоги репозитория, за
|
Создаём рекурсивную функцию и с её помощью обходим все файлы и каталоги репозитория, за
|
||||||
исключением списка из `.gitignore` — строим структуру каталогов в форме дерева. Выводим
|
исключением списка из `.gitignore` — строим структуру каталогов в форме дерева. Выводим
|
||||||
|
@ -23,31 +23,32 @@ date: 2023.08.03
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
# отсортированный список файлов и каталогов
|
|
||||||
function list_directory_contents {
|
|
||||||
# сначала заглавные буквы, потом строчные, сначала каталоги, потом файлы
|
|
||||||
eval "LC_COLLATE=C ls -A --group-directories-first $exclusions $1"
|
|
||||||
}
|
|
||||||
# дерево каталогов со ссылками
|
# дерево каталогов со ссылками
|
||||||
function directory_tree {
|
function directory_tree {
|
||||||
# аргументы
|
# аргументы
|
||||||
local path="$1"
|
local path="$1"
|
||||||
local head="$2"
|
local head="$2"
|
||||||
local tail="$3"
|
local tail="$3"
|
||||||
# префикс для текущего элемента
|
# получить содержимое каталога
|
||||||
[ "one" == "$4" ] && printf '%s' "/" || printf '\n%s' "$head"
|
if [ -d "$path" ]; then
|
||||||
# текущий элемент дерева
|
# сначала заглавные буквы, потом строчные, сначала каталоги, потом файлы
|
||||||
|
ls_sorted="LC_COLLATE=C ls -A --group-directories-first $exclusions $path"
|
||||||
|
# отсортированный массив файлов и каталогов
|
||||||
|
local list && readarray -t list < <(eval "$ls_sorted")
|
||||||
|
# длина массива
|
||||||
|
local size=${#list[@]}
|
||||||
|
# пропустить пустой каталог
|
||||||
|
[ "$size" == 0 ] && return
|
||||||
|
fi
|
||||||
|
# префикс текущего элемента, сворачивать синглтоны в одну строку
|
||||||
|
[ "$4" == "one" ] && printf '%s' "/" || printf '\n%s' "$head"
|
||||||
|
# текущий элемент дерева — относительная гиперссылка
|
||||||
printf '%s' "<a href='${path#*/}'>${path##*/}</a>"
|
printf '%s' "<a href='${path#*/}'>${path##*/}</a>"
|
||||||
# рекурсивные вызовы для подкаталогов
|
# рекурсивные вызовы для подкаталогов
|
||||||
if [ -d "$path" ]; then
|
if [ -d "$path" ]; then
|
||||||
local list # массив файлов и каталогов
|
|
||||||
readarray -t list < <(list_directory_contents "$path")
|
|
||||||
local size=${#list[@]} # длина массива
|
|
||||||
local i # счётчик
|
local i # счётчик
|
||||||
for ((i = 0; i < size; i++)); do
|
for ((i = 0; i < size; i++)); do
|
||||||
if [ -z "${list[$i]}" ]; then
|
if ((size == 1)); then
|
||||||
continue # пропустить пустой каталог
|
|
||||||
elif ((size == 1)); then
|
|
||||||
directory_tree "$path/${list[$i]}" "$tail" "$tail" "one"
|
directory_tree "$path/${list[$i]}" "$tail" "$tail" "one"
|
||||||
elif ((i < size - 1)); then
|
elif ((i < size - 1)); then
|
||||||
directory_tree "$path/${list[$i]}" "$tail├─ " "$tail│ "
|
directory_tree "$path/${list[$i]}" "$tail├─ " "$tail│ "
|
||||||
|
|
|
@ -11,10 +11,10 @@ title_translated: Code with comments
|
||||||
{%- assign articles = "" | split: "" %}
|
{%- assign articles = "" | split: "" %}
|
||||||
{%- assign articles = articles | push: "Дерево каталогов со ссылками" %}
|
{%- assign articles = articles | push: "Дерево каталогов со ссылками" %}
|
||||||
{%- capture article_brief %}
|
{%- capture article_brief %}
|
||||||
Напишем скрипт Bash для построения дерева каталогов для репозитория в файле Markdown.
|
Напишем скрипт Bash для построения дерева каталогов для репозитория в файле Markdown. Будем
|
||||||
Будем использовать только средства Bash и базовое ПО Linux — `ls`, `sed`, `tr` и `echo`
|
использовать только встроенные средства Bash и базовое ПО Linux — `ls`, `sed`, `tr` и `printf`
|
||||||
— без дополнительных программ. Полученный файл `DIRECTORY_TREE.md` будем использовать
|
— без дополнительных программ. Полученный файл `DIRECTORY_TREE.md` пригодится в вёб-интерфейсе
|
||||||
в вёб-интерфейсе для навигации по объектам репозитория.
|
для навигации по объектам репозитория.
|
||||||
|
|
||||||
Создаём рекурсивную функцию и с её помощью обходим все файлы и каталоги репозитория, за
|
Создаём рекурсивную функцию и с её помощью обходим все файлы и каталоги репозитория, за
|
||||||
исключением списка из `.gitignore` — строим структуру каталогов в форме дерева. Выводим
|
исключением списка из `.gitignore` — строим структуру каталогов в форме дерева. Выводим
|
||||||
|
|
Loading…
Add table
Reference in a new issue