Bash file size test




















Since our text file was readable by us, therefore, the output will be true, as shown in the following image:. Since our text file was owned by us, therefore, the output will be true as shown in the following image:. This article briefed the reader about the usage of the different file test operators in Bash using the Linux operating system. By making use of these file test operators, it gets very convenient for the user to work with files without any potential difficulties.

There are some other file test operators too, which can be used for different purposes However, the ones that are discussed in this article are most commonly used. I am a self-motivated information technology professional with a passion for writing.

Using du with --apparent-size flag will return a more precise size as stated on man : print apparent sizes, rather than disk usage; although the apparent size is usually smaller, it may be larger due to holes in 'sparse' files, internal fragmentation, indirect blocks, and the like — Hugo H.

Community Bot 1. I'm guessing that wc has the brains to try to stat the file if only -c is specified. 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 It seems like printf still sees the indentation, e. On the other hand echo ignores the whitespace. Any way to make it consistent? Show 8 more comments. From man stat 1 : -f format : Display information using the specified format. Note that this is a BSD-only solution. It doesn't work with GNU stat , unfortunately. Depends what you mean by size.

This is good for correctness, but bad if the end of the file is on disk and not in memory esp. And very bad if the file is migrated to near-line tape storage , or e.

The problems with device files also remain. It's all sequences of bytes. Some applications may want to interpret those bytes as text but obviously not wc -c which reports the number of bytes. Daniel Thalhuber 3 3 3 bronze badges.

In the example below, the filesize is just under 2KB: -rw-r--r-- 1 user owner index. Druckles Druckles 2 2 silver badges 6 6 bronze badges. I think both ls -l and stat command give reliable size information. I did not find any reference to the contrary. I prefer du -h filename , which gives you the size in a human readable format.

Teddy Teddy 3 3 bronze badges. This flavor of du prints out size in blocks of bytes, not a simple count of bytes. Note that standard du give an output in number of byte units. For files of type directory , that gives the disk usage of the directory but also of all the other files within recursively.

Create small utility functions in your shell scripts that you can delegate to. 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. Then upvote one or more of the existing answers that mention stat; no need to repeat it again JeffSchaller I just upvoted Stephane's answer on your instructions.

I think it is too complicated for my purposes. If you want to say "size is non-zero", you need [ -s file. Improve this answer. Mikel Mikel Only because it's more portable. BSD and Linux stat have different flags. Is it not inefficient to read the file to determine it's size? I think stat will not read the file to see it's size. Note that fstat takes an fd, not a pathname. Show 9 more comments. Daniel C. Sobral Daniel C. Sobral k 83 83 gold badges silver badges bronze badges.

You could use the blocks reported by stat to calculate space used. AjithAntony That's an interesting point which did not occur to me. I can see stat being the right thing in some situations, and sparse files are not relevant in most situations, though certainly not all. Nice, but won't work on OSX, where du doesn't support -b. If your find handles this syntax, you can use it: find -maxdepth 1 -name "file.

BananaNeil BananaNeil 8, 6 6 gold badges 40 40 silver badges 58 58 bronze badges. Finally, we implement a simple if statement to check if the size format is either 1 Bytes , 2 Kilobytes , 3 Megabytes , 4 Gigabyte.

We then use the bc command to convert file size in bytes to the specified format. NOTE: We use a variating scale for the bc command to accommodate the number of decimals per evaluation.

Feel free to improve it and tweak it to your needs. We cannot forget the stat command. Using the stat command, we can display detailed information about a file or the file system. The stat command returns the size in bytes as well. You can use similar logic in the script above to select the format. This tutorial has discussed three methods you can use to get the size of a file using a bash script.



0コメント

  • 1000 / 1000