Skip to main content
Rollback to Revision 3 - Revert
Source Link
Aeronautix
  • 153
  • 1
  • 7

My answer uses wc which requires read permissions for the files. Normally to obtain a file's byte size, like with a stat command, you don't require read permissions but only execute/search permissions to ancestor directories. See also Stéphane Chazelas's helpful comments below. On macOS you can use BSD stat -f %z to obtain a file's byte size, and on systems using the GNU coreutils toolset, you can use GNU stat -c %s. If you have zsh you can use zstat with zmodload -F zsh/stat b:zstat; zstat -H struct_stat_hash <your-file>; echo "${struct_stat_hash[size]}" to get a file byte size, without needing read permissions.

My answer uses wc which requires read permissions for the files. Normally to obtain a file's byte size, like with a stat command, you don't require read permissions but only execute/search permissions to ancestor directories. See also Stéphane Chazelas's helpful comments below. On macOS you can use BSD stat -f %z to obtain a file's byte size, and on systems using the GNU coreutils toolset, you can use GNU stat -c %s. If you have zsh you can use zstat with zstat -H struct_stat_hash <your-file>; echo "${struct_stat_hash[size]}" to get a file byte size, without needing read permissions.

My answer uses wc which requires read permissions for the files. Normally to obtain a file's byte size, like with a stat command, you don't require read permissions but only execute/search permissions to ancestor directories. See also Stéphane Chazelas's helpful comments below. On macOS you can use BSD stat -f %z to obtain a file's byte size, and on systems using the GNU coreutils toolset, you can use GNU stat -c %s. If you have zsh you can use zstat with zmodload -F zsh/stat b:zstat; zstat -H struct_stat_hash <your-file>; echo "${struct_stat_hash[size]}" to get a file byte size, without needing read permissions.

"zmodload -F zsh/stat b:zstat" seems to be an ancient relic from zsh v4.3.5, but is still kept in the docs
Source Link
Aeronautix
  • 153
  • 1
  • 7

My answer uses wc which requires read permissions for the files. Normally to obtain a file's byte size, like with a stat command, you don't require read permissions but only execute/search permissions to ancestor directories. See also Stéphane Chazelas's helpful comments below. On macOS you can use BSD stat -f %z to obtain a file's byte size, and on systems using the GNU coreutils toolset, you can use GNU stat -c %s. If you have zsh you can use zstat with zmodload -F zsh/stat b:zstat; zstat -H struct_stat_hash <your-file>; echo "${struct_stat_hash[size]}" to get a file byte size, without needing read permissions.

My answer uses wc which requires read permissions for the files. Normally to obtain a file's byte size, like with a stat command, you don't require read permissions but only execute/search permissions to ancestor directories. See also Stéphane Chazelas's helpful comments below. On macOS you can use BSD stat -f %z to obtain a file's byte size, and on systems using the GNU coreutils toolset, you can use GNU stat -c %s. If you have zsh you can use zstat with zmodload -F zsh/stat b:zstat; zstat -H struct_stat_hash <your-file>; echo "${struct_stat_hash[size]}" to get a file byte size, without needing read permissions.

My answer uses wc which requires read permissions for the files. Normally to obtain a file's byte size, like with a stat command, you don't require read permissions but only execute/search permissions to ancestor directories. See also Stéphane Chazelas's helpful comments below. On macOS you can use BSD stat -f %z to obtain a file's byte size, and on systems using the GNU coreutils toolset, you can use GNU stat -c %s. If you have zsh you can use zstat with zstat -H struct_stat_hash <your-file>; echo "${struct_stat_hash[size]}" to get a file byte size, without needing read permissions.

Re-read my answer, replace headscratcher wc-terminal-output info with wc-no-filename-output info
Source Link
Aeronautix
  • 153
  • 1
  • 7
  • Store bytes-pathname pairs, separated by a space, into bytes_pathname_arr array.
  • wc -c <"$pathname" gets the number of bytes of the file in a wc-safe manner, without something like a carriage return in the pathname wrecking the output offilename. If wc sees no file operands, stdin is used and no filename is written.
    • Compare echo content > $'ab\rcd'; wc -c $'ab\rcd' withSee also: the echo content > 'normal filename'; wc -c 'normal filename' on your machinePOSIX spec for wc.
  • tr -d ' ' removes space characters. macOS BSD wc outputs extra leading space characters, unlike the wc implementations commonly found on Linux-based operating systems, such as that of busybox, toybox or GNU coreutils (thank you Stéphane Chazelas), so we want to get rid of these.
  • Store bytes-pathname pairs, separated by a space, into bytes_pathname_arr array.
  • wc -c <"$pathname" gets the number of bytes of the file in a wc-safe manner, without something like a carriage return in the pathname wrecking the output of wc.
    • Compare echo content > $'ab\rcd'; wc -c $'ab\rcd' with echo content > 'normal filename'; wc -c 'normal filename' on your machine.
  • tr -d ' ' removes space characters. macOS BSD wc outputs extra leading space characters, unlike the wc implementations commonly found on Linux-based operating systems, such as that of busybox, toybox or GNU coreutils (thank you Stéphane Chazelas), so we want to get rid of these.
  • Store bytes-pathname pairs, separated by a space, into bytes_pathname_arr array.
  • wc -c <"$pathname" gets the number of bytes of the file without the filename. If wc sees no file operands, stdin is used and no filename is written.
  • tr -d ' ' removes space characters. macOS BSD wc outputs extra leading space characters, unlike the wc implementations commonly found on Linux-based operating systems, such as that of busybox, toybox or GNU coreutils (thank you Stéphane Chazelas), so we want to get rid of these.
Update my answer
Source Link
Aeronautix
  • 153
  • 1
  • 7
Loading
Source Link
Aeronautix
  • 153
  • 1
  • 7
Loading