Рекурсивно посчитать количество файлов


Vaulter аватар

Vaulter - Posted on 23 Октябрь 2010

http://otvety.google.ru/otvety/thread?tid=3aa2c9b7d57b61c5

Там тема закрыта, а жаль.

Цель: получить строки вида

$0 --max-depth=3
some/folder1/some: 123
some/folder1/other: 1234
some/folder2: 12
out: 0

Пока родилось только

for d in `ls -1`; do echo -n $d': '; ls -R $d | grep -c ''; done| sort -nk2

Что-то подсказывает, что полное решение лепится из find и awk?

#!/bin/sh
#count files recursive
curdir="${1:-.}"
NL='
'
___________________________________________________________START_SCRIPT_PLACEHOLDER_TO_LOOK_IN_DEBUG=
 
getdirs() {
  find "$1" -maxdepth 1 -type d ! -wholename "$1"
}
 
getfiles() {
  find "$1" -maxdepth 1 -type f
}
 
# Global variable to hold oversize return value of function.
LargeReturnValue=
 
processdir() {
  let "deep += 1" >/dev/null
  local dir="$1"
  local subdirs="$(getdirs "$1")"
  local countfiles="$(getfiles "$1" | wc -l)"
  local statdirs=""
  local subdir
 
  if [ -n "$subdirs" ]; then
 
    for subdir in $subdirs; do
 
      if [ -n "$subdir" -a x"$subdir" != x"$1" ]; then
        statdirs="$statdirs$NL"$(processdir "$subdir")
        let "countfiles += `echo $statdirs | tail -n 1 | awk '{ print $1 }'`" >/dev/null
      fi
    done
  fi
 
  [ -n "$statdirs" ] && (echo "$statdirs" | sort -n >&2)
  printf "%`expr $deep \* 4`s %5d %s" "" $countfiles "$dir"
 
  let "deep -= 1" >/dev/null
  # Unfortunatly, we cannot return value more than 255
  return
}
deep=0
processdir "$curdir"
echo

На память :)

$ sh ~/cf.sh sites/all/themes
                 4 sites/all/themes/dark/i
                 2 sites/all/themes/internet_center/i
                             0 sites/all/themes/vaultsoft_ru/j/.svn/tmp/prop-base
                             0 sites/all/themes/vaultsoft_ru/j/.svn/tmp/props
                             0 sites/all/themes/vaultsoft_ru/j/.svn/tmp/text-base
                         0 sites/all/themes/vaultsoft_ru/j/.svn/prop-base
                         0 sites/all/themes/vaultsoft_ru/j/.svn/props
                         0 sites/all/themes/vaultsoft_ru/j/.svn/text-base
                         0 sites/all/themes/vaultsoft_ru/j/.svn/tmp
                     2 sites/all/themes/vaultsoft_ru/j/.svn
                             0 sites/all/themes/vaultsoft_ru/i/.svn/tmp/prop-base
                             0 sites/all/themes/vaultsoft_ru/i/.svn/tmp/props
                             0 sites/all/themes/vaultsoft_ru/i/.svn/tmp/text-base
                         0 sites/all/themes/vaultsoft_ru/i/.svn/prop-base
                         0 sites/all/themes/vaultsoft_ru/i/.svn/props
                         0 sites/all/themes/vaultsoft_ru/i/.svn/text-base
                         0 sites/all/themes/vaultsoft_ru/i/.svn/tmp
                     3 sites/all/themes/vaultsoft_ru/i/.svn
                             0 sites/all/themes/vaultsoft_ru/m/.svn/tmp/prop-base
                             0 sites/all/themes/vaultsoft_ru/m/.svn/tmp/props
                             0 sites/all/themes/vaultsoft_ru/m/.svn/tmp/text-base
                         0 sites/all/themes/vaultsoft_ru/m/.svn/prop-base
                         0 sites/all/themes/vaultsoft_ru/m/.svn/props
                         0 sites/all/themes/vaultsoft_ru/m/.svn/text-base
                         0 sites/all/themes/vaultsoft_ru/m/.svn/tmp
                     3 sites/all/themes/vaultsoft_ru/m/.svn
                 4 sites/all/themes/vaultsoft_ru/m
                 7 sites/all/themes/vaultsoft_ru/f
                 8 sites/all/themes/vaultsoft_ru/i
                11 sites/all/themes/vaultsoft_ru/j
                13 sites/all/themes/vaultsoft_ru/c
                     1 sites/all/themes/zen/images-source/STARTERKIT
                     2 sites/all/themes/zen/images-source/zen
                    13 sites/all/themes/zen/zen/images
                         1 sites/all/themes/zen/tma/images/featured
                         1 sites/all/themes/zen/tma/images/latest
                         1 sites/all/themes/zen/tma/images/thumbs
                    49 sites/all/themes/zen/tma/images
                 2 sites/all/themes/zen/translations
                 3 sites/all/themes/zen/images-source
                 7 sites/all/themes/zen/STARTERKIT
                42 sites/all/themes/zen/zen
                64 sites/all/themes/zen/tma
                         4 sites/all/themes/rootcandy/icons/i16/status
                        11 sites/all/themes/rootcandy/icons/i16/misc
                        39 sites/all/themes/rootcandy/icons/i16/admin
                         4 sites/all/themes/rootcandy/icons/i24/status
                        11 sites/all/themes/rootcandy/icons/i24/misc
                        41 sites/all/themes/rootcandy/icons/i24/admin
                         1 sites/all/themes/rootcandy/icons/i32/misc
                        56 sites/all/themes/rootcandy/icons/i32/admin
                    54 sites/all/themes/rootcandy/icons/i16
                    56 sites/all/themes/rootcandy/icons/i24
                    57 sites/all/themes/rootcandy/icons/i32
                     4 sites/all/themes/rootcandy/images/buttons
                     5 sites/all/themes/rootcandy/images/panel
                     8 sites/all/themes/rootcandy/images/window
                    14 sites/all/themes/rootcandy/images/tabs
                         3 sites/all/themes/rootcandy/rootcandy_dark/images/buttons
                         4 sites/all/themes/rootcandy/rootcandy_dark/images/panel
                         8 sites/all/themes/rootcandy/rootcandy_dark/images/window
                        16 sites/all/themes/rootcandy/rootcandy_dark/images/tabs
                    37 sites/all/themes/rootcandy/rootcandy_dark/images
                     1 sites/all/themes/rootcandy/rootcandy_fixed/images
                 1 sites/all/themes/rootcandy/slider
                 4 sites/all/themes/rootcandy/color
                 6 sites/all/themes/rootcandy/rootcandy_fixed
                13 sites/all/themes/rootcandy/translations
                41 sites/all/themes/rootcandy/images
                42 sites/all/themes/rootcandy/rootcandy_dark
               167 sites/all/themes/rootcandy/icons
                     5 sites/all/themes/acquia_slate/images/banner
                 2 sites/all/themes/acquia_slate/js
                 4 sites/all/themes/acquia_slate/css
                22 sites/all/themes/acquia_slate/images
                        12 sites/all/themes/fusion/fusion_core/images/skinrpreviews
                     9 sites/all/themes/fusion/fusion_core/js
                    18 sites/all/themes/fusion/fusion_core/images
                    20 sites/all/themes/fusion/fusion_core/css
                     1 sites/all/themes/fusion/fusion_starter_lite/css
                     4 sites/all/themes/fusion/fusion_starter/css
                 7 sites/all/themes/fusion/fusion_starter_lite
                 9 sites/all/themes/fusion/fusion_starter
                63 sites/all/themes/fusion/fusion_core
                     1 sites/all/themes/rich/j/l
                 2 sites/all/themes/rich/j
                 4 sites/all/themes/rich/c
             9 sites/all/themes/dark
             9 sites/all/themes/rich
            19 sites/all/themes/internet_center
            38 sites/all/themes/acquia_slate
            60 sites/all/themes/vaultsoft_ru
            81 sites/all/themes/fusion
           121 sites/all/themes/zen
           291 sites/all/themes/rootcandy
       628 sites/all/themes

1. find + sort + uniq

find . -maxdepth 3 -type f -printf '%h\n' | sort | uniq -c

2. find + awk

find . -maxdepth 3 -type f -printf '%h\n' | awk '{ h[$0]++ } END{ for (k in h) print h[k], k }'

3. find + perl
такая же идея использовать хеши как и в awk

find . -maxdepth 3 -type f -printf '%h\n' | perl -ne '$h{$_}++; END{ print map { "$h{$_} $_"} keys %h }'

Спасибо за "uniq -c". Наиболее близко к ожидаемому:

find . -maxdepth 3 -type f -printf '%h\n' | sort | uniq -c | sort -n

Однако не идеал, конечно, который недостижим :)

все достижимо Ж:-)
а так, ближе?:

find . -maxdepth 3 -type f -printf '%h\n' | sort | uniq -c | sort -n | sed -e 's/^[^0-9]*\([0-9 ]*\)\(.*\)/\2: \1/'

К сожалению, во FreeBSD нет -printf у find, поэтому родилось такой уродец

find . -maxdepth 3 -type f -exec expr \{\} : '\(.*\)/.*$' \; | sort | uniq -c | sort -n

ramok, последний sed-перестановка ИМХО не нужна - цифирь впереди это нагляднее.

Если я правильно понял (по линку не ходил)...

Структура:..

$ tree -L 3 {1/,2}
1/
├── 1.1/
│   └── test.file
└── 1.2/
    └── test.file
2
└── test.file

Результат:..

$ find {1,2} -maxdepth 3 -type f | sed -e 's/\/\([^\/]\+\)$/:\ \1/g'
1/1.2: test.file
1/1.1: test.file
2: test.file

Отправить комментарий

Google Friend Connect (leave a quick comment)
loading...
Содержание этого поля является приватным и не предназначено к показу.