How to find out the size of a file?

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
ITSMERSH

How to find out the size of a file?

#1 Post by ITSMERSH »

Hi.

Title and description says it all.

Any hints?

Thanks

dancytron
Posts: 1519
Joined: Wed 18 Jul 2012, 19:20

#2 Post by dancytron »

Fred has a script in Debian Dog that does it as a right click action. Should be in there someplace. it is /usr/local/bin/howbig

Code: Select all

#!/bin/bash

FILES="$@"
printf '%s\n' "$@" > /tmp/howbig

BASENAMES=$(while read f; do echo "$(basename "$f")"; done <<< "$(cat /tmp/howbig)")

DIRNAME="$(dirname "$(cat /tmp/howbig | head -1)")"

IFS=$'\n'

cd "$DIRNAME"
du -chs --apparent-size $BASENAMES | sort -h  | yad --title="How Big?" --width=400 --height=400 --text-info --tail --center
rm -f /tmp/howbig

exit 0

ITSMERSH

#3 Post by ITSMERSH »

Thanks.

I'm using:

Code: Select all

FSIZE1=`du -c -h --apparent-size "$PRJ"`
FSIZE=`echo $FSIZE1 | cut -d " " -f1`
to get the size of a single file, instead of all files in a directory.

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#4 Post by MochiMoppel »

ITSMERSH wrote:Thanks.

I'm using:

Code: Select all

FSIZE1=`du -c -h --apparent-size "$PRJ"`
FSIZE=`echo $FSIZE1 | cut -d " " -f1`
to get the size of a single file, instead of all files in a directory.
Try

Code: Select all

FSIZE=$(du -h --apparent-size "$PRJ" | cut -f1)
Note that du separates fields by Tab, not by Space. Your code works only because you echo an unquoted $FSIZE1. In this case echo changes Tabs to Spaces.

Slightly faster:

Code: Select all

FSIZE=$(stat -c %s  "$PRJ")

jafadmin
Posts: 1249
Joined: Thu 19 Mar 2009, 15:10

#5 Post by jafadmin »

Code: Select all

ls -al file.name | cut -d " " -f 5

ITSMERSH

#6 Post by ITSMERSH »

@MochiMoppel, jafadmin,

thanks for the replies.
MochiMoppel wrote:
ITSMERSH wrote:Thanks.

I'm using:

Code: Select all

FSIZE1=`du -c -h --apparent-size "$PRJ"`
FSIZE=`echo $FSIZE1 | cut -d " " -f1`
to get the size of a single file, instead of all files in a directory.
Try

Code: Select all

FSIZE=$(du -h --apparent-size "$PRJ" | cut -f1)
Note that du separates fields by Tab, not by Space. Your code works only because you echo an unquoted $FSIZE1. In this case echo changes Tabs to Spaces.

Slightly faster:

Code: Select all

FSIZE=$(stat -c %s  "$PRJ")
I was trying and trying and trying

Code: Select all

FSIZE="`du -c -h --apparent-size "$PRJ" | cut -d " " -f1`"
and wondered why it didn't cut everything after the first space.

Didn't know it is separated by tabs... :roll:

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#7 Post by musher0 »

Hi.

With the help of awk:

Size at beginning followed by file name:

Code: Select all

ls -Algo | awk '{ print $3"\t"$NF }'
or
size at end, file name at beginning:

Code: Select all

ls -Algo | awk '{ print $NF"\t"$3 }'
Bye.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#8 Post by musher0 »

Hello *RSH and all.

You can see file sizes directly in a ROX-Filer window.
Simply change the ROX-Filer's Options, like so:
  • -- Open a ROX-Filer window (it does not matter which directory)

    -- Right-click on en empty part of background. Then click on Options in
    the middle of that menu: ROX's Options panel opens.

    -- Click "Display", 2nd from top.

    -- Go to 2nd sub-title there, named "Icons".

    -- Now in the left colum, 2nd line, where is says "Details by Default",
    there is a list menu: click on "Sizes" there, and then "Save". The next
    time that you open ROX-Filer, it will display the size in bytes (if the file
    is small) or Kb's of files next to the name of the files. Only of files, not
    of directories, though.
Please see illustrations.

Other file managers have similar settings that you can set or unset:
mc, wcm, muCommander, xfe, etc.

IHTH.
Attachments
Result-sizes_setting.jpg
(97.23 KiB) Downloaded 194 times
ROX-Filer-sizes_setting.jpg
(106.47 KiB) Downloaded 192 times
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

ITSMERSH

#9 Post by ITSMERSH »

You can see file sizes directly in a ROX-Filer window.
Yes, I know this. Thanks.

Though as mentioned in my opening post I wanted to do this in a shell script, as I need that information to display the file size along with file name and date of last edition of the file in the status bar or in a disabled entry field of a gtkdialog program I'm working on.

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#10 Post by seaside »

ITSMERSH,

I think "stat" will give you everything you need.

Code: Select all

FN="/root/.bashrc"
bytes_and_last_change="$(stat --printf="%s ""%z" "$FN" | cut -d " " -f1,2)"
echo "$FN ""$bytes_and_last_change" 
  /root/.bashrc 1245 2017-02-10
Cheers,
s

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#11 Post by musher0 »

Thanks seaside.

I didn't know one could do that! :)

Just having fun:

Code: Select all

Info="`stat -c "%n, %s b, %z" .bashrc`";echo "${Info:0:33}"
More general:

Code: Select all

Info="`stat -c "%n, %s b, %z" filename`";echo "${Info:0: -16}"
BFN.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

Post Reply