0

I'm trying to find a way to combine the results that are displayed with the find command, e.g:

$find /home -name "requirements_*"

/home/cobayo/Obligatorio Test 2/requirements_richard
/home/cobayo/Obligatorio Test 2/requirements_paul
/home/cobayo/Obligatorio Test 2/requirements_george
/home/cobayo/Obligatorio Test 1/Subdirectorio/requirements_joe

So here I get all of the paths of the files I am looking for, what I would like to do is to get the file size listed prior the paths. For instance:

34K "/home/cobayo/Obligatorio Test 2/requirements_richard"

I have some commands, even a little script does show me file sizes.

For example, this one:

find /home -name "requirements_*" -mtime -1 -print0 | du --files0-from=- -hc |
    tail -n1*

And it shows the TOTAL size of all files with the name given as a parameter as results.

But then, I also have this little script:

FILENAME=$1                                   
FILESIZE=$(stat -c%s "$FILENAME")
echo "Size of $FILENAME = $FILESIZE bytes."

Which is perfect as I get the file in K of the specific file. I tried to mix them up using a list:

*for i in `find /home -name "requirements_*"`
do
echo `stat -c%s $i`
done*

But I don't get the results Im looking for, in fact it tells me the files don't exist :(

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Erudes
  • 15
  • 6

2 Answers2

0

You could use -exec du -hs {} \;:

$ find -type f -exec du -hs {} \;
4.0K    ./proj-2/README.md
4.0K    ./proj-2/file.c
4.0K    ./proj-2/file.h

or alternatively, if your find supports it:

find -type f -exec du -hs {} +

which calls du just once.

Or -printf with the %k formatting parameter for the size in kB:

$ find -type f -printf '%kK\t%p\n'
4K      ./proj-2/README.md
4K      ./proj-2/file.c
4K      ./proj-2/file.h

Manual references:

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
  • Thank you for your answer Benjamin! In my understanding, the du command will give you the size in **blocks** of your file but not the actual file size. – Allan Nov 17 '17 at 05:25
  • 1
    @Allan The `-h` option prints the normal units, I'd say. – Benjamin W. Nov 17 '17 at 06:20
  • the `-h` option is human readable, so indeed it will be in K,M,G however the size will be counted in blocks, for big files it does not matter a lot but for small files it does! :-) – Allan Nov 17 '17 at 06:50
  • @Allan Can you give an example of where `du -h` does not show the actual disk usage? A tiny file occupies 4 kB. My Ubuntu uses 512 byte blocks, yet `du -h` reports 4.0K for the file. Looks right to me, as in "kilobyte", and no blocks at all. My `du` (GNU Coreutils 8.25) defaults to block sizes of 1 kB if nothing else is specified, so I don't think the solution I'm proposing shows anything other than the actual disk usage of the files. – Benjamin W. Nov 17 '17 at 14:39
  • https://unix.stackexchange.com/questions/16640/how-can-i-get-the-size-of-a-file-in-a-bash-script/185039 More especially: *"du doesn't give the size of the file, it gives an indication of how much space the file uses, which is subtly different (usually the size reported by du is the size of the file rounded up to the nearest number of blocks, where a block is typically 512B or 1kB or 4kB)"* – Allan Nov 17 '17 at 15:05
  • 1
    @Allan That's why I wrote "disk usage". I assumed the OP wanted to use `du` because that's what he had in his attempt. `-printf` does the same, it displays disk space used. I agree that if you want the actual size, you need `stat` (like you did) or something like `wc -c`, which would be quite a bit slower than `du` or `printf '%k`, I imagine. – Benjamin W. Nov 17 '17 at 16:22
0

If you do not put the variable $i between double quotes the spaces will be interpreted as separator and your command will try to asses the following files independently :

/home/cobayo/Obligatorio 
Test 
1/Subdirectorio/requirements_joe
/home/cobayo/Obligatorio 
Test 
2/requirements_richard
/home/cobayo/Obligatorio 
Test 
2/requirements_paul
/home/cobayo/Obligatorio 
Test 
2/requirements_george

what gives you the error: files don't exist

You can try:

find . -name "requirements_*" -print0 | xargs -0 -I {} bash -c "echo -n 'Size of {} = '  && stat -c%s '{}' | tr -d '\n' && echo ' bytes.'"

enter image description here

Allan
  • 12,117
  • 3
  • 27
  • 51