Programing

디렉토리에있는 모든 파일 내용의 총 크기

lottogame 2020. 8. 21. 08:28
반응형

디렉토리에있는 모든 파일 내용의 총 크기


ls또는을 사용하면 du각 파일이 차지하는 디스크 공간의 양을 알 수 있습니다.

각 파일을 열고 바이트를 계산하면 얻을 수있는 파일 및 하위 디렉터리의 모든 데이터의 합계가 필요합니다. 각 파일을 열고 세지 않고도 얻을 수 있다면 보너스 포인트.


디스크의 파일이 차지하는 크기가 아닌 '명확한 크기'(즉, 각 파일의 바이트 수)를 원하면 -b또는 --bytes옵션을 사용하십시오 (GNU coreutils 가있는 Linux 시스템을 사용하는 경우 ).

% du -sbh <directory>

사용 du -sb:

du -sb DIR

선택적 h으로보다 사용자 친화적 인 출력을위한 옵션을 추가합니다 .

du -sbh DIR

디렉토리로 이동 한 다음 :

du -sh

ftw!

원래 여기에 썼습니다 : https://ao.gl/get-the-total-size-of-all-the-files-in-a-directory/


대안 :

ls -lAR | grep -v '^d' | awk '{total += $5} END {print "Total:", total}'

grep -v '^d' 디렉토리를 제외합니다.


통계의 "% s"형식은 파일의 실제 바이트 수를 제공합니다.

 find . -type f |
 xargs stat --format=%s |
 awk '{s+=$1} END {print s}'

숫자를 합산하는 대신 선호하는 방법으로 자유롭게 대체하십시오 .


emebedded 시스템에서 busybox의 "du"를 사용하면 du로 정확한 바이트를 얻을 수 없으며 얻을 수있는 KB 만 얻을 수 있습니다.

BusyBox v1.4.1 (2007-11-30 20:37:49 EST) multi-call binary

Usage: du [-aHLdclsxhmk] [FILE]...

Summarize disk space used for each FILE and/or directory.
Disk space is printed in units of 1024 bytes.

Options:
        -a      Show sizes of files in addition to directories
        -H      Follow symbolic links that are FILE command line args
        -L      Follow all symbolic links encountered
        -d N    Limit output to directories (and files with -a) of depth < N
        -c      Output a grand total
        -l      Count sizes many times if hard linked
        -s      Display only a total for each argument
        -x      Skip directories on different filesystems
        -h      Print sizes in human readable format (e.g., 1K 243M 2G )
        -m      Print sizes in megabytes
        -k      Print sizes in kilobytes(default)

폴더가 생성 될 때 많은 Linux 파일 시스템은 디렉토리 자체에 대한 일부 메타 데이터를 저장하기 위해 4096 바이트를 할당합니다. 이 공간은 디렉토리가 커짐에 따라 4096 바이트의 배수만큼 증가합니다.

du 명령 (-b 옵션 포함 또는 제외) 은 다음 과 같이 입력하는 것처럼 이 공간을 계산합니다 .

mkdir test && du -b test

빈 디렉토리에 대한 결과는 4096 바이트입니다. 따라서 dir 안에 10000 바이트의 파일 2 개를 넣으면 du -sb제공하는 총량 은 24096 바이트가됩니다.

질문을주의 깊게 읽으면 이것은 질문 한 것이 아닙니다. 질문자는 다음과 같이 물었습니다.

각 파일을 열고 바이트를 계산하면 얻을 수있는 파일 및 하위 디렉터리의 모든 데이터의 합계

위의 예에서 24096이 아니라 20000 바이트 여야합니다.

따라서 정답 IMHO는 공백이 포함 된 파일 이름을 처리하기위한 Nelson 답변과 hlovdal 제안을 혼합 한 것일 수 있습니다 .

find . -type f -print0 | xargs -0 stat --format=%s | awk '{s+=$1} END {print s}'

There are at least three ways to get the "sum total of all the data in files and subdirectories" in bytes that work in both Linux/Unix and Git Bash for Windows, listed below in order from fastest to slowest on average. For your reference, they were executed at the root of a fairly deep file system (docroot in a Magento 2 Enterprise installation comprising 71,158 files in 30,027 directories).

1.

$ time find -type f -printf '%s\n' | awk '{ total += $1 }; END { print total" bytes" }'
748660546 bytes

real    0m0.221s
user    0m0.068s
sys     0m0.160s

2.

$ time echo `find -type f -print0 | xargs -0 stat --format=%s | awk '{total+=$1} END {print total}'` bytes
748660546 bytes

real    0m0.256s
user    0m0.164s
sys     0m0.196s

3.

$ time echo `find -type f -exec du -bc {} + | grep -P "\ttotal$" | cut -f1 | awk '{ total += $1 }; END { print total }'` bytes
748660546 bytes

real    0m0.553s
user    0m0.308s
sys     0m0.416s


These two also work, but they rely on commands that don't exist on Git Bash for Windows:

1.

$ time echo `find -type f -printf "%s + " | dc -e0 -f- -ep` bytes
748660546 bytes

real    0m0.233s
user    0m0.116s
sys     0m0.176s

2.

$ time echo `find -type f -printf '%s\n' | paste -sd+ | bc` bytes
748660546 bytes

real    0m0.242s
user    0m0.104s
sys     0m0.152s


If you only want the total for the current directory, then add -maxdepth 1 to find.


Note that some of the suggested solutions don't return accurate results, so I would stick with the solutions above instead.

$ du -sbh
832M    .

$ ls -lR | grep -v '^d' | awk '{total += $5} END {print "Total:", total}'
Total: 583772525

$ find . -type f | xargs stat --format=%s | awk '{s+=$1} END {print s}'
xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option
4390471

$ ls -l| grep -v '^d'| awk '{total = total + $5} END {print "Total" , total}'
Total 968133

For Win32 DOS, you can:

c:> dir /s c:\directory\you\want

and the penultimate line will tell you how many bytes the files take up.

I know this reads all files and directories, but works faster in some situations.


du is handy, but find is useful in case if you want to calculate the size of some files only (for example, using filter by extension). Also note that find themselves can print the size of each file in bytes. To calculate a total size we can connect dc command in the following manner:

find . -type f -printf "%s + " | dc -e0 -f- -ep

Here find generates sequence of commands for dc like 123 + 456 + 11 +. Although, the completed program should be like 0 123 + 456 + 11 + p (remember postfix notation).

So, to get the completed program we need to put 0 on the stack before executing the sequence from stdin, and print the top number after executing (the p command at the end). We achieve it via dc options:

  1. -e0 is just shortcut for -e '0' that puts 0 on the stack,
  2. -f- is for read and execute commands from stdin (that generated by find here),
  3. -ep is for print the result (-e 'p').

To print the size in MiB like 284.06 MiB we can use -e '2 k 1024 / 1024 / n [ MiB] p' in point 3 instead (most spaces are optional).


This may help:

ls -l| grep -v '^d'| awk '{total = total + $5} END {print "Total" , total}'

The above command will sum total all the files leaving the directories size.


Use:

$ du -ckx <DIR> | grep total | awk '{print $1}'

Where <DIR> is the directory you want to inspect.

The '-c' gives you grand total data which is extracted using the 'grep total' portion of the command, and the count in Kbytes is extracted with the awk command.

The only caveat here is if you have a subdirectory containing the text "total" it will get spit out as well.

참고URL : https://stackoverflow.com/questions/1241801/total-size-of-the-contents-of-all-the-files-in-a-directory

반응형