Note: The command below does slightly more than you asked for, but may be extremely useful for other people.
Here is a command I worked out with a colleague and gave to application support teams who need to deal with full disks:
find / -xdev \! -path /var/log/lastlog -printf '%s\t%p\n' | sort -rn | head | cut -f2- | xargs -n1 ls -lh | awk '{print $5, $NF}'
Here's the same command with line breaks for easier reading:
find / -xdev \! -path /var/log/lastlog -printf '%s\t%p\n' |
sort -rn | head | cut -f2- |
xargs -n1 ls -lh | awk '{print $5, $NF}'
This command prints the 10 largest files on the root filesystem, along with the human-readable file size for each.
This command needs to be run as root for accurate results.
-xdev
avoids traversing filesystem boundaries, which can be important if NFS might be slow.
/var/log/lastlog
is ignored as it's a sparse file that falsely reports a huge size (i.e. it's not contributing to disk full issues).
-printf
is specific to GNU find. In this case it prints the file size in bytes followed by the full path of the file.
You can actually leave everything off after the head
command and the only thing you will lose is the human-readable file sizes.
So the following works just fine:
find / -xdev \! -path /var/log/lastlog -printf '%s\t%p\n' | sort -rn | head
This command does not work if you have filenames with newlines in them. But as this command is intended for manual handling by a human operator (not use in scripts), that's not crucial.
Now, to answer your particular question:
My goal is to measure the file size (in MB or GB) for every file on my Linux system, recursively recording the filename and filesize, and piping the output into a tab-delimited text file.
I'm going to ignore the MB or GB requirement and simply report the filesize in bytes, as it is MUCH, MUCH easier to do. See above command for how to do this if you really need to.
sudo find / -printf '%s\t%p\n' > outputfile.txt
du
?ls
.