2

The Linux ls command comes with these options.

   --block-size=SIZE
          scale sizes by SIZE before printing them; e.g., '--block-size=M' prints sizes in units of  1,048,576  bytes;  see
          SIZE format below


   -l     use a long listing format

          list subdirectories recursively

   -s, --size
          print the allocated size of each file, in blocks

I presume that ls -l is the actual file size and ls -s --block-size=1 is the amount of disk space allocated to storing the file. (In this case 991232 = 968x1024 = 968K.)

$ ls -s --block-size=1  summary.pdf 
991232 summary.pdf
$ ls -l summary.pdf 
-rwxrwx---. 1 chris chris 989838 May  1  2015 summary.pdf

Is there an to get the file size in bytes without the additional information in "long listing format"?

ChrisGuest
  • 153
  • 1
  • 2
  • 6

3 Answers3

6

You can use stat with a custom format.

stat -c'%s' summary.pdf 

will output the actual file size in bytes. If you want allocated blocks, use %b for the number of blocks allocated and %B for the block size of each block in bytes.

This also works with wildcards and an additional %n for the file name. See the man page for more format options.

dirkt
  • 32,309
  • define "actual file size". The real "actual file size" is the size in blocks (or in multiple block, depending on the file system), which is the real space allocated in the disk. – Giacomo Catenazzi Oct 06 '16 at 13:47
  • 1
    @GiacomoCatenazzi: actual file size = what's in the st_size field of the inode = what counts when you access the file through read(2), write(2) etc. I chose the wording because it was used in the question itself for the output of ls -l. – dirkt Oct 06 '16 at 14:47
  • Yes, for my purposes I have a lot of large files & I am looking for duplicates. I want to first get a list of file sizes & then do an md5sum on them. So actual file size means character length of the file, not allocated block storage. – ChrisGuest Oct 06 '16 at 21:09
  • So thanks, @dirkt now I can do this: find /mnt/pdffolder -name 'summary.pdf' -exec stat -c'%n %s' {} ; > file_list – ChrisGuest Oct 06 '16 at 21:36
  • If you use something like '%011s %n' instead (or even more than 11 digits if necessary), you can also sort it by size, which may help with finding duplicates. – dirkt Oct 07 '16 at 07:00
1

If you want an alternative ls format which shows just size and filename, this hack does the trick.

ls -l | sed "s/  */ /g" | cut -d ' ' -f 5,9-

or maybe:

alias size_plus_name="sed 's/  */ /g' | cut -d ' ' -f 5,9-"
ls -l | size_plus_name

Use ls with any switches or command line arguments this way. Do not forget to include -l.

Attention! This only works if filenames does not contain any whitespace.
However, 5-9- (instead of 5-9) makes an attempt to handle wsp-containing filenames.
(cf. @Scott 's comment below)

0

du replace * with filename:

du -s -B 1 *
du -s -B 1 * | cut -f 1

it's the same as stat except for directories: du will report recursive size (ex:99999), stat will report 4096 or block size


find also works: same as stat

find * -maxdepth 1 -printf "%s\n"
%p     File's name.
%s     File's size in bytes.

%f Print the basename; the file's name with any leading directories removed

https://man7.org/linux/man-pages/man1/find.1.html

Mr. Doge
  • 121