How can I get the size of a file in a bash script?
up vote
195
down vote
favorite
How can I get the size of a file in a bash script?
How do I assign this to a bash variable so I can use it later?
bash shell files
migrated from stackoverflow.com Jul 14 '11 at 0:59
This question came from our site for professional and enthusiast programmers.
add a comment |
up vote
195
down vote
favorite
How can I get the size of a file in a bash script?
How do I assign this to a bash variable so I can use it later?
bash shell files
migrated from stackoverflow.com Jul 14 '11 at 0:59
This question came from our site for professional and enthusiast programmers.
stackoverflow.com/questions/5920333/how-to-check-size-of-a-file LOL for the migrate :-)
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Oct 1 '15 at 8:06
Pair this withpv
andcat
for a copy command that shows progress and ETA :)
– sudo
Sep 25 '17 at 17:38
add a comment |
up vote
195
down vote
favorite
up vote
195
down vote
favorite
How can I get the size of a file in a bash script?
How do I assign this to a bash variable so I can use it later?
bash shell files
How can I get the size of a file in a bash script?
How do I assign this to a bash variable so I can use it later?
bash shell files
bash shell files
edited Apr 5 '15 at 16:52
aknuds1
1034
1034
asked Jul 13 '11 at 16:22
haunted85
1,106288
1,106288
migrated from stackoverflow.com Jul 14 '11 at 0:59
This question came from our site for professional and enthusiast programmers.
migrated from stackoverflow.com Jul 14 '11 at 0:59
This question came from our site for professional and enthusiast programmers.
stackoverflow.com/questions/5920333/how-to-check-size-of-a-file LOL for the migrate :-)
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Oct 1 '15 at 8:06
Pair this withpv
andcat
for a copy command that shows progress and ETA :)
– sudo
Sep 25 '17 at 17:38
add a comment |
stackoverflow.com/questions/5920333/how-to-check-size-of-a-file LOL for the migrate :-)
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Oct 1 '15 at 8:06
Pair this withpv
andcat
for a copy command that shows progress and ETA :)
– sudo
Sep 25 '17 at 17:38
stackoverflow.com/questions/5920333/how-to-check-size-of-a-file LOL for the migrate :-)
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Oct 1 '15 at 8:06
stackoverflow.com/questions/5920333/how-to-check-size-of-a-file LOL for the migrate :-)
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Oct 1 '15 at 8:06
Pair this with
pv
and cat
for a copy command that shows progress and ETA :)– sudo
Sep 25 '17 at 17:38
Pair this with
pv
and cat
for a copy command that shows progress and ETA :)– sudo
Sep 25 '17 at 17:38
add a comment |
14 Answers
14
active
oldest
votes
up vote
186
down vote
accepted
Your best bet if on a GNU system:
stat --printf="%s" file.any
From man stat:
%s total size, in bytes
NOTE: see @chbrown answer below how to use stat in terminal on Mac OS X
or
#!/bin/bash
FILENAME=/home/heiko/dummy/packages.txt
FILESIZE=$(stat -c%s "$FILENAME")
echo "Size of $FILENAME = $FILESIZE bytes."
you know I'm just a newbie and I'm eager to learn... they say there's not such thing as dumb question; I honestly didn't know about stat. Thank you for your help. Appreciated.
– haunted85
Jul 13 '11 at 16:47
5
@haunted85stat
is the most straightforward way, assuming you're using Linux or Cygwin (stat
isn't standard).wc -c
as suggested by Eugéne is portable.
– Gilles
Jul 14 '11 at 10:02
2
stat: illegal option -- c
– Iulian Onofrei
Apr 27 '17 at 13:42
stat --printf="%s" file.txt
doesn't output anything on Debian Jessie...
– woohoo
Apr 29 '17 at 20:36
4
On MacOS this works:stat -f%z myfile.tar
– ccpizza
May 6 at 10:40
add a comment |
up vote
75
down vote
file_size_kb=`du -k "$filename" | cut -f1`
The problem with using stat
is that it is a GNU (Linux) extension. du -k
and cut -f1
are specified by POSIX and are therefore portable to any Unix system.
Solaris, for example, ships with bash but not with stat
. So this is not entirely hypothetical.
ls
has a similar problem in that the exact format of the output is not specified, so parsing its output cannot be done portably. du -h
is also a GNU extension.
Stick to portable constructs where possible, and you will make somebody's life easier in the future. Maybe your own.
41
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 bydu
is the size of the file rounded up to the nearest number of blocks, where a block is typically 512B or 1kB or 4kB).
– Gilles
Jul 14 '11 at 10:00
6
@Gilles, sparse files (i.e., ones with holes in them) report less than the length.
– vonbrand
Jan 9 '16 at 22:03
add a comment |
up vote
60
down vote
You could also use the "word count" command (wc
):
wc -c "$filename" | awk '{print $1}'
The problem with wc
is that it'll add the filename and indent the output. For example:
$ wc -c somefile.txt
1160 somefile.txt
If you would like to avoid chaining a full interpreted language or stream editor just to get a file size count, just redirect the input from the file so that wc
never sees the filename:
wc -c < "$filename"
This last form can be used with command substitution to easily grab the value you were seeking as a shell variable, as mentioned by Gilles below.
size="$(wc -c <"$filename")"
23
wc -c <"$FILENAME"
gives the size with no other cruft, thussize=$(wc -c <"$FILENAME")
.
– Gilles
Jul 14 '11 at 9:58
4
Just one more point: I just tested it andwc -c < file
seems to be very fast, at least on OS X. I'm guessing that wc has the brains to try to stat the file if only -c is specified.
– Edward Falk
Apr 4 '16 at 16:29
3
@EdwardFalk: GNUwc -c
usesfstat
, but then seeks to second-last block of the file and reads the last up-tost_blksize
bytes. Apparently this is because files in Linux's/proc
and/sys
for example have stat sizes that are only approximate, andwc
wants to report the actual size, not the stat-reported size. I guess it would be weird forwc -c
to report a different size thanwc
, but it's not idea to read data from the file if it's a normal disk file, and it's not in memory. Or worse, near-line tape storage...
– Peter Cordes
Apr 12 '17 at 5:28
1
It seems likeprintf
still sees the indentation, e.g.printf "Size: $size"
->size: <4 spaces> 54339
. On the other handecho
ignores the whitespace. Any way to make it consistent?
– Eugene Kulabuhov
May 2 '17 at 12:43
1
@EugeneKulabuhov yes… Useprintf "Size: %sn" ${size}
and it should generate the expected result...
– Eugéne
May 3 '17 at 14:45
|
show 6 more comments
up vote
37
down vote
BSD's (Mac OS X's) stat
has a different format argument flag, and different field specifiers. From man stat(1)
:
-f format
: Display information using the specified format. See the FORMATS section for a description of valid formats.- ... the FORMATS section ...
z
: The size of file in bytes.
So all together now:
stat -f%z myfile1.txt
add a comment |
up vote
20
down vote
This script combines many ways to calculate the file size:
(
du --apparent-size --block-size=1 "$file" 2>/dev/null ||
gdu --apparent-size --block-size=1 "$file" 2>/dev/null ||
find "$file" -printf "%s" 2>/dev/null ||
gfind "$file" -printf "%s" 2>/dev/null ||
stat --printf="%s" "$file" 2>/dev/null ||
stat -f%z "$file" 2>/dev/null ||
wc -c <"$file" 2>/dev/null
) | awk '{print $1}'
The script works on many Unix systems including Linux, BSD, OSX, Solaris, SunOS, etc.
The file size shows the number of bytes. It is the apparent size, which is the bytes the file uses on a typical disk, without special compression, or special sparse areas, or unallocated blocks, etc.
This script has a production version with more help and more options here:
https://github.com/SixArm/sixarm_unix_shell_scripts/blob/master/file-size
add a comment |
up vote
20
down vote
Depends what you mean by size.
size=$(wc -c < "$file")
will give you the number of bytes that can be read from the file. IOW, it's the size of the content of the file. It will however read the content of the file (except if the file is a regular file or symlink to regular file in most wc
implementations as an optimisation). That may have side effects. For instance, for a named pipe, what has been read can no longer be read again and for things like /dev/zero
or /dev/random
which are of infinite size, it's going to take a while. That also means you need read
permission to the file.
That's standard and portable, however note that some wc
implementations may include leading blanks in that output. One way to get rid of them is to use:
size=$(($(wc -c < "$file")))
or to avoid an error about an empty arithmetic expression in dash
or yash
when wc
produces no output (like when the file can't be opened):
size=$(($(wc -c < "$file") +0))
ksh93
has wc
builtin (provided you enable it, you can also invoke it as command /opt/ast/bin/wc
) which makes it the most efficient for regular files in that shell.
Various systems have a command called stat
that's an interface to the stat()
or lstat()
system calls.
Those report information found in the inode. One of that information is the st_size
attribute. For regular files, that's the size of the content (how much data could be read from it in the absence of error (that's what most wc -c
implementations use in their optimisation)). For symlinks, that's the size in bytes of the target path. For named pipes, depending on the system, it's either 0 or the number of bytes currently in the pipe buffer. Same for block devices where depending on the system, you get 0 or the size in bytes of the underlying storage.
You don't need read permission to the file to get that information, only search permission to the directory it is linked to.
By chronological order, there is:
IRIX
stat
(90's):
stat -qLs -- "$file"
returns the
st_size
attribute of$file
(lstat()
) or:
stat -s -- "$file"
same except when
$file
is a symlink in which case it's thest_size
of the file after symlink resolution.
zsh
stat
builtin (now also known aszstat
) in thezsh/stat
module (loaded withzmodload zsh/stat
) (1997):
stat -L +size -- $file # st_size of file
stat +size -- $file # after symlink resolution
or to store in a variable:
stat -L -A size +size -- $file
obviously, that's the most efficient in that shell.
GNU
stat
(2001); also in BusyBoxstat
since 2005 (copied from GNUstat
):
stat -c %s -- "$file" # st_size of file
stat -Lc %s -- "$file" # after symlink resolution
(note the meaning of
-L
is reversed compared to IRIX orzsh
stat
.
BSDs
stat
(2002):
stat -f %z -- "$file" # st_size of file
stat -Lf %z -- "$file" # after symlink resolution
Or you can use the stat()
/lstat()
function of some scripting language like perl
:
perl -le 'print((lstat shift)[7])' -- "$file"
AIX also has an istat
command which will dump all the stat()
(not lstat()
, so won't work on symlinks) information and which you could post-process with, for example:
LC_ALL=C istat "$file" | awk 'NR == 4 {print $5}'
(thanks @JeffSchaller for the help figuring out the details).
In tcsh
:
@ size = -Z $file:q
(size after symlink resolution)
Long before GNU introduced its stat
command, the same could be achieved with GNU find
command with its -printf
predicate (already in 1991):
find -- "$file" -prune -printf '%sn' # st_size of file
find -L -- "$file" -prune -printf '%sn' # after symlink resolution
One issue though is that doesn't work if $file
starts with -
or is a find
predicate (like !
, (
...).
The standard command to get the stat()
/lstat()
information is ls
.
POSIXly, you can do:
LC_ALL=C ls -dn -- "$file" | awk '{print $5; exit}'
and add -L
for the same after symlink resolution. That doesn't work for device files though where the 5th field is the device major number instead of the size.
For block devices, systems where stat()
returns 0 for st_size
, usually have other APIs to report the size of the block device. For instance, Linux has the BLKGETSIZE64
ioctl()
, and most Linux distributions now ship with a blockdev
command that can make use of it:
blockdev --getsize64 -- "$device_file"
However, you need read permission to the device file for that. It's usually possible to derive the size by other means. For instance (still on Linux):
lsblk -bdno size -- "$device_file"
Should work except for empty devices.
An approach that works for all seekable files (so includes regular files, most block devices and some character devices) is to open the file and seek to the end:
With
zsh
(after loading thezsh/system
module):
{sysseek -w end 0 && size=$((systell(0)))} < $file
With
ksh93
:
< "$file" <#((size=EOF))
or
{ size=$(<#((EOF))); } < "$file"
with
perl
:
perl -le 'seek STDIN, 0, 2 or die "seek: $!"; print tell STDIN' < "$file"
For named pipes, we've seen that some systems (AIX, Solaris, HP/UX at least) make the amount of data in the pipe buffer available in stat()
's st_size
. Some (like Linux or FreeBSD) don't.
On Linux at least, you can use the FIONREAD
ioctl()
after having open the pipe (in read+write mode to avoid it hanging):
fuser -s -- "$fifo_file" &&
perl -le 'require "sys/ioctl.ph";
ioctl(STDIN, &FIONREAD, $n) or die$!;
print unpack "L", $n' <> "$fifo_file"
However note that while it doesn't read the content of the pipe, the mere opening of the named pipe here can still have side effects. We're using fuser
to check first that some process already has the pipe open to alleviate that but that's not foolproof as fuser
may not be able to check all processes.
Now, so far we've only been considering the size of the primary data associated with the files. That doesn't take into account the size of the metadata and all the supporting infrastructure needed to store that file.
Another inode attribute returned by stat()
is st_blocks
. That's the number of 512 byte blocks that is used to store the file's data (and sometimes some of its metadata like the extended attributes on ext4 filesystems on Linux). That doesn't include the inode itself, or the entries in the directories the file is linked to.
Size and disk usage are not necessarily tightly related as compression, sparseness (sometimes some metadata), extra infrastructure like indirect blocks in some filesystems have an influence on the latter.
That's typically what du
uses to report disk usage. Most of the commands listed above will be able to get you that information.
POSIXLY_CORRECT=1 ls -sd -- "$file" | awk '{print $1; exit}'
POSIXLY_CORRECT=1 du -s -- "$file"
(not for directories where that would include the disk usage of the files within).- GNU
find -- "$file" -printf '%bn'
zstat -L +block -- $file
- GNU
stat -c %b -- "$file"
- BSD
stat -f %b -- "$file"
perl -le 'print((lstat shift)[12])' -- "$file"
clearly the most comprehensive and informational answer. thank you. i can use this to create cross platform bash scripts using the BSD and GNU stats info
– oligofren
Jan 11 '17 at 12:50
1
Fun fact: GNU coreutilswc -c
usesfstat
, but then reads the last up-tost_blksize
bytes. Apparently this is because files in Linux's/proc
and/sys
for example have stat sizes that are only approximate. This is good for correctness, but bad if the end of the file is on disk and not in memory (esp. if used on many files in a loop). And very bad if the file is migrated to near-line tape storage, or e.g. a FUSE transparent-decompression filesystem.
– Peter Cordes
Apr 12 '17 at 5:48
wouldnt this work tools -go file | awk '{print $3}'
– Steven Penny
Feb 8 at 13:00
@StevenPenny those-go
would be the SysV ones, they wouldn't work on BSDs (optional (XSI) in POSIX). You'd also needls -god file | awk '{print $3; exit}'
(-d
for it to work on directories,exit
for symlinks with newlines in the target). The problems with device files also remain.
– Stéphane Chazelas
Feb 8 at 22:31
add a comment |
up vote
7
down vote
ls -l filename
will give you lots of information about a file, including its file size, permissions and owner.
The file size in the fifth column, and is displayed in bytes. In the example below, the filesize is just under 2KB:
-rw-r--r-- 1 user owner 1985 2011-07-12 16:48 index.php
Edit: This is apparently not as reliable as the stat
command.
I think bothls -l
andstat
command give reliable size information. I did not find any reference to the contrary.ls -s
will give size in number of blocks.
– dabest1
Dec 31 '12 at 22:23
1
@dabest1 it's not reliable in a sense that in another unix, their output can be different (and in some unixes it is).
– Eugene Bujak
Oct 2 '14 at 14:39
Yes, IIRC, Solaris didn't display the group name by default, leading to fewer columns in the output.
– Edward Falk
Apr 4 '16 at 16:31
Since the size is pure numeric, surrounded by whitespace, and the date year is pure numeric, in a defined format, it would be possible to use a regexp to treat user+owner as one field, whether or not the group was present. (an exercise for the reader !)
– MikeW
Feb 21 '17 at 15:31
add a comment |
up vote
6
down vote
stat appears to do this with the fewest system calls:
$ set debian-live-8.2.0-amd64-xfce-desktop.iso
$ strace stat --format %s $1 | wc
282 2795 27364
$ strace wc --bytes $1 | wc
307 3063 29091
$ strace du --bytes $1 | wc
437 4376 41955
$ strace find $1 -printf %s | wc
604 6061 64793
add a comment |
up vote
4
down vote
du filename
will tell you disk usage in bytes.
I prefer du -h filename
, which gives you the size in a human readable format.
2
that orstat -c "%s"
;)
– c00kiemon5ter
Jul 13 '11 at 16:29
This flavor ofdu
prints out size in blocks of 1024 bytes, not a simple count of bytes.
– Peter Lyons
Sep 17 '15 at 5:10
Note that standarddu
give an output in number of 512-byte units. GNUdu
uses kibibytes instead unless called withPOSIXLY_CORRECT
in its environment.
– Stéphane Chazelas
Nov 8 '16 at 15:01
For files of type directory, that gives the disk usage of the directory but also of all the other files within (recursively).
– Stéphane Chazelas
Nov 8 '16 at 15:02
add a comment |
up vote
3
down vote
I found an AWK 1 liner, and it had a bug but I fixed it. I also added in PetaBytes after TeraBytes.
FILE_SIZE=234234 # FILESIZE IN BYTES
FILE_SIZE=$(echo "${FILE_SIZE}" | awk '{ split( "B KB MB GB TB PB" , v ); s=1; while( $1>1024 ){ $1/=1024; s++ } printf "%.2f %s", $1, v[s] }')
Considering stat is not on every single system, you can almost always use the AWK solution. Example; the Raspberry Pi does not have stat but it does have awk.
Completely NOT what the OP asked, but nice little piece of work.
– Gypsy Spellweaver
Jun 10 at 5:48
add a comment |
up vote
2
down vote
Create small utility functions in your shell scripts that you can delegate to.
Example
#! /bin/sh -
# vim: set ft=sh
# size utility that works on GNU and BSD systems
size(){
case $(uname) in
(Darwin | *BSD*)
stat -Lf %z -- "$1";;
(*) stat -c %s -- "$1"
esac
}
for f do
printf '%sn' "$f : $(gzip < "$f" | wc -c) bytes (versus $(size "$f") bytes)"
done
Based on info from @Stéphane Chazelas' answer.
See alsogzip -v < file > /dev/null
to check the compressibility of a file.
– Stéphane Chazelas
Jan 11 '17 at 14:36
@StéphaneChazelas not sure if i think it was an improvement. those case statements can easily put noobs off; I certainly never remember how to get them right :-) are case statements inherently more portable since you did it? i see the point when there are more than two cases, but otherwise ...+
– oligofren
Jan 11 '17 at 16:48
1
I suppose it's also a matter of taste, but here it's the typical case where you'd want to use acase
statement.case
is the Bourne/POSIX construct to do pattern matching.[[...]]
is ksh/bash/zsh only (with variations).
– Stéphane Chazelas
Jan 11 '17 at 16:55
so indeed more portable! thanks!
– oligofren
Jan 11 '17 at 17:00
add a comment |
up vote
0
down vote
I like the wc option myself. Paired with 'bc,' you can get decimals to as many places as you please.
I was looking to improve a script I had that awk'ed out the 'file size' column of an 'ls -alh' command. I didn't want just integer file sizes, and two decimals seemed to suit, so after reading this discussion, I came up with the code below.
I suggest breaking the line at the semicolons if you include this in a script.
file=$1; string=$(wc -c $file); bite=${string% *}; okay=$(echo "scale=2; $bite/1024" | bc);friend=$(echo -e "$file $okay" "kb"); echo -e "$friend"
My script is called gpfl, for "get picture file length." I use it after doing a mogrify on a file in imagemagick, before opening or re-loading a picture in a GUI jpeg viewer.
I don't know how this rates as an "answer," as it borrows much from what's already been offered and discussed. So I'll leave it there.
BZT
1
I would prefer using "stat" or "ls". Typically I don't like using "wc" to get file sizes because it physically reads the entire file. If you have a lot of files, or particularly large files, this can take a lot of time. But your solution is creative...+1.
– Kevin Fegan
Dec 9 '13 at 19:18
2
I agree with notion of using "stat" over "wc" for filesize, however if you use "wc -c", no data will be read; instead lseek will be used to figure out the number of bytes in a file. lingrok.org/xref/coreutils/src/wc.c#228
– bbaja42
Dec 14 '14 at 14:38
1
@bbaja42: note that GNU Coreutilswc
does read the last block of the file, in casestat.st_size
was only an approximation (like for Linux/proc
and/sys
files). I guess they decided not to make the main comment more complicated when they added that logic a couple lines down: lingrok.org/xref/coreutils/src/wc.c#246
– Peter Cordes
Apr 12 '17 at 5:53
add a comment |
up vote
-1
down vote
That can be done with du command. Here is working example:
This software offer you the way to choose which file in which directory you want to know its file. (also send to git)
#!/bin/bash
# File size
FILE_SIZE=$"du -h"
# Clear the terminal
tput clear
# Request the user where and which file with an example
printf "Which file do you wish to calculate?n"
printf "Please provide its place and name like /home/user/file-size.shn"
read FILE
# Calculate with du -h (human readable) and print the output
$FILE_SIZE $FILE
add a comment |
up vote
-1
down vote
Fastest and simplest (IMO) method is:
bash_var=$(stat -c %s /path/to/filename)
1
Then upvote one or more of the existing answers that mention stat; no need to repeat it again...
– Jeff Schaller
Nov 21 at 1:16
1
@JeffSchaller I just upvoted Stephane's answer on your instructions. I think it is too complicated for my purposes. Which is why I posted this simple answer for like minded souls.
– WinEunuuchs2Unix
Nov 21 at 1:21
1
Thank you; it's just that a sixth instance of a "stat" answer doesn't simplify this Q & A, but would rather make a new reader ask themselves "how is this answer different from the other ones?" and lead to more confusion instead of less.
– Jeff Schaller
Nov 21 at 1:32
@JeffSchaller I guess. But I could complain about the manydu
andwc
answers that should have a disclaimer NEVER DO THIS in real life. I just used my answer in a real life application tonight and thought it was worthwhile sharing. I guess we all have our opinions shrugs.
– WinEunuuchs2Unix
Nov 21 at 1:36
add a comment |
protected by Kusalananda Jul 3 '17 at 7:02
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
14 Answers
14
active
oldest
votes
14 Answers
14
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
186
down vote
accepted
Your best bet if on a GNU system:
stat --printf="%s" file.any
From man stat:
%s total size, in bytes
NOTE: see @chbrown answer below how to use stat in terminal on Mac OS X
or
#!/bin/bash
FILENAME=/home/heiko/dummy/packages.txt
FILESIZE=$(stat -c%s "$FILENAME")
echo "Size of $FILENAME = $FILESIZE bytes."
you know I'm just a newbie and I'm eager to learn... they say there's not such thing as dumb question; I honestly didn't know about stat. Thank you for your help. Appreciated.
– haunted85
Jul 13 '11 at 16:47
5
@haunted85stat
is the most straightforward way, assuming you're using Linux or Cygwin (stat
isn't standard).wc -c
as suggested by Eugéne is portable.
– Gilles
Jul 14 '11 at 10:02
2
stat: illegal option -- c
– Iulian Onofrei
Apr 27 '17 at 13:42
stat --printf="%s" file.txt
doesn't output anything on Debian Jessie...
– woohoo
Apr 29 '17 at 20:36
4
On MacOS this works:stat -f%z myfile.tar
– ccpizza
May 6 at 10:40
add a comment |
up vote
186
down vote
accepted
Your best bet if on a GNU system:
stat --printf="%s" file.any
From man stat:
%s total size, in bytes
NOTE: see @chbrown answer below how to use stat in terminal on Mac OS X
or
#!/bin/bash
FILENAME=/home/heiko/dummy/packages.txt
FILESIZE=$(stat -c%s "$FILENAME")
echo "Size of $FILENAME = $FILESIZE bytes."
you know I'm just a newbie and I'm eager to learn... they say there's not such thing as dumb question; I honestly didn't know about stat. Thank you for your help. Appreciated.
– haunted85
Jul 13 '11 at 16:47
5
@haunted85stat
is the most straightforward way, assuming you're using Linux or Cygwin (stat
isn't standard).wc -c
as suggested by Eugéne is portable.
– Gilles
Jul 14 '11 at 10:02
2
stat: illegal option -- c
– Iulian Onofrei
Apr 27 '17 at 13:42
stat --printf="%s" file.txt
doesn't output anything on Debian Jessie...
– woohoo
Apr 29 '17 at 20:36
4
On MacOS this works:stat -f%z myfile.tar
– ccpizza
May 6 at 10:40
add a comment |
up vote
186
down vote
accepted
up vote
186
down vote
accepted
Your best bet if on a GNU system:
stat --printf="%s" file.any
From man stat:
%s total size, in bytes
NOTE: see @chbrown answer below how to use stat in terminal on Mac OS X
or
#!/bin/bash
FILENAME=/home/heiko/dummy/packages.txt
FILESIZE=$(stat -c%s "$FILENAME")
echo "Size of $FILENAME = $FILESIZE bytes."
Your best bet if on a GNU system:
stat --printf="%s" file.any
From man stat:
%s total size, in bytes
NOTE: see @chbrown answer below how to use stat in terminal on Mac OS X
or
#!/bin/bash
FILENAME=/home/heiko/dummy/packages.txt
FILESIZE=$(stat -c%s "$FILENAME")
echo "Size of $FILENAME = $FILESIZE bytes."
edited Sep 23 at 10:28
user88036
answered Jul 13 '11 at 16:29
b01
1,984196
1,984196
you know I'm just a newbie and I'm eager to learn... they say there's not such thing as dumb question; I honestly didn't know about stat. Thank you for your help. Appreciated.
– haunted85
Jul 13 '11 at 16:47
5
@haunted85stat
is the most straightforward way, assuming you're using Linux or Cygwin (stat
isn't standard).wc -c
as suggested by Eugéne is portable.
– Gilles
Jul 14 '11 at 10:02
2
stat: illegal option -- c
– Iulian Onofrei
Apr 27 '17 at 13:42
stat --printf="%s" file.txt
doesn't output anything on Debian Jessie...
– woohoo
Apr 29 '17 at 20:36
4
On MacOS this works:stat -f%z myfile.tar
– ccpizza
May 6 at 10:40
add a comment |
you know I'm just a newbie and I'm eager to learn... they say there's not such thing as dumb question; I honestly didn't know about stat. Thank you for your help. Appreciated.
– haunted85
Jul 13 '11 at 16:47
5
@haunted85stat
is the most straightforward way, assuming you're using Linux or Cygwin (stat
isn't standard).wc -c
as suggested by Eugéne is portable.
– Gilles
Jul 14 '11 at 10:02
2
stat: illegal option -- c
– Iulian Onofrei
Apr 27 '17 at 13:42
stat --printf="%s" file.txt
doesn't output anything on Debian Jessie...
– woohoo
Apr 29 '17 at 20:36
4
On MacOS this works:stat -f%z myfile.tar
– ccpizza
May 6 at 10:40
you know I'm just a newbie and I'm eager to learn... they say there's not such thing as dumb question; I honestly didn't know about stat. Thank you for your help. Appreciated.
– haunted85
Jul 13 '11 at 16:47
you know I'm just a newbie and I'm eager to learn... they say there's not such thing as dumb question; I honestly didn't know about stat. Thank you for your help. Appreciated.
– haunted85
Jul 13 '11 at 16:47
5
5
@haunted85
stat
is the most straightforward way, assuming you're using Linux or Cygwin (stat
isn't standard). wc -c
as suggested by Eugéne is portable.– Gilles
Jul 14 '11 at 10:02
@haunted85
stat
is the most straightforward way, assuming you're using Linux or Cygwin (stat
isn't standard). wc -c
as suggested by Eugéne is portable.– Gilles
Jul 14 '11 at 10:02
2
2
stat: illegal option -- c
– Iulian Onofrei
Apr 27 '17 at 13:42
stat: illegal option -- c
– Iulian Onofrei
Apr 27 '17 at 13:42
stat --printf="%s" file.txt
doesn't output anything on Debian Jessie...– woohoo
Apr 29 '17 at 20:36
stat --printf="%s" file.txt
doesn't output anything on Debian Jessie...– woohoo
Apr 29 '17 at 20:36
4
4
On MacOS this works:
stat -f%z myfile.tar
– ccpizza
May 6 at 10:40
On MacOS this works:
stat -f%z myfile.tar
– ccpizza
May 6 at 10:40
add a comment |
up vote
75
down vote
file_size_kb=`du -k "$filename" | cut -f1`
The problem with using stat
is that it is a GNU (Linux) extension. du -k
and cut -f1
are specified by POSIX and are therefore portable to any Unix system.
Solaris, for example, ships with bash but not with stat
. So this is not entirely hypothetical.
ls
has a similar problem in that the exact format of the output is not specified, so parsing its output cannot be done portably. du -h
is also a GNU extension.
Stick to portable constructs where possible, and you will make somebody's life easier in the future. Maybe your own.
41
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 bydu
is the size of the file rounded up to the nearest number of blocks, where a block is typically 512B or 1kB or 4kB).
– Gilles
Jul 14 '11 at 10:00
6
@Gilles, sparse files (i.e., ones with holes in them) report less than the length.
– vonbrand
Jan 9 '16 at 22:03
add a comment |
up vote
75
down vote
file_size_kb=`du -k "$filename" | cut -f1`
The problem with using stat
is that it is a GNU (Linux) extension. du -k
and cut -f1
are specified by POSIX and are therefore portable to any Unix system.
Solaris, for example, ships with bash but not with stat
. So this is not entirely hypothetical.
ls
has a similar problem in that the exact format of the output is not specified, so parsing its output cannot be done portably. du -h
is also a GNU extension.
Stick to portable constructs where possible, and you will make somebody's life easier in the future. Maybe your own.
41
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 bydu
is the size of the file rounded up to the nearest number of blocks, where a block is typically 512B or 1kB or 4kB).
– Gilles
Jul 14 '11 at 10:00
6
@Gilles, sparse files (i.e., ones with holes in them) report less than the length.
– vonbrand
Jan 9 '16 at 22:03
add a comment |
up vote
75
down vote
up vote
75
down vote
file_size_kb=`du -k "$filename" | cut -f1`
The problem with using stat
is that it is a GNU (Linux) extension. du -k
and cut -f1
are specified by POSIX and are therefore portable to any Unix system.
Solaris, for example, ships with bash but not with stat
. So this is not entirely hypothetical.
ls
has a similar problem in that the exact format of the output is not specified, so parsing its output cannot be done portably. du -h
is also a GNU extension.
Stick to portable constructs where possible, and you will make somebody's life easier in the future. Maybe your own.
file_size_kb=`du -k "$filename" | cut -f1`
The problem with using stat
is that it is a GNU (Linux) extension. du -k
and cut -f1
are specified by POSIX and are therefore portable to any Unix system.
Solaris, for example, ships with bash but not with stat
. So this is not entirely hypothetical.
ls
has a similar problem in that the exact format of the output is not specified, so parsing its output cannot be done portably. du -h
is also a GNU extension.
Stick to portable constructs where possible, and you will make somebody's life easier in the future. Maybe your own.
answered Jul 13 '11 at 16:34
Nemo
86454
86454
41
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 bydu
is the size of the file rounded up to the nearest number of blocks, where a block is typically 512B or 1kB or 4kB).
– Gilles
Jul 14 '11 at 10:00
6
@Gilles, sparse files (i.e., ones with holes in them) report less than the length.
– vonbrand
Jan 9 '16 at 22:03
add a comment |
41
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 bydu
is the size of the file rounded up to the nearest number of blocks, where a block is typically 512B or 1kB or 4kB).
– Gilles
Jul 14 '11 at 10:00
6
@Gilles, sparse files (i.e., ones with holes in them) report less than the length.
– vonbrand
Jan 9 '16 at 22:03
41
41
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).– Gilles
Jul 14 '11 at 10:00
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).– Gilles
Jul 14 '11 at 10:00
6
6
@Gilles, sparse files (i.e., ones with holes in them) report less than the length.
– vonbrand
Jan 9 '16 at 22:03
@Gilles, sparse files (i.e., ones with holes in them) report less than the length.
– vonbrand
Jan 9 '16 at 22:03
add a comment |
up vote
60
down vote
You could also use the "word count" command (wc
):
wc -c "$filename" | awk '{print $1}'
The problem with wc
is that it'll add the filename and indent the output. For example:
$ wc -c somefile.txt
1160 somefile.txt
If you would like to avoid chaining a full interpreted language or stream editor just to get a file size count, just redirect the input from the file so that wc
never sees the filename:
wc -c < "$filename"
This last form can be used with command substitution to easily grab the value you were seeking as a shell variable, as mentioned by Gilles below.
size="$(wc -c <"$filename")"
23
wc -c <"$FILENAME"
gives the size with no other cruft, thussize=$(wc -c <"$FILENAME")
.
– Gilles
Jul 14 '11 at 9:58
4
Just one more point: I just tested it andwc -c < file
seems to be very fast, at least on OS X. I'm guessing that wc has the brains to try to stat the file if only -c is specified.
– Edward Falk
Apr 4 '16 at 16:29
3
@EdwardFalk: GNUwc -c
usesfstat
, but then seeks to second-last block of the file and reads the last up-tost_blksize
bytes. Apparently this is because files in Linux's/proc
and/sys
for example have stat sizes that are only approximate, andwc
wants to report the actual size, not the stat-reported size. I guess it would be weird forwc -c
to report a different size thanwc
, but it's not idea to read data from the file if it's a normal disk file, and it's not in memory. Or worse, near-line tape storage...
– Peter Cordes
Apr 12 '17 at 5:28
1
It seems likeprintf
still sees the indentation, e.g.printf "Size: $size"
->size: <4 spaces> 54339
. On the other handecho
ignores the whitespace. Any way to make it consistent?
– Eugene Kulabuhov
May 2 '17 at 12:43
1
@EugeneKulabuhov yes… Useprintf "Size: %sn" ${size}
and it should generate the expected result...
– Eugéne
May 3 '17 at 14:45
|
show 6 more comments
up vote
60
down vote
You could also use the "word count" command (wc
):
wc -c "$filename" | awk '{print $1}'
The problem with wc
is that it'll add the filename and indent the output. For example:
$ wc -c somefile.txt
1160 somefile.txt
If you would like to avoid chaining a full interpreted language or stream editor just to get a file size count, just redirect the input from the file so that wc
never sees the filename:
wc -c < "$filename"
This last form can be used with command substitution to easily grab the value you were seeking as a shell variable, as mentioned by Gilles below.
size="$(wc -c <"$filename")"
23
wc -c <"$FILENAME"
gives the size with no other cruft, thussize=$(wc -c <"$FILENAME")
.
– Gilles
Jul 14 '11 at 9:58
4
Just one more point: I just tested it andwc -c < file
seems to be very fast, at least on OS X. I'm guessing that wc has the brains to try to stat the file if only -c is specified.
– Edward Falk
Apr 4 '16 at 16:29
3
@EdwardFalk: GNUwc -c
usesfstat
, but then seeks to second-last block of the file and reads the last up-tost_blksize
bytes. Apparently this is because files in Linux's/proc
and/sys
for example have stat sizes that are only approximate, andwc
wants to report the actual size, not the stat-reported size. I guess it would be weird forwc -c
to report a different size thanwc
, but it's not idea to read data from the file if it's a normal disk file, and it's not in memory. Or worse, near-line tape storage...
– Peter Cordes
Apr 12 '17 at 5:28
1
It seems likeprintf
still sees the indentation, e.g.printf "Size: $size"
->size: <4 spaces> 54339
. On the other handecho
ignores the whitespace. Any way to make it consistent?
– Eugene Kulabuhov
May 2 '17 at 12:43
1
@EugeneKulabuhov yes… Useprintf "Size: %sn" ${size}
and it should generate the expected result...
– Eugéne
May 3 '17 at 14:45
|
show 6 more comments
up vote
60
down vote
up vote
60
down vote
You could also use the "word count" command (wc
):
wc -c "$filename" | awk '{print $1}'
The problem with wc
is that it'll add the filename and indent the output. For example:
$ wc -c somefile.txt
1160 somefile.txt
If you would like to avoid chaining a full interpreted language or stream editor just to get a file size count, just redirect the input from the file so that wc
never sees the filename:
wc -c < "$filename"
This last form can be used with command substitution to easily grab the value you were seeking as a shell variable, as mentioned by Gilles below.
size="$(wc -c <"$filename")"
You could also use the "word count" command (wc
):
wc -c "$filename" | awk '{print $1}'
The problem with wc
is that it'll add the filename and indent the output. For example:
$ wc -c somefile.txt
1160 somefile.txt
If you would like to avoid chaining a full interpreted language or stream editor just to get a file size count, just redirect the input from the file so that wc
never sees the filename:
wc -c < "$filename"
This last form can be used with command substitution to easily grab the value you were seeking as a shell variable, as mentioned by Gilles below.
size="$(wc -c <"$filename")"
edited Apr 13 '17 at 12:36
Community♦
1
1
answered Jul 14 '11 at 8:47
Eugéne
70946
70946
23
wc -c <"$FILENAME"
gives the size with no other cruft, thussize=$(wc -c <"$FILENAME")
.
– Gilles
Jul 14 '11 at 9:58
4
Just one more point: I just tested it andwc -c < file
seems to be very fast, at least on OS X. I'm guessing that wc has the brains to try to stat the file if only -c is specified.
– Edward Falk
Apr 4 '16 at 16:29
3
@EdwardFalk: GNUwc -c
usesfstat
, but then seeks to second-last block of the file and reads the last up-tost_blksize
bytes. Apparently this is because files in Linux's/proc
and/sys
for example have stat sizes that are only approximate, andwc
wants to report the actual size, not the stat-reported size. I guess it would be weird forwc -c
to report a different size thanwc
, but it's not idea to read data from the file if it's a normal disk file, and it's not in memory. Or worse, near-line tape storage...
– Peter Cordes
Apr 12 '17 at 5:28
1
It seems likeprintf
still sees the indentation, e.g.printf "Size: $size"
->size: <4 spaces> 54339
. On the other handecho
ignores the whitespace. Any way to make it consistent?
– Eugene Kulabuhov
May 2 '17 at 12:43
1
@EugeneKulabuhov yes… Useprintf "Size: %sn" ${size}
and it should generate the expected result...
– Eugéne
May 3 '17 at 14:45
|
show 6 more comments
23
wc -c <"$FILENAME"
gives the size with no other cruft, thussize=$(wc -c <"$FILENAME")
.
– Gilles
Jul 14 '11 at 9:58
4
Just one more point: I just tested it andwc -c < file
seems to be very fast, at least on OS X. I'm guessing that wc has the brains to try to stat the file if only -c is specified.
– Edward Falk
Apr 4 '16 at 16:29
3
@EdwardFalk: GNUwc -c
usesfstat
, but then seeks to second-last block of the file and reads the last up-tost_blksize
bytes. Apparently this is because files in Linux's/proc
and/sys
for example have stat sizes that are only approximate, andwc
wants to report the actual size, not the stat-reported size. I guess it would be weird forwc -c
to report a different size thanwc
, but it's not idea to read data from the file if it's a normal disk file, and it's not in memory. Or worse, near-line tape storage...
– Peter Cordes
Apr 12 '17 at 5:28
1
It seems likeprintf
still sees the indentation, e.g.printf "Size: $size"
->size: <4 spaces> 54339
. On the other handecho
ignores the whitespace. Any way to make it consistent?
– Eugene Kulabuhov
May 2 '17 at 12:43
1
@EugeneKulabuhov yes… Useprintf "Size: %sn" ${size}
and it should generate the expected result...
– Eugéne
May 3 '17 at 14:45
23
23
wc -c <"$FILENAME"
gives the size with no other cruft, thus size=$(wc -c <"$FILENAME")
.– Gilles
Jul 14 '11 at 9:58
wc -c <"$FILENAME"
gives the size with no other cruft, thus size=$(wc -c <"$FILENAME")
.– Gilles
Jul 14 '11 at 9:58
4
4
Just one more point: I just tested it and
wc -c < file
seems to be very fast, at least on OS X. I'm guessing that wc has the brains to try to stat the file if only -c is specified.– Edward Falk
Apr 4 '16 at 16:29
Just one more point: I just tested it and
wc -c < file
seems to be very fast, at least on OS X. I'm guessing that wc has the brains to try to stat the file if only -c is specified.– Edward Falk
Apr 4 '16 at 16:29
3
3
@EdwardFalk: GNU
wc -c
uses fstat
, but then seeks to second-last block of the file and reads the last up-to st_blksize
bytes. Apparently this is because files in Linux's /proc
and /sys
for example have stat sizes that are only approximate, and wc
wants to report the actual size, not the stat-reported size. I guess it would be weird for wc -c
to report a different size than wc
, but it's not idea to read data from the file if it's a normal disk file, and it's not in memory. Or worse, near-line tape storage...– Peter Cordes
Apr 12 '17 at 5:28
@EdwardFalk: GNU
wc -c
uses fstat
, but then seeks to second-last block of the file and reads the last up-to st_blksize
bytes. Apparently this is because files in Linux's /proc
and /sys
for example have stat sizes that are only approximate, and wc
wants to report the actual size, not the stat-reported size. I guess it would be weird for wc -c
to report a different size than wc
, but it's not idea to read data from the file if it's a normal disk file, and it's not in memory. Or worse, near-line tape storage...– Peter Cordes
Apr 12 '17 at 5:28
1
1
It seems like
printf
still sees the indentation, e.g. printf "Size: $size"
-> size: <4 spaces> 54339
. On the other hand echo
ignores the whitespace. Any way to make it consistent?– Eugene Kulabuhov
May 2 '17 at 12:43
It seems like
printf
still sees the indentation, e.g. printf "Size: $size"
-> size: <4 spaces> 54339
. On the other hand echo
ignores the whitespace. Any way to make it consistent?– Eugene Kulabuhov
May 2 '17 at 12:43
1
1
@EugeneKulabuhov yes… Use
printf "Size: %sn" ${size}
and it should generate the expected result...– Eugéne
May 3 '17 at 14:45
@EugeneKulabuhov yes… Use
printf "Size: %sn" ${size}
and it should generate the expected result...– Eugéne
May 3 '17 at 14:45
|
show 6 more comments
up vote
37
down vote
BSD's (Mac OS X's) stat
has a different format argument flag, and different field specifiers. From man stat(1)
:
-f format
: Display information using the specified format. See the FORMATS section for a description of valid formats.- ... the FORMATS section ...
z
: The size of file in bytes.
So all together now:
stat -f%z myfile1.txt
add a comment |
up vote
37
down vote
BSD's (Mac OS X's) stat
has a different format argument flag, and different field specifiers. From man stat(1)
:
-f format
: Display information using the specified format. See the FORMATS section for a description of valid formats.- ... the FORMATS section ...
z
: The size of file in bytes.
So all together now:
stat -f%z myfile1.txt
add a comment |
up vote
37
down vote
up vote
37
down vote
BSD's (Mac OS X's) stat
has a different format argument flag, and different field specifiers. From man stat(1)
:
-f format
: Display information using the specified format. See the FORMATS section for a description of valid formats.- ... the FORMATS section ...
z
: The size of file in bytes.
So all together now:
stat -f%z myfile1.txt
BSD's (Mac OS X's) stat
has a different format argument flag, and different field specifiers. From man stat(1)
:
-f format
: Display information using the specified format. See the FORMATS section for a description of valid formats.- ... the FORMATS section ...
z
: The size of file in bytes.
So all together now:
stat -f%z myfile1.txt
answered Feb 16 '15 at 0:23
chbrown
50748
50748
add a comment |
add a comment |
up vote
20
down vote
This script combines many ways to calculate the file size:
(
du --apparent-size --block-size=1 "$file" 2>/dev/null ||
gdu --apparent-size --block-size=1 "$file" 2>/dev/null ||
find "$file" -printf "%s" 2>/dev/null ||
gfind "$file" -printf "%s" 2>/dev/null ||
stat --printf="%s" "$file" 2>/dev/null ||
stat -f%z "$file" 2>/dev/null ||
wc -c <"$file" 2>/dev/null
) | awk '{print $1}'
The script works on many Unix systems including Linux, BSD, OSX, Solaris, SunOS, etc.
The file size shows the number of bytes. It is the apparent size, which is the bytes the file uses on a typical disk, without special compression, or special sparse areas, or unallocated blocks, etc.
This script has a production version with more help and more options here:
https://github.com/SixArm/sixarm_unix_shell_scripts/blob/master/file-size
add a comment |
up vote
20
down vote
This script combines many ways to calculate the file size:
(
du --apparent-size --block-size=1 "$file" 2>/dev/null ||
gdu --apparent-size --block-size=1 "$file" 2>/dev/null ||
find "$file" -printf "%s" 2>/dev/null ||
gfind "$file" -printf "%s" 2>/dev/null ||
stat --printf="%s" "$file" 2>/dev/null ||
stat -f%z "$file" 2>/dev/null ||
wc -c <"$file" 2>/dev/null
) | awk '{print $1}'
The script works on many Unix systems including Linux, BSD, OSX, Solaris, SunOS, etc.
The file size shows the number of bytes. It is the apparent size, which is the bytes the file uses on a typical disk, without special compression, or special sparse areas, or unallocated blocks, etc.
This script has a production version with more help and more options here:
https://github.com/SixArm/sixarm_unix_shell_scripts/blob/master/file-size
add a comment |
up vote
20
down vote
up vote
20
down vote
This script combines many ways to calculate the file size:
(
du --apparent-size --block-size=1 "$file" 2>/dev/null ||
gdu --apparent-size --block-size=1 "$file" 2>/dev/null ||
find "$file" -printf "%s" 2>/dev/null ||
gfind "$file" -printf "%s" 2>/dev/null ||
stat --printf="%s" "$file" 2>/dev/null ||
stat -f%z "$file" 2>/dev/null ||
wc -c <"$file" 2>/dev/null
) | awk '{print $1}'
The script works on many Unix systems including Linux, BSD, OSX, Solaris, SunOS, etc.
The file size shows the number of bytes. It is the apparent size, which is the bytes the file uses on a typical disk, without special compression, or special sparse areas, or unallocated blocks, etc.
This script has a production version with more help and more options here:
https://github.com/SixArm/sixarm_unix_shell_scripts/blob/master/file-size
This script combines many ways to calculate the file size:
(
du --apparent-size --block-size=1 "$file" 2>/dev/null ||
gdu --apparent-size --block-size=1 "$file" 2>/dev/null ||
find "$file" -printf "%s" 2>/dev/null ||
gfind "$file" -printf "%s" 2>/dev/null ||
stat --printf="%s" "$file" 2>/dev/null ||
stat -f%z "$file" 2>/dev/null ||
wc -c <"$file" 2>/dev/null
) | awk '{print $1}'
The script works on many Unix systems including Linux, BSD, OSX, Solaris, SunOS, etc.
The file size shows the number of bytes. It is the apparent size, which is the bytes the file uses on a typical disk, without special compression, or special sparse areas, or unallocated blocks, etc.
This script has a production version with more help and more options here:
https://github.com/SixArm/sixarm_unix_shell_scripts/blob/master/file-size
edited Dec 29 '15 at 22:55
answered Jul 17 '15 at 3:07
joelparkerhenderson
53946
53946
add a comment |
add a comment |
up vote
20
down vote
Depends what you mean by size.
size=$(wc -c < "$file")
will give you the number of bytes that can be read from the file. IOW, it's the size of the content of the file. It will however read the content of the file (except if the file is a regular file or symlink to regular file in most wc
implementations as an optimisation). That may have side effects. For instance, for a named pipe, what has been read can no longer be read again and for things like /dev/zero
or /dev/random
which are of infinite size, it's going to take a while. That also means you need read
permission to the file.
That's standard and portable, however note that some wc
implementations may include leading blanks in that output. One way to get rid of them is to use:
size=$(($(wc -c < "$file")))
or to avoid an error about an empty arithmetic expression in dash
or yash
when wc
produces no output (like when the file can't be opened):
size=$(($(wc -c < "$file") +0))
ksh93
has wc
builtin (provided you enable it, you can also invoke it as command /opt/ast/bin/wc
) which makes it the most efficient for regular files in that shell.
Various systems have a command called stat
that's an interface to the stat()
or lstat()
system calls.
Those report information found in the inode. One of that information is the st_size
attribute. For regular files, that's the size of the content (how much data could be read from it in the absence of error (that's what most wc -c
implementations use in their optimisation)). For symlinks, that's the size in bytes of the target path. For named pipes, depending on the system, it's either 0 or the number of bytes currently in the pipe buffer. Same for block devices where depending on the system, you get 0 or the size in bytes of the underlying storage.
You don't need read permission to the file to get that information, only search permission to the directory it is linked to.
By chronological order, there is:
IRIX
stat
(90's):
stat -qLs -- "$file"
returns the
st_size
attribute of$file
(lstat()
) or:
stat -s -- "$file"
same except when
$file
is a symlink in which case it's thest_size
of the file after symlink resolution.
zsh
stat
builtin (now also known aszstat
) in thezsh/stat
module (loaded withzmodload zsh/stat
) (1997):
stat -L +size -- $file # st_size of file
stat +size -- $file # after symlink resolution
or to store in a variable:
stat -L -A size +size -- $file
obviously, that's the most efficient in that shell.
GNU
stat
(2001); also in BusyBoxstat
since 2005 (copied from GNUstat
):
stat -c %s -- "$file" # st_size of file
stat -Lc %s -- "$file" # after symlink resolution
(note the meaning of
-L
is reversed compared to IRIX orzsh
stat
.
BSDs
stat
(2002):
stat -f %z -- "$file" # st_size of file
stat -Lf %z -- "$file" # after symlink resolution
Or you can use the stat()
/lstat()
function of some scripting language like perl
:
perl -le 'print((lstat shift)[7])' -- "$file"
AIX also has an istat
command which will dump all the stat()
(not lstat()
, so won't work on symlinks) information and which you could post-process with, for example:
LC_ALL=C istat "$file" | awk 'NR == 4 {print $5}'
(thanks @JeffSchaller for the help figuring out the details).
In tcsh
:
@ size = -Z $file:q
(size after symlink resolution)
Long before GNU introduced its stat
command, the same could be achieved with GNU find
command with its -printf
predicate (already in 1991):
find -- "$file" -prune -printf '%sn' # st_size of file
find -L -- "$file" -prune -printf '%sn' # after symlink resolution
One issue though is that doesn't work if $file
starts with -
or is a find
predicate (like !
, (
...).
The standard command to get the stat()
/lstat()
information is ls
.
POSIXly, you can do:
LC_ALL=C ls -dn -- "$file" | awk '{print $5; exit}'
and add -L
for the same after symlink resolution. That doesn't work for device files though where the 5th field is the device major number instead of the size.
For block devices, systems where stat()
returns 0 for st_size
, usually have other APIs to report the size of the block device. For instance, Linux has the BLKGETSIZE64
ioctl()
, and most Linux distributions now ship with a blockdev
command that can make use of it:
blockdev --getsize64 -- "$device_file"
However, you need read permission to the device file for that. It's usually possible to derive the size by other means. For instance (still on Linux):
lsblk -bdno size -- "$device_file"
Should work except for empty devices.
An approach that works for all seekable files (so includes regular files, most block devices and some character devices) is to open the file and seek to the end:
With
zsh
(after loading thezsh/system
module):
{sysseek -w end 0 && size=$((systell(0)))} < $file
With
ksh93
:
< "$file" <#((size=EOF))
or
{ size=$(<#((EOF))); } < "$file"
with
perl
:
perl -le 'seek STDIN, 0, 2 or die "seek: $!"; print tell STDIN' < "$file"
For named pipes, we've seen that some systems (AIX, Solaris, HP/UX at least) make the amount of data in the pipe buffer available in stat()
's st_size
. Some (like Linux or FreeBSD) don't.
On Linux at least, you can use the FIONREAD
ioctl()
after having open the pipe (in read+write mode to avoid it hanging):
fuser -s -- "$fifo_file" &&
perl -le 'require "sys/ioctl.ph";
ioctl(STDIN, &FIONREAD, $n) or die$!;
print unpack "L", $n' <> "$fifo_file"
However note that while it doesn't read the content of the pipe, the mere opening of the named pipe here can still have side effects. We're using fuser
to check first that some process already has the pipe open to alleviate that but that's not foolproof as fuser
may not be able to check all processes.
Now, so far we've only been considering the size of the primary data associated with the files. That doesn't take into account the size of the metadata and all the supporting infrastructure needed to store that file.
Another inode attribute returned by stat()
is st_blocks
. That's the number of 512 byte blocks that is used to store the file's data (and sometimes some of its metadata like the extended attributes on ext4 filesystems on Linux). That doesn't include the inode itself, or the entries in the directories the file is linked to.
Size and disk usage are not necessarily tightly related as compression, sparseness (sometimes some metadata), extra infrastructure like indirect blocks in some filesystems have an influence on the latter.
That's typically what du
uses to report disk usage. Most of the commands listed above will be able to get you that information.
POSIXLY_CORRECT=1 ls -sd -- "$file" | awk '{print $1; exit}'
POSIXLY_CORRECT=1 du -s -- "$file"
(not for directories where that would include the disk usage of the files within).- GNU
find -- "$file" -printf '%bn'
zstat -L +block -- $file
- GNU
stat -c %b -- "$file"
- BSD
stat -f %b -- "$file"
perl -le 'print((lstat shift)[12])' -- "$file"
clearly the most comprehensive and informational answer. thank you. i can use this to create cross platform bash scripts using the BSD and GNU stats info
– oligofren
Jan 11 '17 at 12:50
1
Fun fact: GNU coreutilswc -c
usesfstat
, but then reads the last up-tost_blksize
bytes. Apparently this is because files in Linux's/proc
and/sys
for example have stat sizes that are only approximate. This is good for correctness, but bad if the end of the file is on disk and not in memory (esp. if used on many files in a loop). And very bad if the file is migrated to near-line tape storage, or e.g. a FUSE transparent-decompression filesystem.
– Peter Cordes
Apr 12 '17 at 5:48
wouldnt this work tools -go file | awk '{print $3}'
– Steven Penny
Feb 8 at 13:00
@StevenPenny those-go
would be the SysV ones, they wouldn't work on BSDs (optional (XSI) in POSIX). You'd also needls -god file | awk '{print $3; exit}'
(-d
for it to work on directories,exit
for symlinks with newlines in the target). The problems with device files also remain.
– Stéphane Chazelas
Feb 8 at 22:31
add a comment |
up vote
20
down vote
Depends what you mean by size.
size=$(wc -c < "$file")
will give you the number of bytes that can be read from the file. IOW, it's the size of the content of the file. It will however read the content of the file (except if the file is a regular file or symlink to regular file in most wc
implementations as an optimisation). That may have side effects. For instance, for a named pipe, what has been read can no longer be read again and for things like /dev/zero
or /dev/random
which are of infinite size, it's going to take a while. That also means you need read
permission to the file.
That's standard and portable, however note that some wc
implementations may include leading blanks in that output. One way to get rid of them is to use:
size=$(($(wc -c < "$file")))
or to avoid an error about an empty arithmetic expression in dash
or yash
when wc
produces no output (like when the file can't be opened):
size=$(($(wc -c < "$file") +0))
ksh93
has wc
builtin (provided you enable it, you can also invoke it as command /opt/ast/bin/wc
) which makes it the most efficient for regular files in that shell.
Various systems have a command called stat
that's an interface to the stat()
or lstat()
system calls.
Those report information found in the inode. One of that information is the st_size
attribute. For regular files, that's the size of the content (how much data could be read from it in the absence of error (that's what most wc -c
implementations use in their optimisation)). For symlinks, that's the size in bytes of the target path. For named pipes, depending on the system, it's either 0 or the number of bytes currently in the pipe buffer. Same for block devices where depending on the system, you get 0 or the size in bytes of the underlying storage.
You don't need read permission to the file to get that information, only search permission to the directory it is linked to.
By chronological order, there is:
IRIX
stat
(90's):
stat -qLs -- "$file"
returns the
st_size
attribute of$file
(lstat()
) or:
stat -s -- "$file"
same except when
$file
is a symlink in which case it's thest_size
of the file after symlink resolution.
zsh
stat
builtin (now also known aszstat
) in thezsh/stat
module (loaded withzmodload zsh/stat
) (1997):
stat -L +size -- $file # st_size of file
stat +size -- $file # after symlink resolution
or to store in a variable:
stat -L -A size +size -- $file
obviously, that's the most efficient in that shell.
GNU
stat
(2001); also in BusyBoxstat
since 2005 (copied from GNUstat
):
stat -c %s -- "$file" # st_size of file
stat -Lc %s -- "$file" # after symlink resolution
(note the meaning of
-L
is reversed compared to IRIX orzsh
stat
.
BSDs
stat
(2002):
stat -f %z -- "$file" # st_size of file
stat -Lf %z -- "$file" # after symlink resolution
Or you can use the stat()
/lstat()
function of some scripting language like perl
:
perl -le 'print((lstat shift)[7])' -- "$file"
AIX also has an istat
command which will dump all the stat()
(not lstat()
, so won't work on symlinks) information and which you could post-process with, for example:
LC_ALL=C istat "$file" | awk 'NR == 4 {print $5}'
(thanks @JeffSchaller for the help figuring out the details).
In tcsh
:
@ size = -Z $file:q
(size after symlink resolution)
Long before GNU introduced its stat
command, the same could be achieved with GNU find
command with its -printf
predicate (already in 1991):
find -- "$file" -prune -printf '%sn' # st_size of file
find -L -- "$file" -prune -printf '%sn' # after symlink resolution
One issue though is that doesn't work if $file
starts with -
or is a find
predicate (like !
, (
...).
The standard command to get the stat()
/lstat()
information is ls
.
POSIXly, you can do:
LC_ALL=C ls -dn -- "$file" | awk '{print $5; exit}'
and add -L
for the same after symlink resolution. That doesn't work for device files though where the 5th field is the device major number instead of the size.
For block devices, systems where stat()
returns 0 for st_size
, usually have other APIs to report the size of the block device. For instance, Linux has the BLKGETSIZE64
ioctl()
, and most Linux distributions now ship with a blockdev
command that can make use of it:
blockdev --getsize64 -- "$device_file"
However, you need read permission to the device file for that. It's usually possible to derive the size by other means. For instance (still on Linux):
lsblk -bdno size -- "$device_file"
Should work except for empty devices.
An approach that works for all seekable files (so includes regular files, most block devices and some character devices) is to open the file and seek to the end:
With
zsh
(after loading thezsh/system
module):
{sysseek -w end 0 && size=$((systell(0)))} < $file
With
ksh93
:
< "$file" <#((size=EOF))
or
{ size=$(<#((EOF))); } < "$file"
with
perl
:
perl -le 'seek STDIN, 0, 2 or die "seek: $!"; print tell STDIN' < "$file"
For named pipes, we've seen that some systems (AIX, Solaris, HP/UX at least) make the amount of data in the pipe buffer available in stat()
's st_size
. Some (like Linux or FreeBSD) don't.
On Linux at least, you can use the FIONREAD
ioctl()
after having open the pipe (in read+write mode to avoid it hanging):
fuser -s -- "$fifo_file" &&
perl -le 'require "sys/ioctl.ph";
ioctl(STDIN, &FIONREAD, $n) or die$!;
print unpack "L", $n' <> "$fifo_file"
However note that while it doesn't read the content of the pipe, the mere opening of the named pipe here can still have side effects. We're using fuser
to check first that some process already has the pipe open to alleviate that but that's not foolproof as fuser
may not be able to check all processes.
Now, so far we've only been considering the size of the primary data associated with the files. That doesn't take into account the size of the metadata and all the supporting infrastructure needed to store that file.
Another inode attribute returned by stat()
is st_blocks
. That's the number of 512 byte blocks that is used to store the file's data (and sometimes some of its metadata like the extended attributes on ext4 filesystems on Linux). That doesn't include the inode itself, or the entries in the directories the file is linked to.
Size and disk usage are not necessarily tightly related as compression, sparseness (sometimes some metadata), extra infrastructure like indirect blocks in some filesystems have an influence on the latter.
That's typically what du
uses to report disk usage. Most of the commands listed above will be able to get you that information.
POSIXLY_CORRECT=1 ls -sd -- "$file" | awk '{print $1; exit}'
POSIXLY_CORRECT=1 du -s -- "$file"
(not for directories where that would include the disk usage of the files within).- GNU
find -- "$file" -printf '%bn'
zstat -L +block -- $file
- GNU
stat -c %b -- "$file"
- BSD
stat -f %b -- "$file"
perl -le 'print((lstat shift)[12])' -- "$file"
clearly the most comprehensive and informational answer. thank you. i can use this to create cross platform bash scripts using the BSD and GNU stats info
– oligofren
Jan 11 '17 at 12:50
1
Fun fact: GNU coreutilswc -c
usesfstat
, but then reads the last up-tost_blksize
bytes. Apparently this is because files in Linux's/proc
and/sys
for example have stat sizes that are only approximate. This is good for correctness, but bad if the end of the file is on disk and not in memory (esp. if used on many files in a loop). And very bad if the file is migrated to near-line tape storage, or e.g. a FUSE transparent-decompression filesystem.
– Peter Cordes
Apr 12 '17 at 5:48
wouldnt this work tools -go file | awk '{print $3}'
– Steven Penny
Feb 8 at 13:00
@StevenPenny those-go
would be the SysV ones, they wouldn't work on BSDs (optional (XSI) in POSIX). You'd also needls -god file | awk '{print $3; exit}'
(-d
for it to work on directories,exit
for symlinks with newlines in the target). The problems with device files also remain.
– Stéphane Chazelas
Feb 8 at 22:31
add a comment |
up vote
20
down vote
up vote
20
down vote
Depends what you mean by size.
size=$(wc -c < "$file")
will give you the number of bytes that can be read from the file. IOW, it's the size of the content of the file. It will however read the content of the file (except if the file is a regular file or symlink to regular file in most wc
implementations as an optimisation). That may have side effects. For instance, for a named pipe, what has been read can no longer be read again and for things like /dev/zero
or /dev/random
which are of infinite size, it's going to take a while. That also means you need read
permission to the file.
That's standard and portable, however note that some wc
implementations may include leading blanks in that output. One way to get rid of them is to use:
size=$(($(wc -c < "$file")))
or to avoid an error about an empty arithmetic expression in dash
or yash
when wc
produces no output (like when the file can't be opened):
size=$(($(wc -c < "$file") +0))
ksh93
has wc
builtin (provided you enable it, you can also invoke it as command /opt/ast/bin/wc
) which makes it the most efficient for regular files in that shell.
Various systems have a command called stat
that's an interface to the stat()
or lstat()
system calls.
Those report information found in the inode. One of that information is the st_size
attribute. For regular files, that's the size of the content (how much data could be read from it in the absence of error (that's what most wc -c
implementations use in their optimisation)). For symlinks, that's the size in bytes of the target path. For named pipes, depending on the system, it's either 0 or the number of bytes currently in the pipe buffer. Same for block devices where depending on the system, you get 0 or the size in bytes of the underlying storage.
You don't need read permission to the file to get that information, only search permission to the directory it is linked to.
By chronological order, there is:
IRIX
stat
(90's):
stat -qLs -- "$file"
returns the
st_size
attribute of$file
(lstat()
) or:
stat -s -- "$file"
same except when
$file
is a symlink in which case it's thest_size
of the file after symlink resolution.
zsh
stat
builtin (now also known aszstat
) in thezsh/stat
module (loaded withzmodload zsh/stat
) (1997):
stat -L +size -- $file # st_size of file
stat +size -- $file # after symlink resolution
or to store in a variable:
stat -L -A size +size -- $file
obviously, that's the most efficient in that shell.
GNU
stat
(2001); also in BusyBoxstat
since 2005 (copied from GNUstat
):
stat -c %s -- "$file" # st_size of file
stat -Lc %s -- "$file" # after symlink resolution
(note the meaning of
-L
is reversed compared to IRIX orzsh
stat
.
BSDs
stat
(2002):
stat -f %z -- "$file" # st_size of file
stat -Lf %z -- "$file" # after symlink resolution
Or you can use the stat()
/lstat()
function of some scripting language like perl
:
perl -le 'print((lstat shift)[7])' -- "$file"
AIX also has an istat
command which will dump all the stat()
(not lstat()
, so won't work on symlinks) information and which you could post-process with, for example:
LC_ALL=C istat "$file" | awk 'NR == 4 {print $5}'
(thanks @JeffSchaller for the help figuring out the details).
In tcsh
:
@ size = -Z $file:q
(size after symlink resolution)
Long before GNU introduced its stat
command, the same could be achieved with GNU find
command with its -printf
predicate (already in 1991):
find -- "$file" -prune -printf '%sn' # st_size of file
find -L -- "$file" -prune -printf '%sn' # after symlink resolution
One issue though is that doesn't work if $file
starts with -
or is a find
predicate (like !
, (
...).
The standard command to get the stat()
/lstat()
information is ls
.
POSIXly, you can do:
LC_ALL=C ls -dn -- "$file" | awk '{print $5; exit}'
and add -L
for the same after symlink resolution. That doesn't work for device files though where the 5th field is the device major number instead of the size.
For block devices, systems where stat()
returns 0 for st_size
, usually have other APIs to report the size of the block device. For instance, Linux has the BLKGETSIZE64
ioctl()
, and most Linux distributions now ship with a blockdev
command that can make use of it:
blockdev --getsize64 -- "$device_file"
However, you need read permission to the device file for that. It's usually possible to derive the size by other means. For instance (still on Linux):
lsblk -bdno size -- "$device_file"
Should work except for empty devices.
An approach that works for all seekable files (so includes regular files, most block devices and some character devices) is to open the file and seek to the end:
With
zsh
(after loading thezsh/system
module):
{sysseek -w end 0 && size=$((systell(0)))} < $file
With
ksh93
:
< "$file" <#((size=EOF))
or
{ size=$(<#((EOF))); } < "$file"
with
perl
:
perl -le 'seek STDIN, 0, 2 or die "seek: $!"; print tell STDIN' < "$file"
For named pipes, we've seen that some systems (AIX, Solaris, HP/UX at least) make the amount of data in the pipe buffer available in stat()
's st_size
. Some (like Linux or FreeBSD) don't.
On Linux at least, you can use the FIONREAD
ioctl()
after having open the pipe (in read+write mode to avoid it hanging):
fuser -s -- "$fifo_file" &&
perl -le 'require "sys/ioctl.ph";
ioctl(STDIN, &FIONREAD, $n) or die$!;
print unpack "L", $n' <> "$fifo_file"
However note that while it doesn't read the content of the pipe, the mere opening of the named pipe here can still have side effects. We're using fuser
to check first that some process already has the pipe open to alleviate that but that's not foolproof as fuser
may not be able to check all processes.
Now, so far we've only been considering the size of the primary data associated with the files. That doesn't take into account the size of the metadata and all the supporting infrastructure needed to store that file.
Another inode attribute returned by stat()
is st_blocks
. That's the number of 512 byte blocks that is used to store the file's data (and sometimes some of its metadata like the extended attributes on ext4 filesystems on Linux). That doesn't include the inode itself, or the entries in the directories the file is linked to.
Size and disk usage are not necessarily tightly related as compression, sparseness (sometimes some metadata), extra infrastructure like indirect blocks in some filesystems have an influence on the latter.
That's typically what du
uses to report disk usage. Most of the commands listed above will be able to get you that information.
POSIXLY_CORRECT=1 ls -sd -- "$file" | awk '{print $1; exit}'
POSIXLY_CORRECT=1 du -s -- "$file"
(not for directories where that would include the disk usage of the files within).- GNU
find -- "$file" -printf '%bn'
zstat -L +block -- $file
- GNU
stat -c %b -- "$file"
- BSD
stat -f %b -- "$file"
perl -le 'print((lstat shift)[12])' -- "$file"
Depends what you mean by size.
size=$(wc -c < "$file")
will give you the number of bytes that can be read from the file. IOW, it's the size of the content of the file. It will however read the content of the file (except if the file is a regular file or symlink to regular file in most wc
implementations as an optimisation). That may have side effects. For instance, for a named pipe, what has been read can no longer be read again and for things like /dev/zero
or /dev/random
which are of infinite size, it's going to take a while. That also means you need read
permission to the file.
That's standard and portable, however note that some wc
implementations may include leading blanks in that output. One way to get rid of them is to use:
size=$(($(wc -c < "$file")))
or to avoid an error about an empty arithmetic expression in dash
or yash
when wc
produces no output (like when the file can't be opened):
size=$(($(wc -c < "$file") +0))
ksh93
has wc
builtin (provided you enable it, you can also invoke it as command /opt/ast/bin/wc
) which makes it the most efficient for regular files in that shell.
Various systems have a command called stat
that's an interface to the stat()
or lstat()
system calls.
Those report information found in the inode. One of that information is the st_size
attribute. For regular files, that's the size of the content (how much data could be read from it in the absence of error (that's what most wc -c
implementations use in their optimisation)). For symlinks, that's the size in bytes of the target path. For named pipes, depending on the system, it's either 0 or the number of bytes currently in the pipe buffer. Same for block devices where depending on the system, you get 0 or the size in bytes of the underlying storage.
You don't need read permission to the file to get that information, only search permission to the directory it is linked to.
By chronological order, there is:
IRIX
stat
(90's):
stat -qLs -- "$file"
returns the
st_size
attribute of$file
(lstat()
) or:
stat -s -- "$file"
same except when
$file
is a symlink in which case it's thest_size
of the file after symlink resolution.
zsh
stat
builtin (now also known aszstat
) in thezsh/stat
module (loaded withzmodload zsh/stat
) (1997):
stat -L +size -- $file # st_size of file
stat +size -- $file # after symlink resolution
or to store in a variable:
stat -L -A size +size -- $file
obviously, that's the most efficient in that shell.
GNU
stat
(2001); also in BusyBoxstat
since 2005 (copied from GNUstat
):
stat -c %s -- "$file" # st_size of file
stat -Lc %s -- "$file" # after symlink resolution
(note the meaning of
-L
is reversed compared to IRIX orzsh
stat
.
BSDs
stat
(2002):
stat -f %z -- "$file" # st_size of file
stat -Lf %z -- "$file" # after symlink resolution
Or you can use the stat()
/lstat()
function of some scripting language like perl
:
perl -le 'print((lstat shift)[7])' -- "$file"
AIX also has an istat
command which will dump all the stat()
(not lstat()
, so won't work on symlinks) information and which you could post-process with, for example:
LC_ALL=C istat "$file" | awk 'NR == 4 {print $5}'
(thanks @JeffSchaller for the help figuring out the details).
In tcsh
:
@ size = -Z $file:q
(size after symlink resolution)
Long before GNU introduced its stat
command, the same could be achieved with GNU find
command with its -printf
predicate (already in 1991):
find -- "$file" -prune -printf '%sn' # st_size of file
find -L -- "$file" -prune -printf '%sn' # after symlink resolution
One issue though is that doesn't work if $file
starts with -
or is a find
predicate (like !
, (
...).
The standard command to get the stat()
/lstat()
information is ls
.
POSIXly, you can do:
LC_ALL=C ls -dn -- "$file" | awk '{print $5; exit}'
and add -L
for the same after symlink resolution. That doesn't work for device files though where the 5th field is the device major number instead of the size.
For block devices, systems where stat()
returns 0 for st_size
, usually have other APIs to report the size of the block device. For instance, Linux has the BLKGETSIZE64
ioctl()
, and most Linux distributions now ship with a blockdev
command that can make use of it:
blockdev --getsize64 -- "$device_file"
However, you need read permission to the device file for that. It's usually possible to derive the size by other means. For instance (still on Linux):
lsblk -bdno size -- "$device_file"
Should work except for empty devices.
An approach that works for all seekable files (so includes regular files, most block devices and some character devices) is to open the file and seek to the end:
With
zsh
(after loading thezsh/system
module):
{sysseek -w end 0 && size=$((systell(0)))} < $file
With
ksh93
:
< "$file" <#((size=EOF))
or
{ size=$(<#((EOF))); } < "$file"
with
perl
:
perl -le 'seek STDIN, 0, 2 or die "seek: $!"; print tell STDIN' < "$file"
For named pipes, we've seen that some systems (AIX, Solaris, HP/UX at least) make the amount of data in the pipe buffer available in stat()
's st_size
. Some (like Linux or FreeBSD) don't.
On Linux at least, you can use the FIONREAD
ioctl()
after having open the pipe (in read+write mode to avoid it hanging):
fuser -s -- "$fifo_file" &&
perl -le 'require "sys/ioctl.ph";
ioctl(STDIN, &FIONREAD, $n) or die$!;
print unpack "L", $n' <> "$fifo_file"
However note that while it doesn't read the content of the pipe, the mere opening of the named pipe here can still have side effects. We're using fuser
to check first that some process already has the pipe open to alleviate that but that's not foolproof as fuser
may not be able to check all processes.
Now, so far we've only been considering the size of the primary data associated with the files. That doesn't take into account the size of the metadata and all the supporting infrastructure needed to store that file.
Another inode attribute returned by stat()
is st_blocks
. That's the number of 512 byte blocks that is used to store the file's data (and sometimes some of its metadata like the extended attributes on ext4 filesystems on Linux). That doesn't include the inode itself, or the entries in the directories the file is linked to.
Size and disk usage are not necessarily tightly related as compression, sparseness (sometimes some metadata), extra infrastructure like indirect blocks in some filesystems have an influence on the latter.
That's typically what du
uses to report disk usage. Most of the commands listed above will be able to get you that information.
POSIXLY_CORRECT=1 ls -sd -- "$file" | awk '{print $1; exit}'
POSIXLY_CORRECT=1 du -s -- "$file"
(not for directories where that would include the disk usage of the files within).- GNU
find -- "$file" -printf '%bn'
zstat -L +block -- $file
- GNU
stat -c %b -- "$file"
- BSD
stat -f %b -- "$file"
perl -le 'print((lstat shift)[12])' -- "$file"
edited Mar 1 at 10:35
answered Nov 6 '16 at 21:27
Stéphane Chazelas
294k54555898
294k54555898
clearly the most comprehensive and informational answer. thank you. i can use this to create cross platform bash scripts using the BSD and GNU stats info
– oligofren
Jan 11 '17 at 12:50
1
Fun fact: GNU coreutilswc -c
usesfstat
, but then reads the last up-tost_blksize
bytes. Apparently this is because files in Linux's/proc
and/sys
for example have stat sizes that are only approximate. This is good for correctness, but bad if the end of the file is on disk and not in memory (esp. if used on many files in a loop). And very bad if the file is migrated to near-line tape storage, or e.g. a FUSE transparent-decompression filesystem.
– Peter Cordes
Apr 12 '17 at 5:48
wouldnt this work tools -go file | awk '{print $3}'
– Steven Penny
Feb 8 at 13:00
@StevenPenny those-go
would be the SysV ones, they wouldn't work on BSDs (optional (XSI) in POSIX). You'd also needls -god file | awk '{print $3; exit}'
(-d
for it to work on directories,exit
for symlinks with newlines in the target). The problems with device files also remain.
– Stéphane Chazelas
Feb 8 at 22:31
add a comment |
clearly the most comprehensive and informational answer. thank you. i can use this to create cross platform bash scripts using the BSD and GNU stats info
– oligofren
Jan 11 '17 at 12:50
1
Fun fact: GNU coreutilswc -c
usesfstat
, but then reads the last up-tost_blksize
bytes. Apparently this is because files in Linux's/proc
and/sys
for example have stat sizes that are only approximate. This is good for correctness, but bad if the end of the file is on disk and not in memory (esp. if used on many files in a loop). And very bad if the file is migrated to near-line tape storage, or e.g. a FUSE transparent-decompression filesystem.
– Peter Cordes
Apr 12 '17 at 5:48
wouldnt this work tools -go file | awk '{print $3}'
– Steven Penny
Feb 8 at 13:00
@StevenPenny those-go
would be the SysV ones, they wouldn't work on BSDs (optional (XSI) in POSIX). You'd also needls -god file | awk '{print $3; exit}'
(-d
for it to work on directories,exit
for symlinks with newlines in the target). The problems with device files also remain.
– Stéphane Chazelas
Feb 8 at 22:31
clearly the most comprehensive and informational answer. thank you. i can use this to create cross platform bash scripts using the BSD and GNU stats info
– oligofren
Jan 11 '17 at 12:50
clearly the most comprehensive and informational answer. thank you. i can use this to create cross platform bash scripts using the BSD and GNU stats info
– oligofren
Jan 11 '17 at 12:50
1
1
Fun fact: GNU coreutils
wc -c
uses fstat
, but then reads the last up-to st_blksize
bytes. Apparently this is because files in Linux's /proc
and /sys
for example have stat sizes that are only approximate. This is good for correctness, but bad if the end of the file is on disk and not in memory (esp. if used on many files in a loop). And very bad if the file is migrated to near-line tape storage, or e.g. a FUSE transparent-decompression filesystem.– Peter Cordes
Apr 12 '17 at 5:48
Fun fact: GNU coreutils
wc -c
uses fstat
, but then reads the last up-to st_blksize
bytes. Apparently this is because files in Linux's /proc
and /sys
for example have stat sizes that are only approximate. This is good for correctness, but bad if the end of the file is on disk and not in memory (esp. if used on many files in a loop). And very bad if the file is migrated to near-line tape storage, or e.g. a FUSE transparent-decompression filesystem.– Peter Cordes
Apr 12 '17 at 5:48
wouldnt this work too
ls -go file | awk '{print $3}'
– Steven Penny
Feb 8 at 13:00
wouldnt this work too
ls -go file | awk '{print $3}'
– Steven Penny
Feb 8 at 13:00
@StevenPenny those
-go
would be the SysV ones, they wouldn't work on BSDs (optional (XSI) in POSIX). You'd also need ls -god file | awk '{print $3; exit}'
(-d
for it to work on directories, exit
for symlinks with newlines in the target). The problems with device files also remain.– Stéphane Chazelas
Feb 8 at 22:31
@StevenPenny those
-go
would be the SysV ones, they wouldn't work on BSDs (optional (XSI) in POSIX). You'd also need ls -god file | awk '{print $3; exit}'
(-d
for it to work on directories, exit
for symlinks with newlines in the target). The problems with device files also remain.– Stéphane Chazelas
Feb 8 at 22:31
add a comment |
up vote
7
down vote
ls -l filename
will give you lots of information about a file, including its file size, permissions and owner.
The file size in the fifth column, and is displayed in bytes. In the example below, the filesize is just under 2KB:
-rw-r--r-- 1 user owner 1985 2011-07-12 16:48 index.php
Edit: This is apparently not as reliable as the stat
command.
I think bothls -l
andstat
command give reliable size information. I did not find any reference to the contrary.ls -s
will give size in number of blocks.
– dabest1
Dec 31 '12 at 22:23
1
@dabest1 it's not reliable in a sense that in another unix, their output can be different (and in some unixes it is).
– Eugene Bujak
Oct 2 '14 at 14:39
Yes, IIRC, Solaris didn't display the group name by default, leading to fewer columns in the output.
– Edward Falk
Apr 4 '16 at 16:31
Since the size is pure numeric, surrounded by whitespace, and the date year is pure numeric, in a defined format, it would be possible to use a regexp to treat user+owner as one field, whether or not the group was present. (an exercise for the reader !)
– MikeW
Feb 21 '17 at 15:31
add a comment |
up vote
7
down vote
ls -l filename
will give you lots of information about a file, including its file size, permissions and owner.
The file size in the fifth column, and is displayed in bytes. In the example below, the filesize is just under 2KB:
-rw-r--r-- 1 user owner 1985 2011-07-12 16:48 index.php
Edit: This is apparently not as reliable as the stat
command.
I think bothls -l
andstat
command give reliable size information. I did not find any reference to the contrary.ls -s
will give size in number of blocks.
– dabest1
Dec 31 '12 at 22:23
1
@dabest1 it's not reliable in a sense that in another unix, their output can be different (and in some unixes it is).
– Eugene Bujak
Oct 2 '14 at 14:39
Yes, IIRC, Solaris didn't display the group name by default, leading to fewer columns in the output.
– Edward Falk
Apr 4 '16 at 16:31
Since the size is pure numeric, surrounded by whitespace, and the date year is pure numeric, in a defined format, it would be possible to use a regexp to treat user+owner as one field, whether or not the group was present. (an exercise for the reader !)
– MikeW
Feb 21 '17 at 15:31
add a comment |
up vote
7
down vote
up vote
7
down vote
ls -l filename
will give you lots of information about a file, including its file size, permissions and owner.
The file size in the fifth column, and is displayed in bytes. In the example below, the filesize is just under 2KB:
-rw-r--r-- 1 user owner 1985 2011-07-12 16:48 index.php
Edit: This is apparently not as reliable as the stat
command.
ls -l filename
will give you lots of information about a file, including its file size, permissions and owner.
The file size in the fifth column, and is displayed in bytes. In the example below, the filesize is just under 2KB:
-rw-r--r-- 1 user owner 1985 2011-07-12 16:48 index.php
Edit: This is apparently not as reliable as the stat
command.
answered Jul 13 '11 at 16:24
Druckles
20116
20116
I think bothls -l
andstat
command give reliable size information. I did not find any reference to the contrary.ls -s
will give size in number of blocks.
– dabest1
Dec 31 '12 at 22:23
1
@dabest1 it's not reliable in a sense that in another unix, their output can be different (and in some unixes it is).
– Eugene Bujak
Oct 2 '14 at 14:39
Yes, IIRC, Solaris didn't display the group name by default, leading to fewer columns in the output.
– Edward Falk
Apr 4 '16 at 16:31
Since the size is pure numeric, surrounded by whitespace, and the date year is pure numeric, in a defined format, it would be possible to use a regexp to treat user+owner as one field, whether or not the group was present. (an exercise for the reader !)
– MikeW
Feb 21 '17 at 15:31
add a comment |
I think bothls -l
andstat
command give reliable size information. I did not find any reference to the contrary.ls -s
will give size in number of blocks.
– dabest1
Dec 31 '12 at 22:23
1
@dabest1 it's not reliable in a sense that in another unix, their output can be different (and in some unixes it is).
– Eugene Bujak
Oct 2 '14 at 14:39
Yes, IIRC, Solaris didn't display the group name by default, leading to fewer columns in the output.
– Edward Falk
Apr 4 '16 at 16:31
Since the size is pure numeric, surrounded by whitespace, and the date year is pure numeric, in a defined format, it would be possible to use a regexp to treat user+owner as one field, whether or not the group was present. (an exercise for the reader !)
– MikeW
Feb 21 '17 at 15:31
I think both
ls -l
and stat
command give reliable size information. I did not find any reference to the contrary. ls -s
will give size in number of blocks.– dabest1
Dec 31 '12 at 22:23
I think both
ls -l
and stat
command give reliable size information. I did not find any reference to the contrary. ls -s
will give size in number of blocks.– dabest1
Dec 31 '12 at 22:23
1
1
@dabest1 it's not reliable in a sense that in another unix, their output can be different (and in some unixes it is).
– Eugene Bujak
Oct 2 '14 at 14:39
@dabest1 it's not reliable in a sense that in another unix, their output can be different (and in some unixes it is).
– Eugene Bujak
Oct 2 '14 at 14:39
Yes, IIRC, Solaris didn't display the group name by default, leading to fewer columns in the output.
– Edward Falk
Apr 4 '16 at 16:31
Yes, IIRC, Solaris didn't display the group name by default, leading to fewer columns in the output.
– Edward Falk
Apr 4 '16 at 16:31
Since the size is pure numeric, surrounded by whitespace, and the date year is pure numeric, in a defined format, it would be possible to use a regexp to treat user+owner as one field, whether or not the group was present. (an exercise for the reader !)
– MikeW
Feb 21 '17 at 15:31
Since the size is pure numeric, surrounded by whitespace, and the date year is pure numeric, in a defined format, it would be possible to use a regexp to treat user+owner as one field, whether or not the group was present. (an exercise for the reader !)
– MikeW
Feb 21 '17 at 15:31
add a comment |
up vote
6
down vote
stat appears to do this with the fewest system calls:
$ set debian-live-8.2.0-amd64-xfce-desktop.iso
$ strace stat --format %s $1 | wc
282 2795 27364
$ strace wc --bytes $1 | wc
307 3063 29091
$ strace du --bytes $1 | wc
437 4376 41955
$ strace find $1 -printf %s | wc
604 6061 64793
add a comment |
up vote
6
down vote
stat appears to do this with the fewest system calls:
$ set debian-live-8.2.0-amd64-xfce-desktop.iso
$ strace stat --format %s $1 | wc
282 2795 27364
$ strace wc --bytes $1 | wc
307 3063 29091
$ strace du --bytes $1 | wc
437 4376 41955
$ strace find $1 -printf %s | wc
604 6061 64793
add a comment |
up vote
6
down vote
up vote
6
down vote
stat appears to do this with the fewest system calls:
$ set debian-live-8.2.0-amd64-xfce-desktop.iso
$ strace stat --format %s $1 | wc
282 2795 27364
$ strace wc --bytes $1 | wc
307 3063 29091
$ strace du --bytes $1 | wc
437 4376 41955
$ strace find $1 -printf %s | wc
604 6061 64793
stat appears to do this with the fewest system calls:
$ set debian-live-8.2.0-amd64-xfce-desktop.iso
$ strace stat --format %s $1 | wc
282 2795 27364
$ strace wc --bytes $1 | wc
307 3063 29091
$ strace du --bytes $1 | wc
437 4376 41955
$ strace find $1 -printf %s | wc
604 6061 64793
answered Jan 9 '16 at 20:27
user150821
add a comment |
add a comment |
up vote
4
down vote
du filename
will tell you disk usage in bytes.
I prefer du -h filename
, which gives you the size in a human readable format.
2
that orstat -c "%s"
;)
– c00kiemon5ter
Jul 13 '11 at 16:29
This flavor ofdu
prints out size in blocks of 1024 bytes, not a simple count of bytes.
– Peter Lyons
Sep 17 '15 at 5:10
Note that standarddu
give an output in number of 512-byte units. GNUdu
uses kibibytes instead unless called withPOSIXLY_CORRECT
in its environment.
– Stéphane Chazelas
Nov 8 '16 at 15:01
For files of type directory, that gives the disk usage of the directory but also of all the other files within (recursively).
– Stéphane Chazelas
Nov 8 '16 at 15:02
add a comment |
up vote
4
down vote
du filename
will tell you disk usage in bytes.
I prefer du -h filename
, which gives you the size in a human readable format.
2
that orstat -c "%s"
;)
– c00kiemon5ter
Jul 13 '11 at 16:29
This flavor ofdu
prints out size in blocks of 1024 bytes, not a simple count of bytes.
– Peter Lyons
Sep 17 '15 at 5:10
Note that standarddu
give an output in number of 512-byte units. GNUdu
uses kibibytes instead unless called withPOSIXLY_CORRECT
in its environment.
– Stéphane Chazelas
Nov 8 '16 at 15:01
For files of type directory, that gives the disk usage of the directory but also of all the other files within (recursively).
– Stéphane Chazelas
Nov 8 '16 at 15:02
add a comment |
up vote
4
down vote
up vote
4
down vote
du filename
will tell you disk usage in bytes.
I prefer du -h filename
, which gives you the size in a human readable format.
du filename
will tell you disk usage in bytes.
I prefer du -h filename
, which gives you the size in a human readable format.
answered Jul 13 '11 at 16:25
Teddy
1573
1573
2
that orstat -c "%s"
;)
– c00kiemon5ter
Jul 13 '11 at 16:29
This flavor ofdu
prints out size in blocks of 1024 bytes, not a simple count of bytes.
– Peter Lyons
Sep 17 '15 at 5:10
Note that standarddu
give an output in number of 512-byte units. GNUdu
uses kibibytes instead unless called withPOSIXLY_CORRECT
in its environment.
– Stéphane Chazelas
Nov 8 '16 at 15:01
For files of type directory, that gives the disk usage of the directory but also of all the other files within (recursively).
– Stéphane Chazelas
Nov 8 '16 at 15:02
add a comment |
2
that orstat -c "%s"
;)
– c00kiemon5ter
Jul 13 '11 at 16:29
This flavor ofdu
prints out size in blocks of 1024 bytes, not a simple count of bytes.
– Peter Lyons
Sep 17 '15 at 5:10
Note that standarddu
give an output in number of 512-byte units. GNUdu
uses kibibytes instead unless called withPOSIXLY_CORRECT
in its environment.
– Stéphane Chazelas
Nov 8 '16 at 15:01
For files of type directory, that gives the disk usage of the directory but also of all the other files within (recursively).
– Stéphane Chazelas
Nov 8 '16 at 15:02
2
2
that or
stat -c "%s"
;)– c00kiemon5ter
Jul 13 '11 at 16:29
that or
stat -c "%s"
;)– c00kiemon5ter
Jul 13 '11 at 16:29
This flavor of
du
prints out size in blocks of 1024 bytes, not a simple count of bytes.– Peter Lyons
Sep 17 '15 at 5:10
This flavor of
du
prints out size in blocks of 1024 bytes, not a simple count of bytes.– Peter Lyons
Sep 17 '15 at 5:10
Note that standard
du
give an output in number of 512-byte units. GNU du
uses kibibytes instead unless called with POSIXLY_CORRECT
in its environment.– Stéphane Chazelas
Nov 8 '16 at 15:01
Note that standard
du
give an output in number of 512-byte units. GNU du
uses kibibytes instead unless called with POSIXLY_CORRECT
in its environment.– Stéphane Chazelas
Nov 8 '16 at 15:01
For files of type directory, that gives the disk usage of the directory but also of all the other files within (recursively).
– Stéphane Chazelas
Nov 8 '16 at 15:02
For files of type directory, that gives the disk usage of the directory but also of all the other files within (recursively).
– Stéphane Chazelas
Nov 8 '16 at 15:02
add a comment |
up vote
3
down vote
I found an AWK 1 liner, and it had a bug but I fixed it. I also added in PetaBytes after TeraBytes.
FILE_SIZE=234234 # FILESIZE IN BYTES
FILE_SIZE=$(echo "${FILE_SIZE}" | awk '{ split( "B KB MB GB TB PB" , v ); s=1; while( $1>1024 ){ $1/=1024; s++ } printf "%.2f %s", $1, v[s] }')
Considering stat is not on every single system, you can almost always use the AWK solution. Example; the Raspberry Pi does not have stat but it does have awk.
Completely NOT what the OP asked, but nice little piece of work.
– Gypsy Spellweaver
Jun 10 at 5:48
add a comment |
up vote
3
down vote
I found an AWK 1 liner, and it had a bug but I fixed it. I also added in PetaBytes after TeraBytes.
FILE_SIZE=234234 # FILESIZE IN BYTES
FILE_SIZE=$(echo "${FILE_SIZE}" | awk '{ split( "B KB MB GB TB PB" , v ); s=1; while( $1>1024 ){ $1/=1024; s++ } printf "%.2f %s", $1, v[s] }')
Considering stat is not on every single system, you can almost always use the AWK solution. Example; the Raspberry Pi does not have stat but it does have awk.
Completely NOT what the OP asked, but nice little piece of work.
– Gypsy Spellweaver
Jun 10 at 5:48
add a comment |
up vote
3
down vote
up vote
3
down vote
I found an AWK 1 liner, and it had a bug but I fixed it. I also added in PetaBytes after TeraBytes.
FILE_SIZE=234234 # FILESIZE IN BYTES
FILE_SIZE=$(echo "${FILE_SIZE}" | awk '{ split( "B KB MB GB TB PB" , v ); s=1; while( $1>1024 ){ $1/=1024; s++ } printf "%.2f %s", $1, v[s] }')
Considering stat is not on every single system, you can almost always use the AWK solution. Example; the Raspberry Pi does not have stat but it does have awk.
I found an AWK 1 liner, and it had a bug but I fixed it. I also added in PetaBytes after TeraBytes.
FILE_SIZE=234234 # FILESIZE IN BYTES
FILE_SIZE=$(echo "${FILE_SIZE}" | awk '{ split( "B KB MB GB TB PB" , v ); s=1; while( $1>1024 ){ $1/=1024; s++ } printf "%.2f %s", $1, v[s] }')
Considering stat is not on every single system, you can almost always use the AWK solution. Example; the Raspberry Pi does not have stat but it does have awk.
edited Aug 9 at 18:22
dragon788
1659
1659
answered Jul 3 '17 at 0:21
findrbot_admin
312
312
Completely NOT what the OP asked, but nice little piece of work.
– Gypsy Spellweaver
Jun 10 at 5:48
add a comment |
Completely NOT what the OP asked, but nice little piece of work.
– Gypsy Spellweaver
Jun 10 at 5:48
Completely NOT what the OP asked, but nice little piece of work.
– Gypsy Spellweaver
Jun 10 at 5:48
Completely NOT what the OP asked, but nice little piece of work.
– Gypsy Spellweaver
Jun 10 at 5:48
add a comment |
up vote
2
down vote
Create small utility functions in your shell scripts that you can delegate to.
Example
#! /bin/sh -
# vim: set ft=sh
# size utility that works on GNU and BSD systems
size(){
case $(uname) in
(Darwin | *BSD*)
stat -Lf %z -- "$1";;
(*) stat -c %s -- "$1"
esac
}
for f do
printf '%sn' "$f : $(gzip < "$f" | wc -c) bytes (versus $(size "$f") bytes)"
done
Based on info from @Stéphane Chazelas' answer.
See alsogzip -v < file > /dev/null
to check the compressibility of a file.
– Stéphane Chazelas
Jan 11 '17 at 14:36
@StéphaneChazelas not sure if i think it was an improvement. those case statements can easily put noobs off; I certainly never remember how to get them right :-) are case statements inherently more portable since you did it? i see the point when there are more than two cases, but otherwise ...+
– oligofren
Jan 11 '17 at 16:48
1
I suppose it's also a matter of taste, but here it's the typical case where you'd want to use acase
statement.case
is the Bourne/POSIX construct to do pattern matching.[[...]]
is ksh/bash/zsh only (with variations).
– Stéphane Chazelas
Jan 11 '17 at 16:55
so indeed more portable! thanks!
– oligofren
Jan 11 '17 at 17:00
add a comment |
up vote
2
down vote
Create small utility functions in your shell scripts that you can delegate to.
Example
#! /bin/sh -
# vim: set ft=sh
# size utility that works on GNU and BSD systems
size(){
case $(uname) in
(Darwin | *BSD*)
stat -Lf %z -- "$1";;
(*) stat -c %s -- "$1"
esac
}
for f do
printf '%sn' "$f : $(gzip < "$f" | wc -c) bytes (versus $(size "$f") bytes)"
done
Based on info from @Stéphane Chazelas' answer.
See alsogzip -v < file > /dev/null
to check the compressibility of a file.
– Stéphane Chazelas
Jan 11 '17 at 14:36
@StéphaneChazelas not sure if i think it was an improvement. those case statements can easily put noobs off; I certainly never remember how to get them right :-) are case statements inherently more portable since you did it? i see the point when there are more than two cases, but otherwise ...+
– oligofren
Jan 11 '17 at 16:48
1
I suppose it's also a matter of taste, but here it's the typical case where you'd want to use acase
statement.case
is the Bourne/POSIX construct to do pattern matching.[[...]]
is ksh/bash/zsh only (with variations).
– Stéphane Chazelas
Jan 11 '17 at 16:55
so indeed more portable! thanks!
– oligofren
Jan 11 '17 at 17:00
add a comment |
up vote
2
down vote
up vote
2
down vote
Create small utility functions in your shell scripts that you can delegate to.
Example
#! /bin/sh -
# vim: set ft=sh
# size utility that works on GNU and BSD systems
size(){
case $(uname) in
(Darwin | *BSD*)
stat -Lf %z -- "$1";;
(*) stat -c %s -- "$1"
esac
}
for f do
printf '%sn' "$f : $(gzip < "$f" | wc -c) bytes (versus $(size "$f") bytes)"
done
Based on info from @Stéphane Chazelas' answer.
Create small utility functions in your shell scripts that you can delegate to.
Example
#! /bin/sh -
# vim: set ft=sh
# size utility that works on GNU and BSD systems
size(){
case $(uname) in
(Darwin | *BSD*)
stat -Lf %z -- "$1";;
(*) stat -c %s -- "$1"
esac
}
for f do
printf '%sn' "$f : $(gzip < "$f" | wc -c) bytes (versus $(size "$f") bytes)"
done
Based on info from @Stéphane Chazelas' answer.
edited Jan 11 '17 at 14:27
Stéphane Chazelas
294k54555898
294k54555898
answered Jan 11 '17 at 13:00
oligofren
1256
1256
See alsogzip -v < file > /dev/null
to check the compressibility of a file.
– Stéphane Chazelas
Jan 11 '17 at 14:36
@StéphaneChazelas not sure if i think it was an improvement. those case statements can easily put noobs off; I certainly never remember how to get them right :-) are case statements inherently more portable since you did it? i see the point when there are more than two cases, but otherwise ...+
– oligofren
Jan 11 '17 at 16:48
1
I suppose it's also a matter of taste, but here it's the typical case where you'd want to use acase
statement.case
is the Bourne/POSIX construct to do pattern matching.[[...]]
is ksh/bash/zsh only (with variations).
– Stéphane Chazelas
Jan 11 '17 at 16:55
so indeed more portable! thanks!
– oligofren
Jan 11 '17 at 17:00
add a comment |
See alsogzip -v < file > /dev/null
to check the compressibility of a file.
– Stéphane Chazelas
Jan 11 '17 at 14:36
@StéphaneChazelas not sure if i think it was an improvement. those case statements can easily put noobs off; I certainly never remember how to get them right :-) are case statements inherently more portable since you did it? i see the point when there are more than two cases, but otherwise ...+
– oligofren
Jan 11 '17 at 16:48
1
I suppose it's also a matter of taste, but here it's the typical case where you'd want to use acase
statement.case
is the Bourne/POSIX construct to do pattern matching.[[...]]
is ksh/bash/zsh only (with variations).
– Stéphane Chazelas
Jan 11 '17 at 16:55
so indeed more portable! thanks!
– oligofren
Jan 11 '17 at 17:00
See also
gzip -v < file > /dev/null
to check the compressibility of a file.– Stéphane Chazelas
Jan 11 '17 at 14:36
See also
gzip -v < file > /dev/null
to check the compressibility of a file.– Stéphane Chazelas
Jan 11 '17 at 14:36
@StéphaneChazelas not sure if i think it was an improvement. those case statements can easily put noobs off; I certainly never remember how to get them right :-) are case statements inherently more portable since you did it? i see the point when there are more than two cases, but otherwise ...+
– oligofren
Jan 11 '17 at 16:48
@StéphaneChazelas not sure if i think it was an improvement. those case statements can easily put noobs off; I certainly never remember how to get them right :-) are case statements inherently more portable since you did it? i see the point when there are more than two cases, but otherwise ...+
– oligofren
Jan 11 '17 at 16:48
1
1
I suppose it's also a matter of taste, but here it's the typical case where you'd want to use a
case
statement. case
is the Bourne/POSIX construct to do pattern matching. [[...]]
is ksh/bash/zsh only (with variations).– Stéphane Chazelas
Jan 11 '17 at 16:55
I suppose it's also a matter of taste, but here it's the typical case where you'd want to use a
case
statement. case
is the Bourne/POSIX construct to do pattern matching. [[...]]
is ksh/bash/zsh only (with variations).– Stéphane Chazelas
Jan 11 '17 at 16:55
so indeed more portable! thanks!
– oligofren
Jan 11 '17 at 17:00
so indeed more portable! thanks!
– oligofren
Jan 11 '17 at 17:00
add a comment |
up vote
0
down vote
I like the wc option myself. Paired with 'bc,' you can get decimals to as many places as you please.
I was looking to improve a script I had that awk'ed out the 'file size' column of an 'ls -alh' command. I didn't want just integer file sizes, and two decimals seemed to suit, so after reading this discussion, I came up with the code below.
I suggest breaking the line at the semicolons if you include this in a script.
file=$1; string=$(wc -c $file); bite=${string% *}; okay=$(echo "scale=2; $bite/1024" | bc);friend=$(echo -e "$file $okay" "kb"); echo -e "$friend"
My script is called gpfl, for "get picture file length." I use it after doing a mogrify on a file in imagemagick, before opening or re-loading a picture in a GUI jpeg viewer.
I don't know how this rates as an "answer," as it borrows much from what's already been offered and discussed. So I'll leave it there.
BZT
1
I would prefer using "stat" or "ls". Typically I don't like using "wc" to get file sizes because it physically reads the entire file. If you have a lot of files, or particularly large files, this can take a lot of time. But your solution is creative...+1.
– Kevin Fegan
Dec 9 '13 at 19:18
2
I agree with notion of using "stat" over "wc" for filesize, however if you use "wc -c", no data will be read; instead lseek will be used to figure out the number of bytes in a file. lingrok.org/xref/coreutils/src/wc.c#228
– bbaja42
Dec 14 '14 at 14:38
1
@bbaja42: note that GNU Coreutilswc
does read the last block of the file, in casestat.st_size
was only an approximation (like for Linux/proc
and/sys
files). I guess they decided not to make the main comment more complicated when they added that logic a couple lines down: lingrok.org/xref/coreutils/src/wc.c#246
– Peter Cordes
Apr 12 '17 at 5:53
add a comment |
up vote
0
down vote
I like the wc option myself. Paired with 'bc,' you can get decimals to as many places as you please.
I was looking to improve a script I had that awk'ed out the 'file size' column of an 'ls -alh' command. I didn't want just integer file sizes, and two decimals seemed to suit, so after reading this discussion, I came up with the code below.
I suggest breaking the line at the semicolons if you include this in a script.
file=$1; string=$(wc -c $file); bite=${string% *}; okay=$(echo "scale=2; $bite/1024" | bc);friend=$(echo -e "$file $okay" "kb"); echo -e "$friend"
My script is called gpfl, for "get picture file length." I use it after doing a mogrify on a file in imagemagick, before opening or re-loading a picture in a GUI jpeg viewer.
I don't know how this rates as an "answer," as it borrows much from what's already been offered and discussed. So I'll leave it there.
BZT
1
I would prefer using "stat" or "ls". Typically I don't like using "wc" to get file sizes because it physically reads the entire file. If you have a lot of files, or particularly large files, this can take a lot of time. But your solution is creative...+1.
– Kevin Fegan
Dec 9 '13 at 19:18
2
I agree with notion of using "stat" over "wc" for filesize, however if you use "wc -c", no data will be read; instead lseek will be used to figure out the number of bytes in a file. lingrok.org/xref/coreutils/src/wc.c#228
– bbaja42
Dec 14 '14 at 14:38
1
@bbaja42: note that GNU Coreutilswc
does read the last block of the file, in casestat.st_size
was only an approximation (like for Linux/proc
and/sys
files). I guess they decided not to make the main comment more complicated when they added that logic a couple lines down: lingrok.org/xref/coreutils/src/wc.c#246
– Peter Cordes
Apr 12 '17 at 5:53
add a comment |
up vote
0
down vote
up vote
0
down vote
I like the wc option myself. Paired with 'bc,' you can get decimals to as many places as you please.
I was looking to improve a script I had that awk'ed out the 'file size' column of an 'ls -alh' command. I didn't want just integer file sizes, and two decimals seemed to suit, so after reading this discussion, I came up with the code below.
I suggest breaking the line at the semicolons if you include this in a script.
file=$1; string=$(wc -c $file); bite=${string% *}; okay=$(echo "scale=2; $bite/1024" | bc);friend=$(echo -e "$file $okay" "kb"); echo -e "$friend"
My script is called gpfl, for "get picture file length." I use it after doing a mogrify on a file in imagemagick, before opening or re-loading a picture in a GUI jpeg viewer.
I don't know how this rates as an "answer," as it borrows much from what's already been offered and discussed. So I'll leave it there.
BZT
I like the wc option myself. Paired with 'bc,' you can get decimals to as many places as you please.
I was looking to improve a script I had that awk'ed out the 'file size' column of an 'ls -alh' command. I didn't want just integer file sizes, and two decimals seemed to suit, so after reading this discussion, I came up with the code below.
I suggest breaking the line at the semicolons if you include this in a script.
file=$1; string=$(wc -c $file); bite=${string% *}; okay=$(echo "scale=2; $bite/1024" | bc);friend=$(echo -e "$file $okay" "kb"); echo -e "$friend"
My script is called gpfl, for "get picture file length." I use it after doing a mogrify on a file in imagemagick, before opening or re-loading a picture in a GUI jpeg viewer.
I don't know how this rates as an "answer," as it borrows much from what's already been offered and discussed. So I'll leave it there.
BZT
answered Apr 12 '13 at 18:16
BZT
191
191
1
I would prefer using "stat" or "ls". Typically I don't like using "wc" to get file sizes because it physically reads the entire file. If you have a lot of files, or particularly large files, this can take a lot of time. But your solution is creative...+1.
– Kevin Fegan
Dec 9 '13 at 19:18
2
I agree with notion of using "stat" over "wc" for filesize, however if you use "wc -c", no data will be read; instead lseek will be used to figure out the number of bytes in a file. lingrok.org/xref/coreutils/src/wc.c#228
– bbaja42
Dec 14 '14 at 14:38
1
@bbaja42: note that GNU Coreutilswc
does read the last block of the file, in casestat.st_size
was only an approximation (like for Linux/proc
and/sys
files). I guess they decided not to make the main comment more complicated when they added that logic a couple lines down: lingrok.org/xref/coreutils/src/wc.c#246
– Peter Cordes
Apr 12 '17 at 5:53
add a comment |
1
I would prefer using "stat" or "ls". Typically I don't like using "wc" to get file sizes because it physically reads the entire file. If you have a lot of files, or particularly large files, this can take a lot of time. But your solution is creative...+1.
– Kevin Fegan
Dec 9 '13 at 19:18
2
I agree with notion of using "stat" over "wc" for filesize, however if you use "wc -c", no data will be read; instead lseek will be used to figure out the number of bytes in a file. lingrok.org/xref/coreutils/src/wc.c#228
– bbaja42
Dec 14 '14 at 14:38
1
@bbaja42: note that GNU Coreutilswc
does read the last block of the file, in casestat.st_size
was only an approximation (like for Linux/proc
and/sys
files). I guess they decided not to make the main comment more complicated when they added that logic a couple lines down: lingrok.org/xref/coreutils/src/wc.c#246
– Peter Cordes
Apr 12 '17 at 5:53
1
1
I would prefer using "stat" or "ls". Typically I don't like using "wc" to get file sizes because it physically reads the entire file. If you have a lot of files, or particularly large files, this can take a lot of time. But your solution is creative...+1.
– Kevin Fegan
Dec 9 '13 at 19:18
I would prefer using "stat" or "ls". Typically I don't like using "wc" to get file sizes because it physically reads the entire file. If you have a lot of files, or particularly large files, this can take a lot of time. But your solution is creative...+1.
– Kevin Fegan
Dec 9 '13 at 19:18
2
2
I agree with notion of using "stat" over "wc" for filesize, however if you use "wc -c", no data will be read; instead lseek will be used to figure out the number of bytes in a file. lingrok.org/xref/coreutils/src/wc.c#228
– bbaja42
Dec 14 '14 at 14:38
I agree with notion of using "stat" over "wc" for filesize, however if you use "wc -c", no data will be read; instead lseek will be used to figure out the number of bytes in a file. lingrok.org/xref/coreutils/src/wc.c#228
– bbaja42
Dec 14 '14 at 14:38
1
1
@bbaja42: note that GNU Coreutils
wc
does read the last block of the file, in case stat.st_size
was only an approximation (like for Linux /proc
and /sys
files). I guess they decided not to make the main comment more complicated when they added that logic a couple lines down: lingrok.org/xref/coreutils/src/wc.c#246– Peter Cordes
Apr 12 '17 at 5:53
@bbaja42: note that GNU Coreutils
wc
does read the last block of the file, in case stat.st_size
was only an approximation (like for Linux /proc
and /sys
files). I guess they decided not to make the main comment more complicated when they added that logic a couple lines down: lingrok.org/xref/coreutils/src/wc.c#246– Peter Cordes
Apr 12 '17 at 5:53
add a comment |
up vote
-1
down vote
That can be done with du command. Here is working example:
This software offer you the way to choose which file in which directory you want to know its file. (also send to git)
#!/bin/bash
# File size
FILE_SIZE=$"du -h"
# Clear the terminal
tput clear
# Request the user where and which file with an example
printf "Which file do you wish to calculate?n"
printf "Please provide its place and name like /home/user/file-size.shn"
read FILE
# Calculate with du -h (human readable) and print the output
$FILE_SIZE $FILE
add a comment |
up vote
-1
down vote
That can be done with du command. Here is working example:
This software offer you the way to choose which file in which directory you want to know its file. (also send to git)
#!/bin/bash
# File size
FILE_SIZE=$"du -h"
# Clear the terminal
tput clear
# Request the user where and which file with an example
printf "Which file do you wish to calculate?n"
printf "Please provide its place and name like /home/user/file-size.shn"
read FILE
# Calculate with du -h (human readable) and print the output
$FILE_SIZE $FILE
add a comment |
up vote
-1
down vote
up vote
-1
down vote
That can be done with du command. Here is working example:
This software offer you the way to choose which file in which directory you want to know its file. (also send to git)
#!/bin/bash
# File size
FILE_SIZE=$"du -h"
# Clear the terminal
tput clear
# Request the user where and which file with an example
printf "Which file do you wish to calculate?n"
printf "Please provide its place and name like /home/user/file-size.shn"
read FILE
# Calculate with du -h (human readable) and print the output
$FILE_SIZE $FILE
That can be done with du command. Here is working example:
This software offer you the way to choose which file in which directory you want to know its file. (also send to git)
#!/bin/bash
# File size
FILE_SIZE=$"du -h"
# Clear the terminal
tput clear
# Request the user where and which file with an example
printf "Which file do you wish to calculate?n"
printf "Please provide its place and name like /home/user/file-size.shn"
read FILE
# Calculate with du -h (human readable) and print the output
$FILE_SIZE $FILE
answered Nov 6 '16 at 16:28
aurelien
542418
542418
add a comment |
add a comment |
up vote
-1
down vote
Fastest and simplest (IMO) method is:
bash_var=$(stat -c %s /path/to/filename)
1
Then upvote one or more of the existing answers that mention stat; no need to repeat it again...
– Jeff Schaller
Nov 21 at 1:16
1
@JeffSchaller I just upvoted Stephane's answer on your instructions. I think it is too complicated for my purposes. Which is why I posted this simple answer for like minded souls.
– WinEunuuchs2Unix
Nov 21 at 1:21
1
Thank you; it's just that a sixth instance of a "stat" answer doesn't simplify this Q & A, but would rather make a new reader ask themselves "how is this answer different from the other ones?" and lead to more confusion instead of less.
– Jeff Schaller
Nov 21 at 1:32
@JeffSchaller I guess. But I could complain about the manydu
andwc
answers that should have a disclaimer NEVER DO THIS in real life. I just used my answer in a real life application tonight and thought it was worthwhile sharing. I guess we all have our opinions shrugs.
– WinEunuuchs2Unix
Nov 21 at 1:36
add a comment |
up vote
-1
down vote
Fastest and simplest (IMO) method is:
bash_var=$(stat -c %s /path/to/filename)
1
Then upvote one or more of the existing answers that mention stat; no need to repeat it again...
– Jeff Schaller
Nov 21 at 1:16
1
@JeffSchaller I just upvoted Stephane's answer on your instructions. I think it is too complicated for my purposes. Which is why I posted this simple answer for like minded souls.
– WinEunuuchs2Unix
Nov 21 at 1:21
1
Thank you; it's just that a sixth instance of a "stat" answer doesn't simplify this Q & A, but would rather make a new reader ask themselves "how is this answer different from the other ones?" and lead to more confusion instead of less.
– Jeff Schaller
Nov 21 at 1:32
@JeffSchaller I guess. But I could complain about the manydu
andwc
answers that should have a disclaimer NEVER DO THIS in real life. I just used my answer in a real life application tonight and thought it was worthwhile sharing. I guess we all have our opinions shrugs.
– WinEunuuchs2Unix
Nov 21 at 1:36
add a comment |
up vote
-1
down vote
up vote
-1
down vote
Fastest and simplest (IMO) method is:
bash_var=$(stat -c %s /path/to/filename)
Fastest and simplest (IMO) method is:
bash_var=$(stat -c %s /path/to/filename)
edited Nov 21 at 1:22
answered Nov 21 at 0:13
WinEunuuchs2Unix
270112
270112
1
Then upvote one or more of the existing answers that mention stat; no need to repeat it again...
– Jeff Schaller
Nov 21 at 1:16
1
@JeffSchaller I just upvoted Stephane's answer on your instructions. I think it is too complicated for my purposes. Which is why I posted this simple answer for like minded souls.
– WinEunuuchs2Unix
Nov 21 at 1:21
1
Thank you; it's just that a sixth instance of a "stat" answer doesn't simplify this Q & A, but would rather make a new reader ask themselves "how is this answer different from the other ones?" and lead to more confusion instead of less.
– Jeff Schaller
Nov 21 at 1:32
@JeffSchaller I guess. But I could complain about the manydu
andwc
answers that should have a disclaimer NEVER DO THIS in real life. I just used my answer in a real life application tonight and thought it was worthwhile sharing. I guess we all have our opinions shrugs.
– WinEunuuchs2Unix
Nov 21 at 1:36
add a comment |
1
Then upvote one or more of the existing answers that mention stat; no need to repeat it again...
– Jeff Schaller
Nov 21 at 1:16
1
@JeffSchaller I just upvoted Stephane's answer on your instructions. I think it is too complicated for my purposes. Which is why I posted this simple answer for like minded souls.
– WinEunuuchs2Unix
Nov 21 at 1:21
1
Thank you; it's just that a sixth instance of a "stat" answer doesn't simplify this Q & A, but would rather make a new reader ask themselves "how is this answer different from the other ones?" and lead to more confusion instead of less.
– Jeff Schaller
Nov 21 at 1:32
@JeffSchaller I guess. But I could complain about the manydu
andwc
answers that should have a disclaimer NEVER DO THIS in real life. I just used my answer in a real life application tonight and thought it was worthwhile sharing. I guess we all have our opinions shrugs.
– WinEunuuchs2Unix
Nov 21 at 1:36
1
1
Then upvote one or more of the existing answers that mention stat; no need to repeat it again...
– Jeff Schaller
Nov 21 at 1:16
Then upvote one or more of the existing answers that mention stat; no need to repeat it again...
– Jeff Schaller
Nov 21 at 1:16
1
1
@JeffSchaller I just upvoted Stephane's answer on your instructions. I think it is too complicated for my purposes. Which is why I posted this simple answer for like minded souls.
– WinEunuuchs2Unix
Nov 21 at 1:21
@JeffSchaller I just upvoted Stephane's answer on your instructions. I think it is too complicated for my purposes. Which is why I posted this simple answer for like minded souls.
– WinEunuuchs2Unix
Nov 21 at 1:21
1
1
Thank you; it's just that a sixth instance of a "stat" answer doesn't simplify this Q & A, but would rather make a new reader ask themselves "how is this answer different from the other ones?" and lead to more confusion instead of less.
– Jeff Schaller
Nov 21 at 1:32
Thank you; it's just that a sixth instance of a "stat" answer doesn't simplify this Q & A, but would rather make a new reader ask themselves "how is this answer different from the other ones?" and lead to more confusion instead of less.
– Jeff Schaller
Nov 21 at 1:32
@JeffSchaller I guess. But I could complain about the many
du
and wc
answers that should have a disclaimer NEVER DO THIS in real life. I just used my answer in a real life application tonight and thought it was worthwhile sharing. I guess we all have our opinions shrugs.– WinEunuuchs2Unix
Nov 21 at 1:36
@JeffSchaller I guess. But I could complain about the many
du
and wc
answers that should have a disclaimer NEVER DO THIS in real life. I just used my answer in a real life application tonight and thought it was worthwhile sharing. I guess we all have our opinions shrugs.– WinEunuuchs2Unix
Nov 21 at 1:36
add a comment |
protected by Kusalananda Jul 3 '17 at 7:02
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
stackoverflow.com/questions/5920333/how-to-check-size-of-a-file LOL for the migrate :-)
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Oct 1 '15 at 8:06
Pair this with
pv
andcat
for a copy command that shows progress and ETA :)– sudo
Sep 25 '17 at 17:38