How to use the `cat` command to display file size?
Published on Aug. 22, 2023, 12:18 p.m.
To display the file size using the cat
command in Linux, you can use the stat
command along with cat
. Here’s an example:
cat example.txt | stat --printf="%s bytes\n"
This will display the size of the example.txt
file in bytes. If you want to display the file size in a more human-readable format (such as kilobytes or megabytes), you can use the -c
option along with the du
command, like this:
cat example.txt | du -hc | tail -n 1
This will display the size of the file in a human-readable format, like “1.2K” or “3.4M”. Alternatively, you can just use the -h
option with the ls
command to display the file size in a human-readable format, like this:
ls -lh example.txt
This will display the file size in a format like “1.2K” or “3.4M”, along with other information about the file such as its permissions and modification time.
Note that for large files, it may take some time for the du
command to calculate the file size, so be patient.