특정 디렉토리의 디렉토리 수 계산
특정 디렉터리의 폴더 수를 계산하는 방법. 다음 명령을 사용하고 있지만 항상 추가 명령을 제공합니다.
find /directory/ -maxdepth 1 -type d -print| wc -l
예를 들어, 3 개의 폴더가있는 경우이 명령은 4를 제공합니다. 5 개의 폴더가 포함 된 경우 명령은 6을 제공합니다. 그 이유는 무엇입니까?
find
디렉토리 자체도 인쇄합니다.
$ find .vim/ -maxdepth 1 -type d
.vim/
.vim/indent
.vim/colors
.vim/doc
.vim/after
.vim/autoload
.vim/compiler
.vim/plugin
.vim/syntax
.vim/ftplugin
.vim/bundle
.vim/ftdetect
대신 디렉토리의 하위 항목을 테스트하고 하위 항목으로 내려 가지 않도록 할 수 있습니다.
$ find .vim/* -maxdepth 0 -type d
.vim/after
.vim/autoload
.vim/bundle
.vim/colors
.vim/compiler
.vim/doc
.vim/ftdetect
.vim/ftplugin
.vim/indent
.vim/plugin
.vim/syntax
$ find .vim/* -maxdepth 0 -type d | wc -l
11
$ find .vim/ -maxdepth 1 -type d | wc -l
12
다음을 사용할 수도 있습니다 ls
.
$ ls -l .vim | grep -c ^d
11
$ ls -l .vim
total 52
drwxrwxr-x 3 anossovp anossovp 4096 Aug 29 2012 after
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 autoload
drwxrwxr-x 13 anossovp anossovp 4096 Aug 29 2012 bundle
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 colors
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 compiler
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 doc
-rw-rw-r-- 1 anossovp anossovp 48 Aug 29 2012 filetype.vim
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 ftdetect
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 ftplugin
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 indent
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 plugin
-rw-rw-r-- 1 anossovp anossovp 2505 Aug 29 2012 README.rst
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 syntax
$ ls -l .vim | grep ^d
drwxrwxr-x 3 anossovp anossovp 4096 Aug 29 2012 after
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 autoload
drwxrwxr-x 13 anossovp anossovp 4096 Aug 29 2012 bundle
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 colors
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 compiler
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 doc
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 ftdetect
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 ftplugin
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 indent
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 plugin
drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 syntax
현재 디렉터리의 디렉터리 수만 가져옵니다.
echo */ | wc
당신은 밖으로 나올 것입니다 1 309 4594
2nd digit
아니오를 나타냅니다. 디렉토리의.
또는
tree -L 1 | tail -1
find . -mindepth 1 -maxdepth 1 -type d | wc -l
For find -mindepth
는 디렉토리의 총 수를 의미합니다.
-maxdepth
디렉토리에 영향을 미치는 총 수를 의미합니다.
-type d
디렉토리를 의미
그리고 for wc -l
는 입력의 줄을 세는 것을 의미합니다.
드라이브로 이동하여 실행하는 가장 좋은 방법
ls -lR | grep ^d | wc -l
and to Find all folders in total, including subdirectories?
find /mount/point -type d | wc -l
...or find all folders in the root directory (not including subdirectories)?
find /mount/point -maxdepth 1 -type d | wc -l
Cheers!
Run stat -c %h folder
and subtract 2 from the result. This employs only a single subprocess as opposed to the 2 (or even 3) required by most of the other solutions here (typically find
plus wc
).
Using sh/bash:
cnt=$((`stat -c %h folder` - 2))
echo $cnt # 'echo' is a sh/bash builtin, not an additional process
Using csh/tcsh:
@ cnt = `stat -c %h folder` - 2
echo $cnt # 'echo' is a csh/tcsh builtin, not an additional process
Explanation: stat -c %h folder
prints the number of hardlinks to folder, and each subfolder under folder contains a ../ entry which is a hardlink back to folder. You must subtract 2 because there are two additional hardlinks in the count:
- folder's own self-referential ./ entry, and
- folder's parent's link to folder
I think the easiest is
ls -ld images/* | wc -l
where images
is your target directory. The -d flag limits to directories, and the -l flag will perform a per-line listing, compatible with the very familiar wc -l
for line count.
If you only have directories in the folder and no files this does it:
ls | wc -l
A pure bash solution:
shopt -s nullglob
dirs=( /path/to/directory/*/ )
echo "There are ${#dirs[@]} (non-hidden) directories"
If you also want to count the hidden directories:
shopt -s nullglob dotglob
dirs=( /path/to/directory/*/ )
echo "There are ${#dirs[@]} directories (including hidden ones)"
Note that this will also count links to directories. If you don't want that, it's a bit more difficult with this method.
Using find
:
find /path/to/directory -type d \! -name . -prune -exec printf x \; | wc -c
The trick is to output an x
to stdout each time a directory is found, and then use wc
to count the number of characters. This will count the number of all directories (including hidden ones), excluding links.
The methods presented here are all safe wrt to funny characters that can appear in file names (spaces, newlines, glob characters, etc.).
Some useful examples:
count files in current dir
/bin/ls -lA | egrep -c '^-'
count dirs in current dir
/bin/ls -lA | egrep -c '^d'
count files and dirs in current dir
/bin/ls -lA | egrep -c '^-|^d'
count files and dirs in in one subdirectory
/bin/ls -lA subdir_name/ | egrep -c '^-|^d'
I have noticed a strange thing (at least in my case) :
When I have tried with
ls
instead/bin/ls
the-A
parameterdo not list implied . and ..
NOT WORK as espected. When I usels
that show ./ and ../ So that result wrong count. SOLUTION :/bin/ls
insteadls
Using zsh
:
a=(*(/N)); echo ${#a}
The N
is a nullglob, /
makes it match directories, #
counts. It will neatly cope with spaces in directory names as well as returning 0
if there are no directories.
Best way to do it:
ls -la | grep -v total | wc -l
This gives you the perfect count.
Count all files and subfolders, windows style:
dir=/YOUR/PATH;f=$(find $dir -type f | wc -l); d=$(find $dir -mindepth 1 -type d | wc -l); echo "$f Files, $d Folders"
If you want to use regular expressions, then try:
ls -c | grep "^d" | wc -l
참고URL : https://stackoverflow.com/questions/17648033/counting-number-of-directories-in-a-specific-directory
'Programing' 카테고리의 다른 글
URL을 사용하여 마커가있는 Google지도에 연결 (0) | 2020.09.15 |
---|---|
지정된 원격 저장소에 원격 분기가 있는지 확인하는 방법은 무엇입니까? (0) | 2020.09.15 |
여러 글꼴 두께, 하나의 @ font-face 쿼리 (0) | 2020.09.15 |
Angular2-Http POST 요청 매개 변수 (0) | 2020.09.15 |
/var/lib/gems/2.3.0 디렉토리에 대한 쓰기 권한이 없습니다. (0) | 2020.09.15 |