Linux check if file size

what is the most reliable command to find actual size of a file linux

Recently I tried to find out the size of a file using various command and it showed huge differences. ls -ltr showed its size around 34GB (bytes rounded off by me ) while du -sh filename showed it to be around 11GB. while stat command showed the same to be around 34GB . Any idea which is the most reliable command to find actual size of the file ? There was some copy operation performed on it and we are unsure of if this was appropriately done as after a certain time source file from where copy was being performed was removed by a job of ours.

I guess that is a sparse file, du tells its current disk usage, ls its declared size (that might be reached if you modify the file decreasing the runs of blank blocks)

@l’L’l: Why so unusual? $ truncate -s 10T foo; ls -l foo; du -sh foo will give 10995116277760 and 0 .

@rodrigo, Actually, what I meant is that it would be unusual if the file in question was not sparse, or there was major discrepancy between ls -lsh and du -h; du -bh . The example you’ve shown gives an apparent file size and an actual file size, so of course it’s going to be completely different.

3 Answers 3

There is no inaccuracy or reliability issue here, you’re just comparing two different numbers: logical size vs physical size.

Elaborate illustration better explained by the Wikipedia Article

ls shows the gray+green areas, the logical length of the file. du (without —apparent-size ) shows only the green areas, since those are the ones that take up space.

You can create a sparse file with dd count=0 bs=1M seek=100 of=myfile .

ls shows 100MiB because that’s how long the file is:

$ ls -lh myfile -rw-r----- 1 me me 100M Jul 15 10:57 myfile 

du shows 0, because that’s how much data it’s allocated:

Just note that this does not really answer the literal question posted, although this is likely what the user needed to know

Читайте также:  Синхронизировать время linux centos

@rodrigo On which compressing file systems does du show post-compression blocks? On btrfs it shows pre-compression blocks.

@thatotherguy: I’ll do a few experiments if I got the time. But from the top of my head, I think that ZFS and maybe also ext4 did that.

will give you a long format listing (needed to actually see the file size) and round file sizes up to the nearest MiB .

If you want MB (10^6 bytes) rather than MiB (2^20 bytes) units, use —block-size=MB instead.

If you don’t want the M suffix attached to the file size, you can use something like —block-size=1M . Thanks Stéphane Chazelas for suggesting this.

This is described in the man page for ls ; man ls and search for SIZE . It allows for units other than MB/MiB as well, and from the looks of it (I didn’t try that) arbitrary block sizes as well (so you could see the file size as number of 412-byte blocks, if you want to).

Note that the —block-size parameter is a GNU extension on top of the Open Group’s ls , so this may not work if you don’t have a GNU userland (which most Linux installations do). The ls from GNU coreutils 8.5 does support —block-size as described above.

Источник

File size check, Linux

You define file as an array, which makes sense, because you want to store more than one filename. However a bit below, you use it as a scalar, when you write

2 Answers 2

To read a file size stat is the best for use-case here. On a linux machine with GNU stat

#!/bin/bash while read -r file ; do if [ "$(stat --printf="%s" "$file")" -gt 0 ] then echo "$file" yes else echo "$file" no fi done < video.txt 

This is too complicated approach in my opinion. Just use find

$ touch zero $ echo one > one $ ll total 8 -rw-r--r-- 1 sverma wheel 4B Jul 11 16:20 one -rw-r--r-- 1 sverma wheel 0B Jul 11 16:20 zero 

one is a 4 byte file. Use -size predicate. Here + means greater than, -type f means only files

$ find . -type f -size +0 -print ./one 

You can add a filter for names with something like

$ find . -name '*.mp4' -type f -size +0 

Mostly good, but always add double quotes around variable expansion and command substitution: if [ "$(stat --printf='%s' "$file")" -gt 0 ] ; , printf %s yes\n' "$file" . Also semicolon ; is useless at end of line. See what static analysis shellcheck.net has to tell you.

Читайте также:  Linux file magic file

You probably mean - "how to loop instead of this: file=($(cat video.txt | xargs ls -lah | awk '< print $9>')) " (which is pretty horrible by itself (*) ).

You should just use a while loop:

cat video.txt | while read file; do # something with $file done 

Also, please use stat -c%s FILE instead of wc (which, BTW, also takes a file - you can use wc -c FILE instead of using input redirection), as that looks just at the file system information to check the size, instead of loading and counting each byte.

(*) doing ls -lah and then awk '' is the same as just doing ls , but there are other issues with this code that is very not bash idiomatic.

Источник

How to check if a file's size is greater than a certain value in Bash

With this code, I can get the file name in the variable $FILESIZE, but I am unable to compare it with a fixed integer value.

#!/bin/bash filename=./testfile.txt maxsize=5 filesize=$(stat -c%s "$filename") echo "Size of $filename = $filesize bytes." if (( filesize > maxsize )); then echo "nope" else echo "fine" fi 

if [ (( $FILESIZE > MAXSIZE)) ]; -> if (( FILESIZE > MAXSIZE )); The arithmetic operators can serve as a conditional expression. (you don't need the $ within ((. )) )

2 Answers 2

A couple of syntactic issues.

  1. The variable definitions in Bash do not take spaces. It should have been MAXSIZE=500000 , without spaces.
  2. The way comparison operation is done is incorrect. Instead of if [ (( $FILESIZE > MAXSIZE)) ]; , you could very well use Bash’s own arithmetic operator alone and skip the [ operator to just if (( FILESIZE > MAXSIZE)); then

If you are worried about syntax issues in your script, use ShellCheck to syntax check your scripts and fix the errors as seen from it.

As a general coding practice, use lowercase user-defined variables in Bash to avoid confusing them with the special environment variables which are interpreted for different purposes by the shell (e.g., $HOME and $SHELL ).

You can use the find command to accomplish this as well. See the below examples.

Читайте также:  Хром для astra linux

You can change the stat command for any command you need.

ls -lh test.txt -rw-r--r-- 1 root root 25M Jan 20 19:36 test.txt find ~/test.txt -size 25M -exec stat '<>' \; File: '/root/test.txt' Size: 26214400 Blocks: 51200 IO Block: 4096 regular file Device: fe04h/65028d Inode: 402704 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2022-01-20 19:36:51.222371293 +0000 Modify: 2022-01-20 19:36:51.242373113 +0000 Change: 2022-01-20 19:36:51.242373113 +0000 Birth: - 

If the file does not have the size you are looking for, it won't do anything.

find ~/test.txt -size 99M -exec true \; 

Источник

Check if the file size greater than 1MB using IF condition

By using ls -lh we can get the file size. Is there any way I can check if the file size is greater than 1MB to then print a message like below? I may have files with different sizes like 100mb, 1gb, 10gb, 100kb.

if [ $FileSize > 1MB ]; then echo "File size is grater than 1MB" fi 

2 Answers 2

Using find on a specific file at $filepath :

if [ -n "$(find "$filepath" -prune -size +1000000c)" ]; then printf '%s is strictly larger than 1 MB\n' "$filepath" fi 

This uses find to query the specific file at $filepath for its size. If the size is greater than 1000000 bytes, find will print the pathname of the file, otherwise it will generate nothing. The -n test is true if the string has non-zero length, which in this case means that find outputted something, which in turns means that the file is larger than 1 MB.

You didn't ask about this: Finding all regular files that are larger than 1 MB under some $dirpath and printing a short message for each:

find "$dirpath" -type f -size +1000000c \ -exec printf '%s is larger than 1 MB\n' <> + 

These pieces of code ought be to portable to any Unix.

Note also that using < or >in a test will test whether the two involved strings sort in a particular way lexicographically. These operators do not do numeric comparisons. For that, use -lt ("less than"), -le ("less than or equal to"), -gt ("greater than"), or -ge ("greater than or equal to"), -eq ("equal to"), or -ne ("not equal to"). These operators do integer comparisons.

Источник

Оцените статью
Adblock
detector