George S.
George S.

Reputation: 49

Bash code to count size of files without folders

count size of files in a specific directory but without the folders inside it

so if i have folder with

xxxxx.txt = 8kb
yyyy.exe = 29kb
game folder = 450kb
the answer will be 
37 kb 

when i try to do du -sh it gives me the total size of file so thats not good

also can i control if output is in 'byte/mb' etc?

another quick question: i want to calculate the number of folders and save it to a file and then once i run the script it will show how many folders were added or removed from last time

now my idea is to use the ls and 'wc' to count number of folders and use tee command to output it to a file. but i'm having problem connecting everything together..

thanks!

Upvotes: 3

Views: 1453

Answers (3)

thanasisp
thanasisp

Reputation: 5975

1. Using ls

Here is a solution with ls

ls -l | awk '!/^d/ {sum+=$5} END {print sum}'

Result is in bytes. Pattern matches lines of all types except directories. For example to include only regular files we can use /^-/

from man ls (1p):

   d       Directory.
   b       Block special file.
   c       Character special file.
   l (ell) Symbolic link.
   p       FIFO.
   −       Regular file.


2. Using du

you can get the total estimated disk usage (not always "size") of files excluding dirs with du -Ssh

-S, --separate-dirs
for directories do not include size of subdirectories

with -h, this will calculate minimum one block (4K) for each file, because this is usually the minimum disk usage for the file system. --apparent-size can be used to print estimated actual sizes instead.

--apparent-size
print apparent sizes, rather than disk usage; although the apparent size is usually smaller, it may be larger due to holes in ('sparse') files, internal fragmentation, indirect blocks, and the like

So du -Ssh --apparent-size (in human readable format) or du -Ssb in bytes, are expected to give same results.


In both cases, I notice that when using -S, there is an additional +4K (+4096) calculated for . the current directory. -b sets the counting block size to 1 byte, so I guess the file size estimation becomes accurate. So here is a way to extract the size (in bytes) of files in a directory, excluding subdirectories

du -Ssb | awk '{print $1-4096}'

note that only directories are excluded, so any other types like for example symbolic links will be included.

Upvotes: 1

janos
janos

Reputation: 124646

A fairly easy way:

  • Find only the files, only in the current directory, using find
  • Pass the file list to du -k with xargs
  • The -k flag of du produces result in kilobytes
  • Use awk to compute and print the sum

Like this:

find . -type f -maxdepth 1 -print0 | xargs -0 du -k | awk '{ sum += $1 } END { print sum }'

Upvotes: 1

Indent
Indent

Reputation: 4967

try this :

ls -l        |  # list file 
grep -v '^d' |  # skip directory
awk '{size+=$5} END {print size size/1024 size/(1024*1024)}' # sum size in byte and kByte, etc..

and

ls -l     |  # list file 
grep '^d' |  # select directory
wc -l        # count

both :

ls -l        |  # list file 
awk 'BEGIN{nbdir=0} /-/{sizefile+=$5} /^d/{nbdir+=1}END {print sizefile" bytes "nbdir" folders"}' 

awk : /pattern/{bloc}

Upvotes: 1

Related Questions