I want to watch the growing size of a single file, so i use this command:
texai@maelstrom ~$ ls -lh club_prod.sql | awk '{print $5}' 116M
Now I want to watch that result each 5 seconds so:
texai@maelstrom ~$ watch -n 5 ls -lh club_prod.sql | awk '{print $5}'
but this command doesn't return any result
don't worry we have a got a UNIX command to do that for you and command is "df" which displays the size of the file system in UNIX. You can run "df" UNIX command with the current directory or any specified directory.
Another method we can use to grab the size of a file in a bash script is the wc command. The wc command returns the number of words, size, and the size of a file in bytes.
ls -lh command shows all file size information as K for Kibibyte (KiB), M for Mebibyte (MiB) and so on.. Instead of bits they show information in bytes. ls -lh shows unit (size) information using single character instead of two characters. If no unit information is there, then that is bytes.
Use ls The -l option tells ls to show various metadata about the file, including file size. Without this option, ls only shows filenames. The -h option tells ls to show human-friendly units such as M for megabytes, G for gigabytes, etc.
You can force ls command to display file size in MB with the --block-size flag. The problem with this approach is that all the files with size less than 1 MB will also be displayed with file size 1 MB. The ls command also has -s option to display size. You should combine with -h to show the file size in human readable form.
Let’s find files that are more than 2 GB in file size. The -size option tells find to search for files of a certain size. The + is “greater than” and 2 GB is specified as 2G in the syntax. Example 4. We can also use find to search for files under a certain size.
This watches a given file (or files) for changes and runs the command whenever (the instant) it changes. So you could do it like: echo /etc/my.cnf | \ entr sh -c 'cat /etc/my.cnf | mail -E -s "file changed" [email protected]' (PS. Credit to Denis 's answer for the mail command)
The watch command is a built-in Linux utility used for running user-defined commands at regular intervals. It temporarily clears all the terminal content and displays the output of the attached command, along with the current system date and time.
You're piping the output of watch
into awk
. If you simplify your command line, what you have is:
watch <some arguments> | awk '{print $5}'
That's not what you want. Try:
watch -n 5 "ls -lh club_prod.sql | awk '{print \$5}'"
watch -n 5 "du -h club_prod.sql"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With